Skip to content

Commit

Permalink
feat: add python script (#433)
Browse files Browse the repository at this point in the history
* chore: fix typo

* feat: add python script

* fix review comments

* fix review comments
  • Loading branch information
pythonberg1997 committed Dec 7, 2023
1 parent 4a77036 commit 843e4e3
Show file tree
Hide file tree
Showing 21 changed files with 443 additions and 643 deletions.
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
7 changes: 0 additions & 7 deletions scripts/generate-chainId.js

This file was deleted.

31 changes: 0 additions & 31 deletions scripts/generate-crossChain.sh

This file was deleted.

29 changes: 0 additions & 29 deletions scripts/generate-initHolders.js

This file was deleted.

76 changes: 0 additions & 76 deletions scripts/generate-relayerHub.sh

This file was deleted.

55 changes: 0 additions & 55 deletions scripts/generate-relayerIncentivize.sh

This file was deleted.

Loading

0 comments on commit 843e4e3

Please sign in to comment.