From 2c4864b1d4add56ead3dfe5e2420277abaa96b31 Mon Sep 17 00:00:00 2001 From: Lyova Potyomkin Date: Wed, 18 Nov 2020 04:58:34 +0200 Subject: [PATCH] Format and add git hooks --- .githooks/pre-commit | 6 ++ contracts/contracts/Config.sol | 5 +- contracts/contracts/DeployFactory.sol | 6 +- contracts/contracts/Proxy.sol | 10 +-- contracts/contracts/Utils.sol | 10 +-- contracts/contracts/ZkSync.sol | 123 ++++++++++++-------------- etc/test_config/README.md | 8 +- package.json | 3 +- 8 files changed, 82 insertions(+), 89 deletions(-) diff --git a/.githooks/pre-commit b/.githooks/pre-commit index fb552d5ced..760d2364e5 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -23,3 +23,9 @@ if git diff --cached $VERFIER_CONTRACT_FILE | grep -lq 'constant DUMMY_VERIFIER echo "Please disable the DUMMY_VERIFIER and try to commit changes again" exit 1 fi + +if ! (yarn check:ts && yarn check:md && yarn lint:md) >/dev/null 2>&1; then + echo -e "${RED}Commit error!${NC}" + echo "Please format code via 'yarn fmt' and check markdown lints via 'yarn lint:md'" + echo "Cannot commit unformatted code" +fi diff --git a/contracts/contracts/Config.sol b/contracts/contracts/Config.sol index 44b7618ceb..6dda3427d2 100644 --- a/contracts/contracts/Config.sol +++ b/contracts/contracts/Config.sol @@ -76,7 +76,6 @@ contract Config { /// @notice Notice period before activation preparation status of upgrade mode (in seconds) // 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. - uint constant UPGRADE_NOTICE_PERIOD = MASS_FULL_EXIT_PERIOD + - PRIORITY_EXPIRATION_PERIOD + - TIME_TO_WITHDRAW_FUNDS_FROM_FULL_EXIT; + uint constant UPGRADE_NOTICE_PERIOD = + MASS_FULL_EXIT_PERIOD + PRIORITY_EXPIRATION_PERIOD + TIME_TO_WITHDRAW_FUNDS_FROM_FULL_EXIT; } diff --git a/contracts/contracts/DeployFactory.sol b/contracts/contracts/DeployFactory.sol index ebf3160cd7..cb2d149093 100644 --- a/contracts/contracts/DeployFactory.sol +++ b/contracts/contracts/DeployFactory.sol @@ -55,10 +55,8 @@ contract DeployFactory is TokenDeployInit { Proxy governance = new Proxy(address(_governanceTarget), abi.encode(this)); // set this contract as governor Proxy verifier = new Proxy(address(_verifierTarget), abi.encode()); - Proxy zkSync = new Proxy( - address(_zksyncTarget), - abi.encode(address(governance), address(verifier), _genesisRoot) - ); + Proxy zkSync = + new Proxy(address(_zksyncTarget), abi.encode(address(governance), address(verifier), _genesisRoot)); UpgradeGatekeeper upgradeGatekeeper = new UpgradeGatekeeper(zkSync); diff --git a/contracts/contracts/Proxy.sol b/contracts/contracts/Proxy.sol index e65b5d5a41..89cc10b932 100644 --- a/contracts/contracts/Proxy.sol +++ b/contracts/contracts/Proxy.sol @@ -17,9 +17,8 @@ contract Proxy is Upgradeable, UpgradeableMaster, Ownable { /// @param targetInitializationParameters Target initialization parameters constructor(address target, bytes memory targetInitializationParameters) public Ownable(msg.sender) { setTarget(target); - (bool initializationSuccess, ) = getTarget().delegatecall( - abi.encodeWithSignature("initialize(bytes)", targetInitializationParameters) - ); + (bool initializationSuccess, ) = + getTarget().delegatecall(abi.encodeWithSignature("initialize(bytes)", targetInitializationParameters)); require(initializationSuccess, "uin11"); // uin11 - target initialization failed } @@ -58,9 +57,8 @@ contract Proxy is Upgradeable, UpgradeableMaster, Ownable { requireMaster(msg.sender); setTarget(newTarget); - (bool upgradeSuccess, ) = getTarget().delegatecall( - abi.encodeWithSignature("upgrade(bytes)", newTargetUpgradeParameters) - ); + (bool upgradeSuccess, ) = + getTarget().delegatecall(abi.encodeWithSignature("upgrade(bytes)", newTargetUpgradeParameters)); require(upgradeSuccess, "ufu11"); // ufu11 - target upgrade failed } diff --git a/contracts/contracts/Utils.sol b/contracts/contracts/Utils.sol index 891befa354..25e92aebda 100644 --- a/contracts/contracts/Utils.sol +++ b/contracts/contracts/Utils.sol @@ -26,9 +26,8 @@ library Utils { address _to, uint _amount ) internal returns (bool) { - (bool callSuccess, bytes memory callReturnValueEncoded) = address(_token).call( - abi.encodeWithSignature("transfer(address,uint256)", _to, _amount) - ); + (bool callSuccess, bytes memory callReturnValueEncoded) = + address(_token).call(abi.encodeWithSignature("transfer(address,uint256)", _to, _amount)); // `transfer` method may return (bool) or nothing. bool returnedSuccess = callReturnValueEncoded.length == 0 || abi.decode(callReturnValueEncoded, (bool)); return callSuccess && returnedSuccess; @@ -48,9 +47,8 @@ library Utils { address _to, uint _amount ) internal returns (bool) { - (bool callSuccess, bytes memory callReturnValueEncoded) = address(_token).call( - abi.encodeWithSignature("transferFrom(address,address,uint256)", _from, _to, _amount) - ); + (bool callSuccess, bytes memory callReturnValueEncoded) = + address(_token).call(abi.encodeWithSignature("transferFrom(address,address,uint256)", _from, _to, _amount)); // `transferFrom` method may return (bool) or nothing. bool returnedSuccess = callReturnValueEncoded.length == 0 || abi.decode(callReturnValueEncoded, (bool)); return callSuccess && returnedSuccess; diff --git a/contracts/contracts/ZkSync.sol b/contracts/contracts/ZkSync.sol index 7e04daa0bb..60f4847740 100644 --- a/contracts/contracts/ZkSync.sol +++ b/contracts/contracts/ZkSync.sol @@ -66,10 +66,8 @@ contract ZkSync is UpgradeableMaster, Storage, Config, Events, ReentrancyGuard { function initialize(bytes calldata initializationParameters) external { initializeReentrancyGuard(); - (address _governanceAddress, address _verifierAddress, bytes32 _genesisRoot) = abi.decode( - initializationParameters, - (address, address, bytes32) - ); + (address _governanceAddress, address _verifierAddress, bytes32 _genesisRoot) = + abi.decode(initializationParameters, (address, address, bytes32)); verifier = Verifier(_verifierAddress); governance = Governance(_governanceAddress); @@ -234,12 +232,13 @@ contract ZkSync is UpgradeableMaster, Storage, Config, Events, ReentrancyGuard { } // Priority Queue request - Operations.FullExit memory op = Operations.FullExit({ - accountId: _accountId, - owner: msg.sender, - tokenId: tokenId, - amount: 0 // unknown at this point - }); + Operations.FullExit memory op = + Operations.FullExit({ + accountId: _accountId, + owner: msg.sender, + tokenId: tokenId, + amount: 0 // unknown at this point + }); bytes memory pubData = Operations.writeFullExitPubdata(op); addPriorityRequest(Operations.OpType.FullExit, pubData); @@ -351,8 +350,9 @@ contract ZkSync is UpgradeableMaster, Storage, Config, Events, ReentrancyGuard { /// @dev of existed priority requests expiration block number. /// @return bool flag that is true if the Exodus mode must be entered. function triggerExodusIfNeeded() external returns (bool) { - bool trigger = block.number >= priorityRequests[firstPriorityRequestId].expirationBlock && - priorityRequests[firstPriorityRequestId].expirationBlock != 0; + bool trigger = + block.number >= priorityRequests[firstPriorityRequestId].expirationBlock && + priorityRequests[firstPriorityRequestId].expirationBlock != 0; if (trigger) { if (!exodusMode) { exodusMode = true; @@ -414,12 +414,13 @@ contract ZkSync is UpgradeableMaster, Storage, Config, Events, ReentrancyGuard { address _owner ) internal { // Priority Queue request - Operations.Deposit memory op = Operations.Deposit({ - accountId: 0, // unknown at this point - owner: _owner, - tokenId: _tokenId, - amount: _amount - }); + Operations.Deposit memory op = + Operations.Deposit({ + accountId: 0, // unknown at this point + owner: _owner, + tokenId: _tokenId, + amount: _amount + }); bytes memory pubData = Operations.writeDepositPubdata(op); addPriorityRequest(Operations.OpType.Deposit, pubData); @@ -457,13 +458,8 @@ contract ZkSync is UpgradeableMaster, Storage, Config, Events, ReentrancyGuard { require(verifier.isBlockSizeSupported(blockChunks), "ccb11"); // Create block commitment for verification proof - bytes32 commitment = createBlockCommitment( - _blockNumber, - _feeAccount, - blocks[_blockNumber - 1].stateRoot, - _newRoot, - _publicData - ); + bytes32 commitment = + createBlockCommitment(_blockNumber, _feeAccount, blocks[_blockNumber - 1].stateRoot, _newRoot, _publicData); blocks[_blockNumber] = Block( uint32(block.number), // committed at @@ -558,10 +554,8 @@ contract ZkSync is UpgradeableMaster, Storage, Config, Events, ReentrancyGuard { pubDataPtr += DEPOSIT_BYTES; } else if (opType == Operations.OpType.PartialExit) { - Operations.PartialExit memory data = Operations.readPartialExitPubdata( - _publicData, - pubdataOffset + 1 - ); + Operations.PartialExit memory data = + Operations.readPartialExitPubdata(_publicData, pubdataOffset + 1); bool addToPendingWithdrawalsQueue = true; withdrawalsDataHash = keccak256( @@ -576,10 +570,8 @@ contract ZkSync is UpgradeableMaster, Storage, Config, Events, ReentrancyGuard { pubDataPtr += PARTIAL_EXIT_BYTES; } else if (opType == Operations.OpType.ForcedExit) { - Operations.ForcedExit memory data = Operations.readForcedExitPubdata( - _publicData, - pubdataOffset + 1 - ); + Operations.ForcedExit memory data = + Operations.readForcedExitPubdata(_publicData, pubdataOffset + 1); bool addToPendingWithdrawalsQueue = true; withdrawalsDataHash = keccak256( @@ -617,25 +609,25 @@ contract ZkSync is UpgradeableMaster, Storage, Config, Events, ReentrancyGuard { pubDataPtr += FULL_EXIT_BYTES; } else if (opType == Operations.OpType.ChangePubKey) { require(processedOperationsRequiringEthWitness < _ethWitnessSizes.length, "fcs13"); // eth witness data malformed - Operations.ChangePubKey memory op = Operations.readChangePubKeyPubdata( - _publicData, - pubdataOffset + 1 - ); + Operations.ChangePubKey memory op = + Operations.readChangePubKeyPubdata(_publicData, pubdataOffset + 1); if (_ethWitnessSizes[processedOperationsRequiringEthWitness] != 0) { - bytes memory currentEthWitness = Bytes.slice( - _ethWitness, - ethWitnessOffset, - _ethWitnessSizes[processedOperationsRequiringEthWitness] - ); - - bool valid = verifyChangePubkeySignature( - currentEthWitness, - op.pubKeyHash, - op.nonce, - op.owner, - op.accountId - ); + bytes memory currentEthWitness = + Bytes.slice( + _ethWitness, + ethWitnessOffset, + _ethWitnessSizes[processedOperationsRequiringEthWitness] + ); + + bool valid = + verifyChangePubkeySignature( + currentEthWitness, + op.pubKeyHash, + op.nonce, + op.owner, + op.accountId + ); require(valid, "fpp15"); // failed to verify change pubkey hash signature } else { bool valid = authFacts[op.owner][op.nonce] == keccak256(abi.encodePacked(op.pubKeyHash)); @@ -672,19 +664,20 @@ contract ZkSync is UpgradeableMaster, Storage, Config, Events, ReentrancyGuard { address _ethAddress, uint32 _accountId ) internal pure returns (bool) { - bytes memory signedMessage = abi.encodePacked( - "\x19Ethereum Signed Message:\n152", - "Register zkSync pubkey:\n\n", - Bytes.bytesToHexASCIIBytes(abi.encodePacked(_newPkHash)), - "\n", - "nonce: 0x", - Bytes.bytesToHexASCIIBytes(Bytes.toBytesFromUInt32(_nonce)), - "\n", - "account id: 0x", - Bytes.bytesToHexASCIIBytes(Bytes.toBytesFromUInt32(_accountId)), - "\n\n", - "Only sign this message for a trusted client!" - ); + bytes memory signedMessage = + abi.encodePacked( + "\x19Ethereum Signed Message:\n152", + "Register zkSync pubkey:\n\n", + Bytes.bytesToHexASCIIBytes(abi.encodePacked(_newPkHash)), + "\n", + "nonce: 0x", + Bytes.bytesToHexASCIIBytes(Bytes.toBytesFromUInt32(_nonce)), + "\n", + "account id: 0x", + Bytes.bytesToHexASCIIBytes(Bytes.toBytesFromUInt32(_accountId)), + "\n\n", + "Only sign this message for a trusted client!" + ); address recoveredAddress = Utils.recoverAddressFromEthSignature(_signature, signedMessage); return recoveredAddress == _ethAddress; } @@ -763,8 +756,8 @@ contract ZkSync is UpgradeableMaster, Storage, Config, Events, ReentrancyGuard { uint offset = 0; uint32 localNumberOfPendingWithdrawals = numberOfPendingWithdrawals; while (offset < withdrawalsData.length) { - (bool addToPendingWithdrawalsQueue, address _to, uint16 _tokenId, uint128 _amount) = Operations - .readWithdrawalData(withdrawalsData, offset); + (bool addToPendingWithdrawalsQueue, address _to, uint16 _tokenId, uint128 _amount) = + Operations.readWithdrawalData(withdrawalsData, offset); bytes22 packedBalanceKey = packAddressAndTokenId(_to, _tokenId); uint128 balance = balancesToWithdraw[packedBalanceKey].balanceToWithdraw; diff --git a/etc/test_config/README.md b/etc/test_config/README.md index e1c15eef45..466d7b07a0 100644 --- a/etc/test_config/README.md +++ b/etc/test_config/README.md @@ -4,7 +4,7 @@ This folder contains the data required for various zkSync tests. Directory contains two subfolders: -- `constant`: Data that remains the same between various runs, filled manually and committed to the repository. - For example, private / public keys of test accounts. -- `volatile`: Data that may change, filled by scripts and is **not** committed to the repository. - For example, deployed contracts addresses. +- `constant`: Data that remains the same between various runs, filled manually and committed to the repository. For + example, private / public keys of test accounts. +- `volatile`: Data that may change, filled by scripts and is **not** committed to the repository. For example, deployed + contracts addresses. diff --git a/package.json b/package.json index 3f7d42d7e1..41fa7eb3d6 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,8 @@ "lint:md": "markdownlint $(find . -name '*.md' -a ! -path '*/node_modules/*')", "fmt:sol": "prettier --config .prettier-sol.json --write contracts/{dev-,}contracts/**/*.sol", "check:sol": "prettier --config .prettier-sol.json --check contracts/{dev-,}contracts/**/*.sol", - "lint:sol": "solhint contracts/{dev-,}contracts/**/*.sol" + "lint:sol": "solhint contracts/{dev-,}contracts/**/*.sol", + "fmt": "yarn fmt:ts && yarn fmt:md && yarn fmt:sol" }, "devDependencies": { "markdownlint-cli": "^0.24.0",