Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submitter: only bump gas price for transactions with current nonce #2417

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
11 changes: 9 additions & 2 deletions ethergo/chain/gas/cmp.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package gas

import (
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/core/types"
"github.com/synapsecns/sanguine/core"
"math/big"
)

// CompareGas allows for gas comparisons between txes. In the case of an eip-1559 txOpts and a
Expand Down Expand Up @@ -92,7 +93,13 @@ func bumpDynamicTxFees(opts *bind.TransactOpts, percentIncrease int, baseFee, ma
logger.Warnf("new tip cap %s still less than fee cap %s + base fee %s, bumping tip not and not fee", newTipCap, maxPrice, baseFee)
}

opts.GasFeeCap = maxPrice
newFeeCap := BumpByPercent(opts.GasFeeCap, percentIncrease)
if maxPrice.Cmp(newFeeCap) > 0 {
opts.GasFeeCap = newFeeCap
} else {
opts.GasFeeCap = maxPrice
logger.Warnf("new fee cap %s exceeds max price %s, using max price", newFeeCap, maxPrice)
}
}

// BumpByPercent bumps a gas price by a percentage.
Expand Down
15 changes: 12 additions & 3 deletions ethergo/submitter/chain_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@
continue
}

cq.bumpTX(gCtx, tx)
// should only bump the gas price if we are at current nonce
if tx.Nonce() == currentNonce {
cq.bumpTX(gCtx, tx, true)
} else {
cq.bumpTX(gCtx, tx, false)
}

Check warning on line 92 in ethergo/submitter/chain_queue.go

View check run for this annotation

Codecov / codecov/patch

ethergo/submitter/chain_queue.go#L91-L92

Added lines #L91 - L92 were not covered by tests
}
cq.updateOldTxStatuses(gCtx)

Expand Down Expand Up @@ -144,7 +149,7 @@
}

// nolint: cyclop
func (c *chainQueue) bumpTX(parentCtx context.Context, ogTx db.TX) {
func (c *chainQueue) bumpTX(parentCtx context.Context, ogTx db.TX, bumpGasPrice bool) {
c.g.Go(func() (err error) {
if !c.isBumpIntervalElapsed(ogTx) {
c.addToReprocessQueue(ogTx)
Expand All @@ -171,7 +176,11 @@
transactor.Nonce = new(big.Int).SetUint64(tx.Nonce())
transactor.GasLimit = newGasEstimate

err = c.setGasPrice(ctx, c.client, transactor, c.chainID, ogTx.Transaction)
prevTx := ogTx.Transaction
if !bumpGasPrice {
prevTx = nil
}
err = c.setGasPrice(ctx, c.client, transactor, c.chainID, prevTx)

Check warning on line 183 in ethergo/submitter/chain_queue.go

View check run for this annotation

Codecov / codecov/patch

ethergo/submitter/chain_queue.go#L179-L183

Added lines #L179 - L183 were not covered by tests
if err != nil {
return fmt.Errorf("could not set gas price: %w", err)
}
Expand Down
3 changes: 2 additions & 1 deletion ethergo/submitter/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ const (
DefaultBumpIntervalSeconds = 30

// DefaultGasBumpPercentage is the default percentage to bump the gas price by.
DefaultGasBumpPercentage = 5
// See: https://github.com/ethereum/go-ethereum/blob/8c5576b1ac89473c7ec15c9b03d1ca02e9499dcc/core/txpool/legacypool/legacypool.go#L146
DefaultGasBumpPercentage = 10

// DefaultGasEstimate is the default gas estimate to use for transactions.
DefaultGasEstimate = uint64(1200000)
Expand Down
19 changes: 17 additions & 2 deletions ethergo/submitter/submitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,27 @@ func (t *txSubmitterImpl) setGasPrice(ctx context.Context, client client.EVM,
transactor.GasFeeCap = maxPrice
}

span.SetAttributes(
attribute.String("gas_price", bigPtrToString(transactor.GasPrice)),
attribute.String("gas_fee_cap", bigPtrToString(transactor.GasFeeCap)),
attribute.String("gas_tip_cap", bigPtrToString(transactor.GasTipCap)),
)
metrics.EndSpanWithErr(span, err)
}()

// TODO: cache both of these values
shouldBump := true
if t.config.SupportsEIP1559(int(bigChainID.Uint64())) {
transactor.GasFeeCap = t.config.GetMaxGasPrice(chainID)
transactor.GasFeeCap, err = client.SuggestGasPrice(ctx)
if err != nil {
return fmt.Errorf("could not get gas price: %w", err)
}
// don't bump fee cap if we hit the max configured gas price
if transactor.GasFeeCap.Cmp(maxPrice) > 0 {
transactor.GasFeeCap = maxPrice
shouldBump = false
span.AddEvent("not bumping fee cap since max price is reached")
}

transactor.GasTipCap, err = client.SuggestGasTipCap(ctx)
if err != nil {
Expand All @@ -378,7 +393,7 @@ func (t *txSubmitterImpl) setGasPrice(ctx context.Context, client client.EVM,
t.applyBaseGasPrice(transactor, chainID)

//nolint: nestif
if prevTx != nil {
if prevTx != nil && shouldBump {
gasBlock, err := t.getGasBlock(ctx, client, chainID)
if err != nil {
span.AddEvent("could not get gas block", trace.WithAttributes(attribute.String("error", err.Error())))
Expand Down
2 changes: 1 addition & 1 deletion ethergo/submitter/submitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (s *SubmitterSuite) TestSetGasPrice() {
maxPrice := new(big.Int).Add(gasPrice, new(big.Int).SetUint64(1))
cfg.SetGlobalMaxGasPrice(maxPrice)

client.On(testsuite.GetFunctionName(client.SuggestGasPrice), mock.Anything).Twice().Return(gasPrice, nil)
client.On(testsuite.GetFunctionName(client.SuggestGasPrice), mock.Anything).Times(3).Return(gasPrice, nil)
err = ts.SetGasPrice(s.GetTestContext(), client, transactor, chainID, nil)
s.Require().NoError(err)

Expand Down
Loading