Skip to content

Commit

Permalink
bug: set_ckb_related_info not committed
Browse files Browse the repository at this point in the history
  • Loading branch information
wenyuanhust committed Nov 20, 2023
1 parent 06340ba commit cb14069
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
13 changes: 7 additions & 6 deletions core/executor/src/system_contract/metadata/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ impl MetadataStore {
}

pub fn set_ckb_related_info(&mut self, info: &CkbRelatedInfo) -> ProtocolResult<()> {
self.trie
.insert(
CKB_RELATED_INFO_KEY.as_bytes().to_vec(),
info.encode()?.to_vec(),
)
.map_err(Into::into)
self.trie.insert(
CKB_RELATED_INFO_KEY.as_bytes().to_vec(),
info.encode()?.to_vec(),
)?;
let new_root = self.trie.commit()?;
CURRENT_METADATA_ROOT.with(|r| *r.borrow_mut() = new_root);
Ok(())
}

pub fn append_metadata(&mut self, metadata: &Metadata) -> ProtocolResult<()> {
Expand Down
45 changes: 41 additions & 4 deletions core/executor/src/tests/system_script/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{collections::BTreeMap, str::FromStr};
use std::{collections::BTreeMap, str::FromStr, sync::Arc};

use ethers::abi::AbiEncode;

use core_db::RocksAdapter;
use protocol::types::{MemoryBackend, SignedTransaction, H160, U256};
use protocol::types::{CkbRelatedInfo, MemoryBackend, SignedTransaction, H160, H256, U256};

use crate::{
system_contract::{
Expand All @@ -12,10 +12,10 @@ use crate::{
metadata_abi::{self, ConsensusConfig, Metadata, MetadataVersion, ValidatorExtend},
MetadataContract, MetadataStore,
},
SystemContract, METADATA_CONTRACT_ADDRESS,
SystemContract, METADATA_CONTRACT_ADDRESS, METADATA_DB,
},
tests::{gen_tx, gen_vicinity},
CURRENT_METADATA_ROOT,
RocksTrieDB, CURRENT_METADATA_ROOT,
};

static ROCKSDB_PATH: &str = "./free-space/system-contract/metadata";
Expand Down Expand Up @@ -207,6 +207,43 @@ fn prepare_validator() -> ValidatorExtend {
}
}

#[test]
fn test_set_ckb_related_info() {
// Init metadata db.
{
let inner_db = RocksAdapter::new(ROCKSDB_PATH, Default::default())
.unwrap()
.inner_db();
let mut _db = METADATA_DB.write();
const METADATA_DB_CACHE_SIZE: usize = 10;
_db.replace(Arc::new(RocksTrieDB::new_metadata(
Arc::clone(&inner_db),
METADATA_DB_CACHE_SIZE,
)));
}

let metadata_root = H256::zero();
let mut store = MetadataStore::new(metadata_root).unwrap();
let ckb_infos = CkbRelatedInfo {
metadata_type_id: H256::from_str(
"0xdb0782aba62896c2a7c279f3de8dbbd7fd06729cc8b7b499df93f5c450f61839",
)
.unwrap(),
checkpoint_type_id: H256::zero(),
xudt_args: H256::zero(),
stake_smt_type_id: H256::zero(),
delegate_smt_type_id: H256::zero(),
reward_smt_type_id: H256::zero(),
};
let _result = store.set_ckb_related_info(&ckb_infos);

CURRENT_METADATA_ROOT.with(|root| {
let new_root = *root.borrow();
println!("new_root: {:?}", new_root);
assert_ne!(new_root, metadata_root);
});
}

// #[tokio::test]
// async fn update_consensus_config() {
// let config:
Expand Down

0 comments on commit cb14069

Please sign in to comment.