|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync/atomic" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + abci "github.com/cometbft/cometbft/abci/types" |
| 10 | + rpcclient "github.com/cometbft/cometbft/rpc/client" |
| 11 | + coretypes "github.com/cometbft/cometbft/rpc/core/types" |
| 12 | + cmttypes "github.com/cometbft/cometbft/types" |
| 13 | + servertypes "github.com/cosmos/evm/server/types" |
| 14 | + "github.com/ethereum/go-ethereum/common" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +// mockRPCClient scripts the only four rpcclient.Client methods the indexer |
| 19 | +// service uses. BlockResults fails until failuresLeft drains, tracking the |
| 20 | +// attempt count — the seam for both indexer-loop regressions. |
| 21 | +type mockRPCClient struct { |
| 22 | + rpcclient.Client // nil-embedded: any unscripted call panics loudly |
| 23 | + |
| 24 | + height int64 |
| 25 | + headerCh chan coretypes.ResultEvent |
| 26 | + failuresLeft atomic.Int64 |
| 27 | + attempts atomic.Int64 |
| 28 | +} |
| 29 | + |
| 30 | +func (m *mockRPCClient) Status(context.Context) (*coretypes.ResultStatus, error) { |
| 31 | + return &coretypes.ResultStatus{ |
| 32 | + SyncInfo: coretypes.SyncInfo{LatestBlockHeight: m.height}, |
| 33 | + }, nil |
| 34 | +} |
| 35 | + |
| 36 | +func (m *mockRPCClient) Subscribe(_ context.Context, _, _ string, _ ...int) (<-chan coretypes.ResultEvent, error) { |
| 37 | + return m.headerCh, nil |
| 38 | +} |
| 39 | + |
| 40 | +func (m *mockRPCClient) Block(_ context.Context, height *int64) (*coretypes.ResultBlock, error) { |
| 41 | + return &coretypes.ResultBlock{ |
| 42 | + Block: &cmttypes.Block{Header: cmttypes.Header{Height: *height}}, |
| 43 | + }, nil |
| 44 | +} |
| 45 | + |
| 46 | +func (m *mockRPCClient) BlockResults(_ context.Context, height *int64) (*coretypes.ResultBlockResults, error) { |
| 47 | + m.attempts.Add(1) |
| 48 | + if m.failuresLeft.Add(-1) >= 0 { |
| 49 | + return nil, context.DeadlineExceeded |
| 50 | + } |
| 51 | + return &coretypes.ResultBlockResults{Height: *height}, nil |
| 52 | +} |
| 53 | + |
| 54 | +// mockIndexer records indexed heights; the query methods are never used by |
| 55 | +// the service loop. |
| 56 | +type mockIndexer struct { |
| 57 | + indexed chan int64 |
| 58 | +} |
| 59 | + |
| 60 | +func (m *mockIndexer) LastIndexedBlock() (int64, error) { return 0, nil } |
| 61 | +func (m *mockIndexer) FirstIndexedBlock() (int64, error) { return 0, nil } |
| 62 | +func (m *mockIndexer) IndexBlock(block *cmttypes.Block, _ []*abci.ExecTxResult) error { |
| 63 | + m.indexed <- block.Height |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +func (m *mockIndexer) GetByTxHash(common.Hash) (*servertypes.TxResult, error) { |
| 68 | + panic("not used") |
| 69 | +} |
| 70 | + |
| 71 | +func (m *mockIndexer) GetByBlockAndIndex(int64, int32) (*servertypes.TxResult, error) { |
| 72 | + panic("not used") |
| 73 | +} |
| 74 | + |
| 75 | +// TestIndexerServiceRetriesAfterFetchError guards both historical failure |
| 76 | +// modes of the indexing loop around transient Block/BlockResults errors: |
| 77 | +// |
| 78 | +// - stall (upstream cosmos/evm as of v0.6.0): the fetch error is latched and |
| 79 | +// never cleared, so after the first failure the loop only sleeps and the |
| 80 | +// indexer never progresses again — this test times out on that code; |
| 81 | +// - busy-loop (moca's pre-fix copy): the loop hammered the failing fetch |
| 82 | +// with no backoff — the bounded attempt count below fails on that code. |
| 83 | +// |
| 84 | +// Correct behavior: back off on error, then re-attempt on the next new-block |
| 85 | +// signal (or timeout) and recover. |
| 86 | +func TestIndexerServiceRetriesAfterFetchError(t *testing.T) { |
| 87 | + client := &mockRPCClient{ |
| 88 | + height: 1, |
| 89 | + headerCh: make(chan coretypes.ResultEvent, 4), |
| 90 | + } |
| 91 | + client.failuresLeft.Store(2) // first two BlockResults calls fail, then succeed |
| 92 | + idxr := &mockIndexer{indexed: make(chan int64, 8)} |
| 93 | + |
| 94 | + svc := NewEVMIndexerService(idxr, client) |
| 95 | + go func() { |
| 96 | + _ = svc.OnStart() // loops forever; test asserts via channels |
| 97 | + }() |
| 98 | + |
| 99 | + signal := func(height int64) { |
| 100 | + client.headerCh <- coretypes.ResultEvent{ |
| 101 | + Data: cmttypes.EventDataNewBlockHeader{ |
| 102 | + Header: cmttypes.Header{Height: height}, |
| 103 | + }, |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + // Attempt 1 fails on startup catch-up (height 1). Each signal wakes the |
| 108 | + // wait branch: attempt 2 fails, attempt 3 succeeds and must recover. |
| 109 | + signal(2) |
| 110 | + signal(3) |
| 111 | + |
| 112 | + deadline := time.After(10 * time.Second) |
| 113 | + got := map[int64]bool{} |
| 114 | + for len(got) < 2 { |
| 115 | + select { |
| 116 | + case h := <-idxr.indexed: |
| 117 | + got[h] = true |
| 118 | + case <-deadline: |
| 119 | + t.Fatalf("indexer stalled after transient fetch error; indexed so far: %v (attempts=%d)", |
| 120 | + got, client.attempts.Load()) |
| 121 | + } |
| 122 | + } |
| 123 | + require.True(t, got[1] && got[2], "heights 1 and 2 must be indexed after recovery, got %v", got) |
| 124 | + |
| 125 | + // No busy-loop: while erroring, exactly one fetch attempt per wake-up. |
| 126 | + // Catch-up + two signaled retries + the follow-up block ≈ 4 attempts; |
| 127 | + // leave slack for scheduling, but orders of magnitude below a busy loop. |
| 128 | + require.LessOrEqual(t, client.attempts.Load(), int64(6), |
| 129 | + "fetch attempts should be bounded by wake-ups (busy-loop regression)") |
| 130 | +} |
0 commit comments