From 2f3d09ae4e17fb088584fba0999e1178f0e6afb9 Mon Sep 17 00:00:00 2001 From: Roberto Bayardo Date: Sun, 4 Jun 2023 16:40:44 -0700 Subject: [PATCH] make MinSuggestedOptimismPriorityFee configurable via gasprice.Config --- eth/gasprice/gasprice.go | 19 ++++++++++++++++++- eth/gasprice/optimism-gasprice.go | 8 +------- eth/gasprice/optimism-gasprice_test.go | 13 +++++++------ 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/eth/gasprice/gasprice.go b/eth/gasprice/gasprice.go index 44612f124..ff8b87c48 100644 --- a/eth/gasprice/gasprice.go +++ b/eth/gasprice/gasprice.go @@ -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 { @@ -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. @@ -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 @@ -128,7 +134,7 @@ func NewOracle(backend OracleBackend, params Config) *Oracle { } }() - return &Oracle{ + r := &Oracle{ backend: backend, lastPrice: params.Default, maxPrice: maxPrice, @@ -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 diff --git a/eth/gasprice/optimism-gasprice.go b/eth/gasprice/optimism-gasprice.go index 6f75b1807..71cd021f6 100644 --- a/eth/gasprice/optimism-gasprice.go +++ b/eth/gasprice/optimism-gasprice.go @@ -8,15 +8,9 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/log" - "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rpc" ) -var ( - // TODO: What's the recommended way to make this a configurable parameter? - MinSuggestedOptimismPriorityFee = big.NewInt(1e8 * params.Wei) // 0.1 gwei -) - // 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 @@ -42,7 +36,7 @@ var ( // 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(MinSuggestedOptimismPriorityFee) + 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 diff --git a/eth/gasprice/optimism-gasprice_test.go b/eth/gasprice/optimism-gasprice_test.go index 0b79e3896..dd0140839 100644 --- a/eth/gasprice/optimism-gasprice_test.go +++ b/eth/gasprice/optimism-gasprice_test.go @@ -62,7 +62,7 @@ func (b *opTestBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) } func (b *opTestBackend) ChainConfig() *params.ChainConfig { - return params.TestChainConfig + return params.OptimismTestConfig } func (b *opTestBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { @@ -74,8 +74,8 @@ func newOpTestBackend(t *testing.T, txs []testTxData) *opTestBackend { key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") signer = types.LatestSigner(params.TestChainConfig) ) - // for optimism price suggestion only the most recent block is considered so this is where - // we add the test transactions + // 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{} @@ -105,6 +105,7 @@ func newOpTestBackend(t *testing.T, txs []testTxData) *opTestBackend { } func TestSuggestOptimismPriorityFee(t *testing.T) { + minSuggestion := new(big.Int).SetUint64(1e8 * params.Wei) var cases = []struct { txdata []testTxData want *big.Int @@ -112,12 +113,12 @@ func TestSuggestOptimismPriorityFee(t *testing.T) { { // block well under capacity, expect min priority fee suggestion txdata: []testTxData{testTxData{params.GWei, 21000}}, - want: MinSuggestedOptimismPriorityFee, + want: minSuggestion, }, { // 2 txs, still under capacity, expect min priority fee suggestion txdata: []testTxData{testTxData{params.GWei, 21000}, testTxData{params.GWei, 21000}}, - want: MinSuggestedOptimismPriorityFee, + want: minSuggestion, }, { // 2 txs w same priority fee (1 gwei), but second tx puts it right over capacity @@ -132,7 +133,7 @@ func TestSuggestOptimismPriorityFee(t *testing.T) { } for i, c := range cases { backend := newOpTestBackend(t, c.txdata) - oracle := NewOracle(backend, Config{}) + 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)