-
Notifications
You must be signed in to change notification settings - Fork 104
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
Bound finalized header, execution header and sync committee by pruning older data beyond threshold #810
Changes from all commits
703c0d5
087804b
2551c48
a299b8a
39f40fc
a923233
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>; | ||
} | ||
|
@@ -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>; | ||
|
@@ -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(()) | ||
|
@@ -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!( | ||
|
@@ -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); | ||
} | ||
} | ||
} | ||
|
||
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", | ||
|
@@ -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", | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.