From 182958f00e408715ded77b44dc0fe1f1ecc82834 Mon Sep 17 00:00:00 2001 From: Eason Date: Mon, 24 Jul 2023 15:07:31 +0800 Subject: [PATCH] hide the system contract root key --- core/api/src/adapter.rs | 50 +++++++++++++------------------- core/consensus/src/adapter.rs | 17 +++++------ core/executor/src/adapter/mod.rs | 12 ++++++++ core/executor/src/lib.rs | 22 ++++---------- core/mempool/src/adapter/mod.rs | 11 ++----- 5 files changed, 48 insertions(+), 64 deletions(-) diff --git a/core/api/src/adapter.rs b/core/api/src/adapter.rs index db2015dcb..711584a11 100644 --- a/core/api/src/adapter.rs +++ b/core/api/src/adapter.rs @@ -1,11 +1,5 @@ use std::sync::Arc; -use core_executor::{ - system_contract::metadata::MetadataHandle, AxonExecutor, AxonExecutorAdapter, MPTTrie, -}; -use core_executor::{ - HEADER_CELL_ROOT_KEY, IMAGE_CELL_CONTRACT_ADDRESS, METADATA_CONTRACT_ADDRESS, METADATA_ROOT_KEY, -}; use protocol::traits::{APIAdapter, Context, Executor, ExecutorAdapter, MemPool, Network, Storage}; use protocol::types::{ Account, BigEndianHash, Block, BlockNumber, Bytes, CkbRelatedInfo, ExecutorContext, Hash, @@ -14,6 +8,10 @@ use protocol::types::{ }; use protocol::{async_trait, codec::ProtocolCodec, trie, ProtocolResult}; +use core_executor::{ + system_contract::metadata::MetadataHandle, AxonExecutor, AxonExecutorAdapter, MPTTrie, +}; + use crate::APIError; #[derive(Clone)] @@ -257,34 +255,26 @@ where } async fn get_image_cell_root(&self, ctx: Context) -> ProtocolResult { - let state_root = self.storage.get_latest_block(ctx).await?.header.state_root; - let state_mpt_tree = MPTTrie::from_root(state_root, Arc::clone(&self.trie_db))?; - let raw_account = state_mpt_tree - .get(IMAGE_CELL_CONTRACT_ADDRESS.as_bytes())? - .ok_or_else(|| APIError::Adapter("Can't find this address".to_string()))?; + let state_root = self.storage.get_latest_block_header(ctx).await?.state_root; - let account = Account::decode(raw_account).unwrap(); - let storage_mpt_tree = MPTTrie::from_root(account.storage_root, Arc::clone(&self.trie_db))?; - - Ok(storage_mpt_tree - .get(HEADER_CELL_ROOT_KEY.as_bytes())? - .map(|r| H256::from_slice(&r)) - .unwrap_or_default()) + Ok(AxonExecutorAdapter::from_root( + state_root, + Arc::clone(&self.trie_db), + Arc::clone(&self.storage), + Default::default(), + )? + .get_image_cell_root()) } async fn get_metadata_root(&self, ctx: Context) -> ProtocolResult { - let state_root = self.storage.get_latest_block(ctx).await?.header.state_root; - let state_mpt_tree = MPTTrie::from_root(state_root, Arc::clone(&self.trie_db))?; - let raw_account = state_mpt_tree - .get(METADATA_CONTRACT_ADDRESS.as_bytes())? - .ok_or_else(|| APIError::Adapter("Can't find this address".to_string()))?; + let state_root = self.storage.get_latest_block_header(ctx).await?.state_root; - let account = Account::decode(raw_account).unwrap(); - let storage_mpt_tree = MPTTrie::from_root(account.storage_root, Arc::clone(&self.trie_db))?; - - Ok(storage_mpt_tree - .get(METADATA_ROOT_KEY.as_bytes())? - .map(|r| H256::from_slice(&r)) - .unwrap_or_default()) + Ok(AxonExecutorAdapter::from_root( + state_root, + Arc::clone(&self.trie_db), + Arc::clone(&self.storage), + Default::default(), + )? + .get_metadata_root()) } } diff --git a/core/consensus/src/adapter.rs b/core/consensus/src/adapter.rs index 4abec1205..6e430b87f 100644 --- a/core/consensus/src/adapter.rs +++ b/core/consensus/src/adapter.rs @@ -8,13 +8,11 @@ use parking_lot::RwLock; use common_apm::Instant; use common_apm_derive::trace_span; use core_executor::system_contract::metadata::MetadataHandle; -use core_executor::{ - AxonExecutor, AxonExecutorAdapter, METADATA_CONTRACT_ADDRESS, METADATA_ROOT_KEY, -}; +use core_executor::{AxonExecutor, AxonExecutorAdapter}; use core_network::{PeerId, PeerIdExt}; use protocol::traits::{ - Backend, CommonConsensusAdapter, ConsensusAdapter, Context, Executor, Gossip, MemPool, - MessageTarget, Network, PeerTrust, Priority, Rpc, Storage, SynchronizationAdapter, + CommonConsensusAdapter, ConsensusAdapter, Context, Executor, Gossip, MemPool, MessageTarget, + Network, PeerTrust, Priority, Rpc, Storage, SynchronizationAdapter, }; use protocol::types::{ BatchSignedTxs, Block, BlockNumber, Bytes, ExecResp, Hash, Header, Hex, MerkleRoot, Metadata, @@ -340,7 +338,7 @@ where Arc::clone(&self.storage), proposal.clone().into(), )?; - let root = backend.storage(METADATA_CONTRACT_ADDRESS, *METADATA_ROOT_KEY); + let root = backend.get_metadata_root(); let metadata_handle = MetadataHandle::new(root); let verifier_list = metadata_handle @@ -647,13 +645,14 @@ where async fn get_metadata_handle(&self, ctx: Context) -> ProtocolResult { let current_state_root = self.storage.get_latest_block_header(ctx).await?.state_root; - let backend = AxonExecutorAdapter::from_root( + let root = AxonExecutorAdapter::from_root( current_state_root, Arc::clone(&self.trie_db), Arc::clone(&self.storage), Default::default(), - )?; - let root = backend.storage(METADATA_CONTRACT_ADDRESS, *METADATA_ROOT_KEY); + )? + .get_metadata_root(); + Ok(MetadataHandle::new(root)) } } diff --git a/core/executor/src/adapter/mod.rs b/core/executor/src/adapter/mod.rs index ddf4902c8..aad387afe 100644 --- a/core/executor/src/adapter/mod.rs +++ b/core/executor/src/adapter/mod.rs @@ -14,6 +14,10 @@ use protocol::types::{ }; use protocol::{codec::ProtocolCodec, trie, ProtocolResult}; +use crate::system_contract::{ + HEADER_CELL_ROOT_KEY, IMAGE_CELL_CONTRACT_ADDRESS, METADATA_CONTRACT_ADDRESS, METADATA_ROOT_KEY, +}; + const GET_BLOCK_HASH_NUMBER_RANGE: u64 = 256; macro_rules! blocking_async { @@ -280,6 +284,14 @@ where }) } + pub fn get_metadata_root(&self) -> H256 { + self.storage(METADATA_CONTRACT_ADDRESS, *METADATA_ROOT_KEY) + } + + pub fn get_image_cell_root(&self) -> H256 { + self.storage(IMAGE_CELL_CONTRACT_ADDRESS, *HEADER_CELL_ROOT_KEY) + } + fn apply>( &mut self, address: H160, diff --git a/core/executor/src/lib.rs b/core/executor/src/lib.rs index 85579b96d..963d3a4b8 100644 --- a/core/executor/src/lib.rs +++ b/core/executor/src/lib.rs @@ -10,10 +10,7 @@ mod tests; mod utils; pub use crate::adapter::{AxonExecutorAdapter, MPTTrie, RocksTrieDB}; -pub use crate::system_contract::{ - metadata::MetadataHandle, DataProvider, HEADER_CELL_ROOT_KEY, IMAGE_CELL_CONTRACT_ADDRESS, - METADATA_CONTRACT_ADDRESS, METADATA_ROOT_KEY, -}; +pub use crate::system_contract::{metadata::MetadataHandle, DataProvider}; pub use crate::utils::{ code_address, decode_revert_msg, logs_bloom, DefaultFeeAllocator, FeeInlet, }; @@ -39,6 +36,8 @@ use crate::precompiles::build_precompile_set; use crate::system_contract::{ after_block_hook, before_block_hook, system_contract_dispatch, CkbLightClientContract, ImageCellContract, MetadataContract, NativeTokenContract, SystemContract, + CKB_LIGHT_CLIENT_CONTRACT_ADDRESS, HEADER_CELL_ROOT_KEY, METADATA_CONTRACT_ADDRESS, + METADATA_ROOT_KEY, }; lazy_static::lazy_static! { @@ -314,25 +313,14 @@ impl AxonExecutor { fn init_local_system_contract_roots(&self, adapter: &mut Adapter) { CURRENT_HEADER_CELL_ROOT.with(|root| { *root.borrow_mut() = - adapter.storage(CkbLightClientContract::ADDRESS, *HEADER_CELL_ROOT_KEY); + adapter.storage(CKB_LIGHT_CLIENT_CONTRACT_ADDRESS, *HEADER_CELL_ROOT_KEY); }); CURRENT_METADATA_ROOT.with(|root| { - *root.borrow_mut() = adapter.storage(MetadataContract::ADDRESS, *METADATA_ROOT_KEY); + *root.borrow_mut() = adapter.storage(METADATA_CONTRACT_ADDRESS, *METADATA_ROOT_KEY); }); } - // /// The system contract roots are updated at the end of execute transactions - // /// of a block. - // fn update_system_contract_roots_for_external_module(&self) { - // BLOCK_PERIOD_UPDATED_HEADER_CELL_ROOT.store(Arc::new( - // CURRENT_HEADER_CELL_ROOT.with(|root| *root.borrow()), - // )); - - // BLOCK_PERIOD_METADATA_ROOT - // .store(Arc::new(CURRENT_METADATA_ROOT.with(|root| *root.borrow()))); - // } - #[cfg(test)] fn test_exec( &self, diff --git a/core/mempool/src/adapter/mod.rs b/core/mempool/src/adapter/mod.rs index c323a9390..593af09d7 100644 --- a/core/mempool/src/adapter/mod.rs +++ b/core/mempool/src/adapter/mod.rs @@ -26,10 +26,7 @@ use protocol::{ use common_apm_derive::trace_span; use common_crypto::{Crypto, Secp256k1Recoverable}; -use core_executor::{ - is_call_system_script, AxonExecutorAdapter, DataProvider, MetadataHandle, HEADER_CELL_ROOT_KEY, - IMAGE_CELL_CONTRACT_ADDRESS, METADATA_CONTRACT_ADDRESS, METADATA_ROOT_KEY, -}; +use core_executor::{is_call_system_script, AxonExecutorAdapter, DataProvider, MetadataHandle}; use core_interoperation::InteroperationImpl; use crate::adapter::message::{MsgPullTxs, END_GOSSIP_NEW_TXS, RPC_PULL_TXS}; @@ -187,8 +184,7 @@ where ) -> ProtocolResult { let addr = &stx.sender; let block = self.storage.get_latest_block(ctx.clone()).await?; - let backend = self.executor_backend(ctx).await?; - let root = backend.storage(METADATA_CONTRACT_ADDRESS, *METADATA_ROOT_KEY); + let root = self.executor_backend(ctx).await?.get_metadata_root(); if MetadataHandle::new(root).is_validator(block.header.number + 1, *addr)? { return Ok(U256::zero()); @@ -289,8 +285,7 @@ where return Ok(()); } - let backend = self.executor_backend(ctx).await?; - let root = backend.storage(IMAGE_CELL_CONTRACT_ADDRESS, *HEADER_CELL_ROOT_KEY); + let root = self.executor_backend(ctx).await?.get_image_cell_root(); // Verify interoperation signature match signature.r[0] {