Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,12 @@ func (b *EthAPIBackend) SyncProgress() ethereum.SyncProgress {
return b.eth.Downloader().Progress()
}

func (b *EthAPIBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
return b.gpo.SuggestTipCap(ctx)
func (b *EthAPIBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, *big.Int, error) {
suggestion, err := b.gpo.SuggestTipCap(ctx)
if err != nil {
return nil, nil, err
}
return suggestion, b.gpo.MaxTransactionPrice(), nil
}

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) {
Expand Down
8 changes: 8 additions & 0 deletions eth/gasprice/gasprice.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ func (oracle *Oracle) SuggestTipCap(ctx context.Context) (*big.Int, error) {
return new(big.Int).Set(price), nil
}

// MaxTransactionPrice returns the user-configured maximum transaction price
// they can afford. Before the london it's only applied for the transaction
// tip but after the london the sum of tip and baseFee should be applied with
// this limitation.
func (oracle *Oracle) MaxTransactionPrice() *big.Int {
return oracle.maxPrice
}

type results struct {
values []*big.Int
err error
Expand Down
2 changes: 1 addition & 1 deletion ethclient/ethclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ func testStatusFunctions(t *testing.T, client *rpc.Client) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if gasPrice.Cmp(big.NewInt(1875000000)) != 0 { // 1 gwei tip + 0.875 basefee after a 1 gwei fee empty block
if gasPrice.Cmp(big.NewInt(1765625000)) != 0 { // 1 gwei tip + 0.765625 basefee after two 1 gwei fee empty blocks(genesis, block1)
Comment thread
rjl493456442 marked this conversation as resolved.
Outdated
t.Fatalf("unexpected gas price: %v", gasPrice)
}
// SuggestGasTipCap (should suggest 1 Gwei)
Expand Down
7 changes: 5 additions & 2 deletions ethstats/ethstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type fullNodeBackend interface {
Miner() *miner.Miner
BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error)
CurrentBlock() *types.Block
SuggestGasTipCap(ctx context.Context) (*big.Int, error)
SuggestGasTipCap(ctx context.Context) (*big.Int, *big.Int, error)
}

// Service implements an Ethereum netstats reporting daemon that pushes local
Expand Down Expand Up @@ -780,9 +780,12 @@ func (s *Service) reportStats(conn *connWrapper) error {
sync := fullBackend.SyncProgress()
syncing = fullBackend.CurrentHeader().Number.Uint64() >= sync.HighestBlock

price, _ := fullBackend.SuggestGasTipCap(context.Background())
price, _, _ := fullBackend.SuggestGasTipCap(context.Background())
gasprice = int(price.Uint64())
if basefee := fullBackend.CurrentHeader().BaseFee; basefee != nil {
// In theory, the baseFee of pending block should be applied,
// but the accuracy requirement in ethstat is not that high. It's
// fine to use the head block basefee instead here.
gasprice += int(basefee.Uint64())
}
} else {
Expand Down
14 changes: 10 additions & 4 deletions graphql/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/consensus/misc"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/filters"
Expand Down Expand Up @@ -1191,18 +1192,23 @@ func (r *Resolver) Logs(ctx context.Context, args struct{ Filter FilterCriteria
}

func (r *Resolver) GasPrice(ctx context.Context) (hexutil.Big, error) {
tipcap, err := r.backend.SuggestGasTipCap(ctx)
tipcap, maxPrice, err := r.backend.SuggestGasTipCap(ctx)
if err != nil {
return hexutil.Big{}, err
}
if head := r.backend.CurrentHeader(); head.BaseFee != nil {
tipcap.Add(tipcap, head.BaseFee)
head := r.backend.CurrentHeader()
if r.backend.ChainConfig().IsLondon(new(big.Int).Add(head.Number, common.Big1)) {
baseFee := misc.CalcBaseFee(r.backend.ChainConfig(), head)
tipcap.Add(tipcap, baseFee)
if tipcap.Cmp(maxPrice) > 0 {
tipcap = maxPrice
}
}
return (hexutil.Big)(*tipcap), nil
}

func (r *Resolver) MaxPriorityFeePerGas(ctx context.Context) (hexutil.Big, error) {
tipcap, err := r.backend.SuggestGasTipCap(ctx)
tipcap, _, err := r.backend.SuggestGasTipCap(ctx)
if err != nil {
return hexutil.Big{}, err
}
Expand Down
13 changes: 9 additions & 4 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,24 @@ func NewPublicEthereumAPI(b Backend) *PublicEthereumAPI {

// GasPrice returns a suggestion for a gas price for legacy transactions.
func (s *PublicEthereumAPI) GasPrice(ctx context.Context) (*hexutil.Big, error) {
tipcap, err := s.b.SuggestGasTipCap(ctx)
tipcap, maxPrice, err := s.b.SuggestGasTipCap(ctx)
if err != nil {
return nil, err
}
if head := s.b.CurrentHeader(); head.BaseFee != nil {
tipcap.Add(tipcap, head.BaseFee)
head := s.b.CurrentHeader()
if s.b.ChainConfig().IsLondon(new(big.Int).Add(head.Number, common.Big1)) {
baseFee := misc.CalcBaseFee(s.b.ChainConfig(), head)
tipcap.Add(tipcap, baseFee)
if tipcap.Cmp(maxPrice) > 0 {
tipcap = maxPrice
}
}
return (*hexutil.Big)(tipcap), err
}

// MaxPriorityFeePerGas returns a suggestion for a gas tip cap for dynamic fee transactions.
func (s *PublicEthereumAPI) MaxPriorityFeePerGas(ctx context.Context) (*hexutil.Big, error) {
tipcap, err := s.b.SuggestGasTipCap(ctx)
tipcap, _, err := s.b.SuggestGasTipCap(ctx)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions internal/ethapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ import (
type Backend interface {
// General Ethereum API
SyncProgress() ethereum.SyncProgress

SuggestGasTipCap(ctx context.Context) (*big.Int, error)
SuggestGasTipCap(ctx context.Context) (*big.Int, *big.Int, error)
FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error)
ChainDb() ethdb.Database
AccountManager() *accounts.Manager
Expand Down
21 changes: 17 additions & 4 deletions internal/ethapi/transaction_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/consensus/misc"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rpc"
Expand Down Expand Up @@ -86,10 +87,18 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error {
// In this clause, user left some fields unspecified.
if b.ChainConfig().IsLondon(head.Number) && args.GasPrice == nil {
if args.MaxPriorityFeePerGas == nil {
tip, err := b.SuggestGasTipCap(ctx)
tip, maxprice, err := b.SuggestGasTipCap(ctx)
if err != nil {
return err
}
paid := new(big.Int).Add(tip, head.BaseFee)
if paid.Cmp(maxprice) > 0 {
if maxprice.Cmp(head.BaseFee) > 0 {
tip = new(big.Int).Sub(maxprice, head.BaseFee)
} else {
tip = big.NewInt(0)
}
}
args.MaxPriorityFeePerGas = (*hexutil.Big)(tip)
}
if args.MaxFeePerGas == nil {
Expand All @@ -107,15 +116,19 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error {
return errors.New("maxFeePerGas or maxPriorityFeePerGas specified but london is not active yet")
}
if args.GasPrice == nil {
price, err := b.SuggestGasTipCap(ctx)
price, maxPrice, err := b.SuggestGasTipCap(ctx)
if err != nil {
return err
}
if b.ChainConfig().IsLondon(head.Number) {
if b.ChainConfig().IsLondon(new(big.Int).Add(head.Number, common.Big1)) {
// The legacy tx gas price suggestion should not add 2x base fee
// because all fees are consumed, so it would result in a spiral
// upwards.
price.Add(price, head.BaseFee)
baseFee := misc.CalcBaseFee(b.ChainConfig(), head)
price.Add(price, baseFee)
if price.Cmp(maxPrice) > 0 {
price = maxPrice
}
}
args.GasPrice = (*hexutil.Big)(price)
}
Expand Down
8 changes: 6 additions & 2 deletions les/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,12 @@ func (b *LesApiBackend) ProtocolVersion() int {
return b.eth.LesVersion() + 10000
}

func (b *LesApiBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
return b.gpo.SuggestTipCap(ctx)
func (b *LesApiBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, *big.Int, error) {
suggestion, err := b.gpo.SuggestTipCap(ctx)
if err != nil {
return nil, nil, err
}
return suggestion, b.gpo.MaxTransactionPrice(), nil
}

func (b *LesApiBackend) FeeHistory(ctx context.Context, blockCount int, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (firstBlock *big.Int, reward [][]*big.Int, baseFee []*big.Int, gasUsedRatio []float64, err error) {
Expand Down