Skip to content

Commit

Permalink
Merge pull request bnb-chain#77 from roberto-bayardo/simplify-gas-est…
Browse files Browse the repository at this point in the history
…imate

implement more accurate & predicable priority fee suggestion algorithm for chains like Optimism
  • Loading branch information
ajsutton authored Jun 5, 2023
2 parents 3cb51a2 + 2f3d09a commit a22cadc
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 1 deletion.
24 changes: 23 additions & 1 deletion eth/gasprice/gasprice.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const sampleNumber = 3 // Number of transactions sampled in a block
var (
DefaultMaxPrice = big.NewInt(500 * params.GWei)
DefaultIgnorePrice = big.NewInt(2 * params.Wei)

DefaultMinSuggestedPriorityFee = big.NewInt(1e8 * params.Wei) // 0.1 gwei, for Optimism fee suggestion
)

type Config struct {
Expand All @@ -47,6 +49,8 @@ type Config struct {
Default *big.Int `toml:",omitempty"`
MaxPrice *big.Int `toml:",omitempty"`
IgnorePrice *big.Int `toml:",omitempty"`

MinSuggestedPriorityFee *big.Int `toml:",omitempty"` // for Optimism fee suggestion
}

// OracleBackend includes all necessary background APIs for oracle.
Expand Down Expand Up @@ -74,6 +78,8 @@ type Oracle struct {
maxHeaderHistory, maxBlockHistory uint64

historyCache *lru.Cache[cacheKey, processedFees]

minSuggestedPriorityFee *big.Int // for Optimism fee suggestion
}

// NewOracle returns a new gasprice oracle which can recommend suitable
Expand Down Expand Up @@ -128,7 +134,7 @@ func NewOracle(backend OracleBackend, params Config) *Oracle {
}
}()

