Skip to content

Commit

Permalink
recent-blocks-window configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Dec 26, 2024
1 parent a0ca465 commit 5eae4ff
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 17 deletions.
5 changes: 3 additions & 2 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type EthAPIBackend struct {
allowUnprotectedTxHashes map[common.Hash]struct{} // Invariant: read-only after creation.
allowUnfinalizedQueries bool
historicalProofs bool
recentBlocksWindow uint64
eth *Ethereum
gpo *gasprice.Oracle
}
Expand All @@ -68,8 +69,8 @@ func (b *EthAPIBackend) ChainConfig() *params.ChainConfig {
return b.eth.blockchain.Config()
}

func (b *EthAPIBackend) HistoricalConfig() (historicalProofs bool) {
return b.historicalProofs
func (b *EthAPIBackend) HistoricalConfig() (historicalProofs bool, recentBlocksWindow uint64) {
return b.historicalProofs, b.recentBlocksWindow
}

func (b *EthAPIBackend) IsAllowUnfinalizedQueries() bool {
Expand Down
1 change: 1 addition & 0 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ func New(
allowUnprotectedTxHashes: allowUnprotectedTxHashes,
allowUnfinalizedQueries: config.AllowUnfinalizedQueries,
historicalProofs: config.HistoricalProofs,
recentBlocksWindow: config.RecentBlocksWindow,
eth: eth,
}
if config.AllowUnprotectedTxs {
Expand Down
2 changes: 2 additions & 0 deletions eth/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func NewDefaultConfig() Config {
RPCEVMTimeout: 5 * time.Second,
GPO: DefaultFullGPOConfig,
RPCTxFeeCap: 1, // 1 AVAX
RecentBlocksWindow: 1024,
}
}

Expand All @@ -96,6 +97,7 @@ type Config struct {
SnapshotVerify bool // Whether to verify generated snapshots
SkipSnapshotRebuild bool // Whether to skip rebuilding the snapshot in favor of returning an error (only set to true for tests)
HistoricalProofs bool // HistoricalProofs, if set to true, allows to query historical blocks for proofs.
RecentBlocksWindow uint64 // RecentBlocksWindow is the number of blocks before the last accepted block to be considered as recent and non-historical. It defaults to 1024.

// Database options
SkipBcVersionCheck bool `toml:"-"`
Expand Down
9 changes: 4 additions & 5 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ func (s *BlockChainAPI) GetProof(ctx context.Context, address common.Address, st
//
// Otherwise, a non-nil error is returned.
func (s *BlockChainAPI) isAllowedToBeQueried(blockNumOrHash rpc.BlockNumberOrHash) (err error) {
historicalProofs := s.b.HistoricalConfig()
historicalProofs, recentBlocksWindow := s.b.HistoricalConfig()
if historicalProofs {
return nil
}
Expand All @@ -796,16 +796,15 @@ func (s *BlockChainAPI) isAllowedToBeQueried(blockNumOrHash rpc.BlockNumberOrHas
number = block.NumberU64()
}

const latestBlocksWindow = 1024 // TODO: find a decent default for validators
var oldestContemporaryNumber uint64
if lastNumber > latestBlocksWindow {
oldestContemporaryNumber = lastNumber - latestBlocksWindow
if lastNumber > recentBlocksWindow {
oldestContemporaryNumber = lastNumber - recentBlocksWindow
}
if number >= oldestContemporaryNumber {
return nil
}
return fmt.Errorf("block number %d is before the oldest non-historical block number %d (window of %d blocks)",
number, oldestContemporaryNumber, latestBlocksWindow)
number, oldestContemporaryNumber, recentBlocksWindow)
}

// decodeHash parses a hex-encoded 32-byte hash. The input may optionally
Expand Down
16 changes: 9 additions & 7 deletions internal/ethapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ func (b testBackend) LastAcceptedBlock() *types.Block { panic("implement me") }
func (b testBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
panic("implement me")
}
func (b testBackend) HistoricalConfig() (historicalBlocks bool) {
func (b testBackend) HistoricalConfig() (historicalBlocks bool, recentBlocksWindow uint64) {
panic("implement me")
}

Expand Down Expand Up @@ -2071,6 +2071,8 @@ func testRPCResponseWithFile(t *testing.T, testid int, result interface{}, rpc s
func TestBlockChainAPI_isAllowedToBeQueried(t *testing.T) {
t.Parallel()

const recentBlocksWindow uint64 = 1024

makeBlockWithNumber := func(number uint64) *types.Block {
header := &types.Header{
Number: big.NewInt(int64(number)),
Expand All @@ -2087,15 +2089,15 @@ func TestBlockChainAPI_isAllowedToBeQueried(t *testing.T) {
blockNumOrHash: rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(0)),
makeBackend: func(ctrl *gomock.Controller) *MockBackend {
backend := NewMockBackend(ctrl)
backend.EXPECT().HistoricalConfig().Return(true)
backend.EXPECT().HistoricalConfig().Return(true, recentBlocksWindow)
return backend
},
},
"block_number_recent_below_window": {
blockNumOrHash: rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(1000)),
makeBackend: func(ctrl *gomock.Controller) *MockBackend {
backend := NewMockBackend(ctrl)
backend.EXPECT().HistoricalConfig().Return(false)
backend.EXPECT().HistoricalConfig().Return(false, recentBlocksWindow)
backend.EXPECT().LastAcceptedBlock().Return(makeBlockWithNumber(1020))
return backend
},
Expand All @@ -2104,7 +2106,7 @@ func TestBlockChainAPI_isAllowedToBeQueried(t *testing.T) {
blockNumOrHash: rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(2000)),
makeBackend: func(ctrl *gomock.Controller) *MockBackend {
backend := NewMockBackend(ctrl)
backend.EXPECT().HistoricalConfig().Return(false)
backend.EXPECT().HistoricalConfig().Return(false, recentBlocksWindow)
backend.EXPECT().LastAcceptedBlock().Return(makeBlockWithNumber(2200))
return backend
},
Expand All @@ -2113,7 +2115,7 @@ func TestBlockChainAPI_isAllowedToBeQueried(t *testing.T) {
blockNumOrHash: rpc.BlockNumberOrHashWithHash(common.Hash{99}, false),
makeBackend: func(ctrl *gomock.Controller) *MockBackend {
backend := NewMockBackend(ctrl)
backend.EXPECT().HistoricalConfig().Return(false)
backend.EXPECT().HistoricalConfig().Return(false, recentBlocksWindow)
backend.EXPECT().LastAcceptedBlock().Return(makeBlockWithNumber(2200))
backend.EXPECT().
BlockByNumberOrHash(context.Background(), rpc.BlockNumberOrHashWithHash(common.Hash{99}, false)).
Expand All @@ -2125,7 +2127,7 @@ func TestBlockChainAPI_isAllowedToBeQueried(t *testing.T) {
blockNumOrHash: rpc.BlockNumberOrHashWithHash(common.Hash{99}, false),
makeBackend: func(ctrl *gomock.Controller) *MockBackend {
backend := NewMockBackend(ctrl)
backend.EXPECT().HistoricalConfig().Return(false)
backend.EXPECT().HistoricalConfig().Return(false, recentBlocksWindow)
backend.EXPECT().LastAcceptedBlock().Return(makeBlockWithNumber(2200))
backend.EXPECT().
BlockByNumberOrHash(context.Background(), rpc.BlockNumberOrHashWithHash(common.Hash{99}, false)).
Expand All @@ -2138,7 +2140,7 @@ func TestBlockChainAPI_isAllowedToBeQueried(t *testing.T) {
blockNumOrHash: rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(1000)),
makeBackend: func(ctrl *gomock.Controller) *MockBackend {
backend := NewMockBackend(ctrl)
backend.EXPECT().HistoricalConfig().Return(false)
backend.EXPECT().HistoricalConfig().Return(false, recentBlocksWindow)
backend.EXPECT().LastAcceptedBlock().Return(makeBlockWithNumber(2200))
return backend
},
Expand Down
2 changes: 1 addition & 1 deletion internal/ethapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ type Backend interface {
SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription
BadBlocks() ([]*types.Block, []*core.BadBlockReason)
HistoricalConfig() (historicalBlocks bool)
HistoricalConfig() (historicalBlocks bool, recentBlocksWindow uint64)

// Transaction pool API
SendTx(ctx context.Context, signedTx *types.Transaction) error
Expand Down
5 changes: 3 additions & 2 deletions internal/ethapi/mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions plugin/evm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const (
defaultLogJSONFormat = false
defaultMaxOutboundActiveRequests = 16
defaultPopulateMissingTriesParallelism = 1024
defaultRecentBlocksWindow = 1024
defaultStateSyncServerTrieCache = 64 // MB
defaultAcceptedCacheSize = 32 // blocks

Expand Down Expand Up @@ -125,6 +126,7 @@ type Config struct {
PopulateMissingTriesParallelism int `json:"populate-missing-tries-parallelism"` // Number of concurrent readers to use when re-populating missing tries on startup.
PruneWarpDB bool `json:"prune-warp-db-enabled"` // Determines if the warpDB should be cleared on startup
HistoricalProofs bool `json:"historical-proofs"` // HistoricalProofs, if set to true, allows to query historical block numbers for proofs.
RecentBlocksWindow uint64 `json:"recent-blocks-window"` // RecentBlocksWindow is the number of blocks before the last accepted block to be considered as recent and non-historical. It defaults to 1024.

// Metric Settings
MetricsExpensiveEnabled bool `json:"metrics-expensive-enabled"` // Debug-level metrics that might impact runtime performance
Expand Down Expand Up @@ -275,6 +277,7 @@ func (c *Config) SetDefaults() {
c.LogJSONFormat = defaultLogJSONFormat
c.MaxOutboundActiveRequests = defaultMaxOutboundActiveRequests
c.PopulateMissingTriesParallelism = defaultPopulateMissingTriesParallelism
c.RecentBlocksWindow = defaultRecentBlocksWindow
c.StateSyncServerTrieCache = defaultStateSyncServerTrieCache
c.StateSyncCommitInterval = defaultSyncableCommitInterval
c.StateSyncMinBlocks = defaultStateSyncMinBlocks
Expand Down
1 change: 1 addition & 0 deletions plugin/evm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ func (vm *VM) Initialize(
vm.ethConfig.SnapshotWait = vm.config.SnapshotWait
vm.ethConfig.SnapshotVerify = vm.config.SnapshotVerify
vm.ethConfig.HistoricalProofs = vm.config.HistoricalProofs
vm.ethConfig.RecentBlocksWindow = vm.config.RecentBlocksWindow
vm.ethConfig.OfflinePruning = vm.config.OfflinePruning
vm.ethConfig.OfflinePruningBloomFilterSize = vm.config.OfflinePruningBloomFilterSize
vm.ethConfig.OfflinePruningDataDirectory = vm.config.OfflinePruningDataDirectory
Expand Down

0 comments on commit 5eae4ff

Please sign in to comment.