Skip to content

Commit

Permalink
Merge branch '311-aggregator-wait-for-receipt-for-1-minute-if-not-bum…
Browse files Browse the repository at this point in the history
…p-the-fee-v2' into test-aggregator-bump-fee
  • Loading branch information
MarcosNicolau committed Oct 21, 2024
2 parents 37c5471 + ecaf600 commit 4a8a9f5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
23 changes: 13 additions & 10 deletions core/chainio/avs_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
servicemanager "github.com/yetanotherco/aligned_layer/contracts/bindings/AlignedLayerServiceManager"
connection "github.com/yetanotherco/aligned_layer/core"
"github.com/yetanotherco/aligned_layer/core/config"
"github.com/yetanotherco/aligned_layer/core/utils"
)
Expand Down Expand Up @@ -95,26 +96,28 @@ func (w *AvsWriter) SendAggregatedResponse(batchIdentifierHash [32]byte, batchMe
txOpts.Nonce = txNonce
var i uint64 = 1

beforeTransaction := func(gasPrice *big.Int) error {
txOpts.GasPrice = gasPrice
executeTransaction := func(bumpedGasPrices *big.Int) (*types.Transaction, error) {
txOpts.GasPrice = bumpedGasPrices
w.logger.Infof("Sending ResponseToTask transaction with a gas price of %v", txOpts.GasPrice)
err = w.checkRespondToTaskFeeLimit(tx, txOpts, batchIdentifierHash, senderAddress)
return err
}

executeTransaction := func(gasPrice *big.Int) (*types.Transaction, error) {
if err != nil {
return nil, connection.PermanentError{Inner: err}
}

tx, err = w.AvsContractBindings.ServiceManager.RespondToTaskV2(&txOpts, batchMerkleRoot, senderAddress, nonSignerStakesAndSignature, new(big.Int).SetUint64(i))
if err != nil {
// Retry with fallback
tx, err = w.AvsContractBindings.ServiceManagerFallback.RespondToTaskV2(&txOpts, batchMerkleRoot, senderAddress, nonSignerStakesAndSignature, new(big.Int).SetUint64(i))
i++
return tx, err
if err != nil {
return nil, connection.PermanentError{Inner: err}
}
return tx, nil
}
i++
return tx, err
return tx, nil
}

return utils.SendTransactionWithInfiniteRetryAndBumpingGasPrice(beforeTransaction, executeTransaction, w.Client, tx.GasPrice())
return utils.SendTransactionWithInfiniteRetryAndBumpingGasPrice(executeTransaction, w.Client, tx.GasPrice())
}

func (w *AvsWriter) checkRespondToTaskFeeLimit(tx *types.Transaction, txOpts bind.TransactOpts, batchIdentifierHash [32]byte, senderAddress [20]byte) error {
Expand Down
8 changes: 7 additions & 1 deletion core/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ func Retry(functionToRetry func() error, minDelay uint64, factor float64, maxTri
initialRetryOption := backoff.WithInitialInterval(time.Millisecond * time.Duration(minDelay))
multiplierOption := backoff.WithMultiplier(factor)
expBackoff := backoff.NewExponentialBackOff(randomOption, multiplierOption, initialRetryOption)
maxRetriesBackoff := backoff.WithMaxRetries(expBackoff, maxTries)
var maxRetriesBackoff backoff.BackOff

if maxTries > 0 {
maxRetriesBackoff = backoff.WithMaxRetries(expBackoff, maxTries)
} else {
maxRetriesBackoff = expBackoff
}

return backoff.Retry(f, maxRetriesBackoff)
}
9 changes: 2 additions & 7 deletions core/utils/eth_client_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,14 @@ func CalculateGasPriceBumpBasedOnRetry(currentGasPrice *big.Int, iteration int)

// Sends a transaction and waits for the receipt for three blocks, if not received
// it will try again bumping the gas price based on `CalculateGasPriceBumpBasedOnRetry`
// and pass it to beforeTransaction and executeTransaction (make sure you update the txOpts with the new price)
// and pass it to executeTransaction (make sure you update the txOpts with the new gasPrice)
// This process happens indefinitely until we get the receipt or the receipt status is an err.
func SendTransactionWithInfiniteRetryAndBumpingGasPrice(beforeTransaction func(*big.Int) error, executeTransaction func(*big.Int) (*types.Transaction, error), client eth.InstrumentedClient, baseGasPrice *big.Int) (*types.Receipt, error) {
func SendTransactionWithInfiniteRetryAndBumpingGasPrice(executeTransaction func(*big.Int) (*types.Transaction, error), client eth.InstrumentedClient, baseGasPrice *big.Int) (*types.Receipt, error) {
i := 0
sendTransaction := func() (*types.Receipt, error) {
i++
gasPrice := CalculateGasPriceBumpBasedOnRetry(baseGasPrice, i)

err := beforeTransaction(gasPrice)
if err != nil {
return nil, err
}

tx, err := executeTransaction(gasPrice)
if err != nil {
return nil, err
Expand Down

0 comments on commit 4a8a9f5

Please sign in to comment.