@@ -11,11 +11,13 @@ import (
1111 "github.com/Layr-Labs/eigensdk-go/chainio/clients/eth"
1212 "github.com/Layr-Labs/eigensdk-go/logging"
1313 "github.com/Layr-Labs/eigensdk-go/signer"
14+ "github.com/ethereum/go-ethereum"
1415 "github.com/ethereum/go-ethereum/accounts/abi/bind"
1516 "github.com/ethereum/go-ethereum/common"
1617 "github.com/ethereum/go-ethereum/core/types"
1718 servicemanager "github.com/yetanotherco/aligned_layer/contracts/bindings/AlignedLayerServiceManager"
1819 "github.com/yetanotherco/aligned_layer/core/config"
20+ "github.com/yetanotherco/aligned_layer/core/utils"
1921)
2022
2123type AvsWriter struct {
@@ -70,7 +72,7 @@ func NewAvsWriterFromConfig(baseConfig *config.BaseConfig, ecdsaConfig *config.E
7072 }, nil
7173}
7274
73- func (w * AvsWriter ) SendAggregatedResponse (batchIdentifierHash [32 ]byte , batchMerkleRoot [32 ]byte , senderAddress [20 ]byte , nonSignerStakesAndSignature servicemanager.IBLSSignatureCheckerNonSignerStakesAndSignature ) (* common. Hash , error ) {
75+ func (w * AvsWriter ) SendAggregatedResponse (batchIdentifierHash [32 ]byte , batchMerkleRoot [32 ]byte , senderAddress [20 ]byte , nonSignerStakesAndSignature servicemanager.IBLSSignatureCheckerNonSignerStakesAndSignature ) (* types. Receipt , error ) {
7476 txOpts := * w .Signer .GetTxOpts ()
7577 txOpts .NoSend = true // simulate the transaction
7678 tx , err := w .AvsContractBindings .ServiceManager .RespondToTaskV2 (& txOpts , batchMerkleRoot , senderAddress , nonSignerStakesAndSignature )
@@ -87,21 +89,55 @@ func (w *AvsWriter) SendAggregatedResponse(batchIdentifierHash [32]byte, batchMe
8789 return nil , err
8890 }
8991
90- // Send the transaction
92+ // Set the nonce, as we might have to replace the transaction with a higher fee
93+ txNonce := new (big.Int ).SetUint64 (tx .Nonce ())
9194 txOpts .NoSend = false
92- txOpts .GasLimit = tx .Gas () * 110 / 100 // Add 10% to the gas limit
93- tx , err = w .AvsContractBindings .ServiceManager .RespondToTaskV2 (& txOpts , batchMerkleRoot , senderAddress , nonSignerStakesAndSignature )
94- if err != nil {
95- // Retry with fallback
96- tx , err = w .AvsContractBindings .ServiceManagerFallback .RespondToTaskV2 (& txOpts , batchMerkleRoot , senderAddress , nonSignerStakesAndSignature )
95+ txOpts .Nonce = txNonce
96+
97+ // Send the transaction
98+ var maxRetries uint64 = 5
99+ var i uint64
100+ for i = 1 ; i < maxRetries ; i ++ {
101+ // Add x% to the gas limit, where x 10 <= x <= 50
102+ txOpts .GasLimit = tx .Gas () * (100 + i * 10 ) / 100
103+ w .logger .Debugf ("Sending ResponseToTask transaction for %vth with a gas limit of %v" , i , txOpts .GasLimit )
104+ err = w .checkRespondToTaskFeeLimit (tx , txOpts , batchIdentifierHash , senderAddress )
97105 if err != nil {
98106 return nil , err
99107 }
100- }
101108
102- txHash := tx .Hash ()
109+ tx , err = w .AvsContractBindings .ServiceManager .RespondToTaskV2 (& txOpts , batchMerkleRoot , senderAddress , nonSignerStakesAndSignature )
110+ if err != nil {
111+ // Retry with fallback
112+ tx , err = w .AvsContractBindings .ServiceManagerFallback .RespondToTaskV2 (& txOpts , batchMerkleRoot , senderAddress , nonSignerStakesAndSignature )
113+ if err != nil {
114+ return nil , err
115+ }
116+ }
117+ ctx , _ := context .WithTimeout (context .Background (), time .Second * 20 )
118+ receipt , err := utils .WaitForTransactionReceipt (w .Client , ctx , tx .Hash ())
119+
120+ if receipt != nil {
121+ if receipt .Status == 0 {
122+ return receipt , fmt .Errorf ("transaction failed" )
123+ } else {
124+ // transaction was included in block
125+ w .logger .Debugf ("Sending ResponseToTask transaction for %vth with a gas limit of %v" , i , txOpts .GasLimit )
126+ return receipt , nil
127+ }
128+ }
129+
130+ // transaction not included in block, try again
131+ if err == ethereum .NotFound {
132+ w .logger .Debugf ("Transaction not included in block will try again" )
133+ continue
134+ } else {
135+ return receipt , err
136+ }
137+
138+ }
103139
104- return & txHash , nil
140+ return nil , fmt . Errorf ( "could not send transaction" )
105141}
106142
107143func (w * AvsWriter ) checkRespondToTaskFeeLimit (tx * types.Transaction , txOpts bind.TransactOpts , batchIdentifierHash [32 ]byte , senderAddress [20 ]byte ) error {
0 commit comments