Skip to content

Commit

Permalink
handling priority op in demo contract
Browse files Browse the repository at this point in the history
  • Loading branch information
Ho committed Dec 3, 2021
1 parent 9232266 commit ba1fd07
Show file tree
Hide file tree
Showing 10 changed files with 386 additions and 137 deletions.
135 changes: 93 additions & 42 deletions contracts/Bytes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,86 @@
pragma solidity ^0.8.0;

library Bytes {

function shiftAndReverseBits8(uint8 val, uint8 offset) internal pure returns (uint256 ret) {
function shiftAndReverseBits8(uint8 val, uint8 offset)
internal
pure
returns (uint256 ret)
{
uint16 effectLen = offset < 248 ? 8 : 256 - offset;
for(uint16 i = 0; i < effectLen; i++){
if (val & 1 == 1){
ret += (1 << (255-i-offset));
for (uint16 i = 0; i < effectLen; i++) {
if (val & 1 == 1) {
ret += (1 << (255 - i - offset));
}
val >>=1;
val >>= 1;
}
}

function shiftAndReverseBits16(uint16 val, uint8 offset) internal pure returns (uint256 ret) {
function shiftAndReverseBits16(uint16 val, uint8 offset)
internal
pure
returns (uint256 ret)
{
uint16 effectLen = offset < 240 ? 16 : 256 - offset;
for(uint16 i = 0; i < effectLen; i++){
if (val & 1 == 1){
ret += (1 << (255-i-offset));
for (uint16 i = 0; i < effectLen; i++) {
if (val & 1 == 1) {
ret += (1 << (255 - i - offset));
}
val >>=1;
val >>= 1;
}
}

function shiftAndReverseBits32(uint32 val, uint8 offset) internal pure returns (uint256 ret) {
function shiftAndReverseBits32(uint32 val, uint8 offset)
internal
pure
returns (uint256 ret)
{
uint16 effectLen = offset < 224 ? 32 : 256 - offset;
for(uint16 i = 0; i < effectLen; i++){
if (val & 1 == 1){
ret += (1 << (255-i-offset));
for (uint16 i = 0; i < effectLen; i++) {
if (val & 1 == 1) {
ret += (1 << (255 - i - offset));
}
val >>=1;
val >>= 1;
}
}

function shiftAndReverseBits64(uint64 val, uint8 offset) internal pure returns (uint256 ret) {
function shiftAndReverseBits64(uint64 val, uint8 offset)
internal
pure
returns (uint256 ret)
{
uint16 effectLen = offset < 192 ? 64 : 256 - offset;
for(uint16 i = 0; i < effectLen; i++){
if (val & 1 == 1){
ret += (1 << (255-i-offset));
for (uint16 i = 0; i < effectLen; i++) {
if (val & 1 == 1) {
ret += (1 << (255 - i - offset));
}
val >>=1;
val >>= 1;
}
}

function shiftAndReverseBits128(uint128 val, uint8 offset) internal pure returns (uint256 ret) {
function shiftAndReverseBits128(uint128 val, uint8 offset)
internal
pure
returns (uint256 ret)
{
uint16 effectLen = offset < 128 ? 128 : 256 - offset;
for(uint16 i = 0; i < effectLen; i++){
if (val & 1 == 1){
ret += (1 << (255-i-offset));
for (uint16 i = 0; i < effectLen; i++) {
if (val & 1 == 1) {
ret += (1 << (255 - i - offset));
}
val >>=1;
val >>= 1;
}
}

function shiftAndReverseBits(uint256 val, uint8 offset) internal pure returns (uint256 ret) {
for(uint16 i = 0; i < 256 - offset; i++){
if (val & 1 == 1){
ret += (1 << (255-i-offset));
function shiftAndReverseBits(uint256 val, uint8 offset)
internal
pure
returns (uint256 ret)
{
for (uint16 i = 0; i < 256 - offset; i++) {
if (val & 1 == 1) {
ret += (1 << (255 - i - offset));
}
val >>=1;
val >>= 1;
}
}

Expand All @@ -68,28 +91,52 @@ library Bytes {
v = input;

// swap bytes
v = ((v & 0xFF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00) >> 8) |
((v & 0x00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF) << 8);
v =
((v &
0xFF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00) >>
8) |
((v &
0x00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF) <<
8);

// swap 2-byte long pairs
v = ((v & 0xFFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000) >> 16) |
((v & 0x0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF) << 16);
v =
((v &
0xFFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000) >>
16) |
((v &
0x0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF) <<
16);

// swap 4-byte long pairs
v = ((v & 0xFFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000) >> 32) |
((v & 0x00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF) << 32);
v =
((v &
0xFFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000) >>
32) |
((v &
0x00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF) <<
32);

// swap 8-byte long pairs
v = ((v & 0xFFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF0000000000000000) >> 64) |
((v & 0x0000000000000000FFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF) << 64);
v =
((v &
0xFFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF0000000000000000) >>
64) |
((v &
0x0000000000000000FFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF) <<
64);

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

// See comment at the top of this file for explanation of how this function works.
// NOTE: theoretically possible overflow of (_start + 0x2)
function bytesToUInt16(bytes memory _bytes, uint256 _start) internal pure returns (uint16 r) {
function bytesToUInt16(bytes memory _bytes, uint256 _start)
internal
pure
returns (uint16 r)
{
uint256 offset = _start + 0x2;
require(_bytes.length >= offset, "T");
assembly {
Expand All @@ -98,8 +145,12 @@ library Bytes {
}

// NOTE: theoretically possible overflow of (_offset + 2)
function readUInt16(bytes memory _data, uint256 _offset) internal pure returns (uint256 newOffset, uint16 r) {
function readUInt16(bytes memory _data, uint256 _offset)
internal
pure
returns (uint256 newOffset, uint16 r)
{
newOffset = _offset + 2;
r = bytesToUInt16(_data, _offset);
}
}
}
66 changes: 64 additions & 2 deletions contracts/FluiDex.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import "hardhat/console.sol"; // for debugging
import "./Events.sol";
import "./IFluiDex.sol";
import "./IVerifier.sol";
import "./Storage.sol";

/**
* @title FluiDexDemo
Expand All @@ -21,7 +22,8 @@ contract FluiDexDemo is
Events,
IFluiDex,
Ownable,
ReentrancyGuard
ReentrancyGuard,
Storage
{
using SafeERC20 for IERC20;

Expand Down Expand Up @@ -56,6 +58,7 @@ contract FluiDexDemo is

uint16 public tokenNum;
mapping(uint16 => address) public tokenIdToAddr;
mapping(uint16 => uint8) public tokenScales;
mapping(address => uint16) public tokenAddrToId;

uint16 public userNum;
Expand All @@ -72,6 +75,9 @@ contract FluiDexDemo is
grantRole(TOKEN_ADMIN_ROLE, msg.sender);
grantRole(PLUGIN_ADMIN_ROLE, msg.sender);
grantRole(DELEGATE_ROLE, msg.sender);

//TODO: define defaut scale of ETH: eth (10^18 wei) with prec 6 so we get
tokenScales[0] = 12;
}

/**
Expand Down Expand Up @@ -107,6 +113,9 @@ contract FluiDexDemo is
tokenIdToAddr[tokenId] = tokenAddr;
tokenAddrToId[tokenAddr] = tokenId;

//TODO: should provide token's prec and check token's decimal
tokenScales[tokenId] = 12;

emit NewToken(origin, tokenAddr, tokenId);
return tokenId;
}
Expand All @@ -121,6 +130,25 @@ contract FluiDexDemo is
orCreateUser(origin, to)
onlyRole(DELEGATE_ROLE)
{
uint16 accountId = userBjjPubkeyToUserId[to];
require(accountId != 0, "non-existed user");
uint128 amount = Operations.scaleTokenValueToAmount(
msg.value,
tokenScales[0]
);

Operations.Deposit memory op = Operations.Deposit({
accountId: accountId,
tokenId: 0,
amount: amount
});

addPriorityRequest(
Operations.OpType.Deposit,
Operations.writeDepositPubdataForPriorityQueue(op)
);

//TODO: correct the value to scaled amount?
emit Deposit(ETH_ID, to, msg.value);
}

Expand All @@ -146,6 +174,25 @@ contract FluiDexDemo is
token.safeTransferFrom(origin, address(this), amount);
uint256 balanceAfterDeposit = token.balanceOf(address(this));
uint256 realAmount = balanceAfterDeposit - balanceBeforeDeposit;

uint16 accountId = userBjjPubkeyToUserId[to];
require(accountId != 0, "non-existed user");
uint128 scaledAmount = Operations.scaleTokenValueToAmount(
amount,
tokenScales[tokenId]
);

Operations.Deposit memory op = Operations.Deposit({
accountId: accountId,
tokenId: tokenId,
amount: scaledAmount
});

addPriorityRequest(
Operations.OpType.Deposit,
Operations.writeDepositPubdataForPriorityQueue(op)
);

emit Deposit(tokenId, to, realAmount);
}

Expand Down Expand Up @@ -222,7 +269,8 @@ contract FluiDexDemo is
uint256 _block_id,
uint256[] calldata _public_inputs,
uint256[] calldata _serialized_proof,
bytes calldata _public_data
bytes calldata _public_data,
bytes calldata _priority_op_index
) external override returns (bool) {
require(_public_inputs.length >= 2);
if (_block_id == 0) {
Expand All @@ -231,6 +279,20 @@ contract FluiDexDemo is
assert(_public_inputs[0] == state_roots[_block_id - 1]);
}

//forward priority op
if (_priority_op_index.length != 0) {
(bool pass, uint64 nextIndex) = verifyPriorityOp(
_public_data,
_priority_op_index
);
require(pass, "handling priority ops not correct");
assert(
totalOpenPriorityRequests >= nextIndex - firstPriorityRequestId
);
totalOpenPriorityRequests -= nextIndex - firstPriorityRequestId;
firstPriorityRequestId = nextIndex;
}

if (_serialized_proof.length != 0) {
bool ret = verifyBlock(
_public_inputs,
Expand Down
6 changes: 4 additions & 2 deletions contracts/FluiDexDelegate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,16 @@ contract FluiDexDelegate is AccessControl, IFluiDexDelegate, ReentrancyGuard {
uint256 _block_id,
uint256[] calldata _public_inputs,
uint256[] calldata _serialized_proof,
bytes calldata _public_data
bytes calldata _public_data,
bytes calldata _priority_op_index
) external override returns (bool) {
return
target.submitBlock(
_block_id,
_public_inputs,
_serialized_proof,
_public_data
_public_data,
_priority_op_index
);
}
}
4 changes: 3 additions & 1 deletion contracts/IFluiDex.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ interface IFluiDex {
* @param _public_inputs the public inputs of this block
* @param _serialized_proof the serialized proof of this block
* @param _public_data the serialized tx data inside this block (data availability)
* @param _priority_op_index the positions of priority op in public data
* @return true if the block was accepted
*/
function submitBlock(
uint256 _block_id,
uint256[] calldata _public_inputs,
uint256[] calldata _serialized_proof,
bytes calldata _public_data
bytes calldata _public_data,
bytes calldata _priority_op_index
) external returns (bool);
}
4 changes: 3 additions & 1 deletion contracts/IFluiDexDelegate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ interface IFluiDexDelegate {
* @param _public_inputs the public inputs of this block
* @param _serialized_proof the serialized proof of this block
* @param _public_data the serialized tx data inside this block (data availability)
* @param _priority_op_index the positions of priority op in public data
* @return true if the block was accepted
*/
function submitBlock(
uint256 _block_id,
uint256[] calldata _public_inputs,
uint256[] calldata _serialized_proof,
bytes calldata _public_data
bytes calldata _public_data,
bytes calldata _priority_op_index
) external returns (bool);
}
Loading

0 comments on commit ba1fd07

Please sign in to comment.