From 95d6a5f1ff56910e6f1c7a837669a0ee34e90fc2 Mon Sep 17 00:00:00 2001 From: Roshan Date: Wed, 6 Dec 2023 14:49:29 +0800 Subject: [PATCH 1/4] chore: fix typo --- contracts/BC_fusion/TokenRecoverPortal.sol | 43 ++++++++++--------- .../interface/ITokenRecoverPortal.sol | 2 +- contracts/TendermintLightClient.sol | 4 +- contracts/TokenHub.sol | 8 ++-- scripts/generate-system.sh | 6 +-- scripts/generate-tokenRecoverPortal.sh | 4 +- scripts/generate.sh | 8 ++-- 7 files changed, 37 insertions(+), 38 deletions(-) diff --git a/contracts/BC_fusion/TokenRecoverPortal.sol b/contracts/BC_fusion/TokenRecoverPortal.sol index ae6ffcc3..b1ed3755 100644 --- a/contracts/BC_fusion/TokenRecoverPortal.sol +++ b/contracts/BC_fusion/TokenRecoverPortal.sol @@ -17,13 +17,13 @@ import "./lib/Utils.sol"; * The BC users can recover the token from TokenHub after the merkle tree root is generated. * For more details, please refer to the BEP-299(https://github.com/bnb-chain/BEPs/pull/299). */ -contract TokenRecoverPortal is ITokenRecoverPortal, ReentrancyGuardUpgradeable, System { +contract TokenRecoverPortal is ReentrancyGuardUpgradeable, System { using Utils for string; using Utils for bytes; /*----------------- init parameters -----------------*/ - string public constant sourceChainID = "Binance-Chain-Ganges"; - address public approverAddress = 0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa; + string public constant SOURCE_CHAIN_ID = "Binance-Chain-Ganges"; + address public approvalAddress = 0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa; bytes32 public merkleRoot = 0x0000000000000000000000000000000000000000000000000000000000000000; bool public merkleRootAlreadyInit = false; @@ -66,7 +66,7 @@ contract TokenRecoverPortal is ITokenRecoverPortal, ReentrancyGuardUpgradeable, /*----------------- errors -----------------*/ error AlreadyRecovered(); error InvalidProof(); - error InvalidApproverSignature(); + error InvalidApprovalSignature(); error InvalidOwnerPubKeyLength(); error InvalidOwnerSignatureLength(); error MerkleRootAlreadyInitiated(); @@ -88,7 +88,7 @@ contract TokenRecoverPortal is ITokenRecoverPortal, ReentrancyGuardUpgradeable, * @param node the leaf node of merkle tree. * @return the result of check. */ - function isRecovered(bytes32 node) public view override returns (bool) { + function isRecovered(bytes32 node) public view returns (bool) { return recoveredMap[node]; } @@ -101,7 +101,7 @@ contract TokenRecoverPortal is ITokenRecoverPortal, ReentrancyGuardUpgradeable, * @param amount is the amount of token. * @param ownerPubKey is the secp256k1 public key of the token owner on BC. * @param ownerSignature is the secp256k1 signature of the token owner on BC. - * @param approvalSignature is the eth_secp256k1 signature of the approver. + * @param approvalSignature is the eth_secp256k1 signature of the approval. * @param merkleProof is the merkle proof of the token owner on BC. */ function recover( @@ -111,7 +111,7 @@ contract TokenRecoverPortal is ITokenRecoverPortal, ReentrancyGuardUpgradeable, bytes calldata ownerSignature, bytes calldata approvalSignature, bytes32[] calldata merkleProof - ) external override merkelRootReady whenNotPaused nonReentrant { + ) external merkelRootReady whenNotPaused nonReentrant { // Recover the owner address and check signature. bytes memory ownerAddr = _verifySecp256k1Sig(ownerPubKey, ownerSignature, _tmSignatureHash(tokenSymbol, amount, msg.sender)); @@ -122,7 +122,7 @@ contract TokenRecoverPortal is ITokenRecoverPortal, ReentrancyGuardUpgradeable, if (isRecovered(node)) revert AlreadyRecovered(); // Verify the approval signature. - _verifyApproverSig(msg.sender, ownerSignature, approvalSignature, node, merkleProof); + _verifyApprovalSig(msg.sender, ownerSignature, approvalSignature, node, merkleProof); // Verify the merkle proof. if (!MerkleProof.verify(merkleProof, merkleRoot, node)) revert InvalidProof(); @@ -137,10 +137,10 @@ contract TokenRecoverPortal is ITokenRecoverPortal, ReentrancyGuardUpgradeable, } /** - * verifyApproverSig is used to verify the approver signature. - * @dev The signature is generated by the approver address(need to call a token recovery backend service). + * verifyApprovalSig is used to verify the approval signature. + * @dev The signature is generated by the approval address(need to call a token recovery backend service). */ - function _verifyApproverSig( + function _verifyApprovalSig( address account, bytes memory ownerSignature, bytes memory approvalSignature, @@ -152,8 +152,9 @@ contract TokenRecoverPortal is ITokenRecoverPortal, ReentrancyGuardUpgradeable, buffer = abi.encodePacked(buffer, merkleProof[i]); } // Perform the approvalSignature recovery and ensure the recovered signer is the approval account - bytes32 hash = keccak256(abi.encodePacked(sourceChainID, account, ownerSignature, leafHash, merkleRoot, buffer)); - if (ECDSA.recover(hash, approvalSignature) != approverAddress) revert InvalidApproverSignature(); + bytes32 hash = + keccak256(abi.encodePacked(SOURCE_CHAIN_ID, account, ownerSignature, leafHash, merkleRoot, buffer)); + if (ECDSA.recover(hash, approvalSignature) != approvalAddress) revert InvalidApprovalSignature(); } /** @@ -203,7 +204,7 @@ contract TokenRecoverPortal is ITokenRecoverPortal, ReentrancyGuardUpgradeable, return sha256( abi.encodePacked( '{"account_number":"0","chain_id":"', - sourceChainID, + SOURCE_CHAIN_ID, '","data":null,"memo":"","msgs":[{"amount":"', Utils.bytesToHex(abi.encodePacked(amount), false), '","recipient":"', @@ -216,17 +217,17 @@ contract TokenRecoverPortal is ITokenRecoverPortal, ReentrancyGuardUpgradeable, } /** - * updateParam is used to update the paramters of TokenRecoverPortal. - * @dev The paramters can only be updated by the governor. - * @param key is the key of the paramter. - * @param value is the value of the paramter. + * updateParam is used to update the parameters of TokenRecoverPortal. + * @dev The parameters can only be updated by the governor. + * @param key is the key of the parameter. + * @param value is the value of the parameter. */ function updateParam(string calldata key, bytes calldata value) external onlyGov { - if (key.compareStrings("approverAddress")) { + if (key.compareStrings("approvalAddress")) { if (value.length != 20) revert InvalidValue(key, value); - address newApprovalAddress = Utils.bytesToAddress(value, 20); + address newApprovalAddress = value.bytesToAddress(20); if (newApprovalAddress == address(0)) revert InvalidValue(key, value); - approverAddress = newApprovalAddress; + approvalAddress = newApprovalAddress; } else if (key.compareStrings("merkleRoot")) { if (merkleRootAlreadyInit) revert MerkleRootAlreadyInitiated(); if (value.length != 32) revert InvalidValue(key, value); diff --git a/contracts/BC_fusion/interface/ITokenRecoverPortal.sol b/contracts/BC_fusion/interface/ITokenRecoverPortal.sol index 38fe8e66..1b9c4f93 100644 --- a/contracts/BC_fusion/interface/ITokenRecoverPortal.sol +++ b/contracts/BC_fusion/interface/ITokenRecoverPortal.sol @@ -6,7 +6,7 @@ interface ITokenRecoverPortal { // Returns the merkle root of the merkle tree containing account balances available to recover. function merkleRoot() external view returns (bytes32); // Returns the address of the contract that is allowed to confirm the recover. - function approverAddress() external view returns (address); + function approvalAddress() external view returns (address); // Returns the address of the contract that is allowed to pause the recover. function assetProtector() external view returns (address); // Returns true if the index has been marked recovered. diff --git a/contracts/TendermintLightClient.sol b/contracts/TendermintLightClient.sol index 386f0b15..c4e7fbd1 100644 --- a/contracts/TendermintLightClient.sol +++ b/contracts/TendermintLightClient.sol @@ -23,7 +23,7 @@ contract TendermintLightClient is ILightClient, System, IParamSubscriber{ bytes32 public chainID; bytes constant public INIT_CONSENSUS_STATE_BYTES = hex"42696e616e63652d436861696e2d4e696c650000000000000000000000000000000000000000000229eca254b3859bffefaf85f4c95da9fbd26527766b784272789c30ec56b380b6eb96442aaab207bc59978ba3dd477690f5c5872334fc39e627723daa97e441e88ba4515150ec3182bc82593df36f8abb25a619187fcfab7e552b94e64ed2deed000000e8d4a51000"; - uint256 constant public INIT_REWARD_FOR_VALIDATOR_SER_CHANGE = 1e16; + uint256 constant public INIT_REWARD_FOR_VALIDATOR_SER_CHANGE = 1e16; uint256 public rewardForValidatorSetChange; event initConsensusState(uint64 initHeight, bytes32 appHash); @@ -265,4 +265,4 @@ contract TendermintLightClient is ILightClient, System, IParamSubscriber{ } emit paramChange(key, value); } -} \ No newline at end of file +} diff --git a/contracts/TokenHub.sol b/contracts/TokenHub.sol index 8cd8177d..0945b133 100644 --- a/contracts/TokenHub.sol +++ b/contracts/TokenHub.sol @@ -78,11 +78,11 @@ contract TokenHub is ITokenHub, System, IParamSubscriber, IApplication, ISystemR uint8 constant public MAXIMUM_BEP20_SYMBOL_LEN = 8; uint8 constant public BEP2_TOKEN_DECIMALS = 8; bytes32 constant public BEP2_TOKEN_SYMBOL_FOR_BNB = 0x424E420000000000000000000000000000000000000000000000000000000000; // "BNB" - uint256 constant public MAX_GAS_FOR_CALLING_BEP20=50000; - uint256 constant public MAX_GAS_FOR_TRANSFER_BNB=10000; + uint256 constant public MAX_GAS_FOR_CALLING_BEP20 = 50000; + uint256 constant public MAX_GAS_FOR_TRANSFER_BNB = 10000; - uint256 constant public INIT_MINIMUM_RELAY_FEE =2e15; - uint256 constant public REWARD_UPPER_LIMIT =1e18; + uint256 constant public INIT_MINIMUM_RELAY_FEE = 2e15; + uint256 constant public REWARD_UPPER_LIMIT = 1e18; uint256 constant public TEN_DECIMALS = 1e10; uint256 public relayFee; diff --git a/scripts/generate-system.sh b/scripts/generate-system.sh index a5eb9491..619d8552 100644 --- a/scripts/generate-system.sh +++ b/scripts/generate-system.sh @@ -1,8 +1,7 @@ #!/usr/bin/env bash # Default values -OUTPUT1="./contracts/System.sol" -OUTPUT2="./contracts/BC_fusion/System.sol" +OUTPUT="./contracts/System.sol" HEX_CHAIN_ID="0060" # Parse command line arguments @@ -21,7 +20,6 @@ while [[ "$#" -gt 0 ]]; do done # Replace the specific line -sed -i -e "s/uint16 constant public bscChainID = .*;/uint16 constant public bscChainID = 0x${HEX_CHAIN_ID};/g" "$OUTPUT1" -sed -i -e "s/uint16 public constant bscChainID = .*;/uint16 constant public bscChainID = 0x${HEX_CHAIN_ID};/g" "$OUTPUT2" +sed -i -e "s/uint16 constant public bscChainID = .*;/uint16 constant public bscChainID = 0x${HEX_CHAIN_ID};/g" "$OUTPUT" echo "System file updated." diff --git a/scripts/generate-tokenRecoverPortal.sh b/scripts/generate-tokenRecoverPortal.sh index 37b6349c..11de56b4 100644 --- a/scripts/generate-tokenRecoverPortal.sh +++ b/scripts/generate-tokenRecoverPortal.sh @@ -35,8 +35,8 @@ basedir=$( ) # Replace the specific line -sed -i -e "s/string public constant sourceChainID = .*;/string public constant sourceChainID = \"${SOURCE_CHAIN_ID}\";/g" "$OUTPUT" +sed -i -e "s/string public constant SOURCE_CHAIN_ID = .*;/string public constant SOURCE_CHAIN_ID = \"${SOURCE_CHAIN_ID}\";/g" "$OUTPUT" sed -i -e "s/address public approvalAddress = .*;/address public approvalAddress = ${APPROVAL_ADDRESS};/g" "$OUTPUT" -sed -i -e "s/bytes32 public constant override merkleRoot = .*;/bytes32 public constant override merkleRoot = ${MERKLE_ROOT};/g" "$OUTPUT" +sed -i -e "s/bytes32 public merkleRoot = .*;/bytes32 public merkleRoot = ${MERKLE_ROOT};/g" "$OUTPUT" echo "TokenRecoverPortal file updated." diff --git a/scripts/generate.sh b/scripts/generate.sh index c88acd34..6aa39004 100644 --- a/scripts/generate.sh +++ b/scripts/generate.sh @@ -45,7 +45,7 @@ function generate_local() { bash ${basedir}/generate-validatorSet.sh --initBurnRatio 1000 --network local bash ${basedir}/generate-systemReward.sh --network local bash ${basedir}/generate-slashIndicator.sh --network local - bash ${basedir}/generate-tokenRecoverPortal.sh --sourceChainId Binance-Chain-Ganges --approvalAddress 0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa --merkleRoot 0x0000000000000000000000000000000000000000000000000000000000000000 + bash ${basedir}/generate-tokenRecoverPortal.sh --sourceChainId "Binance-Chain-Ganges" --approvalAddress 0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa --merkleRoot 0x0000000000000000000000000000000000000000000000000000000000000000 forge build node ${basedir}/generate-genesis.js --chainId ${CHAIN_ID} } @@ -56,7 +56,7 @@ function generate_QA() { bash ${basedir}/generate-relayerHub.sh --network QA bash ${basedir}/generate-tendermintLightClient.sh --initConsensusStateBytes "42696e616e63652d436861696e2d47616e67657300000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000aea1ac326886b992a991d21a6eb155f41b77867cbf659e78f31d89d8205122a84d1be64f0e9a466c2e66a53433928192783e29f8fa21beb2133499b5ef770f60000000e8d4a5100099308aa365c40554bc89982af505d85da95251445d5dd4a9bb37dd2584fd92d3000000e8d4a5100001776920ff0b0f38d78cf95c033c21adf7045785114e392a7544179652e0a612000000e8d4a51000" bash ${basedir}/generate-validatorSet.sh --initBurnRatio 1000 --initValidatorSetBytes "f901a880f901a4f844941284214b9b9c85549ab3d2b972df0deef66ac2c9946ddf42a51534fc98d0c0a3b42c963cace8441ddf946ddf42a51534fc98d0c0a3b42c963cace8441ddf8410000000f84494a2959d3f95eae5dc7d70144ce1b73b403b7eb6e0948081ef03f1d9e0bb4a5bf38f16285c879299f07f948081ef03f1d9e0bb4a5bf38f16285c879299f07f8410000000f8449435552c16704d214347f29fa77f77da6d75d7c75294dc4973e838e3949c77aced16ac2315dc2d7ab11194dc4973e838e3949c77aced16ac2315dc2d7ab1118410000000f84494980a75ecd1309ea12fa2ed87a8744fbfc9b863d594cc6ac05c95a99c1f7b5f88de0e3486c82293b27094cc6ac05c95a99c1f7b5f88de0e3486c82293b2708410000000f84494f474cf03cceff28abc65c9cbae594f725c80e12d94e61a183325a18a173319dd8e19c8d069459e217594e61a183325a18a173319dd8e19c8d069459e21758410000000f84494b71b214cb885500844365e95cd9942c7276e7fd894d22ca3ba2141d23adab65ce4940eb7665ea2b6a794d22ca3ba2141d23adab65ce4940eb7665ea2b6a78410000000" - bash ${basedir}/generate-tokenRecoverPortal.sh --sourceChainId Binance-Chain-Ganges --approvalAddress 0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa --merkleRoot 0x0000000000000000000000000000000000000000000000000000000000000000 + bash ${basedir}/generate-tokenRecoverPortal.sh --sourceChainId "Binance-Chain-Ganges" --approvalAddress 0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa --merkleRoot 0x0000000000000000000000000000000000000000000000000000000000000000 forge build node ${basedir}/generate-genesis.js --chainId 714 } @@ -67,7 +67,7 @@ function generate_testnet() { bash ${basedir}/generate-relayerHub.sh --network testnet bash ${basedir}/generate-tendermintLightClient.sh --initConsensusStateBytes "42696e616e63652d436861696e2d47616e67657300000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000aea1ac326886b992a991d21a6eb155f41b77867cbf659e78f31d89d8205122a84d1be64f0e9a466c2e66a53433928192783e29f8fa21beb2133499b5ef770f60000000e8d4a5100099308aa365c40554bc89982af505d85da95251445d5dd4a9bb37dd2584fd92d3000000e8d4a5100001776920ff0b0f38d78cf95c033c21adf7045785114e392a7544179652e0a612000000e8d4a51000" bash ${basedir}/generate-validatorSet.sh --initBurnRatio 1000 --initValidatorSetBytes "f901a880f901a4f844941284214b9b9c85549ab3d2b972df0deef66ac2c9946ddf42a51534fc98d0c0a3b42c963cace8441ddf946ddf42a51534fc98d0c0a3b42c963cace8441ddf8410000000f84494a2959d3f95eae5dc7d70144ce1b73b403b7eb6e0948081ef03f1d9e0bb4a5bf38f16285c879299f07f948081ef03f1d9e0bb4a5bf38f16285c879299f07f8410000000f8449435552c16704d214347f29fa77f77da6d75d7c75294dc4973e838e3949c77aced16ac2315dc2d7ab11194dc4973e838e3949c77aced16ac2315dc2d7ab1118410000000f84494980a75ecd1309ea12fa2ed87a8744fbfc9b863d594cc6ac05c95a99c1f7b5f88de0e3486c82293b27094cc6ac05c95a99c1f7b5f88de0e3486c82293b2708410000000f84494f474cf03cceff28abc65c9cbae594f725c80e12d94e61a183325a18a173319dd8e19c8d069459e217594e61a183325a18a173319dd8e19c8d069459e21758410000000f84494b71b214cb885500844365e95cd9942c7276e7fd894d22ca3ba2141d23adab65ce4940eb7665ea2b6a794d22ca3ba2141d23adab65ce4940eb7665ea2b6a78410000000" - bash ${basedir}/generate-tokenRecoverPortal.sh --sourceChainId Binance-Chain-Ganges --approvalAddress 0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa --merkleRoot 0x0000000000000000000000000000000000000000000000000000000000000000 + bash ${basedir}/generate-tokenRecoverPortal.sh --sourceChainId "Binance-Chain-Ganges" --approvalAddress 0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa --merkleRoot 0x0000000000000000000000000000000000000000000000000000000000000000 forge build node ${basedir}/generate-genesis.js --chainId 97 } @@ -78,7 +78,7 @@ function generate_mainnet() { bash ${basedir}/generate-relayerHub.sh --network mainnet bash ${basedir}/generate-tendermintLightClient.sh --initConsensusStateBytes "42696e616e63652d436861696e2d5469677269730000000000000000000000000000000006915167cedaf7bbf7df47d932fdda630527ee648562cf3e52c5e5f46156a3a971a4ceb443c53a50d8653ef8cf1e5716da68120fb51b636dc6d111ec3277b098ecd42d49d3769d8a1f78b4c17a965f7a30d4181fabbd1f969f46d3c8e83b5ad4845421d8000000e8d4a510002ba4e81542f437b7ae1f8a35ddb233c789a8dc22734377d9b6d63af1ca403b61000000e8d4a51000df8da8c5abfdb38595391308bb71e5a1e0aabdc1d0cf38315d50d6be939b2606000000e8d4a51000b6619edca4143484800281d698b70c935e9152ad57b31d85c05f2f79f64b39f3000000e8d4a510009446d14ad86c8d2d74780b0847110001a1c2e252eedfea4753ebbbfce3a22f52000000e8d4a510000353c639f80cc8015944436dab1032245d44f912edc31ef668ff9f4a45cd0599000000e8d4a51000e81d3797e0544c3a718e1f05f0fb782212e248e784c1a851be87e77ae0db230e000000e8d4a510005e3fcda30bd19d45c4b73688da35e7da1fce7c6859b2c1f20ed5202d24144e3e000000e8d4a51000b06a59a2d75bf5d014fce7c999b5e71e7a960870f725847d4ba3235baeaa08ef000000e8d4a510000c910e2fe650e4e01406b3310b489fb60a84bc3ff5c5bee3a56d5898b6a8af32000000e8d4a5100071f2d7b8ec1c8b99a653429b0118cd201f794f409d0fea4d65b1b662f2b00063000000e8d4a51000" bash ${basedir}/generate-validatorSet.sh --initBurnRatio 1000 --initValidatorSetBytes "f905ec80f905e8f846942a7cdd959bfe8d9487b2a43b33565295a698f7e294b6a7edd747c0554875d3fc531d19ba1497992c5e941ff80f3f7f110ffd8920a3ac38fdef318fe94a3f86048c27395000f846946488aa4d1955ee33403f8ccb1d4de5fb97c7ade294220f003d8bdfaadf52aa1e55ae4cc485e6794875941a87e90e440a39c99aa9cb5cea0ad6a3f0b2407b86048c27395000f846949ef9f4360c606c7ab4db26b016007d3ad0ab86a0946103af86a874b705854033438383c82575f25bc29418e2db06cbff3e3c5f856410a1838649e760175786048c27395000f84694ee01c3b1283aa067c58eab4709f85e99d46de5fe94ee4b9bfb1871c64e2bcabb1dc382dc8b7c4218a29415904ab26ab0e99d70b51c220ccdcccabee6e29786048c27395000f84694685b1ded8013785d6623cc18d214320b6bb6475994a20ef4e5e4e7e36258dbf51f4d905114cb1b34bc9413e39085dc88704f4394d35209a02b1a9520320c86048c27395000f8469478f3adfc719c99674c072166708589033e2d9afe9448a30d5eaa7b64492a160f139e2da2800ec3834e94055838358c29edf4dcc1ba1985ad58aedbb6be2b86048c27395000f84694c2be4ec20253b8642161bc3f444f53679c1f3d479466f50c616d737e60d7ca6311ff0d9c434197898a94d1d678a2506eeaa365056fe565df8bc8659f28b086048c27395000f846942f7be8361c80a4c1e7e9aaf001d0877f1cfde218945f93992ac37f3e61db2ef8a587a436a161fd210b94ecbc4fb1a97861344dad0867ca3cba2b860411f086048c27395000f84694ce2fd7544e0b2cc94692d4a704debef7bcb613289444abc67b4b2fba283c582387f54c9cba7c34bafa948acc2ab395ded08bb75ce85bf0f95ad2abc51ad586048c27395000f84694b8f7166496996a7da21cf1f1b04d9b3e26a3d077946770572763289aac606e4f327c2f6cc1aa3b3e3b94882d745ed97d4422ca8da1c22ec49d880c4c097286048c27395000f846942d4c407bbe49438ed859fe965b140dcf1aab71a9943ad0939e120f33518fbba04631afe7a3ed6327b194b2bbb170ca4e499a2b0f3cc85ebfa6e8c4dfcbea86048c27395000f846946bbad7cf34b5fa511d8e963dbba288b1960e75d694853b0f6c324d1f4e76c8266942337ac1b0af1a229442498946a51ca5924552ead6fc2af08b94fcba648601d1a94a2000f846944430b3230294d12c6ab2aac5c2cd68e80b16b581947b107f4976a252a6939b771202c28e64e03f52d694795811a7f214084116949fc4f53cedbf189eeab28601d1a94a2000f84694ea0a6e3c511bbd10f4519ece37dc24887e11b55d946811ca77acfb221a49393c193f3a22db829fcc8e9464feb7c04830dd9ace164fc5c52b3f5a29e5018a8601d1a94a2000f846947ae2f5b9e386cd1b50a4550696d957cb4900f03a94e83bcc5077e6b873995c24bac871b5ad856047e19464e48d4057a90b233e026c1041e6012ada897fe88601d1a94a2000f8469482012708dafc9e1b880fd083b32182b869be8e09948e5adc73a2d233a1b496ed3115464dd6c7b887509428b383d324bc9a37f4e276190796ba5a8947f5ed8601d1a94a2000f8469422b81f8e175ffde54d797fe11eb03f9e3bf75f1d94a1c3ef7ca38d8ba80cce3bfc53ebd2903ed21658942767f7447f7b9b70313d4147b795414aecea54718601d1a94a2000f8469468bf0b8b6fb4e317a0f9d6f03eaf8ce6675bc60d94675cfe570b7902623f47e7f59c9664b5f5065dcf94d84f0d2e50bcf00f2fc476e1c57f5ca2d57f625b8601d1a94a2000f846948c4d90829ce8f72d0163c1d5cf348a862d5506309485c42a7b34309bee2ed6a235f86d16f059deec5894cc2cedc53f0fa6d376336efb67e43d167169f3b78601d1a94a2000f8469435e7a025f4da968de7e4d7e4004197917f4070f194b1182abaeeb3b4d8eba7e6a4162eac7ace23d57394c4fd0d870da52e73de2dd8ded19fe3d26f43a1138601d1a94a2000f84694d6caa02bbebaebb5d7e581e4b66559e635f805ff94c07335cf083c1c46a487f0325769d88e163b653694efaff03b42e41f953a925fc43720e45fb61a19938601d1a94a2000" - bash ${basedir}/generate-tokenRecoverPortal.sh --sourceChainId Binance-Chain-Tigris --approvalAddress 0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa --merkleRoot 0x0000000000000000000000000000000000000000000000000000000000000000 + bash ${basedir}/generate-tokenRecoverPortal.sh --sourceChainId "Binance-Chain-Tigris" --approvalAddress 0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa --merkleRoot 0x0000000000000000000000000000000000000000000000000000000000000000 forge build node ${basedir}/generate-genesis.js --chainId 56 } From 63fc3dc2016cc4a285c8879c99deb5c8b5da4ebf Mon Sep 17 00:00:00 2001 From: Roshan Date: Wed, 6 Dec 2023 17:36:47 +0800 Subject: [PATCH 2/4] feat: add python script --- README.md | 17 +- scripts/generate.py | 398 ++++++++++++++++++++++++++++++++++ scripts/init_holders.template | 12 +- 3 files changed, 412 insertions(+), 15 deletions(-) create mode 100644 scripts/generate.py diff --git a/README.md b/README.md index 7533242e..491e6832 100644 --- a/README.md +++ b/README.md @@ -54,18 +54,25 @@ All system contracts will be flattened and output into `${workspace}/contracts/f 1. Edit `init_holders.js` file to alloc the initial BNB holder. 2. Edit `validators.js` file to alloc the initial validator set. -3. Run `bash scripts/generate-*.sh` to change system contracts setting. +3. Edit system contracts setting as needed. 4. Run `node scripts/generate-genesis.js` will generate genesis.json ## How to generate mainnet/testnet/QA/local genesis file +You may need install some python dependencies firstly. +Save the following content to `requirements.txt` file, and run `pip install -r requirements.txt` to install them. +```txt +Jinja2==3.1.2 +typer==0.9.0 +``` + + +Then: ```shell -bash scripts/generate.sh mainnet -bash scripts/generate.sh testnet -bash scripts/generate.sh QA -bash scripts/generate.sh local +python scripts/generate.py ${network} ``` Check the `genesis.json` file, and you can get the exact compiled bytecode for different network. +(`python scripts/generate.py --help` for more details) ## How to update contract interface for test diff --git a/scripts/generate.py b/scripts/generate.py new file mode 100644 index 00000000..e8ceebb6 --- /dev/null +++ b/scripts/generate.py @@ -0,0 +1,398 @@ +import fileinput +import os +import re +import shutil +import subprocess + +import jinja2 +import typer +from typing_extensions import Annotated + +work_dir = os.getcwd() +if work_dir.endswith("scripts"): + work_dir = work_dir[:-8] + +network: str +chain_id: int +hex_chain_id: str + +main = typer.Typer() + + +def backup_file(source, destination): + try: + shutil.copyfile(source, destination) + except FileNotFoundError: + print(f"Source file '{source}' not found.") + except PermissionError: + print(f"Permission error: Unable to copy file '{source}' to '{destination}'.") + except Exception as e: + print(f"An error occurred: {e}") + + +def insert(contract, pattern, ins): + pattern = re.compile(pattern) + filepath = os.path.join(work_dir, "contracts", contract) + + found = False + with fileinput.FileInput(filepath, inplace=True) as file: + for line in file: + if not found and pattern.search(line): + print(ins) + found = True + print(line, end="") + + if not found: + raise Exception(f"{pattern} not found") + + +def replace(contract, pattern, repl): + pattern = re.compile(pattern) + filepath = os.path.join(work_dir, "contracts", contract) + + found = False + with fileinput.FileInput(filepath, inplace=True) as file: + for line in file: + if not found and pattern.search(line): + line = pattern.sub(repl, line) + found = True + print(line, end="") + + if not found: + raise Exception(f"{pattern} not found") + + +def replace_parameter(contract, parameter, value): + pattern = rf"{parameter} = .*;" + repl = f"{parameter} = {value};" + + replace(contract, pattern, repl) + + +def convert_chain_id(int_chain_id: int): + try: + hex_representation = hex(int_chain_id)[2:] + padded_hex = hex_representation.zfill(4) + return padded_hex + except Exception as e: + print(f"Error converting {int_chain_id} to hex: {e}") + return None + + +def generate_from_template(data, template_file, output_file): + template_loader = jinja2.FileSystemLoader(work_dir) + template_env = jinja2.Environment(loader=template_loader, autoescape=True) + + template = template_env.get_template(template_file) + result_string = template.render(data) + + output_path = os.path.join(work_dir, output_file) + with open(output_path, 'w') as output_file: + output_file.write(result_string) + + +def generate_cross_chain(init_batch_size="50"): + contract = "CrossChain.sol" + backup_file( + os.path.join(work_dir, "contracts", contract), os.path.join(work_dir, "contracts", contract[:-4] + ".bak") + ) + + replace_parameter(contract, "uint256 constant public CROSS_CHAIN_KEY_PREFIX", f"0x01{hex_chain_id}00") + replace_parameter(contract, "uint256 constant public INIT_BATCH_SIZE", f"{init_batch_size}") + + +def generate_relayer_hub(whitelist_1, whitelist_2): + contract = "RelayerHub.sol" + backup_file( + os.path.join(work_dir, "contracts", contract), os.path.join(work_dir, "contracts", contract[:-4] + ".bak") + ) + + replace_parameter(contract, "address public constant WHITELIST_1", f"{whitelist_1}") + replace_parameter(contract, "address public constant WHITELIST_2", f"{whitelist_2}") + + if network == "local": + replace(contract, r"function whitelistInit\(\) external", "function whitelistInit() public") + insert(contract, "alreadyInit = true;", "\t\twhitelistInit();") + + +def generate_slash_indicator(): + if network == "local": + contract = "SlashIndicator.sol" + backup_file( + os.path.join(work_dir, "contracts", contract), os.path.join(work_dir, "contracts", contract[:-4] + ".bak") + ) + + insert(contract, "alreadyInit = true;", "\t\tenableMaliciousVoteSlash = true;") + + +def generate_system(): + contract = "System.sol" + backup_file( + os.path.join(work_dir, "contracts", contract), os.path.join(work_dir, "contracts", contract[:-4] + ".bak") + ) + + replace_parameter(contract, "uint16 constant public bscChainID", f"0x{hex_chain_id}") + + +def generate_system_reward(): + if network == "local": + contract = "SystemReward.sol" + backup_file( + os.path.join(work_dir, "contracts", contract), os.path.join(work_dir, "contracts", contract[:-4] + ".bak") + ) + + insert(contract, "numOperator = 2;", "\t\toperators[VALIDATOR_CONTRACT_ADDR] = true;") + insert(contract, "numOperator = 2;", "\t\toperators[SLASH_CONTRACT_ADDR] = true;") + replace(contract, "numOperator = 2;", "numOperator = 4;") + + +def generate_tendermint_light_client(init_consensus_state_bytes, init_reward_for_validator_ser_change="1e16"): + contract = "TendermintLightClient.sol" + backup_file( + os.path.join(work_dir, "contracts", contract), os.path.join(work_dir, "contracts", contract[:-4] + ".bak") + ) + + replace_parameter( + contract, "bytes constant public INIT_CONSENSUS_STATE_BYTES", f"hex\"{init_consensus_state_bytes}\"" + ) + replace_parameter( + contract, "uint256 constant public INIT_REWARD_FOR_VALIDATOR_SER_CHANGE", + f"{init_reward_for_validator_ser_change}" + ) + + +def generate_token_hub(max_gas_for_transfer_bnb, max_gas_for_calling_bep20, reward_upper_limit, init_minimum_relay_fee): + contract = "TokenHub.sol" + backup_file( + os.path.join(work_dir, "contracts", contract), os.path.join(work_dir, "contracts", contract[:-4] + ".bak") + ) + + replace_parameter(contract, "uint256 constant public MAX_GAS_FOR_TRANSFER_BNB", f"{max_gas_for_transfer_bnb}") + replace_parameter(contract, "uint256 constant public MAX_GAS_FOR_CALLING_BEP20", f"{max_gas_for_calling_bep20}") + replace_parameter(contract, "uint256 constant public REWARD_UPPER_LIMIT", f"{reward_upper_limit}") + replace_parameter(contract, "uint256 constant public INIT_MINIMUM_RELAY_FEE", f"{init_minimum_relay_fee}") + + +def generate_token_recover_portal(source_chain_id, approval_address, merkle_root): + contract = "BC_fusion/TokenRecoverPortal.sol" + backup_file( + os.path.join(work_dir, "contracts", contract), os.path.join(work_dir, "contracts", contract[:-4] + ".bak") + ) + + replace_parameter(contract, "string public constant SOURCE_CHAIN_ID", f"\"{source_chain_id}\"") + replace_parameter(contract, "address public approvalAddress", f"{approval_address}") + replace_parameter(contract, "bytes32 public merkleRoot", f"{merkle_root}") + + +def generate_validator_set(init_burn_ratio, init_validatorset_bytes): + contract = "BSCValidatorSet.sol" + backup_file( + os.path.join(work_dir, "contracts", contract), os.path.join(work_dir, "contracts", contract[:-4] + ".bak") + ) + + replace_parameter(contract, "uint256 public constant INIT_BURN_RATIO", f"{init_burn_ratio}") + replace_parameter(contract, "bytes public constant INIT_VALIDATORSET_BYTES", f"hex\"{init_validatorset_bytes}\"") + + if network == "local": + insert( + contract, r"for \(uint i; i Date: Wed, 6 Dec 2023 20:17:42 +0800 Subject: [PATCH 3/4] fix review comments --- scripts/generate-chainId.js | 7 -- scripts/generate-crossChain.sh | 31 ------ scripts/generate-initHolders.js | 29 ------ scripts/generate-relayerHub.sh | 76 --------------- scripts/generate-relayerIncentivize.sh | 55 ----------- scripts/generate-slashIndicator.sh | 31 ------ scripts/generate-system.sh | 25 ----- scripts/generate-systemReward.sh | 36 ------- scripts/generate-tendermintLightClient.sh | 31 ------ scripts/generate-tokenHub.sh | 43 --------- scripts/generate-tokenRecoverPortal.sh | 42 --------- scripts/generate-validator.js | 36 ------- scripts/generate-validatorSet.sh | 54 ----------- scripts/generate.sh | 110 ---------------------- scripts/init_holders.template | 8 ++ 15 files changed, 8 insertions(+), 606 deletions(-) delete mode 100644 scripts/generate-chainId.js delete mode 100644 scripts/generate-crossChain.sh delete mode 100644 scripts/generate-initHolders.js delete mode 100644 scripts/generate-relayerHub.sh delete mode 100644 scripts/generate-relayerIncentivize.sh delete mode 100644 scripts/generate-slashIndicator.sh delete mode 100644 scripts/generate-system.sh delete mode 100644 scripts/generate-systemReward.sh delete mode 100644 scripts/generate-tendermintLightClient.sh delete mode 100644 scripts/generate-tokenHub.sh delete mode 100644 scripts/generate-tokenRecoverPortal.sh delete mode 100644 scripts/generate-validator.js delete mode 100644 scripts/generate-validatorSet.sh delete mode 100644 scripts/generate.sh diff --git a/scripts/generate-chainId.js b/scripts/generate-chainId.js deleted file mode 100644 index b164e481..00000000 --- a/scripts/generate-chainId.js +++ /dev/null @@ -1,7 +0,0 @@ -// Use to convert chainId from decimal to hex -function formatChainID(chainId) { - const hexString = (chainId >>> 0).toString(16); // Convert to hexadecimal and treat as unsigned - return hexString.padStart(4, '0'); // Pad with leading zeros to a length of 4 characters -} - -exports = module.exports = formatChainID; diff --git a/scripts/generate-crossChain.sh b/scripts/generate-crossChain.sh deleted file mode 100644 index c700d321..00000000 --- a/scripts/generate-crossChain.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env bash - -# Default values -OUTPUT="./contracts/CrossChain.sol" -HEX_CHAIN_ID="0060" -INIT_BATCH_SIZE_FOR_ORACLE="50" - -# Parse command line arguments -while [[ "$#" -gt 0 ]]; do - case $1 in - --hexChainId) - HEX_CHAIN_ID="$2" - shift - ;; - --initBatchSizeForOracle) - INIT_BATCH_SIZE_FOR_ORACLE="$2" - shift - ;; - *) - echo "Unknown parameter passed: $1" - exit 1 - ;; - esac - shift -done - -# Replace the specific line -sed -i -e "s/uint256 constant public CROSS_CHAIN_KEY_PREFIX = .*;/uint256 constant public CROSS_CHAIN_KEY_PREFIX = 0x01${HEX_CHAIN_ID}00;/g" "$OUTPUT" -sed -i -e "s/uint256 constant public INIT_BATCH_SIZE = .*;/uint256 constant public INIT_BATCH_SIZE = ${INIT_BATCH_SIZE_FOR_ORACLE};/g" "$OUTPUT" - -echo "CrossChain file updated." diff --git a/scripts/generate-initHolders.js b/scripts/generate-initHolders.js deleted file mode 100644 index 137165d7..00000000 --- a/scripts/generate-initHolders.js +++ /dev/null @@ -1,29 +0,0 @@ -const program = require('commander'); -const fs = require('fs'); -const nunjucks = require('nunjucks'); - -nunjucks.configure('views', { autoescape: true }); - -program.version('0.0.1'); -program.option( - '-t, --template