Skip to content
This repository has been archived by the owner on Apr 15, 2024. It is now read-only.

Commit

Permalink
feat: support transaction speed up
Browse files Browse the repository at this point in the history
  • Loading branch information
rach-id committed Dec 1, 2023
1 parent 95e2ae1 commit eb72458
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
6 changes: 2 additions & 4 deletions evm/evm_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package evm
import (
"context"
"errors"
"math/big"
"time"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"math/big"
"time"

gethcommon "github.com/ethereum/go-ethereum/common"
coregethtypes "github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -241,7 +240,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
}
Expand Down
1 change: 1 addition & 0 deletions relayer/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
52 changes: 50 additions & 2 deletions relayer/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"bytes"
"context"
"encoding/hex"
stderrors "errors"
"fmt"
"github.com/ethereum/go-ethereum/ethclient"
"math/big"
"strconv"
"time"
Expand Down Expand Up @@ -113,8 +115,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
}
Expand Down Expand Up @@ -373,6 +374,53 @@ 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 {
for i := 0; i < 10; i++ {
_, err := r.EVMClient.WaitForTransaction(ctx, ethClient, tx, r.RetryTimeout)
if err != nil {
if stderrors.Is(err, context.DeadlineExceeded) {
r.logger.Debug("transaction still not included. updating the gas price", "retry_number", i, "err", err.Error())
newGasPrice, err := ethClient.SuggestGasPrice(ctx)
if err != nil {
return err
}
newTx := toLegacyTransaction(tx)
newTx.GasPrice = newGasPrice.Mul(newGasPrice, big.NewInt(2))
newTx2 := coregethtypes.NewTx(newTx)
err = ethClient.SendTransaction(ctx, newTx2)
if err != nil {
_, err := ethClient.TransactionReceipt(ctx, tx.Hash())
if err == nil {
return nil
}
}
} 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:
Expand Down

0 comments on commit eb72458

Please sign in to comment.