Skip to content

Commit

Permalink
Fix contracts review
Browse files Browse the repository at this point in the history
  • Loading branch information
dvush committed Nov 27, 2020
1 parent 0e33f87 commit fae1939
Show file tree
Hide file tree
Showing 14 changed files with 273 additions and 310 deletions.
6 changes: 5 additions & 1 deletion contracts/contracts/Config.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ contract Config {
/// @dev Notice period before activation preparation status of upgrade mode (in seconds)
/// @dev NOTE: we must reserve for users enough time to send full exit operation, wait maximum time for processing this operation and withdraw funds from it.
uint256 constant UPGRADE_NOTICE_PERIOD =
$(defined(UPGRADE_NOTICE_PERIOD) ? UPGRADE_NOTICE_PERIOD : MASS_FULL_EXIT_PERIOD + PRIORITY_EXPIRATION_PERIOD + TIME_TO_WITHDRAW_FUNDS_FROM_FULL_EXIT);
$(
defined(UPGRADE_NOTICE_PERIOD)
? UPGRADE_NOTICE_PERIOD
: MASS_FULL_EXIT_PERIOD + PRIORITY_EXPIRATION_PERIOD + TIME_TO_WITHDRAW_FUNDS_FROM_FULL_EXIT
);

/// @dev Timestamp - seconds since unix epoch
uint256 constant COMMIT_TIMESTAMP_NOT_OLDER = 8 hours;
Expand Down
5 changes: 4 additions & 1 deletion contracts/contracts/Events.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ interface Events {
/// @notice Event emitted when a block is verified
event BlockVerification(uint32 indexed blockNumber);

/// @notice Event emitted when user send a transaction to withdraw her funds from onchain balance
/// @notice Event emitted when user funds are withdrawn from the account
event OnchainWithdrawal(address indexed owner, uint16 indexed tokenId, uint128 amount);

/// @notice Event emitted when user funds are withdrawn from the rollup
event RollupWithdrawal(address indexed owner, uint16 indexed tokenId, uint128 amount);

/// @notice Event emitted when user send a transaction to deposit her funds
event OnchainDeposit(address indexed sender, uint16 indexed tokenId, uint128 amount, address indexed owner);

Expand Down
22 changes: 8 additions & 14 deletions contracts/contracts/Operations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;

import "./Bytes.sol";
import "./Utils.sol";

/// @title zkSync operations tools
library Operations {
Expand Down Expand Up @@ -84,13 +85,9 @@ 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` and operation type because it is present in block pubdata but not in priority queue
uint256 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);
/// @notice Write deposit pubdata for priority queue check.
function checkDepositInPriorityQueue(Deposit memory op, bytes20 hashedPubdata) internal pure returns (bool) {
return Utils.hashBytesToBytes20(writeDepositPubdata(op)) == hashedPubdata;
}

// FullExit pubdata
Expand Down Expand Up @@ -123,16 +120,13 @@ library Operations {
op.accountId, // accountId
op.owner, // owner
op.tokenId, // tokenId
op.amount // amount
uint128(0) // amount -- ignored
);
}

/// @notice Check that full exit pubdata from request and block matches
function fullExitPubdataMatch(bytes memory _lhs, bytes memory _rhs) internal pure returns (bool) {
// `amount` is ignored because it is present in block pubdata but not in priority queue
uint256 lhs = Bytes.trim(_lhs, PACKED_FULL_EXIT_PUBDATA_BYTES - AMOUNT_BYTES);
uint256 rhs = Bytes.trim(_rhs, PACKED_FULL_EXIT_PUBDATA_BYTES - AMOUNT_BYTES);
return lhs == rhs;
function checkFullExitInPriorityQueue(FullExit memory op, bytes20 hashedPubdata) internal pure returns (bool) {
op.amount = 0;
return Utils.hashBytesToBytes20(writeFullExitPubdata(op)) == hashedPubdata;
}

// PartialExit pubdata
Expand Down
34 changes: 20 additions & 14 deletions contracts/contracts/Storage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,6 @@ contract Storage {
}
mapping(uint32 => Block_DEPRECATED) public blocks_DEPRECATED;

/// @notice Onchain operations - operations processed inside rollup blocks
/// @member opType Onchain operation type
/// @member amount Amount used in the operation
/// @member pubData Operation pubdata
struct OnchainOperation {
Operations.OpType opType;
bytes pubData;
}

/// @notice Flag indicates that a user has exited certain token balance (per account id and tokenId)
mapping(uint32 => mapping(uint16 => bool)) public exited;

Expand All @@ -89,11 +80,11 @@ contract Storage {
/// @notice User authenticated fact hashes for some nonce.
mapping(address => mapping(uint32 => bytes32)) public authFacts;

/// @notice Priority Operation container
/// @notice Old Priority Operation container
/// @member opType Priority operation type
/// @member pubData Priority operation public data
/// @member expirationBlock Expiration block number (ETH block) for this request (must be satisfied before)
struct PriorityOperation {
struct PriorityOperation_DEPRECATED {
Operations.OpType opType;
bytes pubData;
uint256 expirationBlock;
Expand All @@ -102,7 +93,7 @@ contract Storage {
/// @notice Priority Requests mapping (request id - operation)
/// @dev Contains op type, pubdata and expiration block of unsatisfied requests.
/// @dev Numbers are in order of requests receiving
mapping(uint64 => PriorityOperation) public priorityRequests;
mapping(uint64 => PriorityOperation_DEPRECATED) public priorityRequests_DEPRECATED;

/// @notice First open priority request id
uint64 public firstPriorityRequestId;
Expand Down Expand Up @@ -148,6 +139,21 @@ contract Storage {
/// @notice Stored hashed StoredBlockInfo for some block number
mapping(uint32 => bytes32) public storedBlockHashes;

/// @notice Stores verified commitments hashed in one slot.
mapping(bytes32 => bool) public verifiedCommitmentHashes;
/// @notice Total blocks proofed.
uint32 public totalBlocksProofed;

/// @notice Priority Operation container
/// @member hashedPubData Hashed priority operation public data
/// @member expirationBlock Expiration block number (ETH block) for this request (must be satisfied before)
/// @member opType Priority operation type
struct PriorityOperation {
bytes20 hashedPubData;
uint64 expirationBlock;
Operations.OpType opType;
}

/// @notice Priority Requests mapping (request id - operation)
/// @dev Contains op type, pubdata and expiration block of unsatisfied requests.
/// @dev Numbers are in order of requests receiving
mapping(uint64 => PriorityOperation) public priorityRequests;
}
4 changes: 4 additions & 0 deletions contracts/contracts/Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,8 @@ library Utils {
function concatHash(bytes32 _hash, bytes memory _bytes) internal pure returns (bytes32) {
return keccak256(abi.encodePacked(_hash, _bytes));
}

function hashBytesToBytes20(bytes memory _bytes) internal pure returns (bytes20) {
return bytes20(uint160(uint256(keccak256(_bytes))));
}
}
Loading

0 comments on commit fae1939

Please sign in to comment.