diff --git a/common/config-parser/src/types/spec.rs b/common/config-parser/src/types/spec.rs index c712c8e5a..17e4f0422 100644 --- a/common/config-parser/src/types/spec.rs +++ b/common/config-parser/src/types/spec.rs @@ -209,29 +209,24 @@ impl Genesis { transactions_root: RLP_NULL, signed_txs_hash: RLP_EMPTY_LIST, timestamp: self.timestamp, - // todo: if Hardforkinput is empty, it must change to latest hardfork info to init - // genesis - extra_data: { - vec![ExtraData { - inner: { - let mut info = Into::::into(HardforkInput { - hardforks: self.hardforks.clone(), - block_number: 0, - }); - - if info.flags.is_zero() { - info.flags = H256::from_low_u64_be(HardforkName::all().to_be()); - } - - info.encode().unwrap() - }, - }] - }, base_fee_per_gas: self.base_fee_per_gas, chain_id: self.chain_id, ..Default::default() } } + + pub fn generate_hardfork_info(&self) -> HardforkInfoInner { + let mut info = Into::::into(HardforkInput { + hardforks: self.hardforks.clone(), + block_number: 0, + }); + + if info.flags.is_zero() { + info.flags = H256::from_low_u64_be(HardforkName::all().to_be()); + } + + info + } } #[derive(Clone, Debug, Deserialize, Args)] diff --git a/core/executor/src/system_contract/metadata/mod.rs b/core/executor/src/system_contract/metadata/mod.rs index 4fdd5c779..78a40c329 100644 --- a/core/executor/src/system_contract/metadata/mod.rs +++ b/core/executor/src/system_contract/metadata/mod.rs @@ -112,6 +112,10 @@ impl SystemContract fn after_block_hook(&self, adapter: &mut Adapter) { let block_number = adapter.block_number(); + if block_number.is_zero() { + return; + } + let root = CURRENT_METADATA_ROOT.with(|r| *r.borrow()); let mut store = MetadataStore::new(root).unwrap(); @@ -128,12 +132,6 @@ impl SystemContract HARDFORK_INFO.swap(Arc::new(hardfork)); - if block_number.is_zero() { - let changes = generate_mpt_root_changes(adapter, Self::ADDRESS); - adapter.apply(changes, vec![], false); - return; - } - if let Err(e) = store.update_propose_count(block_number.as_u64(), &adapter.origin()) { panic!("Update propose count at {:?} failed: {:?}", block_number, e) } diff --git a/core/executor/src/system_contract/metadata/store.rs b/core/executor/src/system_contract/metadata/store.rs index 96ff43fb2..d3d61c182 100644 --- a/core/executor/src/system_contract/metadata/store.rs +++ b/core/executor/src/system_contract/metadata/store.rs @@ -22,6 +22,7 @@ use crate::{adapter::RocksTrieDB, MPTTrie, CURRENT_METADATA_ROOT}; /// | -------------------- | ------------------------ | /// | EPOCH_SEGMENT_KEY | `EpochSegment.encode()` | /// | CKB_RELATED_INFO_KEY | `CkbRelatedInfo.encode()`| +/// | HARDFORK_KEY | `HardforkInfo.encode()` | /// | epoch_0.be_bytes() | `Metadata.encode()` | /// | epoch_1.be_bytes() | `Metadata.encode()` | /// | ... | ... | @@ -120,10 +121,9 @@ impl MetadataStore { )?; self.trie .insert(inner.epoch.to_be_bytes().to_vec(), inner.encode()?.to_vec())?; - self.trie.insert( - CONSENSUS_CONFIG.as_bytes().to_vec(), - encode_consensus_config(current_hardfork, config.encode()?.to_vec()), - )?; + let config = encode_consensus_config(current_hardfork, config.encode()?.to_vec()); + self.trie + .insert(CONSENSUS_CONFIG.as_bytes().to_vec(), config)?; let new_root = self.trie.commit()?; CURRENT_METADATA_ROOT.with(|r| *r.borrow_mut() = new_root); @@ -231,6 +231,7 @@ impl MetadataStore { }, } }; + self.trie.insert( HARDFORK_KEY.as_bytes().to_vec(), current_info.encode()?.to_vec(), diff --git a/core/executor/src/system_contract/mod.rs b/core/executor/src/system_contract/mod.rs index e6bf19df7..201334a1f 100644 --- a/core/executor/src/system_contract/mod.rs +++ b/core/executor/src/system_contract/mod.rs @@ -28,7 +28,9 @@ use parking_lot::RwLock; use rocksdb::DB; use protocol::traits::{CkbDataProvider, ExecutorAdapter}; -use protocol::types::{Bytes, Hasher, Metadata, SignedTransaction, TxResp, H160, H256}; +use protocol::types::{ + Bytes, HardforkInfoInner, Hasher, Metadata, SignedTransaction, TxResp, H160, H256, +}; use protocol::{ckb_blake2b_256, ProtocolResult}; use crate::adapter::RocksTrieDB; @@ -115,9 +117,10 @@ pub fn init( db: Arc, adapter: &mut Adapter, metadata_list: &[Metadata], + hardfork: HardforkInfoInner, ) -> ProtocolResult<(H256, H256)> { let ret = init_system_contract_db(db, adapter); - init_metadata(adapter, ret.0, metadata_list)?; + init_metadata_and_hardfork(adapter, ret.0, metadata_list, hardfork)?; Ok(ret) } @@ -165,16 +168,18 @@ pub fn init_system_contract_db( /// This method is used for insert the first two metadata, so the /// `metadata_list.len()` should be equal to 2. -fn init_metadata( +fn init_metadata_and_hardfork( adapter: &mut Adapter, metadata_root: H256, metadata_list: &[Metadata], + hardfork: HardforkInfoInner, ) -> ProtocolResult<()> { debug_assert!(metadata_list.len() == 2); let mut store = MetadataStore::new(metadata_root)?; store.append_metadata(&metadata_list[0])?; store.append_metadata(&metadata_list[1])?; + store.set_hardfork_info(hardfork.block_number, hardfork.flags)?; let changes = generate_mpt_root_changes(adapter, METADATA_CONTRACT_ADDRESS); adapter.apply(changes, vec![], false); diff --git a/core/run/src/lib.rs b/core/run/src/lib.rs index 99381440e..f595995a2 100644 --- a/core/run/src/lib.rs +++ b/core/run/src/lib.rs @@ -456,9 +456,13 @@ async fn execute_genesis( tmp }; - let resp = execute_genesis_transactions(&partial_genesis, db_group, &spec.accounts, &[ - metadata_0, metadata_1, - ])?; + let resp = execute_genesis_transactions( + &partial_genesis, + db_group, + &spec.accounts, + &[metadata_0, metadata_1], + spec.genesis.generate_hardfork_info(), + )?; partial_genesis.block.header.state_root = resp.state_root; partial_genesis.block.header.receipts_root = resp.receipt_root; @@ -488,6 +492,7 @@ fn execute_genesis_transactions( db_group: &DatabaseGroup, accounts: &[InitialAccount], metadata_list: &[Metadata], + hardfork: HardforkInfoInner, ) -> ProtocolResult { let state_root = MPTTrie::new(db_group.trie_db()) .insert_accounts(accounts) @@ -500,7 +505,7 @@ fn execute_genesis_transactions( Proposal::new_without_state_root(&rich.block.header).into(), )?; - system_contract::init(db_group.inner_db(), &mut backend, metadata_list)?; + system_contract::init(db_group.inner_db(), &mut backend, metadata_list, hardfork)?; let resp = AxonExecutor.exec(&mut backend, &rich.txs, &[]); diff --git a/core/run/src/tests.rs b/core/run/src/tests.rs index b8ce32664..6901bd866 100644 --- a/core/run/src/tests.rs +++ b/core/run/src/tests.rs @@ -279,10 +279,12 @@ fn generate_memory_mpt_root(metadata_0: Metadata, metadata_1: Metadata) -> Vec Vec