Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add python script #433

Merged
merged 4 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
43 changes: 22 additions & 21 deletions contracts/BC_fusion/TokenRecoverPortal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -66,7 +66,7 @@ contract TokenRecoverPortal is ITokenRecoverPortal, ReentrancyGuardUpgradeable,
/*----------------- errors -----------------*/
error AlreadyRecovered();
error InvalidProof();
error InvalidApproverSignature();
error InvalidApprovalSignature();
error InvalidOwnerPubKeyLength();
error InvalidOwnerSignatureLength();
error MerkleRootAlreadyInitiated();
Expand All @@ -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];
}

Expand All @@ -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(
Expand All @@ -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));
Expand All @@ -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();
Expand All @@ -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,
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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":"',
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion contracts/BC_fusion/interface/ITokenRecoverPortal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions contracts/TendermintLightClient.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -265,4 +265,4 @@ contract TendermintLightClient is ILightClient, System, IParamSubscriber{
}
emit paramChange(key, value);
}
}
}
8 changes: 4 additions & 4 deletions contracts/TokenHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions scripts/generate-system.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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."
4 changes: 2 additions & 2 deletions scripts/generate-tokenRecoverPortal.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Loading
Loading