Skip to content

Commit 90c4290

Browse files
committed
feat: SendAggregatedResponse with infinite retry
1 parent 2627659 commit 90c4290

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

core/chainio/avs_writer.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ 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"
1819
"github.com/yetanotherco/aligned_layer/core/config"
1920
"github.com/yetanotherco/aligned_layer/core/utils"
2021
)
@@ -93,19 +94,13 @@ func (w *AvsWriter) SendAggregatedResponse(batchIdentifierHash [32]byte, batchMe
9394
txNonce := new(big.Int).SetUint64(tx.Nonce())
9495
txOpts.NoSend = false
9596
txOpts.Nonce = txNonce
96-
97-
// Send the transaction
98-
var maxRetries uint64 = 5
99-
var i uint64
100-
for i = 1; i < maxRetries; i++ {
97+
i := 0
98+
sendTransaction := func() (*types.Receipt, error) {
10199
// bump the fee here
102-
// factor = (100 + i * 10) / 100, so 1,1 <= x <= 1,5
103-
factor := (new(big.Int).Add(big.NewInt(100), new(big.Int).Mul(big.NewInt(int64(i)), big.NewInt(10))))
104-
gasPrice := new(big.Int).Mul(tx.GasPrice(), factor)
105-
gasPrice = gasPrice.Div(gasPrice, big.NewInt(100))
100+
gasPrice := utils.CalculateGasPriceBumpBasedOnRetry(tx.GasPrice(), i)
106101
txOpts.GasPrice = gasPrice
107-
108102
w.logger.Infof("Sending ResponseToTask transaction for %vth with a gas price of %v", i, txOpts.GasPrice)
103+
i++
109104
err = w.checkRespondToTaskFeeLimit(tx, txOpts, batchIdentifierHash, senderAddress)
110105
if err != nil {
111106
return nil, err
@@ -124,23 +119,24 @@ func (w *AvsWriter) SendAggregatedResponse(batchIdentifierHash [32]byte, batchMe
124119
receipt, err := utils.WaitForTransactionReceipt(w.Client, ctx, tx.Hash())
125120

126121
if receipt != nil {
127-
if receipt.Status == 0 {
128-
return receipt, fmt.Errorf("transaction failed")
129-
} else {
130-
// transaction was included in block
131-
return receipt, nil
132-
}
122+
return receipt, nil
133123
}
134124

135-
// this means we have reached the timeout (after three blocks it hasn't been included)
125+
// if we are here, this means we have reached the timeout (after three blocks it hasn't been included)
136126
// so we try again by bumping the fee to make sure its included
137127
if err != nil {
138-
continue
128+
return nil, err
139129
}
140-
130+
return nil, fmt.Errorf("transaction failed")
141131
}
132+
receipt, err := connection.RetryWithData(sendTransaction, 1000, 2, 3)
142133

143-
return nil, fmt.Errorf("could not send transaction")
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+
}
144140
}
145141

146142
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package utils
22

33
import (
44
"context"
5+
"math/big"
56
"time"
67

78
"fmt"
@@ -46,3 +47,13 @@ func BytesToQuorumThresholdPercentages(quorumThresholdPercentagesBytes []byte) e
4647
}
4748
return quorumThresholdPercentages
4849
}
50+
51+
// Very basic algorithm to calculate the gasPrice bump based on the currentGasPrice and retry iteration.
52+
// It adds a i/10 percentage to the current prices, where i represents the iteration.
53+
func CalculateGasPriceBumpBasedOnRetry(currentGasPrice *big.Int, iteration int) *big.Int {
54+
factor := (new(big.Int).Add(big.NewInt(100), new(big.Int).Mul(big.NewInt(int64(iteration)), big.NewInt(10))))
55+
gasPrice := new(big.Int).Mul(currentGasPrice, factor)
56+
gasPrice = gasPrice.Div(gasPrice, big.NewInt(100))
57+
58+
return gasPrice
59+
}

0 commit comments

Comments
 (0)