Skip to content

Commit

Permalink
refactor: remove useless test for sol contract (#1355)
Browse files Browse the repository at this point in the history
* refactor: remove useless test for sol contract

* test: add test for update consensus config
  • Loading branch information
driftluo authored Aug 24, 2023
1 parent 01e7b33 commit 8ff7658
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 181 deletions.
55 changes: 13 additions & 42 deletions builtin-contract/metadata/contracts/metadata.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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 {}
}
137 changes: 0 additions & 137 deletions builtin-contract/metadata/test/Metadata.test.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@
],
"name": "updateConsensusConfig",
"outputs": [],
"stateMutability": "nonpayable",
"stateMutability": "view",
"type": "function"
},
{
Expand Down
38 changes: 37 additions & 1 deletion core/executor/src/tests/system_script/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<MemoryBackend<'a>>) {
Expand Down Expand Up @@ -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<MemoryBackend<'a>>,
) {
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 {
Expand Down

0 comments on commit 8ff7658

Please sign in to comment.