Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions txnprovider/shutter/eon_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
67 changes: 67 additions & 0 deletions txnprovider/shutter/eon_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package shutter_test

import (
"bytes"
"context"
"errors"
"strings"
"testing"
"testing/synctest"

Expand All @@ -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"
)
Expand Down Expand Up @@ -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)
})
}