Skip to content

Commit

Permalink
eth, internal/ethapi, internal/web3ext: add eth_historicalGasPrice RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanschneider committed Apr 20, 2021
1 parent 5746098 commit 6acdbb4
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ func (b *EthAPIBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
return b.gpo.SuggestPrice(ctx)
}

func (b *EthAPIBackend) SuggestedPrice(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*big.Int, error) {
return b.gpo.SuggestPrice(ctx)
}

func (b *EthAPIBackend) ChainDb() ethdb.Database {
return b.eth.ChainDb()
}
Expand Down
7 changes: 6 additions & 1 deletion eth/gasprice/gasprice.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Config struct {
// OracleBackend includes all necessary background APIs for oracle.
type OracleBackend interface {
HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error)
HeaderByNumberOrHash(ctx context.Context, number rpc.BlockNumberOrHash) (*types.Header, error)
BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error)
ChainConfig() *params.ChainConfig
}
Expand Down Expand Up @@ -95,7 +96,11 @@ func NewOracle(backend OracleBackend, params Config) *Oracle {
// SuggestPrice returns a gasprice so that newly created transaction can
// have a very high chance to be included in the following blocks.
func (gpo *Oracle) SuggestPrice(ctx context.Context) (*big.Int, error) {
head, _ := gpo.backend.HeaderByNumber(ctx, rpc.LatestBlockNumber)
return gpo.SuggestedPrice(ctx, rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber))
}

func (gpo *Oracle) SuggestedPrice(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*big.Int, error) {
head, _ := gpo.backend.HeaderByNumberOrHash(ctx, blockNrOrHash)
headHash := head.Hash()

// If the latest gasprice is still available, return it.
Expand Down
6 changes: 6 additions & 0 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ func (s *PublicEthereumAPI) GasPrice(ctx context.Context) (*hexutil.Big, error)
return (*hexutil.Big)(price), err
}

// GasPrice returns a suggestion for a gas price.
func (s *PublicEthereumAPI) HistoricalGasPrice(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*hexutil.Big, error) {
price, err := s.b.SuggestedPrice(ctx, blockNrOrHash)
return (*hexutil.Big)(price), err
}

// Syncing returns false in case the node is currently not syncing with the network. It can be up to date or has not
// yet received the latest block headers from its pears. In case it is synchronizing:
// - startingBlock: block number this node started to synchronise from
Expand Down
1 change: 1 addition & 0 deletions internal/ethapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Backend interface {
// General Ethereum API
Downloader() *downloader.Downloader
SuggestPrice(ctx context.Context) (*big.Int, error)
SuggestedPrice(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*big.Int, error)
ChainDb() ethdb.Database
AccountManager() *accounts.Manager
ExtRPCEnabled() bool
Expand Down
6 changes: 6 additions & 0 deletions internal/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,12 @@ web3._extend({
params: 2,
inputFormatter: [null, web3._extend.formatters.inputBlockNumberFormatter],
}),
new web3._extend.Method({
name: 'historicalGasPrice',
call: 'eth_historicalGasPrice',
params: 1,
inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter]
}),
],
properties: [
new web3._extend.Property({
Expand Down
4 changes: 4 additions & 0 deletions les/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ func (b *LesApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
return b.gpo.SuggestPrice(ctx)
}

func (b *LesApiBackend) SuggestedPrice(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*big.Int, error) {
return nil, errors.New("not implemented")
}

func (b *LesApiBackend) ChainDb() ethdb.Database {
return b.eth.chainDb
}
Expand Down

0 comments on commit 6acdbb4

Please sign in to comment.