From 693cc79cc95913f792d27080f882fada4fc69558 Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Fri, 11 Mar 2022 15:34:15 +0800 Subject: [PATCH] Minor Cleanup from #10255 (#10337) * minor cleanup * kasey's review * kasey's review --- beacon-chain/state/stategen/replayer.go | 55 +++++++++++--------- beacon-chain/state/stategen/replayer_test.go | 3 +- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/beacon-chain/state/stategen/replayer.go b/beacon-chain/state/stategen/replayer.go index ad8026b030..4afa447d35 100644 --- a/beacon-chain/state/stategen/replayer.go +++ b/beacon-chain/state/stategen/replayer.go @@ -20,6 +20,7 @@ var ErrFutureSlotRequested = errors.New("cannot replay to future slots") var ErrNoCanonicalBlockForSlot = errors.New("none of the blocks found in the db slot index are canonical") var ErrNoBlocksBelowSlot = errors.New("no blocks found in db below slot") var ErrInvalidDBBlock = errors.New("invalid block found in database") +var ErrReplayTargetSlotExceeded = errors.New("desired replay slot is less than state's slot") // HistoryAccessor describes the minimum set of database methods needed to support the ReplayerBuilder. type HistoryAccessor interface { @@ -54,11 +55,10 @@ type Replayer interface { var _ Replayer = &stateReplayer{} type stateReplayer struct { - s state.BeaconState - descendants []block.SignedBeaconBlock - target types.Slot - method retrievalMethod - chainer chainer + s state.BeaconState + target types.Slot + method retrievalMethod + chainer chainer } // ReplayBlocks applies all the blocks that were accumulated when building the Replayer. @@ -127,28 +127,31 @@ func (rs *stateReplayer) ReplayToSlot(ctx context.Context, replayTo types.Slot) if err != nil { return nil, errors.Wrap(err, "failed to ReplayBlocks") } - - if replayTo > s.Slot() { - start := time.Now() - log.WithFields(logrus.Fields{ - "startSlot": s.Slot(), - "endSlot": replayTo, - "diff": replayTo - s.Slot(), - }).Debug("calling process_slots on remaining slots") - - if replayTo > s.Slot() { - // err will be handled after the bookend log - s, err = ReplayProcessSlots(ctx, s, replayTo) - } - - duration := time.Since(start) - log.WithFields(logrus.Fields{ - "duration": duration, - }).Debug("time spent in process_slots") - if err != nil { - return nil, errors.Wrap(err, fmt.Sprintf("ReplayToSlot failed to seek to slot %d after applying blocks", replayTo)) - } + if replayTo < s.Slot() { + return nil, errors.Wrapf(ErrReplayTargetSlotExceeded, "slot desired=%d, state.slot=%d", replayTo, s.Slot()) } + if replayTo == s.Slot() { + return s, nil + } + + start := time.Now() + log.WithFields(logrus.Fields{ + "startSlot": s.Slot(), + "endSlot": replayTo, + "diff": replayTo - s.Slot(), + }).Debug("calling process_slots on remaining slots") + + // err will be handled after the bookend log + s, err = ReplayProcessSlots(ctx, s, replayTo) + + duration := time.Since(start) + log.WithFields(logrus.Fields{ + "duration": duration, + }).Debug("time spent in process_slots") + if err != nil { + return nil, errors.Wrap(err, fmt.Sprintf("ReplayToSlot failed to seek to slot %d after applying blocks", replayTo)) + } + return s, nil } diff --git a/beacon-chain/state/stategen/replayer_test.go b/beacon-chain/state/stategen/replayer_test.go index 424aa17535..95b96128af 100644 --- a/beacon-chain/state/stategen/replayer_test.go +++ b/beacon-chain/state/stategen/replayer_test.go @@ -6,10 +6,9 @@ import ( "fmt" "testing" - "github.com/prysmaticlabs/prysm/beacon-chain/state" - "github.com/pkg/errors" types "github.com/prysmaticlabs/eth2-types" + "github.com/prysmaticlabs/prysm/beacon-chain/state" "github.com/prysmaticlabs/prysm/encoding/bytesutil" ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/block"