Skip to content

Commit

Permalink
Contracts uncommented
Browse files Browse the repository at this point in the history
  • Loading branch information
dvush committed Nov 16, 2020
1 parent ac006ce commit 8b8efea
Show file tree
Hide file tree
Showing 20 changed files with 224 additions and 185 deletions.
58 changes: 21 additions & 37 deletions contracts/contracts/Operations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ library Operations {

// Byte lengths

uint8 constant OP_TYPE_BYTES = 1;

uint8 constant TOKEN_BYTES = 2;

uint8 constant PUBKEY_BYTES = 32;
Expand Down Expand Up @@ -54,15 +56,15 @@ library Operations {
address owner;
}

uint public constant PACKED_DEPOSIT_PUBDATA_BYTES =
ACCOUNT_ID_BYTES + TOKEN_BYTES + AMOUNT_BYTES + ADDRESS_BYTES + 1;
uint public constant PACKED_DEPOSIT_PUBDATA_BYTES =
OP_TYPE_BYTES + ACCOUNT_ID_BYTES + TOKEN_BYTES + AMOUNT_BYTES + ADDRESS_BYTES;

/// Deserialize deposit pubdata
function readDepositPubdata(bytes memory _data) internal pure
returns (Deposit memory parsed)
{
// NOTE: there is no check that variable sizes are same as constants (i.e. TOKEN_BYTES), fix if possible.
uint offset = 1;
uint offset = OP_TYPE_BYTES;
(offset, parsed.accountId) = Bytes.readUInt32(_data, offset); // accountId
(offset, parsed.tokenId) = Bytes.readUInt16(_data, offset); // tokenId
(offset, parsed.amount) = Bytes.readUInt128(_data, offset); // amount
Expand All @@ -84,9 +86,10 @@ library Operations {

/// @notice Check that deposit pubdata from request and block matches
function depositPubdataMatch(bytes memory _lhs, bytes memory _rhs) internal pure returns (bool) {
// We must ignore `accountId` because it is present in block pubdata but not in priority queue
bytes memory lhs_trimmed = Bytes.slice(_lhs, ACCOUNT_ID_BYTES + 1, PACKED_DEPOSIT_PUBDATA_BYTES - 1 - ACCOUNT_ID_BYTES);
bytes memory rhs_trimmed = Bytes.slice(_rhs, ACCOUNT_ID_BYTES + 1, PACKED_DEPOSIT_PUBDATA_BYTES - 1 - ACCOUNT_ID_BYTES);
// We must ignore `accountId` and operation type because it is present in block pubdata but not in priority queue
uint skipBytes = ACCOUNT_ID_BYTES + OP_TYPE_BYTES;
bytes memory lhs_trimmed = Bytes.slice(_lhs, skipBytes , PACKED_DEPOSIT_PUBDATA_BYTES - skipBytes);
bytes memory rhs_trimmed = Bytes.slice(_rhs, skipBytes, PACKED_DEPOSIT_PUBDATA_BYTES - skipBytes);
return keccak256(lhs_trimmed) == keccak256(rhs_trimmed);
}

Expand All @@ -99,14 +102,14 @@ library Operations {
uint128 amount;
}

uint public constant PACKED_FULL_EXIT_PUBDATA_BYTES = 1 +
ACCOUNT_ID_BYTES + ADDRESS_BYTES + TOKEN_BYTES + AMOUNT_BYTES;
uint public constant PACKED_FULL_EXIT_PUBDATA_BYTES =
OP_TYPE_BYTES + ACCOUNT_ID_BYTES + ADDRESS_BYTES + TOKEN_BYTES + AMOUNT_BYTES;

function readFullExitPubdata(bytes memory _data) internal pure
returns (FullExit memory parsed)
{
// NOTE: there is no check that variable sizes are same as constants (i.e. TOKEN_BYTES), fix if possible.
uint offset = 1;
uint offset = OP_TYPE_BYTES;
(offset, parsed.accountId) = Bytes.readUInt32(_data, offset); // accountId
(offset, parsed.owner) = Bytes.readAddress(_data, offset); // owner
(offset, parsed.tokenId) = Bytes.readUInt16(_data, offset); // tokenId
Expand Down Expand Up @@ -136,37 +139,29 @@ library Operations {
// PartialExit pubdata

struct PartialExit {
//uint8 opType
//uint32 accountId; -- present in pubdata, ignored at serialization
uint16 tokenId;
uint128 amount;
//uint16 fee; -- present in pubdata, ignored at serialization
address owner;
}

function readPartialExitPubdata(bytes memory _data, uint _offset) internal pure
function readPartialExitPubdata(bytes memory _data) internal pure
returns (PartialExit memory parsed)
{
// NOTE: there is no check that variable sizes are same as constants (i.e. TOKEN_BYTES), fix if possible.
uint offset = _offset + 1 + ACCOUNT_ID_BYTES; // accountId (ignored)
uint offset = OP_TYPE_BYTES + ACCOUNT_ID_BYTES; // opType + accountId (ignored)
(offset, parsed.tokenId) = Bytes.readUInt16(_data, offset); // tokenId
(offset, parsed.amount) = Bytes.readUInt128(_data, offset); // amount
offset += FEE_BYTES; // fee (ignored)
(offset, parsed.owner) = Bytes.readAddress(_data, offset); // owner
}

function writePartialExitPubdata(PartialExit memory op) internal pure returns (bytes memory buf) {
buf = abi.encodePacked(
bytes4(0), // accountId (ignored) (update when ACCOUNT_ID_BYTES is changed)
op.tokenId, // tokenId
op.amount, // amount
bytes2(0), // fee (ignored) (update when FEE_BYTES is changed)
op.owner // owner
);
}

// ForcedExit pubdata

struct ForcedExit {
//uint8 opType; -- present in pubdata, ignored at serialization
//uint32 initiatorAccountId; -- present in pubdata, ignored at serialization
//uint32 targetAccountId; -- present in pubdata, ignored at serialization
uint16 tokenId;
Expand All @@ -175,31 +170,20 @@ library Operations {
address target;
}

function readForcedExitPubdata(bytes memory _data, uint _offset) internal pure
function readForcedExitPubdata(bytes memory _data) internal pure
returns (ForcedExit memory parsed)
{
// NOTE: there is no check that variable sizes are same as constants (i.e. TOKEN_BYTES), fix if possible.
uint offset = _offset + 1 + ACCOUNT_ID_BYTES * 2; // initiatorAccountId + targetAccountId (ignored)
uint offset = OP_TYPE_BYTES + ACCOUNT_ID_BYTES * 2; // opType + initiatorAccountId + targetAccountId (ignored)
(offset, parsed.tokenId) = Bytes.readUInt16(_data, offset); // tokenId
(offset, parsed.amount) = Bytes.readUInt128(_data, offset); // amount
offset += FEE_BYTES; // fee (ignored)
(offset, parsed.target) = Bytes.readAddress(_data, offset); // target
}

function writeForcedExitPubdata(ForcedExit memory op) internal pure returns (bytes memory buf) {
buf = abi.encodePacked(
bytes4(0), // initiatorAccountId (ignored) (update when ACCOUNT_ID_BYTES is changed)
bytes4(0), // targetAccountId (ignored) (update when ACCOUNT_ID_BYTES is changed)
op.tokenId, // tokenId
op.amount, // amount
bytes2(0), // fee (ignored) (update when FEE_BYTES is changed)
op.target // target
);
}

// ChangePubKey

struct ChangePubKey {
// uint8 opType; -- present in pubdata, ignored at serialization
uint32 accountId;
bytes20 pubKeyHash;
address owner;
Expand All @@ -208,10 +192,10 @@ library Operations {
//uint16 fee; -- present in pubdata, ignored at serialization
}

function readChangePubKeyPubdata(bytes memory _data, uint _offset) internal pure
function readChangePubKeyPubdata(bytes memory _data) internal pure
returns (ChangePubKey memory parsed)
{
uint offset = _offset;
uint offset = OP_TYPE_BYTES;
(offset, parsed.accountId) = Bytes.readUInt32(_data, offset); // accountId
(offset, parsed.pubKeyHash) = Bytes.readBytes20(_data, offset); // pubKeyHash
(offset, parsed.owner) = Bytes.readAddress(_data, offset); // owner
Expand Down
12 changes: 11 additions & 1 deletion contracts/contracts/Proxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ contract Proxy is Upgradeable, UpgradeableMaster, Ownable {
/// @notice Performs a delegatecall to the contract implementation
/// @dev Fallback function allowing to perform a delegatecall to the given implementation
/// This function will return whatever the implementation call returns
fallback () external payable {
function _fallback () internal {
address _target = getTarget();
assembly {
// The pointer to the free memory slot
Expand Down Expand Up @@ -104,6 +104,16 @@ contract Proxy is Upgradeable, UpgradeableMaster, Ownable {
}
}

/// @notice Will run when no functions matches call data
fallback () external payable {
_fallback();
}

/// @notice Same as fallback but called when calldata is empty
receive () external payable {
_fallback();
}

/// UpgradeableMaster functions

/// @notice Notice period before activation preparation status of upgrade mode
Expand Down
18 changes: 18 additions & 0 deletions contracts/contracts/Storage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

pragma solidity ^0.7.0;

pragma experimental ABIEncoderV2;

import "./IERC20.sol";

import "./Governance.sol";
Expand Down Expand Up @@ -127,7 +129,23 @@ contract Storage {
return balancesToWithdraw[packAddressAndTokenId(_address, _tokenId)].balanceToWithdraw;
}

/// @notice Block info stored hashed in contract storage
struct StoredBlockInfo {
uint32 blockNumber;
uint64 priorityOperations;
bytes32 processableOnchainOperationsHash;
bytes32 stateHash;
bytes32 commitment;
}

/// @notice Hash StoredBlockInfo
function hashStoredBlockInfo(StoredBlockInfo memory _storedBlockInfo) internal pure returns (bytes32) {
return keccak256(abi.encode(_storedBlockInfo));
}

/// @notice Stored hashed StoredBlockInfo for some block number
mapping(uint32 => bytes32) public hashedBlocks;

/// @notice Stores verified commitments hashed in one slot.
mapping(bytes32 => bool) public hashedVerifiedCommitments;
}
5 changes: 5 additions & 0 deletions contracts/contracts/Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,9 @@ library Utils {

return ecrecover(keccak256(_message), signV, signR, signS);
}

/// @notice Returns new_hash = hash(old_hash + bytes)
function addBytesToHash(bytes32 _hash, bytes memory _bytes) internal pure returns (bytes32) {
return keccak256(abi.encodePacked(_hash, _bytes));
}
}
Loading

0 comments on commit 8b8efea

Please sign in to comment.