Skip to content

Commit

Permalink
fix(executor)!: set_ckb_related_info transaction is not committed (#1576
Browse files Browse the repository at this point in the history
)
  • Loading branch information
wenyuanhust authored Nov 23, 2023
1 parent 6a574cd commit 264a676
Show file tree
Hide file tree
Showing 2 changed files with 55 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
52 changes: 48 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,13 +12,14 @@ 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";
static CKB_INFO_ROCKSDB_PATH: &str = "./free-space/system-contract/ckb_info";

#[test]
fn test_write_functions() {
Expand Down Expand Up @@ -207,6 +208,49 @@ fn prepare_validator() -> ValidatorExtend {
}
}

#[test]
fn test_set_ckb_related_info() {
// Init ckb info db.
{
let inner_db = RocksAdapter::new(CKB_INFO_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 old_metadata_root = H256::zero();
let metadata_type_id =
H256::from_str("0xdb0782aba62896c2a7c279f3de8dbbd7fd06729cc8b7b499df93f5c450f61839")
.unwrap();
let mut store = MetadataStore::new(old_metadata_root).unwrap();
let ckb_infos = CkbRelatedInfo {
metadata_type_id,
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);
assert!(result.is_ok());

let result = store.get_ckb_related_info();
assert!(result.is_ok());
let result = result.unwrap();
assert_eq!(result.metadata_type_id, metadata_type_id);

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

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

0 comments on commit 264a676

Please sign in to comment.