Skip to content

Commit

Permalink
Optimize ReplayBlocks for Zero Diff (#13198)
Browse files Browse the repository at this point in the history
* Stategen: replay block return early when zero diff

* Fix test setup
  • Loading branch information
terencechain authored Nov 17, 2023
1 parent bba8dd6 commit d035be2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
8 changes: 5 additions & 3 deletions beacon-chain/state/stategen/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,11 @@ func newMockHistory(t *testing.T, hist []mockHistorySpec, current primitives.Slo
b, err = blocktest.SetBlockParentRoot(b, pr)
require.NoError(t, err)

// now do process_block
s, err = transition.ProcessBlockForStateRoot(ctx, s, b)
require.NoError(t, err)
// now do process_block only if block slot is greater than latest header slot
if b.Block().Slot() > s.LatestBlockHeader().Slot {
s, err = transition.ProcessBlockForStateRoot(ctx, s, b)
require.NoError(t, err)
}

sr, err := s.HashTreeRoot(ctx)
require.NoError(t, err)
Expand Down
4 changes: 4 additions & 0 deletions beacon-chain/state/stategen/replayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ func (rs *stateReplayer) ReplayBlocks(ctx context.Context) (state.BeaconState, e
msg := fmt.Sprintf("error subtracting state.slot %d from replay target slot %d", s.Slot(), rs.target)
return nil, errors.Wrap(err, msg)
}
if diff == 0 {
return s, nil
}

log.WithFields(logrus.Fields{
"startSlot": s.Slot(),
"endSlot": rs.target,
Expand Down
12 changes: 12 additions & 0 deletions beacon-chain/state/stategen/replayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v4/testing/require"
logTest "github.com/sirupsen/logrus/hooks/test"
)

func headerFromBlock(b interfaces.ReadOnlySignedBeaconBlock) (*ethpb.BeaconBlockHeader, error) {
Expand All @@ -27,6 +28,17 @@ func headerFromBlock(b interfaces.ReadOnlySignedBeaconBlock) (*ethpb.BeaconBlock
}, nil
}

func TestReplayBlocks_ZeroDiff(t *testing.T) {
logHook := logTest.NewGlobal()
ctx := context.Background()
specs := []mockHistorySpec{{slot: 0}}
hist := newMockHistory(t, specs, 0)
ch := NewCanonicalHistory(hist, hist, hist)
_, err := ch.ReplayerForSlot(0).ReplayBlocks(ctx)
require.NoError(t, err)
require.LogsDoNotContain(t, logHook, "Replaying canonical blocks from most recent state")
}

func TestReplayBlocks(t *testing.T) {
ctx := context.Background()
var zero, one, two, three, four, five primitives.Slot = 50, 51, 150, 151, 152, 200
Expand Down

0 comments on commit d035be2

Please sign in to comment.