Skip to content

Commit

Permalink
Remove FinalizedHeaderSlotsCache (Snowfork#842)
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong authored May 24, 2023
1 parent 789fa3e commit aa45732
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 57 deletions.
3 changes: 0 additions & 3 deletions parachain/pallets/ethereum-beacon-client/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,4 @@ pub const DOMAIN_SYNC_COMMITTEE: [u8; 4] = [7, 0, 0, 0];
pub const PUBKEY_SIZE: usize = 48;
pub const SIGNATURE_SIZE: usize = 96;

/// FIXME: Remove before production release
pub const SLOT_CACHE_SIZE: u32 = 1000;

const_assert!(SYNC_COMMITTEE_BITS_SIZE == SYNC_COMMITTEE_SIZE / 8);
16 changes: 0 additions & 16 deletions parachain/pallets/ethereum-beacon-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ mod benchmarking;

use frame_support::{
dispatch::DispatchResult, log, pallet_prelude::OptionQuery, traits::Get, transactional,
BoundedVec,
};
use frame_system::ensure_signed;
use primitives::{
Expand Down Expand Up @@ -167,13 +166,6 @@ pub mod pallet {
pub(crate) type ExecutionHeaderMapping<T: Config> =
StorageMap<_, Identity, u32, H256, ValueQuery>;

/// FIXME: Remove before using in production
/// A cache of slot numbers for finalized headers that have been recently imported.
/// Is used by the offchain relayer to produce ancestry proofs for execution headers.
#[pallet::storage]
pub(super) type FinalizedHeaderSlotsCache<T: Config> =
StorageValue<_, BoundedVec<u64, ConstU32<{ config::SLOT_CACHE_SIZE }>>, ValueQuery>;

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
Expand Down Expand Up @@ -550,14 +542,6 @@ pub mod pallet {
);
<LatestFinalizedBlockRoot<T>>::set(header_root);

// Add the slot of the most recently finalized header to the slot cache
<FinalizedHeaderSlotsCache<T>>::mutate(|slots| {
if slots.len() as u32 == config::SLOT_CACHE_SIZE {
slots.remove(0);
}
slots.try_push(header.slot).expect("checked above; qed");
});

log::info!(
target: "ethereum-beacon-client",
"💫 Updated latest finalized block root {} at slot {}.",
Expand Down
15 changes: 0 additions & 15 deletions relayer/chain/parachain/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,21 +222,6 @@ func (wr *ParachainWriter) GetLastBasicChannelNonceByAddress(address common.Addr
return uint64(nonce), nil
}

func (wr *ParachainWriter) GetFinalizedSlots() ([]uint64, error) {
key, err := types.CreateStorageKey(wr.conn.Metadata(), "EthereumBeaconClient", "FinalizedHeaderSlotsCache", nil, nil)
if err != nil {
return nil, fmt.Errorf("create storage key for basic channel nonces: %w", err)
}

var slots []uint64
_, err = wr.conn.API().RPC.State.GetStorageLatest(key, &slots)
if err != nil {
return nil, fmt.Errorf("get storage for latest basic channel nonces (err): %w", err)
}

return slots, nil
}

func (wr *ParachainWriter) GetLastExecutionHeaderState() (state.ExecutionHeader, error) {
key, err := types.CreateStorageKey(wr.conn.Metadata(), "EthereumBeaconClient", "LatestExecutionHeader", nil, nil)
if err != nil {
Expand Down
30 changes: 7 additions & 23 deletions relayer/relays/beacon/header/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ func (h *Header) Sync(ctx context.Context, eg *errgroup.Group) error {

log.WithField("period", latestSyncedPeriod).Info("set cache: last beacon synced sync committee period")

finalizedSlots, err := h.writer.GetFinalizedSlots()
if err != nil {
return fmt.Errorf("fetch parachain finalized header slots: %w", err)
}

h.cache.AddCheckPointSlots(finalizedSlots)
h.cache.AddCheckPointSlots([]uint64{lastFinalizedHeaderState.BeaconSlot})

log.WithField("finalizedSlots", h.cache.Finalized.Checkpoints.Slots).Info("set cache: finalized checkpoint slots")

Expand Down Expand Up @@ -163,8 +158,6 @@ func (h *Header) SyncCommitteePeriodUpdate(ctx context.Context, period uint64) e
}
}

h.cache.AddCheckPoint(update.FinalizedHeaderBlockRoot, update.BlockRootsTree, uint64(update.Payload.FinalizedHeader.Slot))

log.WithFields(log.Fields{
"finalized_header_slot": update.Payload.FinalizedHeader.Slot,
"period": period,
Expand All @@ -175,19 +168,10 @@ func (h *Header) SyncCommitteePeriodUpdate(ctx context.Context, period uint64) e
return err
}

// lastFinalizedHeader, err := h.writer.GetLastFinalizedHeaderState()
// if err != nil {
// return fmt.Errorf("fetch last finalized header from parachain: %w", err)
// }
// lastFinalizedHeaderPeriod := h.syncer.ComputeSyncPeriodAtSlot(lastFinalizedHeader.BeaconSlot)

// Period + 1 because the sync committee update contains the next period's sync committee
// if lastFinalizedHeaderPeriod != period+1 {
// return fmt.Errorf("synced committee period %d not imported successfully", lastFinalizedHeaderPeriod)
// }

h.cache.SetLastSyncedSyncCommitteePeriod(period)

h.cache.AddCheckPoint(update.FinalizedHeaderBlockRoot, update.BlockRootsTree, uint64(update.Payload.FinalizedHeader.Slot))

return nil
}

Expand Down Expand Up @@ -222,8 +206,6 @@ func (h *Header) SyncFinalizedHeader(ctx context.Context) (scale.Update, error)
h.cache.Finalized.LastAttemptedSyncHash = update.FinalizedHeaderBlockRoot
h.cache.Finalized.LastAttemptedSyncSlot = uint64(update.Payload.FinalizedHeader.Slot)

h.cache.AddCheckPoint(update.FinalizedHeaderBlockRoot, update.BlockRootsTree, uint64(update.Payload.FinalizedHeader.Slot))

lastFinalizedHeaderState, err := h.writer.GetLastFinalizedHeaderState()
if err != nil {
return scale.Update{}, fmt.Errorf("fetch last finalized header state: %w", err)
Expand All @@ -241,6 +223,8 @@ func (h *Header) SyncFinalizedHeader(ctx context.Context) (scale.Update, error)
return scale.Update{}, ErrFinalizedHeaderNotImported
}

h.cache.AddCheckPoint(update.FinalizedHeaderBlockRoot, update.BlockRootsTree, uint64(update.Payload.FinalizedHeader.Slot))

return update, err
}

Expand Down Expand Up @@ -416,8 +400,6 @@ func (h *Header) syncLaggingExecutionHeaders(ctx context.Context, lastFinalizedH
}
}

h.cache.AddCheckPoint(lastFinalizedHeader, blockRootsProof.Tree, lastFinalizedSlot)

if executionHeaderState.BlockNumber == 0 {
log.Info("start of syncing, no execution header lag found")

Expand Down Expand Up @@ -448,6 +430,8 @@ func (h *Header) syncLaggingExecutionHeaders(ctx context.Context, lastFinalizedH
return fmt.Errorf("sync headers: %w", err)
}

h.cache.AddCheckPoint(lastFinalizedHeader, blockRootsProof.Tree, lastFinalizedSlot)

return nil
}

Expand Down

0 comments on commit aa45732

Please sign in to comment.