diff --git a/beacon-chain/powchain/log_processing.go b/beacon-chain/powchain/log_processing.go index ce46ad0be0..d666a531fa 100644 --- a/beacon-chain/powchain/log_processing.go +++ b/beacon-chain/powchain/log_processing.go @@ -23,6 +23,7 @@ import ( "github.com/prysmaticlabs/prysm/encoding/bytesutil" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" protodb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" + "github.com/prysmaticlabs/prysm/time/slots" "github.com/sirupsen/logrus" ) @@ -374,9 +375,21 @@ func (s *Service) processPastLogs(ctx context.Context) error { if fRoot == params.BeaconConfig().ZeroHash { return nil } - fState, err := s.cfg.stateGen.StateByRoot(ctx, fRoot) - if err != nil { - return err + fState := s.cfg.finalizedStateAtStartup + isNil := fState == nil || fState.IsNil() + + // If processing past logs take a long time, we + // need to check if this is the correct finalized + // state we are referring to and whether our cached + // finalized state is referring to our current finalized checkpoint. + // The current code does ignore an edge case where the finalized + // block is in a different epoch from the checkpoint's epoch. + // This only happens in skipped slots, so pruning it is not an issue. + if isNil || slots.ToEpoch(fState.Slot()) != c.Epoch { + fState, err = s.cfg.stateGen.StateByRoot(ctx, fRoot) + if err != nil { + return err + } } if fState != nil && !fState.IsNil() && fState.Eth1DepositIndex() > 0 { s.cfg.depositCache.PrunePendingDeposits(ctx, int64(fState.Eth1DepositIndex()))