Skip to content

Commit 0554671

Browse files
authored
Resolve latest to the safe watermark in tracer block lookups (#4552)
`debug_traceCall`, `debug_traceBlockByNumber` and the other tracer paths that go through `Backend.BlockByNumber` resolved `latest`, `safe` and `finalized` with `ConvertBlockNumber`, which returns the app's latest height. The watermarks can trail that height by a block (for example, while the receipt store catches up). The concrete height then failed the watermark check with `requested height N is not yet available; safe latest is N-1`, even though the caller never asked for a specific height. This is the failure the nightly `TestNightlyGigaMixedRelease` run hit on 10-07. Part of SEI-10413. `BlockByNumber` now resolves the tag with `getBlockNumber`, the helper the other block lookups already use. It returns a nil height for `latest`, `safe`, `finalized` and `pending`, which `blockByNumberRespectingWatermarks` resolves to the safe latest, and it returns an error when the genesis lookup for `earliest` fails. `ConvertBlockNumber` had no other callers and is removed along with its test. It also panicked on `pending` and on a failed genesis lookup, so `debug_traceBlockByNumber` at `pending` now traces the latest block, as the other endpoints already do for `pending`. The new test puts the block store and app state one block ahead of the receipt store and checks that all four tags resolve to the safe latest. Without the fix it fails with the same error as the nightly run.
1 parent 1e58601 commit 0554671

2 files changed

Lines changed: 67 additions & 40 deletions

File tree

‎evmrpc/simulate.go‎

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -418,26 +418,12 @@ func (b *Backend) ChainDb() ethdb.Database {
418418
panic("implement me")
419419
}
420420

421-
func (b Backend) ConvertBlockNumber(bn rpc.BlockNumber) int64 {
422-
blockNum := bn.Int64()
423-
switch blockNum {
424-
case rpc.SafeBlockNumber.Int64(), rpc.FinalizedBlockNumber.Int64(), rpc.LatestBlockNumber.Int64():
425-
blockNum = b.ctxProvider(LatestCtxHeight).BlockHeight()
426-
case rpc.EarliestBlockNumber.Int64():
427-
genesisRes, err := b.tmClient.Genesis(context.Background())
428-
if err != nil {
429-
panic("could not get genesis info from tendermint")
430-
}
431-
blockNum = genesisRes.Genesis.InitialHeight
432-
case rpc.PendingBlockNumber.Int64():
433-
panic("tracing on pending block is not supported")
434-
}
435-
return blockNum
436-
}
437-
438421
func (b Backend) BlockByNumber(ctx context.Context, bn rpc.BlockNumber) (*ethtypes.Block, []tracersutils.TraceBlockMetadata, error) {
439-
blockNum := b.ConvertBlockNumber(bn)
440-
tmBlock, err := blockByNumberRespectingWatermarks(ctx, b.tmClient, b.watermarks, &blockNum, 1)
422+
heightPtr, err := getBlockNumber(ctx, b.tmClient, bn)
423+
if err != nil {
424+
return nil, nil, err
425+
}
426+
tmBlock, err := blockByNumberRespectingWatermarks(ctx, b.tmClient, b.watermarks, heightPtr, 1)
441427
if err != nil {
442428
return nil, nil, err
443429
}

‎evmrpc/simulate_test.go‎

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -381,27 +381,6 @@ func TestNewRevertError(t *testing.T) {
381381
require.Equal(t, "0x", err.ErrorData())
382382
}
383383

384-
func TestConvertBlockNumber(t *testing.T) {
385-
tmClient := &MockClient{}
386-
watermarks := evmrpc.NewWatermarkManager(tmClient, func(i int64) sdk.Context {
387-
if i == evmrpc.LatestCtxHeight {
388-
return sdk.Context{}.WithBlockHeight(1000)
389-
}
390-
return sdk.Context{}
391-
}, nil, nil)
392-
backend := evmrpc.NewBackend(func(i int64) sdk.Context {
393-
if i == evmrpc.LatestCtxHeight {
394-
return sdk.Context{}.WithBlockHeight(1000)
395-
}
396-
return sdk.Context{}
397-
}, nil, legacyabci.BeginBlockKeepers{}, nil, &MockClient{}, nil, nil, nil, evmrpc.NewBlockCache(3000), &sync.Mutex{}, watermarks)
398-
require.Equal(t, int64(10), backend.ConvertBlockNumber(10))
399-
require.Equal(t, int64(1), backend.ConvertBlockNumber(0))
400-
require.Equal(t, int64(1000), backend.ConvertBlockNumber(-2))
401-
require.Equal(t, int64(1000), backend.ConvertBlockNumber(-3))
402-
require.Equal(t, int64(1000), backend.ConvertBlockNumber(-4))
403-
}
404-
405384
func TestPreV620UpgradeUsesBaseFeeNil(t *testing.T) {
406385
// Set up a test context with a height before v6.2.0 upgrade
407386
// For pacific-1 chain, we need to set a height that's before the v6.2.0 upgrade
@@ -980,6 +959,68 @@ func (c *fixedBlockClient) Validators(context.Context, *int64, *int, *int) (*cor
980959
return &coretypes.ResultValidators{}, nil
981960
}
982961

962+
// heightBlockClient serves an empty block at every height up to latest.
963+
type heightBlockClient struct {
964+
fixedBlockClient
965+
latest int64
966+
}
967+
968+
func (c *heightBlockClient) Block(_ context.Context, height *int64) (*coretypes.ResultBlock, error) {
969+
h := c.latest
970+
if height != nil {
971+
h = *height
972+
}
973+
if h > c.latest {
974+
return nil, fmt.Errorf("height %d is above latest %d", h, c.latest)
975+
}
976+
return &coretypes.ResultBlock{
977+
Block: &tmtypes.Block{Header: mockBlockHeader(h), LastCommit: &tmtypes.Commit{Height: h}},
978+
}, nil
979+
}
980+
981+
func (c *heightBlockClient) BlockResults(ctx context.Context, height *int64) (*coretypes.ResultBlockResults, error) {
982+
block, err := c.Block(ctx, height)
983+
if err != nil {
984+
return nil, err
985+
}
986+
return &coretypes.ResultBlockResults{Height: block.Block.Height}, nil
987+
}
988+
989+
func (c *heightBlockClient) Status(_ context.Context) (*coretypes.ResultStatus, error) {
990+
return &coretypes.ResultStatus{
991+
SyncInfo: coretypes.SyncInfo{LatestBlockHeight: c.latest, EarliestBlockHeight: 1},
992+
}, nil
993+
}
994+
995+
func TestBlockByNumberResolvesLatestToSafeLatest(t *testing.T) {
996+
const safeLatest = int64(42)
997+
998+
testApp := app.Setup(t, false, false, false)
999+
ctx := testApp.GetContextForDeliverTx([]byte{}).WithBlockHeight(safeLatest + 1)
1000+
// The receipt store trails the block store and app state by one block.
1001+
primeReceiptStore(t, testApp.EvmKeeper.ReceiptStore(), safeLatest)
1002+
ctxProvider := func(height int64) sdk.Context {
1003+
if height == evmrpc.LatestCtxHeight {
1004+
return ctx
1005+
}
1006+
return ctx.WithBlockHeight(height)
1007+
}
1008+
tmClient := &heightBlockClient{latest: safeLatest + 1}
1009+
watermarks := evmrpc.NewWatermarkManager(tmClient, ctxProvider, nil, testApp.EvmKeeper.ReceiptStore())
1010+
backend := evmrpc.NewBackend(
1011+
ctxProvider, &testApp.EvmKeeper, legacyabci.BeginBlockKeepers{},
1012+
func(int64) client.TxConfig { return TxConfig }, tmClient, &SConfig,
1013+
testApp.BaseApp, testApp.TracerAnteHandler,
1014+
evmrpc.NewBlockCache(3000), &sync.Mutex{}, watermarks,
1015+
)
1016+
1017+
for _, bn := range []rpc.BlockNumber{rpc.LatestBlockNumber, rpc.SafeBlockNumber, rpc.FinalizedBlockNumber, rpc.PendingBlockNumber} {
1018+
ethBlock, _, err := backend.BlockByNumber(t.Context(), bn)
1019+
require.NoError(t, err, bn.String())
1020+
require.Equal(t, safeLatest, ethBlock.Number().Int64(), bn.String())
1021+
}
1022+
}
1023+
9831024
func TestStateAtBlockReplaysIncrementalTallyActivationAndGapBoundary(t *testing.T) {
9841025
const (
9851026
v67UpgradeHeight = int64(200)

0 commit comments

Comments
 (0)