Skip to content

Commit

Permalink
hide the system contract root key
Browse files Browse the repository at this point in the history
  • Loading branch information
KaoImin committed Jul 24, 2023
1 parent d5dacc6 commit dff856c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 64 deletions.
50 changes: 20 additions & 30 deletions core/api/src/adapter.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)]
Expand Down Expand Up @@ -257,34 +255,26 @@ where
}

async fn get_image_cell_root(&self, ctx: Context) -> ProtocolResult<H256> {
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<H256> {
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())
}
}
17 changes: 8 additions & 9 deletions core/consensus/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -647,13 +645,14 @@ where

async fn get_metadata_handle(&self, ctx: Context) -> ProtocolResult<MetadataHandle> {
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))
}
}
12 changes: 12 additions & 0 deletions core/executor/src/adapter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<I: IntoIterator<Item = (H256, H256)>>(
&mut self,
address: H160,
Expand Down
22 changes: 5 additions & 17 deletions core/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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! {
Expand Down Expand Up @@ -314,25 +313,14 @@ impl AxonExecutor {
fn init_local_system_contract_roots<Adapter: ExecutorAdapter>(&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<Adapter: ExecutorAdapter>(
&self,
Expand Down
11 changes: 3 additions & 8 deletions core/mempool/src/adapter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -187,8 +184,7 @@ where
) -> ProtocolResult<U256> {
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());
Expand Down Expand Up @@ -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] {
Expand Down

0 comments on commit dff856c

Please sign in to comment.