Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bound finalized header, execution header and sync committee by pruning older data beyond threshold #810

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions parachain/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions parachain/pallets/ethereum-beacon-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ snowbridge-ethereum = { path = "../../primitives/ethereum", default-features = f
snowbridge-beacon-primitives = { path = "../../primitives/beacon", default-features = false }

[dev-dependencies]
rand = "0.8.5"
sp-keyring = { git = "https://github.com/paritytech/substrate.git", branch = "master" }
snowbridge-testutils = { path = "../../primitives/testutils" }
serde_json = "1.0.96"
Expand Down
105 changes: 94 additions & 11 deletions parachain/pallets/ethereum-beacon-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,14 @@ pub mod pallet {
type MaxSlotsPerHistoricalRoot: Get<u64>;
#[pallet::constant]
type MaxFinalizedHeaderSlotArray: Get<u32>;
/// Maximum execution headers to be stored
#[pallet::constant]
type ExecutionHeadersPruneThreshold: Get<u64>;
#[pallet::constant]
type ForkVersions: Get<ForkVersions>;
/// Maximum sync committees to be stored
#[pallet::constant]
type SyncCommitteePruneThreshold: Get<u64>;
type WeightInfo: WeightInfo;
type WeakSubjectivityPeriodSeconds: Get<u64>;
}
Expand Down Expand Up @@ -165,21 +171,31 @@ pub mod pallet {

#[pallet::storage]
pub(super) type FinalizedBeaconHeaderSlots<T: Config> =
StorageValue<_, BoundedVec<u64, T::MaxFinalizedHeaderSlotArray>, ValueQuery>;
StorageValue<_, BoundedVec<(u64, H256), T::MaxFinalizedHeaderSlotArray>, ValueQuery>;

#[pallet::storage]
pub(super) type FinalizedBeaconHeadersBlockRoot<T: Config> =
StorageMap<_, Identity, H256, H256, ValueQuery>;

/// Header mapping bounds. Used by pruning algorithm to prune oldest value and add
/// latest value.
#[pallet::storage]
pub(super) type ExecutionHeadersOldestMapping<T: Config> =
StorageValue<_, (u64, u64), OptionQuery>;

/// Mapping of count -> Execution header hash. Used to prune older headers
#[pallet::storage]
pub(super) type ExecutionHeadersMapping<T: Config> =
StorageMap<_, Identity, u64, H256, ValueQuery>;

#[pallet::storage]
pub(super) type ExecutionHeaders<T: Config> =
StorageMap<_, Identity, H256, ExecutionHeader, OptionQuery>;
CountedStorageMap<_, Identity, H256, ExecutionHeader, OptionQuery>;

/// Current sync committee corresponding to the active header.
/// TODO prune older sync committees than xxx
#[pallet::storage]
pub(super) type SyncCommittees<T: Config> =
StorageMap<_, Identity, u64, SyncCommitteeOf<T>, ValueQuery>;
CountedStorageMap<_, Identity, u64, SyncCommitteeOf<T>, ValueQuery>;

#[pallet::storage]
pub(super) type ValidatorsRoot<T: Config> = StorageValue<_, H256, ValueQuery>;
Expand Down Expand Up @@ -384,7 +400,7 @@ pub mod pallet {
};

<FinalizedBeaconHeaders<T>>::insert(block_root, initial_sync.header);
Self::add_finalized_header_slot(slot)?;
Self::add_finalized_header_slot(slot, block_root)?;
<LatestFinalizedHeaderState<T>>::set(last_finalized_header);

Ok(())
Expand Down Expand Up @@ -864,7 +880,7 @@ pub mod pallet {
Ok(())
}

fn store_sync_committee(period: u64, sync_committee: SyncCommitteeOf<T>) {
pub(crate) fn store_sync_committee(period: u64, sync_committee: SyncCommitteeOf<T>) {
<SyncCommittees<T>>::insert(period, sync_committee);

log::trace!(
Expand All @@ -874,15 +890,39 @@ pub mod pallet {
);

<LatestSyncCommitteePeriod<T>>::set(period);
Self::prune_older_sync_committees();

Self::deposit_event(Event::SyncCommitteeUpdated { period });
}

// Contract: It is assumed that the light client do not skip sync committee.
fn prune_older_sync_committees() {
let threshold = T::SyncCommitteePruneThreshold::get();
let stored_sync_committees = <SyncCommittees<T>>::count();

if stored_sync_committees as u64 > threshold {
let latest_sync_committee_period = <LatestSyncCommitteePeriod<T>>::get();
let highest_period_to_remove = latest_sync_committee_period - threshold;

let mut current_sync_committee_to_remove = highest_period_to_remove;
let mut number_of_sync_committees_to_remove =
stored_sync_committees as u64 - threshold;

while number_of_sync_committees_to_remove > 0 {
<SyncCommittees<T>>::remove(current_sync_committee_to_remove);
number_of_sync_committees_to_remove =
number_of_sync_committees_to_remove.saturating_sub(1);
current_sync_committee_to_remove =
current_sync_committee_to_remove.saturating_sub(1);
}
}
Comment on lines +911 to +918
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem here is the performance overhead of the while loop which could use up all the block weight.

Copy link
Contributor Author

@ParthDesai ParthDesai Apr 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while loop would run only 1 time. As it runs after every insert.

}

fn store_finalized_header(block_root: Root, header: BeaconHeader) -> DispatchResult {
let slot = header.slot;

<FinalizedBeaconHeaders<T>>::insert(block_root, header);
Self::add_finalized_header_slot(slot)?;
Self::add_finalized_header_slot(slot, block_root)?;

log::info!(
target: "ethereum-beacon-client",
Expand All @@ -902,27 +942,39 @@ pub mod pallet {
Ok(())
}

fn add_finalized_header_slot(slot: u64) -> DispatchResult {
pub(super) fn add_finalized_header_slot(
slot: u64,
finalized_header_hash: H256,
) -> DispatchResult {
<FinalizedBeaconHeaderSlots<T>>::try_mutate(|b_vec| {
if b_vec.len() as u32 == T::MaxFinalizedHeaderSlotArray::get() {
b_vec.remove(0);
let (_slot, finalized_header_hash) = b_vec.remove(0);
// Removing corresponding finalized header data of popped slot
// as that data will not be used by relayer anyway.
<FinalizedBeaconHeadersBlockRoot<T>>::remove(finalized_header_hash);
<FinalizedBeaconHeaders<T>>::remove(finalized_header_hash);
<FinalizedBeaconHeadersBlockRoot<T>>::remove(finalized_header_hash);
}
b_vec.try_push(slot)
b_vec.try_push((slot, finalized_header_hash))
})
.map_err(|_| <Error<T>>::FinalizedBeaconHeaderSlotsExceeded)?;

Ok(())
}

fn store_execution_header(
pub(super) fn store_execution_header(
block_hash: H256,
header: ExecutionHeader,
beacon_slot: u64,
beacon_block_root: H256,
) {
let block_number = header.block_number;

let (_, latest_mapping_to_insert) = Self::get_mapping_bound();
<ExecutionHeadersMapping<T>>::insert(latest_mapping_to_insert, block_hash);
Self::update_mapping_bound(None, Some(latest_mapping_to_insert + 1));
<ExecutionHeaders<T>>::insert(block_hash, header);
Self::prune_older_execution_headers();

log::trace!(
target: "ethereum-beacon-client",
Expand All @@ -941,6 +993,37 @@ pub mod pallet {
Self::deposit_event(Event::ExecutionHeaderImported { block_hash, block_number });
}

fn get_mapping_bound() -> (u64, u64) {
<ExecutionHeadersOldestMapping<T>>::get().unwrap_or((1, 1))
}

fn update_mapping_bound(oldest: Option<u64>, latest: Option<u64>) {
let (previous_oldest, previous_latest) =
<ExecutionHeadersOldestMapping<T>>::get().unwrap_or((1, 1));
let new_oldest = oldest.unwrap_or(previous_oldest);
let new_latest = latest.unwrap_or(previous_latest);
<ExecutionHeadersOldestMapping<T>>::put((new_oldest, new_latest));
}

fn prune_older_execution_headers() {
let threshold = T::ExecutionHeadersPruneThreshold::get();
let stored_execution_headers = <ExecutionHeaders<T>>::count() as u64;

if stored_execution_headers > threshold {
let (mut oldest_mapping_to_delete, _) = Self::get_mapping_bound();
let execution_headers_to_delete =
stored_execution_headers.saturating_sub(threshold);
for _i in 0..execution_headers_to_delete {
let execution_header_hash =
<ExecutionHeadersMapping<T>>::get(oldest_mapping_to_delete);
<ExecutionHeadersMapping<T>>::remove(oldest_mapping_to_delete);
<ExecutionHeaders<T>>::remove(execution_header_hash);
oldest_mapping_to_delete += 1;
}
Comment on lines +1016 to +1022
Copy link
Contributor

@yrong yrong Apr 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@ParthDesai ParthDesai Apr 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while loop would run only 1 time. As it runs after every insert.

Self::update_mapping_bound(Some(oldest_mapping_to_delete), None);
}
}

fn store_validators_root(validators_root: H256) {
<ValidatorsRoot<T>>::set(validators_root);
}
Expand Down
10 changes: 9 additions & 1 deletion parachain/pallets/ethereum-beacon-client/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ pub mod mock_minimal {
pub const MaxPublicKeySize: u32 = config::PUBKEY_SIZE as u32;
pub const MaxSignatureSize: u32 = config::SIGNATURE_SIZE as u32;
pub const MaxSlotsPerHistoricalRoot: u64 = 64;
pub const MaxFinalizedHeaderSlotArray: u32 = 1000;
pub const MaxFinalizedHeaderSlotArray: u32 = 12;
pub const SyncCommitteePruneThreshold: u64 = 4;
pub const ExecutionHeadersPruneThreshold: u64 = 10;
pub const WeakSubjectivityPeriodSeconds: u32 = 97200;
pub const ChainForkVersions: ForkVersions = ForkVersions{
genesis: Fork {
Expand Down Expand Up @@ -112,6 +114,8 @@ pub mod mock_minimal {
type MaxSlotsPerHistoricalRoot = MaxSlotsPerHistoricalRoot;
type MaxFinalizedHeaderSlotArray = MaxFinalizedHeaderSlotArray;
type ForkVersions = ChainForkVersions;
type SyncCommitteePruneThreshold = SyncCommitteePruneThreshold;
type ExecutionHeadersPruneThreshold = ExecutionHeadersPruneThreshold;
type WeakSubjectivityPeriodSeconds = WeakSubjectivityPeriodSeconds;
type WeightInfo = ();
}
Expand Down Expand Up @@ -203,6 +207,8 @@ pub mod mock_mainnet {
epoch: 162304,
},
};
pub const SyncCommitteePruneThreshold: u64 = 4;
pub const ExecutionHeadersPruneThreshold: u64 = 10;
}

impl ethereum_beacon_client::Config for Test {
Expand All @@ -218,6 +224,8 @@ pub mod mock_mainnet {
type MaxSlotsPerHistoricalRoot = MaxSlotsPerHistoricalRoot;
type MaxFinalizedHeaderSlotArray = MaxFinalizedHeaderSlotArray;
type ForkVersions = ChainForkVersions;
type SyncCommitteePruneThreshold = SyncCommitteePruneThreshold;
type ExecutionHeadersPruneThreshold = ExecutionHeadersPruneThreshold;
type WeakSubjectivityPeriodSeconds = WeakSubjectivityPeriodSeconds;
type WeightInfo = ();
}
Expand Down
Loading