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: don't bump transactions without sufficient gas #2664

Merged
merged 6 commits into from
Jun 8, 2024
Merged
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
41 changes: 38 additions & 3 deletions ethergo/submitter/chain_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,18 @@ func (t *txSubmitterImpl) chainPendingQueue(parentCtx context.Context, chainID *
client: chainClient,
}

sufficientGas, err := cq.hasSufficientGas(gCtx)
if err != nil {
return fmt.Errorf("could not check gas balance: %w", err)
}
span.SetAttributes(attribute.Bool("sufficient_gas", sufficientGas))

// so now, we have a list of transactions that need to be resubmitted.
// these are already ordered by nonce and we should have at most one per nonce, so we can just iterate through them.
// we need to figure out which ones are still valid, and which ones need to be bumped.
// once this is done, we'll be ready to submit them
// we're going to handle this by updating txes in place
// once this is done, we'll be ready to submit them.
// we're going to handle this by updating txes in place.
// note that if we do not have sufficient gas on this chain, no txes will be bumped.
for i := range txes {
tx := txes[i]

Expand All @@ -94,7 +101,9 @@ func (t *txSubmitterImpl) chainPendingQueue(parentCtx context.Context, chainID *
continue
}

cq.bumpTX(gCtx, tx)
if sufficientGas {
cq.bumpTX(gCtx, tx)
}
}
cq.updateOldTxStatuses(gCtx)

Expand Down Expand Up @@ -337,3 +346,29 @@ func (c *chainQueue) updateOldTxStatuses(parentCtx context.Context) {
return nil
})
}

func (c *chainQueue) hasSufficientGas(parentCtx context.Context) (sufficient bool, err error) {
ctx, span := c.metrics.Tracer().Start(parentCtx, "hasSufficientGas")
defer func() {
metrics.EndSpanWithErr(span, err)
}()

// assume that if we don't have a min gas balance, we have enough gas.
minGasBalance := c.config.GetMinGasBalance(c.chainIDInt())
if minGasBalance == nil || minGasBalance.Cmp(big.NewInt(0)) == 0 {
return true, nil
}

gasBalance, err := c.client.BalanceAt(ctx, c.signer.Address(), nil)
if err != nil {
return false, fmt.Errorf("could not get gas balance: %w", err)
}
sufficient = gasBalance.Cmp(minGasBalance) >= 0

span.SetAttributes(
attribute.String("min_gas_balance", minGasBalance.String()),
attribute.String("gas_balance", gasBalance.String()),
attribute.Bool("sufficient", sufficient),
)
return sufficient, nil
}
11 changes: 11 additions & 0 deletions ethergo/submitter/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type ChainConfig struct {
DynamicGasEstimate bool `yaml:"dynamic_gas_estimate"`
// SupportsEIP1559 is whether or not this chain supports EIP1559
SupportsEIP1559 bool `yaml:"supports_eip_1559"`
// MinGasBalance is the minimum gas balance needed to bump a transaction
MinGasBalance *big.Int `yaml:"min_gas_balance"`
}

const (
Expand Down Expand Up @@ -200,6 +202,15 @@ func (c *Config) SupportsEIP1559(chainID int) bool {
return c.ChainConfig.SupportsEIP1559
}

// GetMinGasBalance returns the minimum gas balance needed to bump a transaction.
func (c *Config) GetMinGasBalance(chainID int) *big.Int {
chainConfig, ok := c.Chains[chainID]
if ok {
return chainConfig.MinGasBalance
}
return c.MinGasBalance
}

// SetGlobalMaxGasPrice is a helper function that sets the global gas price.
func (c *Config) SetGlobalMaxGasPrice(maxPrice *big.Int) {
c.MaxGasPrice = maxPrice
Expand Down
2 changes: 2 additions & 0 deletions ethergo/submitter/config/iconfig_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading