-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(internal/ethapi)!: reject
eth_getProof
queries for historical …
…blocks (#719) - default behavior for pruning mode to reject blocks before the 32 blocks preceding the last accepted block - default behavior for archive mode to reject blocks before ~24h worth of blocks preceding the last accepted block - archive mode new option `historical-proof-query-window` to customize the blocks window, or set it to 0 to accept any block number
- Loading branch information
Showing
15 changed files
with
991 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// (c) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package ethapi | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/ava-labs/coreth/core/types" | ||
"github.com/ava-labs/coreth/rpc" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
func TestBlockChainAPI_stateQueryBlockNumberAllowed(t *testing.T) { | ||
t.Parallel() | ||
|
||
const queryWindow uint64 = 1024 | ||
|
||
makeBlockWithNumber := func(number uint64) *types.Block { | ||
header := &types.Header{ | ||
Number: big.NewInt(int64(number)), | ||
} | ||
return types.NewBlock(header, nil, nil, nil, nil) | ||
} | ||
|
||
testCases := map[string]struct { | ||
blockNumOrHash rpc.BlockNumberOrHash | ||
makeBackend func(ctrl *gomock.Controller) *MockBackend | ||
wantErrMessage string | ||
}{ | ||
"zero_query_window": { | ||
blockNumOrHash: rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(1000)), | ||
makeBackend: func(ctrl *gomock.Controller) *MockBackend { | ||
backend := NewMockBackend(ctrl) | ||
backend.EXPECT().IsArchive().Return(true) | ||
backend.EXPECT().HistoricalProofQueryWindow().Return(uint64(0)) | ||
return backend | ||
}, | ||
}, | ||
"block_number_allowed_below_window": { | ||
blockNumOrHash: rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(1000)), | ||
makeBackend: func(ctrl *gomock.Controller) *MockBackend { | ||
backend := NewMockBackend(ctrl) | ||
backend.EXPECT().IsArchive().Return(true) | ||
backend.EXPECT().HistoricalProofQueryWindow().Return(queryWindow) | ||
backend.EXPECT().LastAcceptedBlock().Return(makeBlockWithNumber(1020)) | ||
return backend | ||
}, | ||
}, | ||
"block_number_allowed": { | ||
blockNumOrHash: rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(2000)), | ||
makeBackend: func(ctrl *gomock.Controller) *MockBackend { | ||
backend := NewMockBackend(ctrl) | ||
backend.EXPECT().IsArchive().Return(true) | ||
backend.EXPECT().HistoricalProofQueryWindow().Return(queryWindow) | ||
backend.EXPECT().LastAcceptedBlock().Return(makeBlockWithNumber(2200)) | ||
return backend | ||
}, | ||
}, | ||
"block_number_allowed_by_hash": { | ||
blockNumOrHash: rpc.BlockNumberOrHashWithHash(common.Hash{99}, false), | ||
makeBackend: func(ctrl *gomock.Controller) *MockBackend { | ||
backend := NewMockBackend(ctrl) | ||
backend.EXPECT().IsArchive().Return(true) | ||
backend.EXPECT().HistoricalProofQueryWindow().Return(queryWindow) | ||
backend.EXPECT().LastAcceptedBlock().Return(makeBlockWithNumber(2200)) | ||
backend.EXPECT(). | ||
BlockByNumberOrHash(gomock.Any(), gomock.Any()). | ||
Return(makeBlockWithNumber(2000), nil) | ||
return backend | ||
}, | ||
}, | ||
"block_number_allowed_by_hash_error": { | ||
blockNumOrHash: rpc.BlockNumberOrHashWithHash(common.Hash{99}, false), | ||
makeBackend: func(ctrl *gomock.Controller) *MockBackend { | ||
backend := NewMockBackend(ctrl) | ||
backend.EXPECT().IsArchive().Return(true) | ||
backend.EXPECT().HistoricalProofQueryWindow().Return(queryWindow) | ||
backend.EXPECT().LastAcceptedBlock().Return(makeBlockWithNumber(2200)) | ||
backend.EXPECT(). | ||
BlockByNumberOrHash(gomock.Any(), gomock.Any()). | ||
Return(nil, fmt.Errorf("test error")) | ||
return backend | ||
}, | ||
wantErrMessage: "failed to get block from hash: test error", | ||
}, | ||
"block_number_out_of_window": { | ||
blockNumOrHash: rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(1000)), | ||
makeBackend: func(ctrl *gomock.Controller) *MockBackend { | ||
backend := NewMockBackend(ctrl) | ||
backend.EXPECT().IsArchive().Return(true) | ||
backend.EXPECT().HistoricalProofQueryWindow().Return(queryWindow) | ||
backend.EXPECT().LastAcceptedBlock().Return(makeBlockWithNumber(2200)) | ||
return backend | ||
}, | ||
wantErrMessage: "block number 1000 is before the oldest allowed block number 1176 (window of 1024 blocks)", | ||
}, | ||
"block_number_out_of_window_non_archive": { | ||
blockNumOrHash: rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(1000)), | ||
makeBackend: func(ctrl *gomock.Controller) *MockBackend { | ||
backend := NewMockBackend(ctrl) | ||
backend.EXPECT().IsArchive().Return(false) | ||
// query window is 32 as set to core.TipBufferSize | ||
backend.EXPECT().LastAcceptedBlock().Return(makeBlockWithNumber(1033)) | ||
return backend | ||
}, | ||
wantErrMessage: "block number 1000 is before the oldest allowed block number 1001 (window of 32 blocks)", | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
ctrl := gomock.NewController(t) | ||
|
||
api := &BlockChainAPI{ | ||
b: testCase.makeBackend(ctrl), | ||
} | ||
|
||
err := api.stateQueryBlockNumberAllowed(testCase.blockNumOrHash) | ||
if testCase.wantErrMessage == "" { | ||
assert.NoError(t, err) | ||
} else { | ||
assert.EqualError(t, err, testCase.wantErrMessage) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package ethapi | ||
|
||
//go:generate go run go.uber.org/mock/mockgen -package=$GOPACKAGE -destination=mocks_test.go . Backend |
Oops, something went wrong.