Skip to content

Commit

Permalink
remove Intermediate roots from pallet executive
Browse files Browse the repository at this point in the history
  • Loading branch information
vedhavyas committed Jan 9, 2025
1 parent c34427f commit fa4fce9
Show file tree
Hide file tree
Showing 7 changed files with 2 additions and 78 deletions.
3 changes: 0 additions & 3 deletions crates/sp-domains/src/core_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ sp_api::decl_runtime_apis! {
tx_range: &U256,
) -> bool;

/// Returns the intermediate storage roots in an encoded form.
fn intermediate_roots() -> Vec<[u8; 32]>;

/// Returns the storage root after initializing the block.
fn initialize_block_with_post_state_root(header: &Block::Header) -> Vec<u8>;

Expand Down
22 changes: 0 additions & 22 deletions domains/client/block-builder/src/custom_api.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
//! Custom API that is efficient to collect storage roots.
// TODO: remove in later commits
#![allow(dead_code)]

use codec::{Codec, Decode, Encode};
use hash_db::{HashDB, Hasher, Prefix};
use sc_client_api::{backend, ExecutorProvider, StateBackend};
Expand All @@ -26,9 +23,6 @@ pub(crate) type TrieDeltaBackendFor<'a, State, Block> = TrieBackend<
HashingFor<Block>,
>;

type TrieBackendFor<State, Block> =
TrieBackend<TrieBackendStorageFor<State, Block>, HashingFor<Block>>;

/// Storage changes are the collected throughout the execution.
pub struct CollectedStorageChanges<H: Hasher> {
/// Storage changes that are captured during the execution.
Expand Down Expand Up @@ -102,22 +96,6 @@ where
}
}

impl<'a, S, H> DeltaBackend<'a, S, H>
where
S: 'a + TrieBackendStorage<H>,
H: 'a + Hasher,
{
fn consolidate_delta(&mut self, db: BackendTransaction<H>) {
let delta = if let Some(mut delta) = self.delta.take() {
delta.consolidate(db);
delta
} else {
db
};
self.delta = Some(delta);
}
}

pub(crate) struct TrieBackendApi<Client, Block: BlockT, Backend: backend::Backend<Block>, Exec> {
parent_hash: Block::Hash,
parent_number: NumberFor<Block>,
Expand Down
39 changes: 2 additions & 37 deletions domains/pallets/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,26 +182,10 @@ mod pallet {
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(_block_number: BlockNumberFor<T>) -> Weight {
// Reset the intermediate storage roots from last block.
IntermediateRoots::<T>::kill();
// TODO: Probably needs a different value
Weight::from_parts(1, 0)
}
}

/// Intermediate storage roots collected during the block execution.
#[pallet::storage]
#[pallet::getter(fn intermediate_roots)]
pub(super) type IntermediateRoots<T: Config> = StorageValue<_, Vec<[u8; 32]>, ValueQuery>;
}

impl<T: Config> Pallet<T> {
pub(crate) fn push_root(root: Vec<u8>) {
IntermediateRoots::<T>::append(
TryInto::<[u8; 32]>::try_into(root)
.expect("root is a SCALE encoded hash which uses H256; qed"),
);
}
}

/// Same semantics with `frame_executive::Executive`.
Expand Down Expand Up @@ -388,16 +372,10 @@ where
panic!("{}", err)
}
});

// Note the storage root before finalizing the block so that the block imported during the
// syncing process produces the same storage root with the one processed based on
// the consensus block.
Pallet::<ExecutiveConfig>::push_root(Self::storage_root());
}

/// Wrapped `frame_executive::Executive::finalize_block`.
pub fn finalize_block() -> HeaderFor<ExecutiveConfig> {
Pallet::<ExecutiveConfig>::push_root(Self::storage_root());
frame_executive::Executive::<
ExecutiveConfig,
BlockOf<ExecutiveConfig>,
Expand Down Expand Up @@ -429,8 +407,6 @@ where
///
/// Note the storage root in the beginning.
pub fn apply_extrinsic(uxt: ExtrinsicOf<ExecutiveConfig>) -> ApplyExtrinsicResult {
Pallet::<ExecutiveConfig>::push_root(Self::storage_root());

// apply the extrinsic within another transaction so that changes can be reverted.
let res = with_storage_layer(|| {
frame_executive::Executive::<
Expand All @@ -455,7 +431,7 @@ where
// - Pre and Post dispatch fails. Check the test `test_domain_block_builder_include_ext_with_failed_predispatch`
// why this could happen. If it fail due to this, then we revert the inner storage changes
// but still include extrinsic so that we can clear inconsistency between block body and trace roots.
let res = match res {
match res {
Ok(dispatch_outcome) => Ok(dispatch_outcome),
Err(err) => {
let encoded = uxt.encode();
Expand Down Expand Up @@ -509,18 +485,7 @@ where
<frame_system::Pallet<ExecutiveConfig>>::note_applied_extrinsic(&r, dispatch_info);
Ok(Err(err))
}
};

// TODO: Critical!!! https://github.com/paritytech/substrate/pull/10922#issuecomment-1068997467
log::debug!(
target: "domain::runtime::executive",
"[apply_extrinsic] after: {:?}",
{
use codec::Decode;
BlockHashOf::<ExecutiveConfig>::decode(&mut Self::storage_root().as_slice()).unwrap()
}
);
res
}
}

// TODO: https://github.com/paritytech/substrate/issues/10711
Expand Down
4 changes: 0 additions & 4 deletions domains/runtime/auto-id/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,10 +758,6 @@ impl_runtime_apis! {
}
}

fn intermediate_roots() -> Vec<[u8; 32]> {
ExecutivePallet::intermediate_roots()
}

fn initialize_block_with_post_state_root(header: &<Block as BlockT>::Header) -> Vec<u8> {
Executive::initialize_block(header);
Executive::storage_root()
Expand Down
4 changes: 0 additions & 4 deletions domains/runtime/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1169,10 +1169,6 @@ impl_runtime_apis! {
}
}

fn intermediate_roots() -> Vec<[u8; 32]> {
ExecutivePallet::intermediate_roots()
}

fn initialize_block_with_post_state_root(header: &<Block as BlockT>::Header) -> Vec<u8> {
Executive::initialize_block(header);
Executive::storage_root()
Expand Down
4 changes: 0 additions & 4 deletions domains/test/runtime/auto-id/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,10 +749,6 @@ impl_runtime_apis! {
}
}

fn intermediate_roots() -> Vec<[u8; 32]> {
ExecutivePallet::intermediate_roots()
}

fn initialize_block_with_post_state_root(header: &<Block as BlockT>::Header) -> Vec<u8> {
Executive::initialize_block(header);
Executive::storage_root()
Expand Down
4 changes: 0 additions & 4 deletions domains/test/runtime/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,10 +1124,6 @@ impl_runtime_apis! {
}
}

fn intermediate_roots() -> Vec<[u8; 32]> {
ExecutivePallet::intermediate_roots()
}

fn initialize_block_with_post_state_root(header: &<Block as BlockT>::Header) -> Vec<u8> {
Executive::initialize_block(header);
Executive::storage_root()
Expand Down

0 comments on commit fa4fce9

Please sign in to comment.