Skip to content

Commit

Permalink
Sync geth nits (#585)
Browse files Browse the repository at this point in the history
* Change feehistory input type from int to uint64

* Add err handling for UnpackLog with zero topics in log
  • Loading branch information
aaronbuchwald authored Mar 20, 2023
1 parent a2d9bd7 commit 737e9c4
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 18 deletions.
15 changes: 13 additions & 2 deletions accounts/abi/bind/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
14 changes: 7 additions & 7 deletions eth/gasprice/feehistory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions eth/gasprice/feehistory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions eth/gasprice/gasprice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -126,7 +126,7 @@ type Oracle struct {

checkBlocks, percentile int
maxLookbackSeconds uint64
maxCallBlockHistory int
maxCallBlockHistory uint64
maxBlockHistory int
historyCache *lru.Cache
feeInfoProvider *feeInfoProvider
Expand Down
4 changes: 2 additions & 2 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/ethapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 737e9c4

Please sign in to comment.