|
| 1 | +// (c) 2019-2024, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package ethapi |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "math/big" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/ava-labs/coreth/core/types" |
| 12 | + "github.com/ava-labs/coreth/rpc" |
| 13 | + "github.com/ethereum/go-ethereum/common" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "go.uber.org/mock/gomock" |
| 16 | +) |
| 17 | + |
| 18 | +func TestBlockChainAPI_stateQueryBlockNumberAllowed(t *testing.T) { |
| 19 | + t.Parallel() |
| 20 | + |
| 21 | + const queryWindow uint64 = 1024 |
| 22 | + |
| 23 | + makeBlockWithNumber := func(number uint64) *types.Block { |
| 24 | + header := &types.Header{ |
| 25 | + Number: big.NewInt(int64(number)), |
| 26 | + } |
| 27 | + return types.NewBlock(header, nil, nil, nil, nil) |
| 28 | + } |
| 29 | + |
| 30 | + testCases := map[string]struct { |
| 31 | + blockNumOrHash rpc.BlockNumberOrHash |
| 32 | + makeBackend func(ctrl *gomock.Controller) *MockBackend |
| 33 | + wantErrMessage string |
| 34 | + }{ |
| 35 | + "zero_query_window": { |
| 36 | + blockNumOrHash: rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(1000)), |
| 37 | + makeBackend: func(ctrl *gomock.Controller) *MockBackend { |
| 38 | + backend := NewMockBackend(ctrl) |
| 39 | + backend.EXPECT().IsArchive().Return(true) |
| 40 | + backend.EXPECT().HistoricalProofQueryWindow().Return(uint64(0)) |
| 41 | + return backend |
| 42 | + }, |
| 43 | + }, |
| 44 | + "block_number_allowed_below_window": { |
| 45 | + blockNumOrHash: rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(1000)), |
| 46 | + makeBackend: func(ctrl *gomock.Controller) *MockBackend { |
| 47 | + backend := NewMockBackend(ctrl) |
| 48 | + backend.EXPECT().IsArchive().Return(true) |
| 49 | + backend.EXPECT().HistoricalProofQueryWindow().Return(queryWindow) |
| 50 | + backend.EXPECT().LastAcceptedBlock().Return(makeBlockWithNumber(1020)) |
| 51 | + return backend |
| 52 | + }, |
| 53 | + }, |
| 54 | + "block_number_allowed": { |
| 55 | + blockNumOrHash: rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(2000)), |
| 56 | + makeBackend: func(ctrl *gomock.Controller) *MockBackend { |
| 57 | + backend := NewMockBackend(ctrl) |
| 58 | + backend.EXPECT().IsArchive().Return(true) |
| 59 | + backend.EXPECT().HistoricalProofQueryWindow().Return(queryWindow) |
| 60 | + backend.EXPECT().LastAcceptedBlock().Return(makeBlockWithNumber(2200)) |
| 61 | + return backend |
| 62 | + }, |
| 63 | + }, |
| 64 | + "block_number_allowed_by_hash": { |
| 65 | + blockNumOrHash: rpc.BlockNumberOrHashWithHash(common.Hash{99}, false), |
| 66 | + makeBackend: func(ctrl *gomock.Controller) *MockBackend { |
| 67 | + backend := NewMockBackend(ctrl) |
| 68 | + backend.EXPECT().IsArchive().Return(true) |
| 69 | + backend.EXPECT().HistoricalProofQueryWindow().Return(queryWindow) |
| 70 | + backend.EXPECT().LastAcceptedBlock().Return(makeBlockWithNumber(2200)) |
| 71 | + backend.EXPECT(). |
| 72 | + BlockByNumberOrHash(gomock.Any(), gomock.Any()). |
| 73 | + Return(makeBlockWithNumber(2000), nil) |
| 74 | + return backend |
| 75 | + }, |
| 76 | + }, |
| 77 | + "block_number_allowed_by_hash_error": { |
| 78 | + blockNumOrHash: rpc.BlockNumberOrHashWithHash(common.Hash{99}, false), |
| 79 | + makeBackend: func(ctrl *gomock.Controller) *MockBackend { |
| 80 | + backend := NewMockBackend(ctrl) |
| 81 | + backend.EXPECT().IsArchive().Return(true) |
| 82 | + backend.EXPECT().HistoricalProofQueryWindow().Return(queryWindow) |
| 83 | + backend.EXPECT().LastAcceptedBlock().Return(makeBlockWithNumber(2200)) |
| 84 | + backend.EXPECT(). |
| 85 | + BlockByNumberOrHash(gomock.Any(), gomock.Any()). |
| 86 | + Return(nil, fmt.Errorf("test error")) |
| 87 | + return backend |
| 88 | + }, |
| 89 | + wantErrMessage: "failed to get block from hash: test error", |
| 90 | + }, |
| 91 | + "block_number_out_of_window": { |
| 92 | + blockNumOrHash: rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(1000)), |
| 93 | + makeBackend: func(ctrl *gomock.Controller) *MockBackend { |
| 94 | + backend := NewMockBackend(ctrl) |
| 95 | + backend.EXPECT().IsArchive().Return(true) |
| 96 | + backend.EXPECT().HistoricalProofQueryWindow().Return(queryWindow) |
| 97 | + backend.EXPECT().LastAcceptedBlock().Return(makeBlockWithNumber(2200)) |
| 98 | + return backend |
| 99 | + }, |
| 100 | + wantErrMessage: "block number 1000 is before the oldest allowed block number 1176 (window of 1024 blocks)", |
| 101 | + }, |
| 102 | + "block_number_out_of_window_non_archive": { |
| 103 | + blockNumOrHash: rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(1000)), |
| 104 | + makeBackend: func(ctrl *gomock.Controller) *MockBackend { |
| 105 | + backend := NewMockBackend(ctrl) |
| 106 | + backend.EXPECT().IsArchive().Return(false) |
| 107 | + // query window is 32 as set to core.TipBufferSize |
| 108 | + backend.EXPECT().LastAcceptedBlock().Return(makeBlockWithNumber(1033)) |
| 109 | + return backend |
| 110 | + }, |
| 111 | + wantErrMessage: "block number 1000 is before the oldest allowed block number 1001 (window of 32 blocks)", |
| 112 | + }, |
| 113 | + } |
| 114 | + |
| 115 | + for name, testCase := range testCases { |
| 116 | + t.Run(name, func(t *testing.T) { |
| 117 | + t.Parallel() |
| 118 | + ctrl := gomock.NewController(t) |
| 119 | + |
| 120 | + api := &BlockChainAPI{ |
| 121 | + b: testCase.makeBackend(ctrl), |
| 122 | + } |
| 123 | + |
| 124 | + err := api.stateQueryBlockNumberAllowed(testCase.blockNumOrHash) |
| 125 | + if testCase.wantErrMessage == "" { |
| 126 | + assert.NoError(t, err) |
| 127 | + } else { |
| 128 | + assert.EqualError(t, err, testCase.wantErrMessage) |
| 129 | + } |
| 130 | + }) |
| 131 | + } |
| 132 | +} |
0 commit comments