From cc39d831d37e691d4d3061cc560c4cde32697848 Mon Sep 17 00:00:00 2001 From: CHAMI Rachid Date: Mon, 4 Dec 2023 14:09:32 +0100 Subject: [PATCH] feat: speed up transactions if they don't get included initially (#636) * feat: support transaction speed up * chore: better logs * chore: better logs * chore: not send transaction if new gas price is lower than current one --- evm/evm_client.go | 1 - relayer/errors.go | 1 + relayer/relayer.go | 57 ++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/evm/evm_client.go b/evm/evm_client.go index f5d1647b..7aba080f 100644 --- a/evm/evm_client.go +++ b/evm/evm_client.go @@ -241,7 +241,6 @@ func (ec *Client) WaitForTransaction( ec.logger.Info("transaction confirmed", "hash", tx.Hash().String(), "block", receipt.BlockNumber.Uint64()) return receipt, nil } - ec.logger.Error("transaction failed", "hash", tx.Hash().String()) return receipt, err } diff --git a/relayer/errors.go b/relayer/errors.go index 6326049d..8e2fff62 100644 --- a/relayer/errors.go +++ b/relayer/errors.go @@ -9,4 +9,5 @@ var ( ErrAttestationNotDataCommitmentRequest = errors.New("attestation is not a data commitment request") ErrAttestationNotFound = errors.New("attestation not found") ErrValidatorSetMismatch = errors.New("p2p validator set is different from the trusted contract one") + ErrTransactionStillPending = errors.New("evm transaction still pending") ) diff --git a/relayer/relayer.go b/relayer/relayer.go index a79ea0bf..db8e2937 100644 --- a/relayer/relayer.go +++ b/relayer/relayer.go @@ -4,11 +4,14 @@ import ( "bytes" "context" "encoding/hex" + stderrors "errors" "fmt" "math/big" "strconv" "time" + "github.com/ethereum/go-ethereum/ethclient" + "github.com/ipfs/go-datastore" badger "github.com/ipfs/go-ds-badger2" @@ -113,8 +116,7 @@ func (r *Relayer) Start(ctx context.Context) error { return err } - // wait for transaction to be mined - _, err = r.EVMClient.WaitForTransaction(ctx, ethClient, tx, r.RetryTimeout) + err = r.waitForTransactionAndRetryIfNeeded(ctx, ethClient, tx) if err != nil { return err } @@ -373,6 +375,57 @@ func (r *Relayer) SaveDataCommitmentSignaturesToStore(ctx context.Context, att c return batch.Commit(ctx) } +// waitForTransactionAndRetryIfNeeded waits for transaction to be mined. If it's not mined in the provided timeout, it will +// attempt to speed it up via updating the gas price. +func (r *Relayer) waitForTransactionAndRetryIfNeeded(ctx context.Context, ethClient *ethclient.Client, tx *coregethtypes.Transaction) error { + r.logger.Debug("submitted transaction", "hash", tx.Hash().Hex(), "gas_price", tx.GasPrice().Uint64()) + newTx := tx + for i := 0; i < 10; i++ { + _, err := r.EVMClient.WaitForTransaction(ctx, ethClient, newTx, r.RetryTimeout) + if err != nil { + if stderrors.Is(err, context.DeadlineExceeded) { + newGasPrice, err := ethClient.SuggestGasPrice(ctx) + if err != nil { + return err + } + if newGasPrice.Uint64() <= newTx.GasPrice().Uint64() { + // no need to resend the transaction if the suggested gas price is lower than the original one + continue + } + legacyTx := toLegacyTransaction(newTx) + legacyTx.GasPrice = newGasPrice + newTx = coregethtypes.NewTx(legacyTx) + r.logger.Debug("transaction still not included. updating the gas price", "retry_number", i) + err = ethClient.SendTransaction(ctx, newTx) + r.logger.Info("submitted speed up transaction", "hash", newTx.Hash().Hex(), "new_gas_price", newTx.GasPrice().Uint64()) + if err != nil { + r.logger.Debug("response of sending speed up transaction", "resp", err.Error()) + } + } else { + return err + } + } else { + return nil + } + } + return ErrTransactionStillPending +} + +func toLegacyTransaction(tx *coregethtypes.Transaction) *coregethtypes.LegacyTx { + v, r, s := tx.RawSignatureValues() + return &coregethtypes.LegacyTx{ + Nonce: tx.Nonce(), + GasPrice: tx.GasPrice(), + Gas: tx.Gas(), + To: tx.To(), + Value: tx.Value(), + Data: tx.Data(), + V: v, + R: r, + S: s, + } +} + // matchAttestationConfirmSigs matches and sorts the confirm signatures with the valset // members as expected by the Blobstream contract. // Also, it leaves the non provided signatures as nil in the `sigs` slice: