Skip to content

Commit

Permalink
remove state root storage since relayer would not use it anymore but …
Browse files Browse the repository at this point in the history
…instead rely on on Confirmed domain blocks
  • Loading branch information
vedhavyas committed Feb 6, 2024
1 parent c209306 commit 62f1387
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 99 deletions.
23 changes: 0 additions & 23 deletions crates/pallet-domains/src/block_tree.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Domain block tree
use crate::pallet::StateRoots;
use crate::{
BalanceOf, BlockTree, BlockTreeNodes, Config, ConsensusBlockHash, DomainBlockNumberFor,
DomainHashingFor, ExecutionInbox, ExecutionReceiptOf, HeadReceiptExtended, HeadReceiptNumber,
Expand Down Expand Up @@ -313,12 +312,6 @@ pub(crate) fn process_execution_receipt<T: Config>(
operator_ids,
} = BlockTreeNodes::<T>::take(receipt_hash).ok_or(Error::MissingDomainBlock)?;

_ = StateRoots::<T>::take((
domain_id,
to_prune,
execution_receipt.domain_block_hash,
));

// Collect the paid bundle storage fees and the invalid bundle author
let mut paid_bundle_storage_fees = BTreeMap::new();
let mut invalid_bundle_authors = Vec::new();
Expand Down Expand Up @@ -395,14 +388,6 @@ fn add_new_receipt_to_block_tree<T: Config>(
// Construct and add a new domain block to the block tree
let er_hash = execution_receipt.hash::<DomainHashingFor<T>>();
let domain_block_number = execution_receipt.domain_block_number;
StateRoots::<T>::insert(
(
domain_id,
domain_block_number,
execution_receipt.domain_block_hash,
),
execution_receipt.final_state_root,
);

BlockTree::<T>::insert(domain_id, domain_block_number, er_hash);
let block_tree_node = BlockTreeNode {
Expand All @@ -425,14 +410,6 @@ pub(crate) fn import_genesis_receipt<T: Config>(
};
// NOTE: no need to update the head receipt number as `HeadReceiptNumber` is using `ValueQuery`
BlockTree::<T>::insert(domain_id, domain_block_number, er_hash);
StateRoots::<T>::insert(
(
domain_id,
domain_block_number,
block_tree_node.execution_receipt.domain_block_hash,
),
block_tree_node.execution_receipt.final_state_root,
);
BlockTreeNodes::<T>::insert(er_hash, block_tree_node);
}

Expand Down
33 changes: 3 additions & 30 deletions crates/pallet-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,17 +502,6 @@ mod pallet {
pub(super) type HeadReceiptExtended<T: Config> =
StorageMap<_, Identity, DomainId, bool, ValueQuery>;

/// State root mapped again each domain (block, hash)
/// This acts as an index for other protocols like XDM to fetch state roots faster.
#[pallet::storage]
pub(super) type StateRoots<T: Config> = StorageMap<
_,
Identity,
(DomainId, DomainBlockNumberFor<T>, T::DomainHash),
T::DomainHash,
OptionQuery,
>;

/// The consensus block hash used to verify ER,
/// only store the consensus block hash for a domain
/// if that consensus block contains bundle of the domain, the hash will be pruned when the ER
Expand Down Expand Up @@ -996,17 +985,9 @@ mod pallet {
let receipt_hash = BlockTree::<T>::take(domain_id, to_prune)
.ok_or::<Error<T>>(FraudProofError::BadReceiptNotFound.into())?;

let BlockTreeNode {
execution_receipt,
operator_ids,
} = BlockTreeNodes::<T>::take(receipt_hash)
.ok_or::<Error<T>>(FraudProofError::BadReceiptNotFound.into())?;

let _ = StateRoots::<T>::take((
domain_id,
execution_receipt.domain_block_number,
execution_receipt.domain_block_hash,
));
let BlockTreeNode { operator_ids, .. } =
BlockTreeNodes::<T>::take(receipt_hash)
.ok_or::<Error<T>>(FraudProofError::BadReceiptNotFound.into())?;

// NOTE: the operator id will be deduplicated since we are using `BTreeMap`
// and slashed reason will hold earliest bad execution receipt hash which this
Expand Down Expand Up @@ -1506,14 +1487,6 @@ impl<T: Config> Pallet<T> {
Some(HeadDomainNumber::<T>::get(domain_id))
}

pub fn domain_state_root(
domain_id: DomainId,
domain_block_number: DomainBlockNumberFor<T>,
domain_block_hash: T::DomainHash,
) -> Option<T::DomainHash> {
StateRoots::<T>::get((domain_id, domain_block_number, domain_block_hash))
}

pub fn runtime_id(domain_id: DomainId) -> Option<RuntimeId> {
DomainRegistry::<T>::get(domain_id)
.map(|domain_object| domain_object.domain_config.runtime_id)
Expand Down
6 changes: 0 additions & 6 deletions crates/sp-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1044,12 +1044,6 @@ sp_api::decl_runtime_apis! {
/// Returns the current best number of the domain.
fn domain_best_number(domain_id: DomainId) -> Option<HeaderNumberFor<DomainHeader>>;

/// Returns the chain state root at the given block.
fn domain_state_root(
domain_id: DomainId,
number: HeaderNumberFor<DomainHeader>,
hash: HeaderHashFor<DomainHeader>) -> Option<HeaderHashFor<DomainHeader>>;

/// Returns the execution receipt
fn execution_receipt(receipt_hash: HeaderHashFor<DomainHeader>) -> Option<ExecutionReceiptFor<DomainHeader, Block, Balance>>;

Expand Down
4 changes: 0 additions & 4 deletions crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1076,10 +1076,6 @@ impl_runtime_apis! {
Domains::domain_best_number(domain_id)
}

fn domain_state_root(domain_id: DomainId, number: DomainNumber, hash: DomainHash) -> Option<DomainHash>{
Domains::domain_state_root(domain_id, number, hash)
}

fn execution_receipt(receipt_hash: DomainHash) -> Option<ExecutionReceiptFor<DomainHeader, Block, Balance>> {
Domains::execution_receipt(receipt_hash)
}
Expand Down
35 changes: 4 additions & 31 deletions domains/client/relayer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ pub enum Error {
ApiError(sp_api::ApiError),
/// Emits when the core domain block is not yet confirmed on the system domain.
DomainNonConfirmedOnConsensusChain,
/// Emits when the core domain block state root is invalid.
DomainStateRootInvalid,
/// Failed to submit a cross domain message
UnableToSubmitCrossDomainMessage(TrySendError<GossipMessage>),
/// Invalid ChainId
Expand Down Expand Up @@ -321,36 +319,11 @@ where
return Ok(());
}

// verify if the state root is matching.
let domain_number = *domain_block_header.number();
if !consensus_chain_api
.domain_state_root(
best_consensus_chain_hash,
domain_id,
domain_number,
confirmed_block_hash,
)?
.map(|state_root| state_root == (*domain_block_header.state_root()))
.unwrap_or_else(|| {
// if this is genesis block, ignore as state root of genesis for domain is not tracked on runtime
domain_number.is_zero()
})
{
tracing::error!(
target: LOG_TARGET,
"Domain state root mismatch at: Number: {:?}, Hash: {:?}",
domain_number,
confirmed_block_hash
);
return Err(Error::DomainStateRootInvalid);
}

// generate domain proof that points to the state root of the domain block on Consensus chain.
// generate domain proof that points to the confirmed domain block on consensus chain
let storage_key = consensus_chain_api
.confirmed_domain_block_storage_key(best_consensus_chain_hash, domain_id)?;

// construct storage proof for the core domain state root using system domain backend.
let domain_state_root_proof = consensus_chain_client.read_proof(
let domain_proof = consensus_chain_client.read_proof(
best_consensus_chain_hash,
&mut [storage_key.as_ref()].into_iter(),
)?;
Expand All @@ -365,7 +338,7 @@ where
block_hash,
key,
*best_consensus_chain_block_header.state_root(),
domain_state_root_proof.clone(),
domain_proof.clone(),
)
},
|msg| Self::gossip_outbox_message(domain_client, msg, gossip_message_sink),
Expand All @@ -381,7 +354,7 @@ where
block_id,
key,
*best_consensus_chain_block_header.state_root(),
domain_state_root_proof.clone(),
domain_proof.clone(),
)
},
|msg| Self::gossip_inbox_message_response(domain_client, msg, gossip_message_sink),
Expand Down
2 changes: 1 addition & 1 deletion domains/primitives/messenger-host-functions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ std = [
"sp-std/std",
]

runtime-benchmarks = []
runtime-benchmarks = []
4 changes: 0 additions & 4 deletions test/subspace-test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,10 +1268,6 @@ impl_runtime_apis! {
Domains::domain_best_number(domain_id)
}

fn domain_state_root(domain_id: DomainId, number: DomainNumber, hash: DomainHash) -> Option<DomainHash>{
Domains::domain_state_root(domain_id, number, hash)
}

fn execution_receipt(receipt_hash: DomainHash) -> Option<ExecutionReceiptFor<DomainHeader, Block, Balance>> {
Domains::execution_receipt(receipt_hash)
}
Expand Down

0 comments on commit 62f1387

Please sign in to comment.