From ddf712d420776602e34e345a016b37343971ecbd Mon Sep 17 00:00:00 2001 From: driftluo Date: Thu, 24 Aug 2023 10:52:13 +0800 Subject: [PATCH 1/2] refactor: remove useless test for sol contract --- .../metadata/contracts/metadata.sol | 55 ++----- .../metadata/test/Metadata.test.js | 137 ------------------ .../metadata/abi/metadata_abi.json | 2 +- 3 files changed, 14 insertions(+), 180 deletions(-) delete mode 100644 builtin-contract/metadata/test/Metadata.test.js diff --git a/builtin-contract/metadata/contracts/metadata.sol b/builtin-contract/metadata/contracts/metadata.sol index 8a8f9133f..f9795bb8d 100644 --- a/builtin-contract/metadata/contracts/metadata.sol +++ b/builtin-contract/metadata/contracts/metadata.sol @@ -45,6 +45,15 @@ contract MetadataManager { uint64 interval; } + struct CkbRelatedInfo { + bytes32 metadata_type_id; + bytes32 checkpoint_type_id; + bytes32 xudt_args; + bytes32 stake_smt_type_id; + bytes32 delegate_smt_type_id; + bytes32 reward_smt_type_id; + } + // to store all metadata with epoch as key mapping(uint64 => Metadata) metadata_set; @@ -112,7 +121,7 @@ contract MetadataManager { } // update current consensus_config - function updateConsensusConfig(ConsensusConfig memory config) public { + function updateConsensusConfig(ConsensusConfig memory config) public view { Metadata memory highest_metadata = metadata_set[highest_epoch]; bool find_sender = false; @@ -136,45 +145,7 @@ contract MetadataManager { return metadata; } - function verifierList() external view returns (address[] memory, uint256) { - uint256 length = metadata_set[highest_epoch].verifier_list.length; - address[] memory verifiers = new address[](length); - - for (uint256 i = 0; i < length; ++i) { - verifiers[i] = metadata_set[highest_epoch] - .verifier_list[i] - .address_; - } - - return (verifiers, highest_epoch); - } - - function isProposer(address verifier) external view returns (bool) { - ValidatorExtend memory proposer; - - uint256 length = metadata_set[highest_epoch].verifier_list.length; - for (uint256 i = 0; i < length; ++i) { - if ( - metadata_set[highest_epoch].verifier_list[i].propose_weight > - proposer.propose_weight - ) { - proposer = metadata_set[highest_epoch].verifier_list[i]; - } - } - - return verifier == proposer.address_; - } - - function isVerifier(address relayer) external view returns (bool) { - uint256 length = metadata_set[highest_epoch].verifier_list.length; - for (uint256 i = 0; i < length; ++i) { - if ( - metadata_set[highest_epoch].verifier_list[i].address_ == relayer - ) { - return true; - } - } - - return false; - } + function setCkbRelatedInfo( + CkbRelatedInfo memory ckbRelatedInfo + ) public view {} } diff --git a/builtin-contract/metadata/test/Metadata.test.js b/builtin-contract/metadata/test/Metadata.test.js deleted file mode 100644 index b4b07b6e2..000000000 --- a/builtin-contract/metadata/test/Metadata.test.js +++ /dev/null @@ -1,137 +0,0 @@ -const { expect } = require("chai") -const { ethers, upgrades } = require("hardhat") -const ABI = require('../artifacts/contracts/metadata.sol/MetadataManager.json'); -const ERC1967Proxy = require('@openzeppelin/upgrades-core/artifacts/@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol/ERC1967Proxy.json'); - -function hexToBytes(hex) { - for (var bytes = [], c = 0; c < hex.length; c += 2) - bytes.push(parseInt(hex.substr(c, 2), 16)); - return bytes; -} - -async function deployProxy(deployer, { params, initializer }) { - // deploy proxy implementation - const impl = await deployer.deploy(); - // console.log('metadata:', impl); - await impl.deployed(); - // make proxy construction params - const abi = new ethers.utils.Interface(ABI.abi); - const data = abi.encodeFunctionData(initializer, params); - // deploy proxy contract - const proxy_deployer = await ethers.getContractFactory(ERC1967Proxy.abi, ERC1967Proxy.bytecode); - const proxy = await proxy_deployer.deploy(impl.address, data); - await proxy.deployed(); - // attach proxy contract - return deployer.attach(proxy.address); -} - -describe("Testing MetadataManager", () => { - let wallets = null - let contract = null - let metadata = null - - before(async () => { - let deployer = await ethers.getContractFactory("MetadataManager") - // contract = await deployer.deploy() - // contract = await upgrades.deployProxy(deployer, [], { initializer: 'construct' }); - // await contract.deployed() - contract = await deployProxy(deployer, { params: [], initializer: 'construct' }); - wallets = await ethers.getSigners() - metadata = { - version: { - start: 1, - end: 2 - }, - epoch: 0, - gas_limit: 1000, - gas_price: 1000000, - interval: 20, - verifier_list: [ - { - bls_pub_key: hexToBytes("68656c6c6f20776f726c64"), - pub_key: hexToBytes("68656c6c6f20776f726c64"), - address_: wallets[0].address, - propose_weight: 3, - vote_weight: 10 - } - ], - propose_ratio: 23, - prevote_ratio: 135, - precommit_ratio: 8, - brake_ratio: 1008, - tx_num_limit: 55, - max_tx_size: 400000, - last_checkpoint_block_hash: hexToBytes("63b9fe46a9217a85203fc0cd7b67f3238ec93889d21fefb7cf11d40a1c3ddd9c"), - } - }) - - it("`getMetadata` failed on non-indexed epoch of 0", async () => { - await expect(contract.getMetadata(0)) - .to.be.revertedWith("fatal/non-indexed epoch") - }) - - it("`appendMetadata` successed on epoch of 0 and on version of [1, 2]", async () => { - metadata.epoch = 0 - metadata.version = { start: 1, end: 2 } - await expect(contract.appendMetadata(metadata)) - .to.be.not.reverted - }) - - it("`getMetadata` successed on epoch of 0", async () => { - await expect(contract.getMetadata(0)) - .to.be.not.reverted - }) - - it("`appendMetadata` successed on epoch of 1 and on version of [3, 4]", async () => { - metadata.epoch = 1 - metadata.version = { start: 3, end: 4 } - await expect(contract.appendMetadata(metadata)) - .to.be.not.reverted - }) - - it("`appendMetadata` failed on mismatched address", async () => { - metadata.epoch = 2 - metadata.version = { start: 5, end: 6 } - metadata.verifier_list[0].address_ = wallets[1].address - await expect(contract.appendMetadata(metadata)) - .to.be.revertedWith("fatal/verifier_list has no sender") - }) - - it("`appendMetadata` failed on discontinuous epoch of 3", async () => { - metadata.epoch = 3 - metadata.version = { start: 5, end: 6 } - metadata.verifier_list[0].address_ = wallets[0].address - await expect(contract.appendMetadata(metadata)) - .to.be.revertedWith("fatal/discontinuous epoch") - }) - - it("`appendMetadata` failed on discontinuous version of [6, 7]", async () => { - metadata.epoch = 2 - metadata.version = { start: 6, end: 7 } - await expect(contract.appendMetadata(metadata)) - .to.be.revertedWith("fatal/discontinuous version") - }) - - it("`isProposer` should return false while address is not a proposer", async () => { - expect(await contract.isProposer(wallets[10].address)).false; - }); - - it("`isProposer` should return true while address is a proposer", async () => { - expect(await contract.isProposer(wallets[0].address)).true; - }); - - it("`isVerifier` should return false while address is not a verifier", async () => { - expect(await contract.isVerifier(wallets[10].address)).false; - }); - - it("`isVerifier` should return true while address is a verifier", async () => { - expect(await contract.isVerifier(wallets[0].address)).true; - }); - - // it("`appendMetadata` failed on block.number out of version of [5, 6]", async () => { - // metadata.epoch = 2 - // metadata.version = { start: 5, end: 6 } - // await expect(contract.appendMetadata(metadata)) - // .to.be.revertedWith("fatal/invalid version") - // }) -}) diff --git a/core/executor/src/system_contract/metadata/abi/metadata_abi.json b/core/executor/src/system_contract/metadata/abi/metadata_abi.json index 0d6720e6e..d2b18b5a1 100644 --- a/core/executor/src/system_contract/metadata/abi/metadata_abi.json +++ b/core/executor/src/system_contract/metadata/abi/metadata_abi.json @@ -338,7 +338,7 @@ ], "name": "updateConsensusConfig", "outputs": [], - "stateMutability": "nonpayable", + "stateMutability": "view", "type": "function" }, { From b134867ce44dc839290f0f3556e88fc65f25e848 Mon Sep 17 00:00:00 2001 From: driftluo Date: Thu, 24 Aug 2023 12:04:53 +0800 Subject: [PATCH 2/2] test: add test for update consensus config --- .../src/tests/system_script/metadata.rs | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/core/executor/src/tests/system_script/metadata.rs b/core/executor/src/tests/system_script/metadata.rs index 8ac729f5c..d5ad4b715 100644 --- a/core/executor/src/tests/system_script/metadata.rs +++ b/core/executor/src/tests/system_script/metadata.rs @@ -10,11 +10,12 @@ use crate::{ init, metadata::{ metadata_abi::{self, ConsensusConfig, Metadata, MetadataVersion, ValidatorExtend}, - MetadataContract, + MetadataContract, MetadataStore, }, SystemContract, METADATA_CONTRACT_ADDRESS, }, tests::{gen_tx, gen_vicinity}, + CURRENT_METADATA_ROOT, }; static ROCKSDB_PATH: &str = "./free-space/system-contract/metadata"; @@ -38,6 +39,8 @@ fn test_write_functions() { test_second(&mut backend, &executor); test_validator(&mut backend, &executor); + + test_update_consensus_config(&mut backend, &executor); } fn test_init<'a>(backend: &mut MemoryBackend<'a>, executor: &MetadataContract>) { @@ -138,6 +141,39 @@ fn prepare_tx_5(addr: &H160) -> SignedTransaction { gen_tx(*addr, METADATA_CONTRACT_ADDRESS, 1000, data.encode()) } +fn prepare_tx_with_consensus_config(addr: &H160, interval: u64) -> SignedTransaction { + let data = metadata_abi::UpdateConsensusConfigCall { + config: { + let mut config = prepare_metadata().consensus_config; + config.interval = interval; + config + }, + }; + + gen_tx(*addr, METADATA_CONTRACT_ADDRESS, 1000, data.encode()) +} + +// change consensus interval test +fn test_update_consensus_config<'a>( + backend: &mut MemoryBackend<'a>, + executor: &MetadataContract>, +) { + let interval = 10; + let addr = H160::from_str("0xf000000000000000000000000000000000000000").unwrap(); + let tx = prepare_tx_with_consensus_config(&addr, interval); + + let r = executor.exec_(backend, &tx); + assert!(r.exit_reason.is_succeed()); + + let root = CURRENT_METADATA_ROOT.with(|r| *r.borrow()); + + let store = MetadataStore::new(root).unwrap(); + + let current_config = store.get_metadata(1).unwrap().consensus_config; + + assert_eq!(current_config.interval, interval) +} + fn prepare_metadata() -> Metadata { Metadata { version: MetadataVersion {