return &Oracle{
r := &Oracle{
backend: backend,
lastPrice: params.Default,
maxPrice: maxPrice,
Expand All @@ -139,6 +145,17 @@ func NewOracle(backend OracleBackend, params Config) *Oracle {
maxBlockHistory: maxBlockHistory,
historyCache: cache,
}

if backend.ChainConfig().IsOptimism() {
r.minSuggestedPriorityFee = params.MinSuggestedPriorityFee
if r.minSuggestedPriorityFee == nil || r.minSuggestedPriorityFee.Int64() <= 0 {
r.minSuggestedPriorityFee = DefaultMinSuggestedPriorityFee
log.Warn("Sanitizing invalid optimism gasprice oracle min priority fee suggestion",
"provided", params.MinSuggestedPriorityFee,
"updated", r.minSuggestedPriorityFee)
}
}
return r
}

// SuggestTipCap returns a tip cap so that newly created transaction can have a
Expand Down Expand Up @@ -168,6 +185,11 @@ func (oracle *Oracle) SuggestTipCap(ctx context.Context) (*big.Int, error) {
if headHash == lastHead {
return new(big.Int).Set(lastPrice), nil
}

if oracle.backend.ChainConfig().IsOptimism() {
return oracle.SuggestOptimismPriorityFee(ctx, head, headHash), nil
}

var (
sent, exp int
number = head.Number.Uint64()
Expand Down
110 changes: 110 additions & 0 deletions eth/gasprice/optimism-gasprice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package gasprice

import (
"context"
"math/big"
"sort"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rpc"
)

// SuggestOptimismPriorityFee returns a max priority fee value that can be used such that newly
// created transactions have a very high chance to be included in the following blocks, using a
// simplified and more predictable algorithm appropriate for chains like Optimism with a single
// known block builder.
//
// In the typical case, which results whenever the last block had room for more transactions, this
// function returns a minimum suggested priority fee value. Otherwise it returns the higher of this
// minimum suggestion or 10% over the median effective priority fee from the last block.
//
// Rationale: For a chain such as Optimism where there is a single block builder whose behavior is
// known, we know priority fee (as long as it is non-zero) has no impact on the probability for tx
// inclusion as long as there is capacity for it in the block. In this case then, there's no reason
// to return any value higher than some fixed minimum. Blocks typically reach capacity only under
// extreme events such as airdrops, meaning predicting whether the next block is going to be at
// capacity is difficult *except* in the case where we're already experiencing the increased demand
// from such an event. We therefore expect whether the last known block is at capacity to be one of
// the best predictors of whether the next block is likely to be at capacity. (An even better
// predictor is to look at the state of the transaction pool, but we want an algorithm that works
// even if the txpool is private or unavailable.)
//
// In the event the next block may be at capacity, the algorithm should allow for average fees to
// rise in order to reach a market price that appropriately reflects demand. We accomplish this by
// returning a suggestion that is a significant amount (10%) higher than the median effective
// priority fee from the previous block.
func (oracle *Oracle) SuggestOptimismPriorityFee(ctx context.Context, h *types.Header, headHash common.Hash) *big.Int {
suggestion := new(big.Int).Set(oracle.minSuggestedPriorityFee)

// find the maximum gas used by any of the transactions in the block to use as the capacity
// margin
receipts, err := oracle.backend.GetReceipts(ctx, headHash)
if receipts == nil || err != nil {
log.Error("failed to get block receipts", "err", err)
return suggestion
}
var maxTxGasUsed uint64
for i := range receipts {
gu := receipts[i].GasUsed
if gu > maxTxGasUsed {
maxTxGasUsed = gu
}
}

// sanity check the max gas used value
if maxTxGasUsed > h.GasLimit {
log.Error("found tx consuming more gas than the block limit", "gas", maxTxGasUsed)
return suggestion
}

if h.GasUsed+maxTxGasUsed > h.GasLimit {
// A block is "at capacity" if, when it is built, there is a pending tx in the txpool that
// could not be included because the block's gas limit would be exceeded. Since we don't
// have access to the txpool, we instead adopt the following heuristic: consider a block as
// at capacity if the total gas consumed by its transactions is within max-tx-gas-used of
// the block limit, where max-tx-gas-used is the most gas used by any one transaction
// within the block. This heuristic is almost perfectly accurate when transactions always
// consume the same amount of gas, but becomes less accurate as tx gas consumption begins
// to vary. The typical error is we assume a block is at capacity when it was not because
// max-tx-gas-used will in most cases over-estimate the "capacity margin". But it's better
// to err on the side of returning a higher-than-needed suggestion than a lower-than-needed
// one in order to satisfy our desire for high chance of inclusion and rising fees under
// high demand.
block, err := oracle.backend.BlockByNumber(ctx, rpc.BlockNumber(h.Number.Int64()))
if block == nil || err != nil {
log.Error("failed to get last block", "err", err)
return suggestion
}
baseFee := block.BaseFee()
txs := block.Transactions()
if len(txs) == 0 {
log.Error("block was at capacity but doesn't have transactions")
return suggestion
}
tips := bigIntArray(make([]*big.Int, len(txs)))
for i := range txs {
tips[i] = txs[i].EffectiveGasTipValue(baseFee)
}
sort.Sort(tips)
median := tips[len(tips)/2]
newSuggestion := new(big.Int).Add(median, new(big.Int).Div(median, big.NewInt(10)))
// use the new suggestion only if it's bigger than the minimum
if newSuggestion.Cmp(suggestion) > 0 {
suggestion = newSuggestion
}
}

// the suggestion should be capped by oracle.maxPrice
if suggestion.Cmp(oracle.maxPrice) > 0 {
suggestion.Set(oracle.maxPrice)
}

oracle.cacheLock.Lock()
oracle.lastHead = headHash
oracle.lastPrice = suggestion
oracle.cacheLock.Unlock()

return new(big.Int).Set(suggestion)
}
142 changes: 142 additions & 0 deletions eth/gasprice/optimism-gasprice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright 2020 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package gasprice

import (
"context"
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/trie"
)

const (
blockGasLimit = params.TxGas * 3
)

type testTxData struct {
priorityFee int64
gasLimit uint64
}

type opTestBackend struct {
block *types.Block
receipts []*types.Receipt
}

func (b *opTestBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
panic("not implemented")
}

func (b *opTestBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
return b.block, nil
}

func (b *opTestBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
return b.receipts, nil
}

func (b *opTestBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) {
panic("not implemented")
}

func (b *opTestBackend) ChainConfig() *params.ChainConfig {
return params.OptimismTestConfig
}

func (b *opTestBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
return nil
}

func newOpTestBackend(t *testing.T, txs []testTxData) *opTestBackend {
var (
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
signer = types.LatestSigner(params.TestChainConfig)
)
// only the most recent block is considered for optimism priority fee suggestions, so this is
// where we add the test transactions
ts := []*types.Transaction{}
rs := []*types.Receipt{}
header := types.Header{}
header.GasLimit = blockGasLimit
var nonce uint64
for _, tx := range txs {
txdata := &types.DynamicFeeTx{
ChainID: params.TestChainConfig.ChainID,
Nonce: nonce,
To: &common.Address{},
Gas: params.TxGas,
GasFeeCap: big.NewInt(100 * params.GWei),
GasTipCap: big.NewInt(tx.priorityFee),
Data: []byte{},
}
t := types.MustSignNewTx(key, signer, txdata)
ts = append(ts, t)
r := types.Receipt{}
r.GasUsed = tx.gasLimit
header.GasUsed += r.GasUsed
rs = append(rs, &r)
nonce++
}
hasher := trie.NewStackTrie(nil)
b := types.NewBlock(&header, ts, nil, nil, hasher)
return &opTestBackend{block: b, receipts: rs}
}

func TestSuggestOptimismPriorityFee(t *testing.T) {
minSuggestion := new(big.Int).SetUint64(1e8 * params.Wei)
var cases = []struct {
txdata []testTxData
want *big.Int
}{
{
// block well under capacity, expect min priority fee suggestion
txdata: []testTxData{testTxData{params.GWei, 21000}},
want: minSuggestion,
},
{
// 2 txs, still under capacity, expect min priority fee suggestion
txdata: []testTxData{testTxData{params.GWei, 21000}, testTxData{params.GWei, 21000}},
want: minSuggestion,
},
{
// 2 txs w same priority fee (1 gwei), but second tx puts it right over capacity
txdata: []testTxData{testTxData{params.GWei, 21000}, testTxData{params.GWei, 21001}},
want: big.NewInt(1100000000), // 10 percent over 1 gwei, the median
},
{
// 3 txs, full block. return 10% over the median tx (10 gwei * 10% == 11 gwei)
txdata: []testTxData{testTxData{10 * params.GWei, 21000}, testTxData{1 * params.GWei, 21000}, testTxData{100 * params.GWei, 21000}},
want: big.NewInt(11 * params.GWei),
},
}
for i, c := range cases {
backend := newOpTestBackend(t, c.txdata)
oracle := NewOracle(backend, Config{MinSuggestedPriorityFee: minSuggestion})
got := oracle.SuggestOptimismPriorityFee(context.Background(), backend.block.Header(), backend.block.Hash())
if got.Cmp(c.want) != 0 {
t.Errorf("Gas price mismatch for test case %d: want %d, got %d", i, c.want, got)
}
}
}

0 comments on commit a22cadc

Please sign in to comment.