Skip to content

Commit

Permalink
polygon/heimdall: fix snapshot store last entity to check in snapshot…
Browse files Browse the repository at this point in the history
…s too (#13845)

highlighted by #13746
relates to
#13844 (comment)
snapshot store should return last entity from snapshots if there are
none in the db
  • Loading branch information
taratorio authored Feb 17, 2025
1 parent efdd02a commit a1c438d
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion polygon/heimdall/snapshot_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ func (s *SpanSnapshotStore) LastEntityId(ctx context.Context) (uint64, bool, err
return lastId, ok, err
}

func (s *SpanSnapshotStore) LastEntity(ctx context.Context) (*Span, bool, error) {
return snapshotStoreLastEntity(ctx, s)
}

func (s *SpanSnapshotStore) ValidateSnapshots(logger log.Logger, failFast bool) error {
return validateSnapshots(logger, failFast, s.snapshots, s.SnapType(), generics.New[Span])
}
Expand Down Expand Up @@ -312,6 +316,10 @@ func (s *MilestoneSnapshotStore) Entity(ctx context.Context, id uint64) (*Milest
return nil, false, fmt.Errorf("%w: %w", ErrMilestoneNotFound, err)
}

func (s *MilestoneSnapshotStore) LastEntity(ctx context.Context) (*Milestone, bool, error) {
return snapshotStoreLastEntity(ctx, s)
}

func (s *MilestoneSnapshotStore) ValidateSnapshots(logger log.Logger, failFast bool) error {
return validateSnapshots(logger, failFast, s.snapshots, s.SnapType(), generics.New[Milestone])
}
Expand Down Expand Up @@ -345,7 +353,7 @@ func (s *CheckpointSnapshotStore) WithTx(tx kv.Tx) EntityStore[*Checkpoint] {
return &CheckpointSnapshotStore{txEntityStore[*Checkpoint]{s.EntityStore.(*mdbxEntityStore[*Checkpoint]), tx}, s.snapshots}
}

func (s *CheckpointSnapshotStore) LastCheckpointId(ctx context.Context, tx kv.Tx) (uint64, bool, error) {
func (s *CheckpointSnapshotStore) LastEntityId(ctx context.Context) (uint64, bool, error) {
lastId, ok, err := s.EntityStore.LastEntityId(ctx)

snapshotLastCheckpointId := s.LastFrozenEntityId()
Expand Down Expand Up @@ -425,6 +433,10 @@ func (s *CheckpointSnapshotStore) LastFrozenEntityId() uint64 {
return index.BaseDataID() + index.KeyCount() - 1
}

func (s *CheckpointSnapshotStore) LastEntity(ctx context.Context) (*Checkpoint, bool, error) {
return snapshotStoreLastEntity(ctx, s)
}

func (s *CheckpointSnapshotStore) ValidateSnapshots(logger log.Logger, failFast bool) error {
return validateSnapshots(logger, failFast, s.snapshots, s.SnapType(), generics.New[Checkpoint])
}
Expand Down Expand Up @@ -482,3 +494,12 @@ func validateSnapshots[T Entity](logger log.Logger, failFast bool, snaps *RoSnap

return accumulatedErr
}

func snapshotStoreLastEntity[T Entity](ctx context.Context, store EntityStore[T]) (T, bool, error) {
entityId, ok, err := store.LastEntityId(ctx)
if err != nil || !ok {
return generics.Zero[T](), false, err
}

return store.Entity(ctx, entityId)
}

0 comments on commit a1c438d

Please sign in to comment.