Skip to content

Commit

Permalink
Format and add git hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
ly0va committed Nov 18, 2020
1 parent 1fba8b6 commit 2c4864b
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 89 deletions.
6 changes: 6 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 2 additions & 3 deletions contracts/contracts/Config.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 2 additions & 4 deletions contracts/contracts/DeployFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
10 changes: 4 additions & 6 deletions contracts/contracts/Proxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
10 changes: 4 additions & 6 deletions contracts/contracts/Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
123 changes: 58 additions & 65 deletions contracts/contracts/ZkSync.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions etc/test_config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 2c4864b

Please sign in to comment.