Skip to content

Commit d561685

Browse files
author
Ho
committed
handling priority op in demo contract
1 parent 772c3c2 commit d561685

File tree

10 files changed

+398
-140
lines changed

10 files changed

+398
-140
lines changed

contracts/Bytes.sol

Lines changed: 93 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,86 @@
33
pragma solidity ^0.8.0;
44

55
library Bytes {
6-
7-
function shiftAndReverseBits8(uint8 val, uint8 offset) internal pure returns (uint256 ret) {
6+
function shiftAndReverseBits8(uint8 val, uint8 offset)
7+
internal
8+
pure
9+
returns (uint256 ret)
10+
{
811
uint16 effectLen = offset < 248 ? 8 : 256 - offset;
9-
for(uint16 i = 0; i < effectLen; i++){
10-
if (val & 1 == 1){
11-
ret += (1 << (255-i-offset));
12+
for (uint16 i = 0; i < effectLen; i++) {
13+
if (val & 1 == 1) {
14+
ret += (1 << (255 - i - offset));
1215
}
13-
val >>=1;
16+
val >>= 1;
1417
}
1518
}
1619

17-
function shiftAndReverseBits16(uint16 val, uint8 offset) internal pure returns (uint256 ret) {
20+
function shiftAndReverseBits16(uint16 val, uint8 offset)
21+
internal
22+
pure
23+
returns (uint256 ret)
24+
{
1825
uint16 effectLen = offset < 240 ? 16 : 256 - offset;
19-
for(uint16 i = 0; i < effectLen; i++){
20-
if (val & 1 == 1){
21-
ret += (1 << (255-i-offset));
26+
for (uint16 i = 0; i < effectLen; i++) {
27+
if (val & 1 == 1) {
28+
ret += (1 << (255 - i - offset));
2229
}
23-
val >>=1;
30+
val >>= 1;
2431
}
2532
}
2633

27-
function shiftAndReverseBits32(uint32 val, uint8 offset) internal pure returns (uint256 ret) {
34+
function shiftAndReverseBits32(uint32 val, uint8 offset)
35+
internal
36+
pure
37+
returns (uint256 ret)
38+
{
2839
uint16 effectLen = offset < 224 ? 32 : 256 - offset;
29-
for(uint16 i = 0; i < effectLen; i++){
30-
if (val & 1 == 1){
31-
ret += (1 << (255-i-offset));
40+
for (uint16 i = 0; i < effectLen; i++) {
41+
if (val & 1 == 1) {
42+
ret += (1 << (255 - i - offset));
3243
}
33-
val >>=1;
44+
val >>= 1;
3445
}
3546
}
3647

37-
function shiftAndReverseBits64(uint64 val, uint8 offset) internal pure returns (uint256 ret) {
48+
function shiftAndReverseBits64(uint64 val, uint8 offset)
49+
internal
50+
pure
51+
returns (uint256 ret)
52+
{
3853
uint16 effectLen = offset < 192 ? 64 : 256 - offset;
39-
for(uint16 i = 0; i < effectLen; i++){
40-
if (val & 1 == 1){
41-
ret += (1 << (255-i-offset));
54+
for (uint16 i = 0; i < effectLen; i++) {
55+
if (val & 1 == 1) {
56+
ret += (1 << (255 - i - offset));
4257
}
43-
val >>=1;
58+
val >>= 1;
4459
}
4560
}
4661

47-
function shiftAndReverseBits128(uint128 val, uint8 offset) internal pure returns (uint256 ret) {
62+
function shiftAndReverseBits128(uint128 val, uint8 offset)
63+
internal
64+
pure
65+
returns (uint256 ret)
66+
{
4867
uint16 effectLen = offset < 128 ? 128 : 256 - offset;
49-
for(uint16 i = 0; i < effectLen; i++){
50-
if (val & 1 == 1){
51-
ret += (1 << (255-i-offset));
68+
for (uint16 i = 0; i < effectLen; i++) {
69+
if (val & 1 == 1) {
70+
ret += (1 << (255 - i - offset));
5271
}
53-
val >>=1;
72+
val >>= 1;
5473
}
5574
}
5675

57-
function shiftAndReverseBits(uint256 val, uint8 offset) internal pure returns (uint256 ret) {
58-
for(uint16 i = 0; i < 256 - offset; i++){
59-
if (val & 1 == 1){
60-
ret += (1 << (255-i-offset));
76+
function shiftAndReverseBits(uint256 val, uint8 offset)
77+
internal
78+
pure
79+
returns (uint256 ret)
80+
{
81+
for (uint16 i = 0; i < 256 - offset; i++) {
82+
if (val & 1 == 1) {
83+
ret += (1 << (255 - i - offset));
6184
}
62-
val >>=1;
85+
val >>= 1;
6386
}
6487
}
6588

@@ -68,28 +91,52 @@ library Bytes {
6891
v = input;
6992

7093
// swap bytes
71-
v = ((v & 0xFF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00) >> 8) |
72-
((v & 0x00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF) << 8);
94+
v =
95+
((v &
96+
0xFF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00) >>
97+
8) |
98+
((v &
99+
0x00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF) <<
100+
8);
73101

74102
// swap 2-byte long pairs
75-
v = ((v & 0xFFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000) >> 16) |
76-
((v & 0x0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF) << 16);
103+
v =
104+
((v &
105+
0xFFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000) >>
106+
16) |
107+
((v &
108+
0x0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF) <<
109+
16);
77110

78111
// swap 4-byte long pairs
79-
v = ((v & 0xFFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000) >> 32) |
80-
((v & 0x00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF) << 32);
112+
v =
113+
((v &
114+
0xFFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000) >>
115+
32) |
116+
((v &
117+
0x00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF) <<
118+
32);
81119

82120
// swap 8-byte long pairs
83-
v = ((v & 0xFFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF0000000000000000) >> 64) |
84-
((v & 0x0000000000000000FFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF) << 64);
121+
v =
122+
((v &
123+
0xFFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF0000000000000000) >>
124+
64) |
125+
((v &
126+
0x0000000000000000FFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF) <<
127+
64);
85128

86129
// swap 16-byte long pairs
87130
v = (v >> 128) | (v << 128);
88131
}
89132

90133
// See comment at the top of this file for explanation of how this function works.
91134
// NOTE: theoretically possible overflow of (_start + 0x2)
92-
function bytesToUInt16(bytes memory _bytes, uint256 _start) internal pure returns (uint16 r) {
135+
function bytesToUInt16(bytes memory _bytes, uint256 _start)
136+
internal
137+
pure
138+
returns (uint16 r)
139+
{
93140
uint256 offset = _start + 0x2;
94141
require(_bytes.length >= offset, "T");
95142
assembly {
@@ -98,8 +145,12 @@ library Bytes {
98145
}
99146

100147
// NOTE: theoretically possible overflow of (_offset + 2)
101-
function readUInt16(bytes memory _data, uint256 _offset) internal pure returns (uint256 newOffset, uint16 r) {
148+
function readUInt16(bytes memory _data, uint256 _offset)
149+
internal
150+
pure
151+
returns (uint256 newOffset, uint16 r)
152+
{
102153
newOffset = _offset + 2;
103154
r = bytesToUInt16(_data, _offset);
104155
}
105-
}
156+
}

contracts/FluiDex.sol

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@ import "hardhat/console.sol"; // for debugging
1111

1212
import "./IFluiDex.sol";
1313
import "./IVerifier.sol";
14+
import "./Storage.sol";
1415

1516
/**
1617
* @title FluiDexDemo
1718
*/
18-
contract FluiDexDemo is AccessControl, IFluiDex, Ownable, ReentrancyGuard {
19+
contract FluiDexDemo is
20+
AccessControl,
21+
IFluiDex,
22+
Ownable,
23+
ReentrancyGuard,
24+
Storage
25+
{
1926
using SafeERC20 for IERC20;
2027

2128
bytes32 public constant PLUGIN_ADMIN_ROLE = keccak256("PLUGIN_ADMIN_ROLE");
@@ -37,6 +44,7 @@ contract FluiDexDemo is AccessControl, IFluiDex, Ownable, ReentrancyGuard {
3744

3845
uint16 public tokenNum;
3946
mapping(uint16 => address) public tokenIdToAddr;
47+
mapping(uint16 => uint8) public tokenScales;
4048
mapping(address => uint16) public tokenAddrToId;
4149

4250
uint16 public userNum;
@@ -51,6 +59,9 @@ contract FluiDexDemo is AccessControl, IFluiDex, Ownable, ReentrancyGuard {
5159
_setRoleAdmin(DELEGATE_ROLE, DEFAULT_ADMIN_ROLE);
5260
grantRole(PLUGIN_ADMIN_ROLE, msg.sender);
5361
grantRole(DELEGATE_ROLE, msg.sender);
62+
63+
//TODO: define defaut scale of ETH: eth (10^18 wei) with prec 6 so we get
64+
tokenScales[0] = 12;
5465
}
5566

5667
/**
@@ -85,6 +96,10 @@ contract FluiDexDemo is AccessControl, IFluiDex, Ownable, ReentrancyGuard {
8596
tokenIdToAddr[tokenId] = tokenAddr;
8697
tokenAddrToId[tokenAddr] = tokenId;
8798

99+
//TODO: should provide token's prec and check token's decimal
100+
tokenScales[tokenId] = 12;
101+
102+
emit NewToken(origin, tokenAddr, tokenId);
88103
return tokenId;
89104
}
90105

@@ -96,12 +111,33 @@ contract FluiDexDemo is AccessControl, IFluiDex, Ownable, ReentrancyGuard {
96111
payable
97112
override
98113
onlyRole(DELEGATE_ROLE)
99-
{}
114+
{
115+
uint16 accountId = userBjjPubkeyToUserId[to];
116+
require(accountId != 0, "non-existed user");
117+
uint128 amount = Operations.scaleTokenValueToAmount(
118+
msg.value,
119+
tokenScales[0]
120+
);
121+
122+
Operations.Deposit memory op = Operations.Deposit({
123+
accountId: accountId,
124+
tokenId: 0,
125+
amount: amount
126+
});
127+
128+
addPriorityRequest(
129+
Operations.OpType.Deposit,
130+
Operations.writeDepositPubdataForPriorityQueue(op)
131+
);
132+
133+
//TODO: correct the value to scaled amount?
134+
emit Deposit(ETH_ID, to, msg.value);
135+
}
100136

101137
/**
102138
* @param amount the deposit amount.
103139
*/
104-
function depositERC20(IERC20 token, bytes32 to uint256 amount)
140+
function depositERC20(IERC20 token, bytes32 to, uint256 amount)
105141
external
106142
override
107143
nonReentrant
@@ -114,7 +150,27 @@ contract FluiDexDemo is AccessControl, IFluiDex, Ownable, ReentrancyGuard {
114150
uint256 balanceBeforeDeposit = token.balanceOf(address(this));
115151
token.safeTransferFrom(msg.sender, address(this), amount);
116152
uint256 balanceAfterDeposit = token.balanceOf(address(this));
117-
realAmount = balanceAfterDeposit - balanceBeforeDeposit;
153+
uint256 realAmount = balanceAfterDeposit - balanceBeforeDeposit;
154+
155+
uint16 accountId = userBjjPubkeyToUserId[to];
156+
require(accountId != 0, "non-existed user");
157+
uint128 scaledAmount = Operations.scaleTokenValueToAmount(
158+
amount,
159+
tokenScales[tokenId]
160+
);
161+
162+
Operations.Deposit memory op = Operations.Deposit({
163+
accountId: accountId,
164+
tokenId: tokenId,
165+
amount: scaledAmount
166+
});
167+
168+
addPriorityRequest(
169+
Operations.OpType.Deposit,
170+
Operations.writeDepositPubdataForPriorityQueue(op)
171+
);
172+
173+
emit Deposit(tokenId, to, realAmount);
118174
}
119175

120176
/**
@@ -195,7 +251,8 @@ contract FluiDexDemo is AccessControl, IFluiDex, Ownable, ReentrancyGuard {
195251
uint256 _block_id,
196252
uint256[] calldata _public_inputs,
197253
uint256[] calldata _serialized_proof,
198-
bytes calldata _public_data
254+
bytes calldata _public_data,
255+
bytes calldata _priority_op_index
199256
) external override returns (bool) {
200257
require(_public_inputs.length >= 2);
201258
if (_block_id == 0) {
@@ -204,6 +261,20 @@ contract FluiDexDemo is AccessControl, IFluiDex, Ownable, ReentrancyGuard {
204261
assert(_public_inputs[0] == state_roots[_block_id - 1]);
205262
}
206263

264+
//forward priority op
265+
if (_priority_op_index.length != 0) {
266+
(bool pass, uint64 nextIndex) = verifyPriorityOp(
267+
_public_data,
268+
_priority_op_index
269+
);
270+
require(pass, "handling priority ops not correct");
271+
assert(
272+
totalOpenPriorityRequests >= nextIndex - firstPriorityRequestId
273+
);
274+
totalOpenPriorityRequests -= nextIndex - firstPriorityRequestId;
275+
firstPriorityRequestId = nextIndex;
276+
}
277+
207278
if (_serialized_proof.length != 0) {
208279
bool ret = verifyBlock(
209280
_public_inputs,

contracts/FluiDexDelegate.sol

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,16 @@ contract FluiDexDelegate is
105105
uint256 _block_id,
106106
uint256[] calldata _public_inputs,
107107
uint256[] calldata _serialized_proof,
108-
bytes calldata _public_data
108+
bytes calldata _public_data,
109+
bytes calldata _priority_op_index
109110
) external override returns (bool) {
110111
return
111112
target.submitBlock(
112113
_block_id,
113114
_public_inputs,
114115
_serialized_proof,
115-
_public_data
116+
_public_data,
117+
_priority_op_index
116118
);
117119
}
118120

contracts/IFluiDex.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ interface IFluiDex {
4545
* @param _public_inputs the public inputs of this block
4646
* @param _serialized_proof the serialized proof of this block
4747
* @param _public_data the serialized tx data inside this block (data availability)
48+
* @param _priority_op_index the positions of priority op in public data
4849
* @return true if the block was accepted
4950
*/
5051
function submitBlock(
5152
uint256 _block_id,
5253
uint256[] calldata _public_inputs,
5354
uint256[] calldata _serialized_proof,
54-
bytes calldata _public_data
55+
bytes calldata _public_data,
56+
bytes calldata _priority_op_index
5557
) external returns (bool);
5658

5759
/**

contracts/IFluiDexDelegate.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ interface IFluiDexDelegate {
3333
* @param _public_inputs the public inputs of this block
3434
* @param _serialized_proof the serialized proof of this block
3535
* @param _public_data the serialized tx data inside this block (data availability)
36+
* @param _priority_op_index the positions of priority op in public data
3637
* @return true if the block was accepted
3738
*/
3839
function submitBlock(
3940
uint256 _block_id,
4041
uint256[] calldata _public_inputs,
4142
uint256[] calldata _serialized_proof,
42-
bytes calldata _public_data
43+
bytes calldata _public_data,
44+
bytes calldata _priority_op_index
4345
) external returns (bool);
4446
}

0 commit comments

Comments
 (0)