From 737e9c42cb5f02ffead0a4fd4a7970d8c3eb3e68 Mon Sep 17 00:00:00 2001 From: aaronbuchwald Date: Mon, 20 Mar 2023 17:03:28 -0400 Subject: [PATCH] Sync geth nits (#585) * Change feehistory input type from int to uint64 * Add err handling for UnpackLog with zero topics in log --- accounts/abi/bind/base.go | 15 +++++++++++++-- eth/api_backend.go | 2 +- eth/gasprice/feehistory.go | 14 +++++++------- eth/gasprice/feehistory_test.go | 4 ++-- eth/gasprice/gasprice.go | 6 +++--- internal/ethapi/api.go | 4 ++-- internal/ethapi/backend.go | 2 +- 7 files changed, 29 insertions(+), 18 deletions(-) diff --git a/accounts/abi/bind/base.go b/accounts/abi/bind/base.go index b94990fe39..536e01de64 100644 --- a/accounts/abi/bind/base.go +++ b/accounts/abi/bind/base.go @@ -42,6 +42,11 @@ import ( "github.com/ethereum/go-ethereum/event" ) +var ( + errNoEventSignature = errors.New("no event signature") + errEventSignatureMismatch = errors.New("event signature mismatch") +) + // SignerFn is a signer function callback when a contract requires a method to // sign the transaction before submission. type SignerFn func(common.Address, *types.Transaction) (*types.Transaction, error) @@ -494,8 +499,11 @@ func (c *BoundContract) WatchLogs(opts *WatchOpts, name string, query ...[]inter // UnpackLog unpacks a retrieved log into the provided output structure. func (c *BoundContract) UnpackLog(out interface{}, event string, log types.Log) error { + if len(log.Topics) == 0 { + return errNoEventSignature + } if log.Topics[0] != c.abi.Events[event].ID { - return fmt.Errorf("event signature mismatch") + return errEventSignatureMismatch } if len(log.Data) > 0 { if err := c.abi.UnpackIntoInterface(out, event, log.Data); err != nil { @@ -513,8 +521,11 @@ func (c *BoundContract) UnpackLog(out interface{}, event string, log types.Log) // UnpackLogIntoMap unpacks a retrieved log into the provided map. func (c *BoundContract) UnpackLogIntoMap(out map[string]interface{}, event string, log types.Log) error { + if len(log.Topics) == 0 { + return errNoEventSignature + } if log.Topics[0] != c.abi.Events[event].ID { - return fmt.Errorf("event signature mismatch") + return errEventSignatureMismatch } if len(log.Data) > 0 { if err := c.abi.UnpackIntoMap(out, event, log.Data); err != nil { diff --git a/eth/api_backend.go b/eth/api_backend.go index 14aeda94af..fa1b43acc5 100644 --- a/eth/api_backend.go +++ b/eth/api_backend.go @@ -375,7 +375,7 @@ func (b *EthAPIBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) return b.gpo.SuggestTipCap(ctx) } -func (b *EthAPIBackend) FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (firstBlock *big.Int, reward [][]*big.Int, baseFee []*big.Int, gasUsedRatio []float64, err error) { +func (b *EthAPIBackend) FeeHistory(ctx context.Context, blockCount uint64, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (firstBlock *big.Int, reward [][]*big.Int, baseFee []*big.Int, gasUsedRatio []float64, err error) { return b.gpo.FeeHistory(ctx, blockCount, lastBlock, rewardPercentiles) } diff --git a/eth/gasprice/feehistory.go b/eth/gasprice/feehistory.go index ab8a847a4d..7c681a1c55 100644 --- a/eth/gasprice/feehistory.go +++ b/eth/gasprice/feehistory.go @@ -124,7 +124,7 @@ func (sb *slimBlock) processPercentiles(percentiles []float64) ([]*big.Int, *big // enforcing backend specific limitations. // Note: an error is only returned if retrieving the head header has failed. If there are no // retrievable blocks in the specified range then zero block count is returned with no error. -func (oracle *Oracle) resolveBlockRange(ctx context.Context, lastBlock rpc.BlockNumber, blocks int) (uint64, int, error) { +func (oracle *Oracle) resolveBlockRange(ctx context.Context, lastBlock rpc.BlockNumber, blocks uint64) (uint64, uint64, error) { // Query either pending block or head header and set headBlock if lastBlock == rpc.PendingBlockNumber { // Pending block not supported by backend, process until latest block @@ -149,12 +149,12 @@ func (oracle *Oracle) resolveBlockRange(ctx context.Context, lastBlock rpc.Block } // Ensure not trying to retrieve before genesis if rpc.BlockNumber(blocks) > lastBlock+1 { - blocks = int(lastBlock + 1) + blocks = uint64(lastBlock + 1) } // Truncate blocks range if extending past [oracle.maxBlockHistory] oldestQueriedIndex := lastBlock - rpc.BlockNumber(blocks) + 1 if queryDepth := lastAcceptedBlock - oldestQueriedIndex; queryDepth > maxQueryDepth { - overage := int(queryDepth - maxQueryDepth) + overage := uint64(queryDepth - maxQueryDepth) blocks -= overage } // It is not possible that [blocks] could be <= 0 after @@ -177,7 +177,7 @@ func (oracle *Oracle) resolveBlockRange(ctx context.Context, lastBlock rpc.Block // // Note: baseFee includes the next block after the newest of the returned range, because this // value can be derived from the newest block. -func (oracle *Oracle) FeeHistory(ctx context.Context, blocks int, unresolvedLastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error) { +func (oracle *Oracle) FeeHistory(ctx context.Context, blocks uint64, unresolvedLastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error) { if blocks < 1 { return common.Big0, nil, nil, nil, nil // returning with no data and no error means there are no retrievable blocks } @@ -197,7 +197,7 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks int, unresolvedLast if err != nil || blocks == 0 { return common.Big0, nil, nil, nil, err } - oldestBlock := lastBlock + 1 - uint64(blocks) + oldestBlock := lastBlock + 1 - blocks var ( reward = make([][]*big.Int, blocks) @@ -206,13 +206,13 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks int, unresolvedLast firstMissing = blocks ) - for blockNumber := oldestBlock; blockNumber < oldestBlock+uint64(blocks); blockNumber++ { + for blockNumber := oldestBlock; blockNumber < oldestBlock+blocks; blockNumber++ { // Check if the context has errored if err := ctx.Err(); err != nil { return common.Big0, nil, nil, nil, err } - i := int(blockNumber - oldestBlock) + i := blockNumber - oldestBlock var sb *slimBlock if sbRaw, ok := oracle.historyCache.Get(blockNumber); ok { sb = sbRaw.(*slimBlock) diff --git a/eth/gasprice/feehistory_test.go b/eth/gasprice/feehistory_test.go index cc075658af..a4b281112e 100644 --- a/eth/gasprice/feehistory_test.go +++ b/eth/gasprice/feehistory_test.go @@ -44,9 +44,9 @@ import ( func TestFeeHistory(t *testing.T) { var cases = []struct { pending bool - maxCallBlock int + maxCallBlock uint64 maxBlock int - count int + count uint64 last rpc.BlockNumber percent []float64 expFirst uint64 diff --git a/eth/gasprice/gasprice.go b/eth/gasprice/gasprice.go index 253778c6af..749f28436b 100644 --- a/eth/gasprice/gasprice.go +++ b/eth/gasprice/gasprice.go @@ -51,7 +51,7 @@ import ( const ( // DefaultMaxCallBlockHistory is the number of blocks that can be fetched in // a single call to eth_feeHistory. - DefaultMaxCallBlockHistory int = 2048 + DefaultMaxCallBlockHistory uint64 = 2048 // DefaultMaxBlockHistory is the number of blocks from the last accepted // block that can be fetched in eth_feeHistory. // @@ -83,7 +83,7 @@ type Config struct { MaxLookbackSeconds uint64 // MaxCallBlockHistory specifies the maximum number of blocks that can be // fetched in a single eth_feeHistory call. - MaxCallBlockHistory int + MaxCallBlockHistory uint64 // MaxBlockHistory specifies the furthest back behind the last accepted block that can // be requested by fee history. MaxBlockHistory int @@ -126,7 +126,7 @@ type Oracle struct { checkBlocks, percentile int maxLookbackSeconds uint64 - maxCallBlockHistory int + maxCallBlockHistory uint64 maxBlockHistory int historyCache *lru.Cache feeInfoProvider *feeInfoProvider diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 39ba95fa12..3083753aa9 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -102,8 +102,8 @@ type feeHistoryResult struct { } // FeeHistory returns the fee market history. -func (s *EthereumAPI) FeeHistory(ctx context.Context, blockCount rpc.DecimalOrHex, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*feeHistoryResult, error) { - oldest, reward, baseFee, gasUsed, err := s.b.FeeHistory(ctx, int(blockCount), lastBlock, rewardPercentiles) +func (s *EthereumAPI) FeeHistory(ctx context.Context, blockCount math.HexOrDecimal64, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*feeHistoryResult, error) { + oldest, reward, baseFee, gasUsed, err := s.b.FeeHistory(ctx, uint64(blockCount), lastBlock, rewardPercentiles) if err != nil { return nil, err } diff --git a/internal/ethapi/backend.go b/internal/ethapi/backend.go index ce09984381..ef82ae5a76 100644 --- a/internal/ethapi/backend.go +++ b/internal/ethapi/backend.go @@ -54,7 +54,7 @@ type Backend interface { EstimateBaseFee(ctx context.Context) (*big.Int, error) SuggestPrice(ctx context.Context) (*big.Int, error) SuggestGasTipCap(ctx context.Context) (*big.Int, error) - FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error) + FeeHistory(ctx context.Context, blockCount uint64, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error) ChainDb() ethdb.Database AccountManager() *accounts.Manager ExtRPCEnabled() bool