Skip to content

Commit 10597b0

Browse files
committed
feat: retry function with bumping fee
1 parent 90c4290 commit 10597b0

File tree

2 files changed

+47
-34
lines changed

2 files changed

+47
-34
lines changed

core/chainio/avs_writer.go

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/ethereum/go-ethereum/common"
1616
"github.com/ethereum/go-ethereum/core/types"
1717
servicemanager "github.com/yetanotherco/aligned_layer/contracts/bindings/AlignedLayerServiceManager"
18-
connection "github.com/yetanotherco/aligned_layer/core"
1918
"github.com/yetanotherco/aligned_layer/core/config"
2019
"github.com/yetanotherco/aligned_layer/core/utils"
2120
)
@@ -94,49 +93,25 @@ func (w *AvsWriter) SendAggregatedResponse(batchIdentifierHash [32]byte, batchMe
9493
txNonce := new(big.Int).SetUint64(tx.Nonce())
9594
txOpts.NoSend = false
9695
txOpts.Nonce = txNonce
97-
i := 0
98-
sendTransaction := func() (*types.Receipt, error) {
99-
// bump the fee here
100-
gasPrice := utils.CalculateGasPriceBumpBasedOnRetry(tx.GasPrice(), i)
96+
97+
beforeTransaction := func(gasPrice *big.Int) error {
10198
txOpts.GasPrice = gasPrice
102-
w.logger.Infof("Sending ResponseToTask transaction for %vth with a gas price of %v", i, txOpts.GasPrice)
103-
i++
99+
w.logger.Infof("Sending ResponseToTask transaction with a gas price of %v", txOpts.GasPrice)
104100
err = w.checkRespondToTaskFeeLimit(tx, txOpts, batchIdentifierHash, senderAddress)
105-
if err != nil {
106-
return nil, err
107-
}
101+
return err
102+
}
108103

104+
executeTransaction := func(gasPrice *big.Int) (*types.Transaction, error) {
109105
tx, err = w.AvsContractBindings.ServiceManager.RespondToTaskV2(&txOpts, batchMerkleRoot, senderAddress, nonSignerStakesAndSignature)
110106
if err != nil {
111107
// Retry with fallback
112108
tx, err = w.AvsContractBindings.ServiceManagerFallback.RespondToTaskV2(&txOpts, batchMerkleRoot, senderAddress, nonSignerStakesAndSignature)
113-
if err != nil {
114-
return nil, err
115-
}
109+
return tx, err
116110
}
117-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*36)
118-
defer cancel()
119-
receipt, err := utils.WaitForTransactionReceipt(w.Client, ctx, tx.Hash())
120-
121-
if receipt != nil {
122-
return receipt, nil
123-
}
124-
125-
// if we are here, this means we have reached the timeout (after three blocks it hasn't been included)
126-
// so we try again by bumping the fee to make sure its included
127-
if err != nil {
128-
return nil, err
129-
}
130-
return nil, fmt.Errorf("transaction failed")
111+
return tx, err
131112
}
132-
receipt, err := connection.RetryWithData(sendTransaction, 1000, 2, 3)
133113

134-
if receipt.Status == 0 {
135-
return receipt, fmt.Errorf("transaction failed")
136-
} else {
137-
// transaction was included in block
138-
return receipt, nil
139-
}
114+
return utils.SendTransactionWithInfiniteRetryAndBumpingGasPrice(beforeTransaction, executeTransaction, w.Client, tx.GasPrice())
140115
}
141116

142117
func (w *AvsWriter) checkRespondToTaskFeeLimit(tx *types.Transaction, txOpts bind.TransactOpts, batchIdentifierHash [32]byte, senderAddress [20]byte) error {

core/utils/eth_client_utils.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
eigentypes "github.com/Layr-Labs/eigensdk-go/types"
1212
gethcommon "github.com/ethereum/go-ethereum/common"
1313
"github.com/ethereum/go-ethereum/core/types"
14+
connection "github.com/yetanotherco/aligned_layer/core"
1415
)
1516

1617
const maxRetries = 25
@@ -57,3 +58,40 @@ func CalculateGasPriceBumpBasedOnRetry(currentGasPrice *big.Int, iteration int)
5758

5859
return gasPrice
5960
}
61+
62+
// Sends a transaction and waits for the receipt for three blocks, if not received
63+
// it will try again bumping the gas price based on `CalculateGasPriceBumpBasedOnRetry`.
64+
// This process happens indefinitely until we get the receipt or the receipt status is an err
65+
func SendTransactionWithInfiniteRetryAndBumpingGasPrice(beforeTransaction func(*big.Int) error, executeTransaction func(*big.Int) (*types.Transaction, error), client eth.InstrumentedClient, baseGasPrice *big.Int) (*types.Receipt, error) {
66+
i := 0
67+
sendTransaction := func() (*types.Receipt, error) {
68+
i++
69+
gasPrice := CalculateGasPriceBumpBasedOnRetry(baseGasPrice, i)
70+
71+
err := beforeTransaction(gasPrice)
72+
if err != nil {
73+
return nil, err
74+
}
75+
76+
tx, err := executeTransaction(gasPrice)
77+
if err != nil {
78+
return nil, err
79+
}
80+
81+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*36)
82+
defer cancel()
83+
receipt, err := WaitForTransactionReceipt(client, ctx, tx.Hash())
84+
85+
if receipt != nil {
86+
return receipt, nil
87+
}
88+
// if we are here, this means we have reached the timeout (after three blocks it hasn't been included)
89+
// so we try again by bumping the fee to make sure its included
90+
if err != nil {
91+
return nil, err
92+
}
93+
return nil, fmt.Errorf("transaction failed")
94+
95+
}
96+
return connection.RetryWithData(sendTransaction, 1000, 2, 0)
97+
}

0 commit comments

Comments
 (0)