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 foundry test #194

Merged
merged 4 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 9 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
node_modules/
build/
.idea
pids
logs
out/
.idea/
pids/
logs/
cache/

contracts/flattened/*.sol

#Hardhat files
cache
artifacts
# foundry lib
lib/forge-std/

.env

types
typechain-types
package-lock.json
yarn.lock
3 changes: 0 additions & 3 deletions .prettierignore

This file was deleted.

16 changes: 0 additions & 16 deletions .prettierrc

This file was deleted.

9 changes: 0 additions & 9 deletions .soliumignore

This file was deleted.

9 changes: 0 additions & 9 deletions .soliumrc.json

This file was deleted.

73 changes: 38 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,68 @@ This repo hold all the genesis contracts on Binance Smart chain. More details in

## Prepare

Install dependency:
Install node.js dependency:
```shell script
npm install
```
```

Node: v12.18.3
Truffle: Truffle v5.1.31
Solc: 0.6.4+commit.1dca32f3.Darwin.appleclang
Ganache-cli: v6.10.1
Install foundry:
```shell script
curl -L https://foundry.paradigm.xyz | bash
foundryup
forge install --no-git --no-commit foundry-rs/[email protected]
```

Please make sure your dependency version is as follows:

## unit test
Node: v12.18.3

Generate contracts for testing:
```shell script
# the first account of ganache
node generate-system.js --mock true --network local
node generate-systemReward.js --mock true
node generate-validatorset.js --mock true
node generate-slash.js --mock true
node generate-crosschain.js --mock true
node generate-tokenhub.js --mock true
node generate-relayerhub.js --mock true
node generate-tendermintlightclient.js --mock true
node generate-relayerincentivizecontract.js --roundSize 30 --maximumWeight 3 --mock true
```
Truffle: v5.1.31

Start ganache:
```shell script
ganache-cli --mnemonic 'clock radar mass judge dismiss just intact mind resemble fringe diary casino' --gasLimit 100000000 -e 10000 --allowUnlimitedContractSize
```
Solc: 0.6.4+commit.1dca32f3

Run truffle test:
```shell script
truffle compile
truffle migrate
truffle test
## Unit test

Add follow line to .env file in project dir, replace `archive_node` with a valid bsc mainnet node url which should be in archive mode:

```text
RPC_BSC=${archive_node}
```

Run hardhat test:
You can get a free archive node endpoint from https://nodereal.io/.

Run forge test:
```shell script
npx hardhat compile
npx hardhat test
forge test
```

Flatten all system contracts:
## Flatten all system contracts

```shell script
npm run flatten
```

## how to generate genesis file.

All system contracts will be flattened and output into `${workspace}/contracts/flattened/`.

## How to generate genesis file

1. Edit `init_holders.js` file to alloc the initial BNB holder.
2. Edit `validators.js` file to alloc the initial validator set.
3. Edit `generate-validatorset.js` file to change `fromChainId` and `toChainId`,
4. Edit `generate-tokenhub.js` file to change `refundRelayReward`, `minimumRelayFee` and `maxGasForCallingBEP20`.
5. Edit `generate-tendermintlightclient.js` file to change `chainID` and `initConsensusStateBytes`.
6. run ` node generate-genesis.js` will generate genesis.json

## How to update contract interface for test

```shell script
// get metadata
forge build

// generate interface
cast interface ${workspace}/out/{contract_name}.sol/${contract_name}.json -p ^0.8.10 -n ${contract_name} > ${workspace}/lib/interface/I${contract_name}.sol
```

## License

The library is licensed under the [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0),
Expand Down
6 changes: 2 additions & 4 deletions contracts/CrossChain.template
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import "./lib/Memory.sol";
import "./lib/BytesToTypes.sol";
import "./interface/IParamSubscriber.sol";
import "./System.sol";
{% if mock %}import "./mock/MerkleProof.sol";
{% else %}import "./MerkleProof.sol";
{% endif %}
import "./MerkleProof.sol";

contract CrossChain is System, ICrossChain, IParamSubscriber{

Expand Down Expand Up @@ -328,4 +326,4 @@ contract CrossChain is System, ICrossChain, IParamSubscriber{
}
emit paramChange(key, value);
}
}
}
18 changes: 0 additions & 18 deletions contracts/Migrations.sol

This file was deleted.

10 changes: 1 addition & 9 deletions contracts/RelayerHub.template
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,10 @@ contract RelayerHub is IRelayerHub, System, IParamSubscriber{
dues = INIT_DUES;
alreadyInit = true;
}
{% if mock %}
function register() external payable noExist onlyInit notContract noProxy{
require(msg.value == requiredDeposit, "deposit value is not exactly the same");
relayers[msg.sender] = relayer(requiredDeposit, dues);
relayersExistMap[msg.sender] = true;
emit relayerRegister(msg.sender);
}
{% else %}

function register() external payable noExist onlyInit notContract noProxy{
revert("register suspended");
}
{% endif %}

function unregister() external exist onlyInit{
relayer memory r = relayers[msg.sender];
Expand Down
4 changes: 0 additions & 4 deletions contracts/RelayerIncentivize.template
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ contract RelayerIncentivize is IRelayerIncentivize, System, IParamSubscriber {

receive() external payable{}

{% if mock %}
function addReward(address payable headerRelayerAddr, address payable packageRelayer, uint256 amount, bool fromSystemReward) external override returns (bool) {
{% else %}
function addReward(address payable headerRelayerAddr, address payable packageRelayer, uint256 amount, bool fromSystemReward) onlyInit onlyCrossChainContract external override returns (bool) {
{% endif %}
uint256 actualAmount;
if (fromSystemReward) {
actualAmount = ISystemReward(SYSTEM_REWARD_ADDR).claimRewards(address(uint160(INCENTIVIZE_ADDR)), amount.add(dynamicExtraIncentiveAmount));
Expand Down
38 changes: 2 additions & 36 deletions contracts/System.template
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,7 @@ contract System {
uint8 constant public SLASH_CHANNELID = 0x0b;
uint8 constant public CROSS_STAKE_CHANNELID = 0x10;
uint16 constant public bscChainID = 0x{{bscChainId}};
{% if mock %}
address public VALIDATOR_CONTRACT_ADDR;
address public SLASH_CONTRACT_ADDR;
address public SYSTEM_REWARD_ADDR;
address public LIGHT_CLIENT_ADDR;
address public TOKEN_HUB_ADDR;
address public INCENTIVIZE_ADDR;
address public RELAYERHUB_CONTRACT_ADDR;
address public GOV_HUB_ADDR;
address public TOKEN_MANAGER_ADDR;
address public CROSS_CHAIN_CONTRACT_ADDR;
address public STAKING_CONTRACT_ADDR;

function updateContractAddr(address valAddr, address slashAddr, address rewardAddr, address lightAddr, address tokenHubAddr,
address incentivizeAddr, address relayerHubAddr, address govHub, address tokenManagerAddr, address crossChain, address staking) external {
VALIDATOR_CONTRACT_ADDR = valAddr;
SLASH_CONTRACT_ADDR = slashAddr;
SYSTEM_REWARD_ADDR = rewardAddr;
LIGHT_CLIENT_ADDR = lightAddr;
TOKEN_HUB_ADDR = tokenHubAddr;
INCENTIVIZE_ADDR = incentivizeAddr;
RELAYERHUB_CONTRACT_ADDR = relayerHubAddr;
GOV_HUB_ADDR = govHub;
TOKEN_MANAGER_ADDR = tokenManagerAddr;
CROSS_CHAIN_CONTRACT_ADDR = crossChain;
STAKING_CONTRACT_ADDR = staking;
}{% else %}

address public constant VALIDATOR_CONTRACT_ADDR = 0x0000000000000000000000000000000000001000;
address public constant SLASH_CONTRACT_ADDR = 0x0000000000000000000000000000000000001001;
address public constant SYSTEM_REWARD_ADDR = 0x0000000000000000000000000000000000001002;
Expand All @@ -57,20 +31,12 @@ contract System {
address public constant TOKEN_MANAGER_ADDR = 0x0000000000000000000000000000000000001008;
address public constant CROSS_CHAIN_CONTRACT_ADDR = 0x0000000000000000000000000000000000002000;
address public constant STAKING_CONTRACT_ADDR = 0x0000000000000000000000000000000000002001;
{% endif %}
{% if mock %}
address public constant SYSTEM_ADDRESS = 0x9fB29AAc15b9A4B7F17c3385939b007540f4d791;

modifier onlyCoinbase() {
require(msg.sender == SYSTEM_ADDRESS, "the message sender must be the block producer");
_;
}
{% else %}
modifier onlyCoinbase() {
require(msg.sender == block.coinbase, "the message sender must be the block producer");
_;
}
{% endif %}

modifier onlyNotInit() {
require(!alreadyInit, "the contract already init");
_;
Expand Down
7 changes: 0 additions & 7 deletions contracts/mock/MerkleProof.sol

This file was deleted.

30 changes: 0 additions & 30 deletions contracts/mock/MockLightClient.sol

This file was deleted.

10 changes: 0 additions & 10 deletions contracts/mock/MockRelayerHub.sol

This file was deleted.

21 changes: 0 additions & 21 deletions contracts/mock/MockSystemReward.sol

This file was deleted.

Loading