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/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-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