Skip to content

Commit

Permalink
Merge pull request #2162 from subspace/disallow-multiple-segment-head…
Browse files Browse the repository at this point in the history
…ers-inherents

Only allow one `store_segment_headers` inherent per block
  • Loading branch information
nazar-pc authored Oct 25, 2023
2 parents 3a33091 + 75f9ec5 commit 9b7f434
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions crates/pallet-subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,13 @@ pub mod pallet {
subspace_core_primitives::SegmentCommitment,
>;

/// Whether the segment headers inherent has been processed in this block (temporary value).
///
/// This value is updated to `true` when processing `store_segment_headers` by a node.
/// It is then cleared at the end of each block execution in the `on_finalize` hook.
#[pallet::storage]
pub(super) type DidProcessSegmentHeaders<T: Config> = StorageValue<_, bool, ValueQuery>;

/// Storage of previous vote verification data, updated on each block during finalization.
#[pallet::storage]
pub(super) type ParentVoteVerificationData<T> = StorageValue<_, VoteVerificationData>;
Expand Down Expand Up @@ -1001,6 +1008,8 @@ impl<T: Config> Pallet<T> {
ParentVoteVerificationData::<T>::put(current_vote_verification_data::<T>(true));

ParentBlockVoters::<T>::put(CurrentBlockVoters::<T>::take().unwrap_or_default());

DidProcessSegmentHeaders::<T>::take();
}

fn do_report_equivocation(
Expand All @@ -1019,6 +1028,11 @@ impl<T: Config> Pallet<T> {
}

fn do_store_segment_headers(segment_headers: Vec<SegmentHeader>) -> DispatchResult {
assert!(
!DidProcessSegmentHeaders::<T>::exists(),
"Segment headers must be updated only once in the block"
);

for segment_header in segment_headers {
SegmentCommitment::<T>::insert(
segment_header.segment_index(),
Expand All @@ -1031,6 +1045,8 @@ impl<T: Config> Pallet<T> {
));
Self::deposit_event(Event::SegmentHeaderStored { segment_header });
}

DidProcessSegmentHeaders::<T>::put(true);
Ok(())
}

Expand Down

0 comments on commit 9b7f434

Please sign in to comment.