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

Commit

Permalink
Merge branch 'main' into backup-realyer
Browse files Browse the repository at this point in the history
  • Loading branch information
rach-id authored Dec 4, 2023
2 parents 89cd730 + cc39d83 commit e935809
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
1 change: 0 additions & 1 deletion evm/evm_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
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")
)
57 changes: 55 additions & 2 deletions relayer/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -131,8 +134,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 @@ -397,6 +399,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:
Expand Down

0 comments on commit e935809

Please sign in to comment.