diff --git a/txnprovider/shutter/eon_tracker.go b/txnprovider/shutter/eon_tracker.go index 04d4fc04b12..eceae4f619a 100644 --- a/txnprovider/shutter/eon_tracker.go +++ b/txnprovider/shutter/eon_tracker.go @@ -176,9 +176,7 @@ func (et *KsmEonTracker) handleBlockEvent(blockEvent BlockEvent) error { eon, ok, err := et.readEonAtNewBlockEvent(blockNum) if errors.Is(err, bind.ErrNoCode) { // A reset or unwind can precede deployment, invalidating all cached eons. - et.currentEon = nil - et.recentEons.Clear(false) - et.lastCleanupBlockNum = 0 + et.resetEonCache() return nil } if err != nil { @@ -199,6 +197,12 @@ func (et *KsmEonTracker) handleBlockEvent(blockEvent BlockEvent) error { return nil } +func (et *KsmEonTracker) resetEonCache() { + et.currentEon = nil + et.recentEons.Clear(false) + et.lastCleanupBlockNum = 0 +} + func (et *KsmEonTracker) readEonAtNewBlockEvent(blockNum uint64) (_ Eon, _ bool, err error) { var cached bool startTime := time.Now() @@ -211,7 +215,8 @@ func (et *KsmEonTracker) readEonAtNewBlockEvent(blockNum uint64) (_ Eon, _ bool, }() callOpts := &bind.CallOpts{BlockNumber: uint256.NewInt(blockNum)} - if et.currentEon == nil { + beforeCurrentActivation := et.currentEon != nil && blockNum < et.currentEon.ActivationBlock + if et.currentEon == nil || beforeCurrentActivation { var numKeyperSets uint64 numKeyperSets, err = et.ksmContract.GetNumKeyperSets(callOpts) if err != nil { @@ -220,6 +225,9 @@ func (et *KsmEonTracker) readEonAtNewBlockEvent(blockNum uint64) (_ Eon, _ bool, // wait until we have at least one keyper set added and past activation block // to avoid execution reverts for GetKeyperSetIndexByBlock if numKeyperSets == 0 { + if beforeCurrentActivation { + et.resetEonCache() + } et.logger.Warn("no keyper sets found", "blockNum", blockNum) return Eon{}, false, nil } @@ -230,6 +238,9 @@ func (et *KsmEonTracker) readEonAtNewBlockEvent(blockNum uint64) (_ Eon, _ bool, return Eon{}, false, fmt.Errorf("initial get keyper set activation block: %w", err) } if blockNum < activationBlock { + if beforeCurrentActivation { + et.resetEonCache() + } et.logger.Warn("initial keyper set not yet activated", "blockNum", blockNum, "activationAt", activationBlock) return Eon{}, false, nil } diff --git a/txnprovider/shutter/eon_tracker_test.go b/txnprovider/shutter/eon_tracker_test.go index d658b28cc33..b137d47b270 100644 --- a/txnprovider/shutter/eon_tracker_test.go +++ b/txnprovider/shutter/eon_tracker_test.go @@ -17,7 +17,10 @@ package shutter_test import ( + "bytes" "context" + "errors" + "strings" "testing" "testing/synctest" @@ -28,9 +31,12 @@ import ( "github.com/erigontech/erigon/common" "github.com/erigontech/erigon/common/log/v3" + "github.com/erigontech/erigon/execution/abi" + "github.com/erigontech/erigon/execution/abi/bind" "github.com/erigontech/erigon/execution/chain/networkname" "github.com/erigontech/erigon/node/gointerfaces/remoteproto" "github.com/erigontech/erigon/txnprovider/shutter" + "github.com/erigontech/erigon/txnprovider/shutter/internal/contracts" "github.com/erigontech/erigon/txnprovider/shutter/internal/testhelpers" "github.com/erigontech/erigon/txnprovider/shutter/shuttercfg" ) @@ -101,3 +107,64 @@ func TestEonTrackerRecoversBeforeManagerDeployment(t *testing.T) { require.True(t, ok) }) } + +func TestEonTrackerRecoversBeforeFirstKeyperSetActivation(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + logger := log.New() + backend := NewMockContractBackend(gomock.NewController(t), logger) + config := shuttercfg.ConfigByChainName(networkname.Chiado) + manager := common.HexToAddress(config.KeyperSetManagerContractAddress) + const activationBlock = 10 + managerABI, err := abi.JSON(strings.NewReader(contracts.KeyperSetManagerABI)) + require.NoError(t, err) + indexCall, err := managerABI.Pack("getKeyperSetIndexByBlock", uint64(activationBlock-1)) + require.NoError(t, err) + backend.EXPECT().CallContract(gomock.Any(), gomock.Cond(func(msg bind.CallMsg) bool { + return msg.To != nil && *msg.To == manager && bytes.Equal(msg.Data, indexCall) + }), uint256.NewInt(activationBlock-1)).Return(nil, errors.New("execution reverted")).AnyTimes() + backend.PrepareMocks() + batches := make(chan *remoteproto.StateChangeBatch) + listener := shutter.NewBlockListener(logger, testhelpers.NewStateChangesClientMock(ctx, batches)) + tracker := shutter.NewKsmEonTracker(logger, config, listener, backend) + eg, runCtx := errgroup.WithContext(ctx) + eg.Go(func() error { return listener.Run(runCtx) }) + eg.Go(func() error { return tracker.Run(runCtx) }) + defer func() { + cancel() + require.ErrorIs(t, eg.Wait(), context.Canceled) + }() + synctest.Wait() + + handle := PoolTestHandle{config: config, contractBackend: backend} + initial, err := testhelpers.MockEonKeyGeneration(0, 1, 2, activationBlock) + require.NoError(t, err) + handle.SimulateInitialEonRead(t, initial) + batches <- &remoteproto.StateChangeBatch{ChangeBatch: []*remoteproto.StateChange{{BlockHeight: activationBlock}}} + synctest.Wait() + eon, ok := tracker.CurrentEon() + require.True(t, ok) + require.Equal(t, initial.Eon(), eon) + + handle.SimulateCallResult(manager, uint256.NewInt(1).PaddedBytes(32)) + handle.SimulateCallResult(manager, uint256.NewInt(activationBlock).PaddedBytes(32)) + batches <- &remoteproto.StateChangeBatch{ChangeBatch: []*remoteproto.StateChange{{ + BlockHeight: activationBlock - 1, + Direction: remoteproto.Direction_UNWIND, + }}} + synctest.Wait() + _, ok = tracker.CurrentEon() + require.False(t, ok, "the first keyper set is no longer active") + _, ok = tracker.RecentEon(initial.EonIndex) + require.False(t, ok, "the old eon must not remain cached") + + replacement, err := testhelpers.MockEonKeyGeneration(0, 1, 2, activationBlock) + require.NoError(t, err) + handle.SimulateInitialEonRead(t, replacement) + batches <- &remoteproto.StateChangeBatch{ChangeBatch: []*remoteproto.StateChange{{BlockHeight: activationBlock}}} + synctest.Wait() + eon, ok = tracker.CurrentEon() + require.True(t, ok) + require.Equal(t, replacement.Eon(), eon) + }) +}