Skip to content

Commit

Permalink
Add the DomainBlockDescendants storage item to pallet-domains
Browse files Browse the repository at this point in the history
This storage item is used to keek track of the immediate descendants of an
ER, it is used during fraud proof processing to prune all the descendants of
the targetted fraudulent ER. This storage item can be removed after this task
#1731 is implemented, by then every
ER should only have one immediate descendant

Signed-off-by: linning <linningde25@gmail.com>
  • Loading branch information
NingLin-P committed Sep 6, 2023
1 parent 6c98769 commit 5ac6bd9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
22 changes: 15 additions & 7 deletions crates/pallet-domains/src/block_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use crate::pallet::StateRoots;
use crate::{
BalanceOf, BlockTree, Config, ConsensusBlockHash, DomainBlocks, ExecutionInbox,
ExecutionReceiptOf, HeadReceiptNumber, InboxedBundle,
BalanceOf, BlockTree, Config, ConsensusBlockHash, DomainBlockDescendants, DomainBlocks,
ExecutionInbox, ExecutionReceiptOf, HeadReceiptNumber, InboxedBundle,
};
use codec::{Decode, Encode};
use frame_support::{ensure, PalletError};
Expand Down Expand Up @@ -257,18 +257,19 @@ pub(crate) fn process_execution_receipt<T: Config>(
return Err(Error::MultipleERsAfterChallengePeriod);
}

let receipt = receipts_at_number
let receipt_hash = receipts_at_number
.first()
.cloned()
.expect("should always have a value due to check above");

let domain_block =
DomainBlocks::<T>::take(receipt).ok_or(Error::MissingDomainBlock)?;
DomainBlocks::<T>::take(receipt_hash).ok_or(Error::MissingDomainBlock)?;
_ = StateRoots::<T>::take((
domain_id,
domain_block.execution_receipt.domain_block_number,
domain_block.execution_receipt.domain_block_hash,
));
_ = DomainBlockDescendants::<T>::take(receipt_hash);

// Remove the block's `ExecutionInbox` and `InboxedBundle` as the block is pruned and
// does not need to verify its receipt's `extrinsics_root` anymore.
Expand Down Expand Up @@ -323,13 +324,19 @@ fn add_new_receipt_to_block_tree<T: Config>(
execution_receipt.final_state_root,
);

BlockTree::<T>::mutate(domain_id, domain_block_number, |er_hashes| {
er_hashes.insert(er_hash);
});
DomainBlockDescendants::<T>::mutate(
execution_receipt.parent_domain_block_receipt_hash,
|er_hashes| {
er_hashes.insert(er_hash);
},
);
let domain_block = DomainBlock {
execution_receipt,
operator_ids: sp_std::vec![submitter],
};
BlockTree::<T>::mutate(domain_id, domain_block_number, |er_hashes| {
er_hashes.insert(er_hash);
});
DomainBlocks::<T>::insert(er_hash, domain_block);
}

Expand Down Expand Up @@ -631,6 +638,7 @@ mod tests {
pruned_receipt.consensus_block_number,
)
.is_none());
assert!(DomainBlockDescendants::<Test>::get(pruned_receipt.hash()).is_empty());
});
}

Expand Down
7 changes: 7 additions & 0 deletions crates/pallet-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,13 @@ mod pallet {
OptionQuery,
>;

// Mapping of the parent ER to all its immediate descendants ER
// TODO: remove this mapping once https://github.com/subspace/subspace/issues/1731 is implemented
// by then every parent ER should only have one immediate descendants ER
#[pallet::storage]
pub(super) type DomainBlockDescendants<T: Config> =
StorageMap<_, Identity, ReceiptHash, BTreeSet<ReceiptHash>, ValueQuery>;

/// The head receipt number of each domain
#[pallet::storage]
pub(super) type HeadReceiptNumber<T: Config> =
Expand Down

0 comments on commit 5ac6bd9

Please sign in to comment.