Skip to content

Commit

Permalink
Merge branch 'dvush-new-sc' into ib-zks123-timestamps_from_reddit_dem…
Browse files Browse the repository at this point in the history
…o_branch
  • Loading branch information
dvush committed Jan 12, 2021
2 parents 143224a + c027a54 commit 0cde1cd
Show file tree
Hide file tree
Showing 84 changed files with 2,447 additions and 2,069 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions changelog/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to the core components will be documented in this file.

## Unreleased

### Changed

- The fee for normal transfers is set as expensive as the fee for transfer_to_new.

## Prior to 2020-12-23

### Added
Expand Down
25 changes: 12 additions & 13 deletions contracts/contracts/Bytes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ library Bytes {
// Copies 'len' lower bytes from 'self' into a new 'bytes memory'.
// Returns the newly created 'bytes memory'. The returned bytes will be of length 'len'.
function toBytesFromUIntTruncated(uint256 self, uint8 byteLength) private pure returns (bytes memory bts) {
require(byteLength <= 32, "a");
require(byteLength <= 32, "Q");
bts = new bytes(byteLength);
// Even though the bytes will allocate a full word, we don't want
// any potential garbage bytes in there.
Expand All @@ -53,7 +53,7 @@ library Bytes {
// NOTE: theoretically possible overflow of (_start + 20)
function bytesToAddress(bytes memory self, uint256 _start) internal pure returns (address addr) {
uint256 offset = _start + 20;
require(self.length >= offset, "b");
require(self.length >= offset, "R");
assembly {
addr := mload(add(self, offset))
}
Expand All @@ -63,7 +63,7 @@ library Bytes {
// NOTE: that bytes1..32 is stored in the beginning of the word unlike other primitive types
// NOTE: theoretically possible overflow of (_start + 20)
function bytesToBytes20(bytes memory self, uint256 _start) internal pure returns (bytes20 r) {
require(self.length >= (_start + 20), "c");
require(self.length >= (_start + 20), "S");
assembly {
r := mload(add(add(self, 0x20), _start))
}
Expand All @@ -73,7 +73,7 @@ library Bytes {
// NOTE: theoretically possible overflow of (_start + 0x2)
function bytesToUInt16(bytes memory _bytes, uint256 _start) internal pure returns (uint16 r) {
uint256 offset = _start + 0x2;
require(_bytes.length >= offset, "d");
require(_bytes.length >= offset, "T");
assembly {
r := mload(add(_bytes, offset))
}
Expand All @@ -83,7 +83,7 @@ library Bytes {
// NOTE: theoretically possible overflow of (_start + 0x3)
function bytesToUInt24(bytes memory _bytes, uint256 _start) internal pure returns (uint24 r) {
uint256 offset = _start + 0x3;
require(_bytes.length >= offset, "e");
require(_bytes.length >= offset, "U");
assembly {
r := mload(add(_bytes, offset))
}
Expand All @@ -92,7 +92,7 @@ library Bytes {
// NOTE: theoretically possible overflow of (_start + 0x4)
function bytesToUInt32(bytes memory _bytes, uint256 _start) internal pure returns (uint32 r) {
uint256 offset = _start + 0x4;
require(_bytes.length >= offset, "f");
require(_bytes.length >= offset, "V");
assembly {
r := mload(add(_bytes, offset))
}
Expand All @@ -101,7 +101,7 @@ library Bytes {
// NOTE: theoretically possible overflow of (_start + 0x10)
function bytesToUInt128(bytes memory _bytes, uint256 _start) internal pure returns (uint128 r) {
uint256 offset = _start + 0x10;
require(_bytes.length >= offset, "g");
require(_bytes.length >= offset, "W");
assembly {
r := mload(add(_bytes, offset))
}
Expand All @@ -111,7 +111,7 @@ library Bytes {
// NOTE: theoretically possible overflow of (_start + 0x14)
function bytesToUInt160(bytes memory _bytes, uint256 _start) internal pure returns (uint160 r) {
uint256 offset = _start + 0x14;
require(_bytes.length >= offset, "h");
require(_bytes.length >= offset, "X");
assembly {
r := mload(add(_bytes, offset))
}
Expand All @@ -120,7 +120,7 @@ library Bytes {
// NOTE: theoretically possible overflow of (_start + 0x20)
function bytesToBytes32(bytes memory _bytes, uint256 _start) internal pure returns (bytes32 r) {
uint256 offset = _start + 0x20;
require(_bytes.length >= offset, "i");
require(_bytes.length >= offset, "Y");
assembly {
r := mload(add(_bytes, offset))
}
Expand All @@ -135,12 +135,11 @@ library Bytes {
uint256 _start,
uint256 _length
) internal pure returns (bytes memory) {
require(_bytes.length >= (_start + _length), "j"); // bytes length is less then start byte + length bytes
require(_bytes.length >= (_start + _length), "Z"); // bytes length is less then start byte + length bytes

bytes memory tempBytes = new bytes(_length);

if (_length != 0) {
// TODO: Review this thoroughly.
assembly {
let slice_curr := add(tempBytes, 0x20)
let slice_end := add(slice_curr, _length)
Expand Down Expand Up @@ -234,8 +233,8 @@ library Bytes {

/// Trim bytes into single word
function trim(bytes memory _data, uint256 _new_length) internal pure returns (uint256 r) {
require(_new_length <= 0x20, "k"); // new_length is longer than word
require(_data.length >= _new_length, "l"); // data is to short
require(_new_length <= 0x20, "10"); // new_length is longer than word
require(_data.length >= _new_length, "11"); // data is to short

uint256 a;
assembly {
Expand Down
12 changes: 6 additions & 6 deletions contracts/contracts/Config.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ pragma solidity ^0.7.0;
/// @title zkSync configuration constants
/// @author Matter Labs
contract Config {
/// @dev ERC20 token withdrawal gas limit, used only for complete withdrawals
uint256 constant ERC20_WITHDRAWAL_GAS_LIMIT = 50000;

/// @dev ETH token withdrawal gas limit, used only for complete withdrawals
uint256 constant ETH_WITHDRAWAL_GAS_LIMIT = 10000;
/// @dev ERC20 tokens and ETH withdrawals gas limit, used only for complete withdrawals
uint256 constant WITHDRAWAL_GAS_LIMIT = 100000;

/// @dev Bytes in one chunk
uint8 constant CHUNK_BYTES = 9;
Expand Down Expand Up @@ -70,7 +67,7 @@ contract Config {
uint64 constant MAX_PRIORITY_REQUESTS_TO_DELETE_IN_VERIFY = 6;

/// @dev Reserved time for users to send full exit priority operation in case of an upgrade (in seconds)
uint256 constant MASS_FULL_EXIT_PERIOD = 3 days;
uint256 constant MASS_FULL_EXIT_PERIOD = 9 days;

/// @dev Reserved time for users to withdraw funds from full exit priority operation in case of an upgrade (in seconds)
uint256 constant TIME_TO_WITHDRAW_FUNDS_FROM_FULL_EXIT = 2 days;
Expand All @@ -93,4 +90,7 @@ contract Config {

/// @dev Bit mask to apply for verifier public input before verifying.
uint256 constant INPUT_MASK = $$(~uint256(0) >> 3);

/// @dev Auth fact reset timelock
uint256 constant AUTH_FACT_RESET_TIMELOCK = 1 days;
}
12 changes: 2 additions & 10 deletions contracts/contracts/Events.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ interface Events {
/// @notice Event emitted when a block is verified
event BlockVerification(uint32 indexed blockNumber);

/// @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 zkSync contract
event OnchainWithdrawal(address indexed owner, uint16 indexed tokenId, uint128 amount, bool success);

/// @notice Event emitted when user funds are withdrawn from the rollup
event RollupWithdrawal(address indexed owner, uint16 indexed tokenId, uint128 amount);
Expand Down Expand Up @@ -58,14 +58,6 @@ interface Events {
uint16 indexed tokenId,
uint128 amount
);

/// @notice Pending withdrawals index range that were added in the verifyBlock operation.
/// NOTE: processed indexes in the queue map are [queueStartIndex, queueEndIndex)
event PendingWithdrawalsAdd(uint32 queueStartIndex, uint32 queueEndIndex);

/// @notice Pending withdrawals index range that were executed in the completeWithdrawals operation.
/// NOTE: processed indexes in the queue map are [queueStartIndex, queueEndIndex)
event PendingWithdrawalsComplete(uint32 queueStartIndex, uint32 queueEndIndex);
}

/// @title Upgrade events
Expand Down
12 changes: 5 additions & 7 deletions contracts/contracts/Governance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ contract Governance is Config {
/// @notice Paused tokens list, deposits are impossible to create for paused tokens
mapping(uint16 => bool) public pausedTokens;

constructor() {}

/// @notice Governance contract initialization. Can be external because Proxy contract intercepts illegal calls of this function.
/// @param initializationParameters Encoded representation of initialization parameters:
/// _networkGovernor The address of network governor
Expand Down Expand Up @@ -65,8 +63,8 @@ contract Governance is Config {
/// @param _token Token address
function addToken(address _token) external {
requireGovernor(msg.sender);
require(tokenIds[_token] == 0, "bz"); // token exists
require(totalTokens < MAX_AMOUNT_OF_REGISTERED_TOKENS, "ca"); // no free identifiers for tokens
require(tokenIds[_token] == 0, "1e"); // token exists
require(totalTokens < MAX_AMOUNT_OF_REGISTERED_TOKENS, "1f"); // no free identifiers for tokens

totalTokens++;
uint16 newTokenId = totalTokens; // it is not `totalTokens - 1` because tokenId = 0 is reserved for eth
Expand Down Expand Up @@ -103,13 +101,13 @@ contract Governance is Config {
/// @notice Check if specified address is is governor
/// @param _address Address to check
function requireGovernor(address _address) public view {
require(_address == networkGovernor, "cb"); // only by governor
require(_address == networkGovernor, "1g"); // only by governor
}

/// @notice Checks if validator is active
/// @param _address Validator address
function requireActiveValidator(address _address) external view {
require(validators[_address], "cc"); // validator is not active
require(validators[_address], "1h"); // validator is not active
}

/// @notice Validate token id (must be less than or equal to total tokens amount)
Expand All @@ -124,7 +122,7 @@ contract Governance is Config {
/// @return tokens id
function validateTokenAddress(address _tokenAddr) external view returns (uint16) {
uint16 tokenId = tokenIds[_tokenAddr];
require(tokenId != 0, "cd"); // 0 is not a valid token
require(tokenId != 0, "1i"); // 0 is not a valid token
return tokenId;
}
}
34 changes: 7 additions & 27 deletions contracts/contracts/Operations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ library Operations {
(offset, parsed.amount) = Bytes.readUInt128(_data, offset); // amount
(offset, parsed.owner) = Bytes.readAddress(_data, offset); // owner

require(offset == PACKED_DEPOSIT_PUBDATA_BYTES, "m"); // reading invalid deposit pubdata size
require(offset == PACKED_DEPOSIT_PUBDATA_BYTES, "N"); // reading invalid deposit pubdata size
}

/// Serialize deposit pubdata
function writeDepositPubdata(Deposit memory op) internal pure returns (bytes memory buf) {
function writeDepositPubdataForPriorityQueue(Deposit memory op) internal pure returns (bytes memory buf) {
buf = abi.encodePacked(
uint8(OpType.Deposit),
bytes4(0), // accountId (ignored) (update when ACCOUNT_ID_BYTES is changed)
Expand All @@ -87,7 +87,7 @@ library Operations {

/// @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;
return Utils.hashBytesToBytes20(writeDepositPubdataForPriorityQueue(op)) == hashedPubdata;
}

// FullExit pubdata
Expand All @@ -111,10 +111,10 @@ library Operations {
(offset, parsed.tokenId) = Bytes.readUInt16(_data, offset); // tokenId
(offset, parsed.amount) = Bytes.readUInt128(_data, offset); // amount

require(offset == PACKED_FULL_EXIT_PUBDATA_BYTES, "n"); // reading invalid full exit pubdata size
require(offset == PACKED_FULL_EXIT_PUBDATA_BYTES, "O"); // reading invalid full exit pubdata size
}

function writeFullExitPubdata(FullExit memory op) internal pure returns (bytes memory buf) {
function writeFullExitPubdataForPriorityQueue(FullExit memory op) internal pure returns (bytes memory buf) {
buf = abi.encodePacked(
uint8(OpType.FullExit),
op.accountId, // accountId
Expand All @@ -125,14 +125,13 @@ library Operations {
}

function checkFullExitInPriorityQueue(FullExit memory op, bytes20 hashedPubdata) internal pure returns (bool) {
op.amount = 0;
return Utils.hashBytesToBytes20(writeFullExitPubdata(op)) == hashedPubdata;
return Utils.hashBytesToBytes20(writeFullExitPubdataForPriorityQueue(op)) == hashedPubdata;
}

// PartialExit pubdata

struct PartialExit {
//uint8 opType
//uint8 opType; -- present in pubdata, ignored at serialization
//uint32 accountId; -- present in pubdata, ignored at serialization
uint16 tokenId;
uint128 amount;
Expand Down Expand Up @@ -191,23 +190,4 @@ library Operations {
(offset, parsed.owner) = Bytes.readAddress(_data, offset); // owner
(offset, parsed.nonce) = Bytes.readUInt32(_data, offset); // nonce
}

// Withdrawal data process

function readWithdrawalData(bytes memory _data, uint256 _offset)
internal
pure
returns (
bool _addToPendingWithdrawalsQueue,
address _to,
uint16 _tokenId,
uint128 _amount
)
{
uint256 offset = _offset;
(offset, _addToPendingWithdrawalsQueue) = Bytes.readBool(_data, offset);
(offset, _to) = Bytes.readAddress(_data, offset);
(offset, _tokenId) = Bytes.readUInt16(_data, offset);
(offset, _amount) = Bytes.readUInt128(_data, offset);
}
}
4 changes: 2 additions & 2 deletions contracts/contracts/Ownable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ contract Ownable {
/// @notice Check if specified address is master
/// @param _address Address to check
function requireMaster(address _address) internal view {
require(_address == getMaster(), "oro11"); // oro11 - only by master
require(_address == getMaster(), "1c"); // oro11 - only by master
}

/// @notice Returns contract masters address
Expand All @@ -43,7 +43,7 @@ contract Ownable {
/// @param _newMaster New masters address
function transferMastership(address _newMaster) external {
requireMaster(msg.sender);
require(_newMaster != address(0), "otp11"); // otp11 - new masters address can't be zero address
require(_newMaster != address(0), "1d"); // otp11 - new masters address can't be zero address
setMaster(_newMaster);
}
}
2 changes: 1 addition & 1 deletion contracts/contracts/ReentrancyGuard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ contract ReentrancyGuard {
}

// On the first call to nonReentrant, _notEntered will be true
require(notEntered, "o");
require(notEntered, "1b");

// Any calls to nonReentrant after this point will fail
assembly {
Expand Down
10 changes: 5 additions & 5 deletions contracts/contracts/SafeCast.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ library SafeCast {
* - input must fit into 128 bits
*/
function toUint128(uint256 value) internal pure returns (uint128) {
require(value < 2**128, "p");
require(value < 2**128, "16");
return uint128(value);
}

Expand All @@ -46,7 +46,7 @@ library SafeCast {
* - input must fit into 64 bits
*/
function toUint64(uint256 value) internal pure returns (uint64) {
require(value < 2**64, "q");
require(value < 2**64, "17");
return uint64(value);
}

Expand All @@ -61,7 +61,7 @@ library SafeCast {
* - input must fit into 32 bits
*/
function toUint32(uint256 value) internal pure returns (uint32) {
require(value < 2**32, "r");
require(value < 2**32, "18");
return uint32(value);
}

Expand All @@ -76,7 +76,7 @@ library SafeCast {
* - input must fit into 16 bits
*/
function toUint16(uint256 value) internal pure returns (uint16) {
require(value < 2**16, "s");
require(value < 2**16, "19");
return uint16(value);
}

Expand All @@ -91,7 +91,7 @@ library SafeCast {
* - input must fit into 8 bits.
*/
function toUint8(uint256 value) internal pure returns (uint8) {
require(value < 2**8, "t");
require(value < 2**8, "1a");
return uint8(value);
}
}
Loading

0 comments on commit 0cde1cd

Please sign in to comment.