From 5ea6fdbaefe1b22b2211be0c59641b79231a0a48 Mon Sep 17 00:00:00 2001 From: Sergey <2901744+evercoinx@users.noreply.github.com.> Date: Fri, 20 Sep 2024 19:28:48 +0200 Subject: [PATCH] Reformat source code of some contracts, scripts and tests --- .husky/pre-commit | 2 +- .prettierignore | 2 + contracts/MaticX.sol | 4 +- contracts/lib/ExitPayloadReader.sol | 129 +++++++----------- contracts/lib/Merkle.sol | 2 +- contracts/lib/MerklePatriciaProof.sol | 17 +-- contracts/lib/RLPReader.sol | 64 ++++----- contracts/mocks/FxRootMock.sol | 7 +- contracts/mocks/ValidatorShareMock.sol | 50 +++---- .../state-transfer/FxStateChildTunnel.sol | 32 ++--- .../state-transfer/FxStateRootTunnel.sol | 8 +- contracts/state-transfer/RateProvider.sol | 8 +- contracts/tunnel/FxBaseRootTunnel.sol | 13 +- package.json | 3 +- scripts/deploy.ts | 5 +- scripts/upgradeMaticX.ts | 5 +- 16 files changed, 136 insertions(+), 215 deletions(-) diff --git a/.husky/pre-commit b/.husky/pre-commit index 45f5d67e..ade33f52 100644 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1 +1 @@ -npm run check && npm test +npm run format && npm run lint && npm run check && npm test diff --git a/.prettierignore b/.prettierignore index 69ec74d8..c5b092c0 100644 --- a/.prettierignore +++ b/.prettierignore @@ -3,6 +3,8 @@ .vscode/ artifacts/ cache/ +contracts/ChildPool.sol +contracts/interfaces/IChildPool.sol coverage/ crytic-corpus/ crytic-export/ diff --git a/contracts/MaticX.sol b/contracts/MaticX.sol index 7787c955..ef8861f9 100644 --- a/contracts/MaticX.sol +++ b/contracts/MaticX.sol @@ -312,8 +312,8 @@ contract MaticX is ); IValidatorShare(userRequest.validatorAddress).unstakeClaimTokens_newPOL( - userRequest.validatorNonce - ); + userRequest.validatorNonce + ); userRequests[_idx] = userRequests[userRequests.length - 1]; userRequests.pop(); diff --git a/contracts/lib/ExitPayloadReader.sol b/contracts/lib/ExitPayloadReader.sol index 0cfd9b93..51ecb7b6 100644 --- a/contracts/lib/ExitPayloadReader.sol +++ b/contracts/lib/ExitPayloadReader.sol @@ -28,11 +28,7 @@ library ExitPayloadReader { } // copy paste of private copy() from RLPReader to avoid changing of existing contracts - function copy( - uint256 src, - uint256 dest, - uint256 len - ) private pure { + function copy(uint256 src, uint256 dest, uint256 len) private pure { if (len == 0) return; // copy as many word sizes as possible @@ -46,7 +42,7 @@ library ExitPayloadReader { } // left over bytes. Mask is used to remove unwanted bytes from the word - uint256 mask = 256**(WORD_SIZE - len) - 1; + uint256 mask = 256 ** (WORD_SIZE - len) - 1; assembly { let srcpart := and(mload(src), not(mask)) // zero out src let destpart := and(mload(dest), mask) // retrieve the bytes @@ -54,69 +50,53 @@ library ExitPayloadReader { } } - function toExitPayload(bytes memory data) - internal - pure - returns (ExitPayload memory) - { + function toExitPayload( + bytes memory data + ) internal pure returns (ExitPayload memory) { RLPReader.RLPItem[] memory payloadData = data.toRlpItem().toList(); return ExitPayload(payloadData); } - function getHeaderNumber(ExitPayload memory payload) - internal - pure - returns (uint256) - { + function getHeaderNumber( + ExitPayload memory payload + ) internal pure returns (uint256) { return payload.data[0].toUint(); } - function getBlockProof(ExitPayload memory payload) - internal - pure - returns (bytes memory) - { + function getBlockProof( + ExitPayload memory payload + ) internal pure returns (bytes memory) { return payload.data[1].toBytes(); } - function getBlockNumber(ExitPayload memory payload) - internal - pure - returns (uint256) - { + function getBlockNumber( + ExitPayload memory payload + ) internal pure returns (uint256) { return payload.data[2].toUint(); } - function getBlockTime(ExitPayload memory payload) - internal - pure - returns (uint256) - { + function getBlockTime( + ExitPayload memory payload + ) internal pure returns (uint256) { return payload.data[3].toUint(); } - function getTxRoot(ExitPayload memory payload) - internal - pure - returns (bytes32) - { + function getTxRoot( + ExitPayload memory payload + ) internal pure returns (bytes32) { return bytes32(payload.data[4].toUint()); } - function getReceiptRoot(ExitPayload memory payload) - internal - pure - returns (bytes32) - { + function getReceiptRoot( + ExitPayload memory payload + ) internal pure returns (bytes32) { return bytes32(payload.data[5].toUint()); } - function getReceipt(ExitPayload memory payload) - internal - pure - returns (Receipt memory receipt) - { + function getReceipt( + ExitPayload memory payload + ) internal pure returns (Receipt memory receipt) { receipt.raw = payload.data[6].toBytes(); RLPReader.RLPItem memory receiptItem = receipt.raw.toRlpItem(); @@ -142,44 +122,34 @@ library ExitPayloadReader { return receipt; } - function getReceiptProof(ExitPayload memory payload) - internal - pure - returns (bytes memory) - { + function getReceiptProof( + ExitPayload memory payload + ) internal pure returns (bytes memory) { return payload.data[7].toBytes(); } - function getBranchMaskAsBytes(ExitPayload memory payload) - internal - pure - returns (bytes memory) - { + function getBranchMaskAsBytes( + ExitPayload memory payload + ) internal pure returns (bytes memory) { return payload.data[8].toBytes(); } - function getBranchMaskAsUint(ExitPayload memory payload) - internal - pure - returns (uint256) - { + function getBranchMaskAsUint( + ExitPayload memory payload + ) internal pure returns (uint256) { return payload.data[8].toUint(); } - function getReceiptLogIndex(ExitPayload memory payload) - internal - pure - returns (uint256) - { + function getReceiptLogIndex( + ExitPayload memory payload + ) internal pure returns (uint256) { return payload.data[9].toUint(); } // Receipt methods - function toBytes(Receipt memory receipt) - internal - pure - returns (bytes memory) - { + function toBytes( + Receipt memory receipt + ) internal pure returns (bytes memory) { return receipt.raw; } @@ -195,11 +165,9 @@ library ExitPayloadReader { return RLPReader.toAddress(log.list[0]); } - function getTopics(Log memory log) - internal - pure - returns (LogTopics memory) - { + function getTopics( + Log memory log + ) internal pure returns (LogTopics memory) { return LogTopics(log.list[1].toList()); } @@ -212,11 +180,10 @@ library ExitPayloadReader { } // LogTopics methods - function getField(LogTopics memory topics, uint256 index) - internal - pure - returns (RLPReader.RLPItem memory) - { + function getField( + LogTopics memory topics, + uint256 index + ) internal pure returns (RLPReader.RLPItem memory) { return topics.data[index]; } } diff --git a/contracts/lib/Merkle.sol b/contracts/lib/Merkle.sol index 4a086255..b3a85d01 100644 --- a/contracts/lib/Merkle.sol +++ b/contracts/lib/Merkle.sol @@ -12,7 +12,7 @@ library Merkle { uint256 proofHeight = proof.length / 32; // Proof of size n means, height of the tree is n+1. // In a tree of height n+1, max #leafs possible is 2 ^ n - require(index < 2**proofHeight, "Leaf index is too big"); + require(index < 2 ** proofHeight, "Leaf index is too big"); bytes32 proofElement; bytes32 computedHash = leaf; diff --git a/contracts/lib/MerklePatriciaProof.sol b/contracts/lib/MerklePatriciaProof.sol index fe9a7d88..b1bab44f 100644 --- a/contracts/lib/MerklePatriciaProof.sol +++ b/contracts/lib/MerklePatriciaProof.sol @@ -121,11 +121,9 @@ library MerklePatriciaProof { } // bytes b must be hp encoded - function _getNibbleArray(bytes memory b) - internal - pure - returns (bytes memory) - { + function _getNibbleArray( + bytes memory b + ) internal pure returns (bytes memory) { bytes memory nibbles = ""; if (b.length > 0) { uint8 offset; @@ -147,11 +145,10 @@ library MerklePatriciaProof { return nibbles; } - function _getNthNibbleOfBytes(uint256 n, bytes memory str) - private - pure - returns (bytes1) - { + function _getNthNibbleOfBytes( + uint256 n, + bytes memory str + ) private pure returns (bytes1) { return bytes1( n % 2 == 0 ? uint8(str[n / 2]) / 0x10 : uint8(str[n / 2]) % 0x10 diff --git a/contracts/lib/RLPReader.sol b/contracts/lib/RLPReader.sol index 4e4464e0..9cf02eb2 100644 --- a/contracts/lib/RLPReader.sol +++ b/contracts/lib/RLPReader.sol @@ -49,11 +49,9 @@ library RLPReader { /* * @param item RLP encoded bytes */ - function toRlpItem(bytes memory item) - internal - pure - returns (RLPItem memory) - { + function toRlpItem( + bytes memory item + ) internal pure returns (RLPItem memory) { uint256 memPtr; assembly { memPtr := add(item, 0x20) @@ -67,11 +65,9 @@ library RLPReader { * @param self The RLP item. * @return An 'Iterator' over the item. */ - function iterator(RLPItem memory self) - internal - pure - returns (Iterator memory) - { + function iterator( + RLPItem memory self + ) internal pure returns (Iterator memory) { require(isList(self)); uint256 ptr = self.memPtr + _payloadOffset(self.memPtr); @@ -95,11 +91,9 @@ library RLPReader { /* * @param item RLP encoded list in bytes */ - function toList(RLPItem memory item) - internal - pure - returns (RLPItem[] memory) - { + function toList( + RLPItem memory item + ) internal pure returns (RLPItem[] memory) { require(isList(item)); uint256 items = numItems(item); @@ -134,11 +128,9 @@ library RLPReader { * @dev A cheaper version of keccak256(toRlpBytes(item)) that avoids copying memory. * @return keccak256 hash of RLP encoded bytes. */ - function rlpBytesKeccak256(RLPItem memory item) - internal - pure - returns (bytes32) - { + function rlpBytesKeccak256( + RLPItem memory item + ) internal pure returns (bytes32) { uint256 ptr = item.memPtr; uint256 len = item.len; bytes32 result; @@ -148,11 +140,9 @@ library RLPReader { return result; } - function payloadLocation(RLPItem memory item) - internal - pure - returns (uint256, uint256) - { + function payloadLocation( + RLPItem memory item + ) internal pure returns (uint256, uint256) { uint256 offset = _payloadOffset(item.memPtr); uint256 memPtr = item.memPtr + offset; uint256 len = item.len - offset; // data length @@ -163,11 +153,9 @@ library RLPReader { * @dev A cheaper version of keccak256(toBytes(item)) that avoids copying memory. * @return keccak256 hash of the item payload. */ - function payloadKeccak256(RLPItem memory item) - internal - pure - returns (bytes32) - { + function payloadKeccak256( + RLPItem memory item + ) internal pure returns (bytes32) { (uint256 memPtr, uint256 len) = payloadLocation(item); bytes32 result; assembly { @@ -179,11 +167,9 @@ library RLPReader { /** RLPItem conversions into data types **/ // @returns raw rlp encoding in bytes - function toRlpBytes(RLPItem memory item) - internal - pure - returns (bytes memory) - { + function toRlpBytes( + RLPItem memory item + ) internal pure returns (bytes memory) { bytes memory result = new bytes(item.len); if (result.length == 0) return result; @@ -341,11 +327,7 @@ library RLPReader { * @param dest Pointer to destination * @param len Amount of memory to copy from the source */ - function copy( - uint256 src, - uint256 dest, - uint256 len - ) private pure { + function copy(uint256 src, uint256 dest, uint256 len) private pure { if (len == 0) return; // copy as many word sizes as possible @@ -361,7 +343,7 @@ library RLPReader { if (len == 0) return; // left over bytes. Mask is used to remove unwanted bytes from the word - uint256 mask = 256**(WORD_SIZE - len) - 1; + uint256 mask = 256 ** (WORD_SIZE - len) - 1; assembly { let srcpart := and(mload(src), not(mask)) // zero out src diff --git a/contracts/mocks/FxRootMock.sol b/contracts/mocks/FxRootMock.sol index 2d75fb0e..6015973c 100644 --- a/contracts/mocks/FxRootMock.sol +++ b/contracts/mocks/FxRootMock.sol @@ -6,9 +6,10 @@ import "../tunnel/FxBaseChildTunnel.sol"; contract FxRootMock { uint256 stateId; - function sendMessageToChild(address fxChildTunnel, bytes memory _message) - external - { + function sendMessageToChild( + address fxChildTunnel, + bytes memory _message + ) external { FxBaseChildTunnel(fxChildTunnel).processMessageFromRoot( stateId, msg.sender, diff --git a/contracts/mocks/ValidatorShareMock.sol b/contracts/mocks/ValidatorShareMock.sol index bfaf895e..773790c3 100644 --- a/contracts/mocks/ValidatorShareMock.sol +++ b/contracts/mocks/ValidatorShareMock.sol @@ -7,7 +7,7 @@ import "../interfaces/IValidatorShare.sol"; import "../interfaces/IStakeManager.sol"; contract ValidatorShareMock is IValidatorShare { - uint256 constant REWARD_PRECISION = 10**25; + uint256 constant REWARD_PRECISION = 10 ** 25; address public token; @@ -29,30 +29,24 @@ contract ValidatorShareMock is IValidatorShare { IStakeManager stakeManager; address public override stakingLogger; - constructor( - address _token, - address _stakeManager, - uint256 _id - ) { + constructor(address _token, address _stakeManager, uint256 _id) { token = _token; stakeManager = IStakeManager(_stakeManager); validatorId = _id; delegation = true; } - function buyVoucher(uint256 _amount, uint256) - external - override - returns (uint256) - { + function buyVoucher( + uint256 _amount, + uint256 + ) external override returns (uint256) { return _buyVoucher(_amount); } - function buyVoucherPOL(uint256 _amount, uint256) - external - override - returns (uint256) - { + function buyVoucherPOL( + uint256 _amount, + uint256 + ) external override returns (uint256) { return _buyVoucher(_amount); } @@ -60,7 +54,10 @@ contract ValidatorShareMock is IValidatorShare { _sellVoucher_new(_claimAmount); } - function sellVoucher_newPOL(uint256 _claimAmount, uint256) external override { + function sellVoucher_newPOL( + uint256 _claimAmount, + uint256 + ) external override { _sellVoucher_new(_claimAmount); } @@ -93,12 +90,9 @@ contract ValidatorShareMock is IValidatorShare { _withdrawReward(msg.sender); } - function getTotalStake(address) - external - view - override - returns (uint256, uint256) - { + function getTotalStake( + address + ) external view override returns (uint256, uint256) { //getTotalStake returns totalStake of msg.sender but we need withdrawPool return (totalStaked, 1); } @@ -119,12 +113,10 @@ contract ValidatorShareMock is IValidatorShare { return reward; } - function unbonds_new(address _address, uint256 _unbondNonce) - external - view - override - returns (DelegatorUnbond memory) - { + function unbonds_new( + address _address, + uint256 _unbondNonce + ) external view override returns (DelegatorUnbond memory) { DelegatorUnbond memory unbond = DelegatorUnbond( user2WithdrawPoolShare[_address][_unbondNonce], 2 diff --git a/contracts/state-transfer/FxStateChildTunnel.sol b/contracts/state-transfer/FxStateChildTunnel.sol index 48c2f348..a18de959 100644 --- a/contracts/state-transfer/FxStateChildTunnel.sol +++ b/contracts/state-transfer/FxStateChildTunnel.sol @@ -17,11 +17,9 @@ contract FxStateChildTunnel is FxBaseChildTunnel, AccessControl { _setupRole(DEFAULT_ADMIN_ROLE, msg.sender); } - function setFxRootTunnel(address _fxRootTunnel) - external - override - onlyRole(DEFAULT_ADMIN_ROLE) - { + function setFxRootTunnel( + address _fxRootTunnel + ) external override onlyRole(DEFAULT_ADMIN_ROLE) { fxRootTunnel = _fxRootTunnel; } @@ -53,15 +51,9 @@ contract FxStateChildTunnel is FxBaseChildTunnel, AccessControl { return balanceInMATIC; } - function convertMaticXToMatic(uint256 _balance) - public - view - returns ( - uint256, - uint256, - uint256 - ) - { + function convertMaticXToMatic( + uint256 _balance + ) public view returns (uint256, uint256, uint256) { (uint256 maticX, uint256 matic) = getReserves(); maticX = maticX == 0 ? 1 : maticX; matic = matic == 0 ? 1 : matic; @@ -71,15 +63,9 @@ contract FxStateChildTunnel is FxBaseChildTunnel, AccessControl { return (balanceInMATIC, maticX, matic); } - function convertMaticToMaticX(uint256 _balance) - public - view - returns ( - uint256, - uint256, - uint256 - ) - { + function convertMaticToMaticX( + uint256 _balance + ) public view returns (uint256, uint256, uint256) { (uint256 maticX, uint256 matic) = getReserves(); maticX = maticX == 0 ? 1 : maticX; matic = matic == 0 ? 1 : matic; diff --git a/contracts/state-transfer/FxStateRootTunnel.sol b/contracts/state-transfer/FxStateRootTunnel.sol index bf76b329..00ee0561 100644 --- a/contracts/state-transfer/FxStateRootTunnel.sol +++ b/contracts/state-transfer/FxStateRootTunnel.sol @@ -35,11 +35,9 @@ contract FxStateRootTunnel is FxBaseRootTunnel, AccessControl { maticX = _maticX; } - function setFxChildTunnel(address _fxChildTunnel) - external - override - onlyRole(DEFAULT_ADMIN_ROLE) - { + function setFxChildTunnel( + address _fxChildTunnel + ) external override onlyRole(DEFAULT_ADMIN_ROLE) { fxChildTunnel = _fxChildTunnel; } } diff --git a/contracts/state-transfer/RateProvider.sol b/contracts/state-transfer/RateProvider.sol index a40b6d95..14205161 100644 --- a/contracts/state-transfer/RateProvider.sol +++ b/contracts/state-transfer/RateProvider.sol @@ -22,11 +22,9 @@ contract RateProvider is IRateProvider, AccessControl { return IFxStateChildTunnel(fxChild).getRate(); } - function setFxChild(address _fxChild) - external - override - onlyRole(DEFAULT_ADMIN_ROLE) - { + function setFxChild( + address _fxChild + ) external override onlyRole(DEFAULT_ADMIN_ROLE) { fxChild = _fxChild; } } diff --git a/contracts/tunnel/FxBaseRootTunnel.sol b/contracts/tunnel/FxBaseRootTunnel.sol index 8a756ef3..e0678f5e 100644 --- a/contracts/tunnel/FxBaseRootTunnel.sol +++ b/contracts/tunnel/FxBaseRootTunnel.sol @@ -7,8 +7,10 @@ import { Merkle } from "../lib/Merkle.sol"; import "../lib/ExitPayloadReader.sol"; interface IFxStateSender { - function sendMessageToChild(address _receiver, bytes calldata _data) - external; + function sendMessageToChild( + address _receiver, + bytes calldata _data + ) external; } contract ICheckpointManager { @@ -69,10 +71,9 @@ abstract contract FxBaseRootTunnel { fxRoot.sendMessageToChild(fxChildTunnel, message); } - function _validateAndExtractMessage(bytes memory inputData) - internal - returns (bytes memory) - { + function _validateAndExtractMessage( + bytes memory inputData + ) internal returns (bytes memory) { ExitPayloadReader.ExitPayload memory payload = inputData .toExitPayload(); diff --git a/package.json b/package.json index 4aed5f3b..bde03296 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,7 @@ "description": "MaticX contracts", "scripts": { "prepare": "husky", - "format:sol": "prettier --write 'contracts/**/*.sol'", - "format:ts": "prettier --write 'scripts/**/*.ts' 'test/**/*.ts'", + "format": "prettier --write 'contracts/**/*.sol' 'scripts/**/*.ts' 'test/**/*.ts'", "lint": "eslint --ext .js,.ts .", "compile": "hardhat compile", "check": "hardhat check", diff --git a/scripts/deploy.ts b/scripts/deploy.ts index 11e673b7..4e68647d 100644 --- a/scripts/deploy.ts +++ b/scripts/deploy.ts @@ -4,9 +4,8 @@ import { MaticXDeployer } from "./deployers"; const main = async () => { const [rootSigner] = await ethers.getSigners(); - const maticXDeployer = await MaticXDeployer.CreateMaticXDeployer( - rootSigner - ); + const maticXDeployer = + await MaticXDeployer.CreateMaticXDeployer(rootSigner); await maticXDeployer.deploy(); await maticXDeployer.export(); }; diff --git a/scripts/upgradeMaticX.ts b/scripts/upgradeMaticX.ts index a7a3bc57..f8626774 100644 --- a/scripts/upgradeMaticX.ts +++ b/scripts/upgradeMaticX.ts @@ -12,9 +12,8 @@ const upgradeMaticX = async () => { )) as MaticX__factory; await upgrades.upgradeProxy(MaticXAddress, MaticXFactory); - const MaticXImplAddress = await upgrades.erc1967.getImplementationAddress( - MaticXAddress - ); + const MaticXImplAddress = + await upgrades.erc1967.getImplementationAddress(MaticXAddress); console.log("MaticX upgraded"); console.log("proxy:", MaticXAddress);