Skip to content

Commit 66bf4ac

Browse files
committed
Make BlockHash in pallet-domain-registry domain-aware
While updating the docs, I realized that `BlockHash` must be domain-aware too otherwise if any core domain stalls but the other domains keep moving steadily, then the state corrupts.
1 parent 81f25e2 commit 66bf4ac

File tree

2 files changed

+23
-13
lines changed
  • crates/pallet-domains/src
  • domains/pallets/domain-registry/src

2 files changed

+23
-13
lines changed

crates/pallet-domains/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,10 @@ mod pallet {
307307
}
308308
}
309309

310-
/// Map of block number to block hash.
310+
/// Map of primary block number to primary block hash.
311311
///
312312
/// NOTE: The oldest block hash will be pruned once the oldest receipt is pruned. However, if the
313-
/// execution chain stalls, i.e., no receipts are included in the primary chain for a long time,
313+
/// system domain stalls, i.e., no receipts are included in the primary chain for a long time,
314314
/// this mapping will grow indefinitely.
315315
#[pallet::storage]
316316
pub(super) type BlockHash<T: Config> =

domains/pallets/domain-registry/src/lib.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,6 @@ mod pallet {
179179
pub(super) type DomainTotalStakeWeight<T: Config> =
180180
StorageMap<_, Twox64Concat, DomainId, T::StakeWeight, OptionQuery>;
181181

182-
/// Map of primary block number to primary block hash.
183-
///
184-
/// NOTE: The oldest block hash will be pruned once the oldest receipt is pruned. However, if the
185-
/// execution chain stalls, i.e., no receipts are included in the primary chain for a long time,
186-
/// this mapping will grow indefinitely.
187-
#[pallet::storage]
188-
pub(super) type BlockHash<T: Config> =
189-
StorageMap<_, Twox64Concat, T::BlockNumber, T::Hash, ValueQuery>;
190-
191182
//////////////////////////////////////////////////////////////////////////////////////////////////////////
192183
//// Same receipt tracking data structure as in pallet-domains, with the dimension `domain_id` added.
193184
//////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -236,6 +227,25 @@ mod pallet {
236227
OptionQuery,
237228
>;
238229

230+
/// Map of primary block number to primary block hash for tracking bounded receipts per domain.
231+
///
232+
/// NOTE: This storage item is extended on adding a new non-system receipt since each receipt
233+
/// is validated to point to a valid primary block on the primary chain.
234+
///
235+
/// The oldest block hash will be pruned once the oldest receipt is pruned. However, if a
236+
/// core domain stalls, i.e., no receipts are included in the system domain for a long time,
237+
/// the corresponding entry will grow indefinitely.
238+
#[pallet::storage]
239+
pub(super) type BlockHash<T: Config> = StorageDoubleMap<
240+
_,
241+
Twox64Concat,
242+
DomainId,
243+
Twox64Concat,
244+
T::BlockNumber,
245+
T::Hash,
246+
ValueQuery,
247+
>;
248+
239249
#[pallet::call]
240250
impl<T: Config> Pallet<T> {
241251
/// Creates a new domain with some deposit locked.
@@ -1007,7 +1017,7 @@ impl<T: Config> Pallet<T> {
10071017

10081018
// (primary_number, primary_hash) has been verified on the primary chain, thus it
10091019
// can be used directly.
1010-
<BlockHash<T>>::insert(primary_number, primary_hash);
1020+
<BlockHash<T>>::insert(domain_id, primary_number, primary_hash);
10111021

10121022
// Apply the new best receipt.
10131023
<Receipts<T>>::insert(domain_id, receipt_hash, execution_receipt);
@@ -1032,7 +1042,7 @@ impl<T: Config> Pallet<T> {
10321042

10331043
// Remove the expired receipts once the receipts cache is full.
10341044
if let Some(to_prune) = primary_number.checked_sub(&T::ReceiptsPruningDepth::get()) {
1035-
BlockHash::<T>::mutate_exists(to_prune, |maybe_block_hash| {
1045+
BlockHash::<T>::mutate_exists(domain_id, to_prune, |maybe_block_hash| {
10361046
if let Some(block_hash) = maybe_block_hash.take() {
10371047
for (receipt_hash, _) in
10381048
<ReceiptVotes<T>>::drain_prefix((domain_id, block_hash))

0 commit comments

Comments
 (0)