Skip to content

Commit

Permalink
test: extend unit test scope
Browse files Browse the repository at this point in the history
  • Loading branch information
KaoImin committed Oct 8, 2023
1 parent 0051aa8 commit dacb972
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions core/executor/src/system_contract/metadata/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod abi;
pub(crate) mod handle;
mod segment;
pub mod segment;
mod store;

pub use abi::metadata_abi;
Expand Down Expand Up @@ -30,7 +30,7 @@ pub const METADATA_CONTRACT_ADDRESS: H160 = system_contract_address(0x1);
const METADATA_CACHE_SIZE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(10) };

lazy_static::lazy_static! {
static ref EPOCH_SEGMENT_KEY: H256 = Hasher::digest("epoch_segment");
pub static ref EPOCH_SEGMENT_KEY: H256 = Hasher::digest("epoch_segment");
static ref CKB_RELATED_INFO_KEY: H256 = Hasher::digest("ckb_related_info");
static ref HARDFORK_KEY: H256 = Hasher::digest("hardfork");
static ref HARDFORK_INFO: ArcSwap<H256> = ArcSwap::new(Arc::new(H256::zero()));
Expand Down
1 change: 1 addition & 0 deletions core/run/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jemallocator = { version = "0.5", features = ["profiling", "stats", "unprefixed_

[dev-dependencies]
clap = "4.4"
hasher = "0.1"
tempfile = "3.6"

[features]
Expand Down
74 changes: 67 additions & 7 deletions core/run/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
use std::{
collections::BTreeMap,
convert::AsRef,
env::{current_dir, set_current_dir},
fs, io,
path::{Path, PathBuf},
str::FromStr,
sync::Arc,
};

use clap::{builder::TypedValueParser as _, Command};
use hasher::HasherKeccak;

use common_config_parser::types::{
spec::{ChainSpec, ChainSpecValueParser, PrivateKeyFileValueParser},
Config, ConfigValueParser,
};
use common_crypto::Secp256k1RecoverablePrivateKey;
use core_executor::{AxonExecutorApplyAdapter, MetadataHandle};
use core_executor::{
system_contract::metadata::{segment::EpochSegment, EPOCH_SEGMENT_KEY},
AxonExecutorApplyAdapter, MetadataHandle,
};
use protocol::{
codec::hex_decode,
codec::{hex_decode, ProtocolCodec as _},
tokio,
trie::{MemoryDB, PatriciaTrie, Trie as _},
types::{Header, Metadata, Proposal, RichBlock, H256},
};

Expand Down Expand Up @@ -129,6 +136,8 @@ async fn check_genesis_data<'a>(case: &TestCase<'a>) {
genesis.block.header.hash(),
);

assert!(genesis.txs.is_empty());

for (i, (block_cached, tx)) in genesis
.block
.tx_hashes
Expand Down Expand Up @@ -214,6 +223,11 @@ fn check_state(spec: &ChainSpec, genesis_header: &Header, db_group: &DatabaseGro
};
let handle = MetadataHandle::new(backend.get_metadata_root());

assert_eq!(
backend.get_metadata_root().as_bytes(),
generate_memory_mpt_root(metadata_0.clone(), metadata_1.clone())
);

assert_metadata(metadata_0, handle.get_metadata_by_epoch(0).unwrap());
assert_metadata(metadata_1, handle.get_metadata_by_epoch(1).unwrap());
}
Expand All @@ -227,11 +241,11 @@ fn check_hashes(chain: &str, name: &str, expected_str: &str, actual: H256) {
);
}

fn assert_metadata(metadata_0: Metadata, metadata_1: Metadata) {
assert_eq!(metadata_0.version, metadata_1.version);
assert_eq!(metadata_0.epoch, metadata_1.epoch);
assert_eq!(metadata_0.verifier_list, metadata_1.verifier_list);
assert_eq!(metadata_0.consensus_config, metadata_1.consensus_config);
fn assert_metadata(left: Metadata, right: Metadata) {
assert_eq!(left.version, right.version);
assert_eq!(left.epoch, right.epoch);
assert_eq!(left.verifier_list, right.verifier_list);
assert_eq!(left.consensus_config, right.consensus_config);
}

fn copy_dir(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
Expand All @@ -247,3 +261,49 @@ fn copy_dir(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
}
Ok(())
}

fn generate_memory_mpt_root(metadata_0: Metadata, metadata_1: Metadata) -> Vec<u8> {
let metadata_0 = sort_metadata(metadata_0);
let metadata_1 = sort_metadata(metadata_1);
let mut memory_mpt = PatriciaTrie::new(
Arc::new(MemoryDB::new(false)),
Arc::new(HasherKeccak::new()),
);
let mut seg = EpochSegment::new();

memory_mpt
.insert(EPOCH_SEGMENT_KEY.as_bytes().to_vec(), seg.as_bytes())
.unwrap();
seg.append_endpoint(metadata_0.version.end).unwrap();
memory_mpt
.insert(EPOCH_SEGMENT_KEY.as_bytes().to_vec(), seg.as_bytes())
.unwrap();
seg.append_endpoint(metadata_1.version.end).unwrap();
memory_mpt
.insert(EPOCH_SEGMENT_KEY.as_bytes().to_vec(), seg.as_bytes())
.unwrap();

memory_mpt
.insert(
metadata_0.epoch.to_be_bytes().to_vec(),
metadata_0.encode().unwrap().to_vec(),
)
.unwrap();
memory_mpt
.insert(
metadata_1.epoch.to_be_bytes().to_vec(),
metadata_1.encode().unwrap().to_vec(),
)
.unwrap();
memory_mpt.root().unwrap()
}

fn sort_metadata(mut metadata: Metadata) -> Metadata {
let map = metadata
.verifier_list
.iter()
.map(|v| (v.address, 0u64))
.collect::<BTreeMap<_, _>>();
metadata.propose_counter = map.into_iter().map(Into::into).collect();
metadata
}

0 comments on commit dacb972

Please sign in to comment.