Skip to content

Commit

Permalink
tx relayer num retries as option (#1983)
Browse files Browse the repository at this point in the history
  • Loading branch information
igorcrevar committed Oct 12, 2023
1 parent c27d222 commit 5abd428
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions txrelayer/txrelayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
defaultGasPrice = 1879048192 // 0x70000000
DefaultGasLimit = 5242880 // 0x500000
DefaultRPCAddress = "http://127.0.0.1:8545"
numRetries = 1000
defaultNumRetries = 1000
gasLimitIncreasePercentage = 100
feeIncreasePercentage = 100
)
Expand All @@ -46,6 +46,7 @@ type TxRelayerImpl struct {
ipAddress string
client *jsonrpc.Client
receiptTimeout time.Duration
numRetries int

lock sync.Mutex

Expand All @@ -56,6 +57,7 @@ func NewTxRelayer(opts ...TxRelayerOption) (TxRelayer, error) {
t := &TxRelayerImpl{
ipAddress: DefaultRPCAddress,
receiptTimeout: 50 * time.Millisecond,
numRetries: defaultNumRetries,
}
for _, opt := range opts {
opt(t)
Expand Down Expand Up @@ -242,9 +244,12 @@ func (t *TxRelayerImpl) sendTransactionLocalLocked(txn *ethgo.Transaction) (ethg
}

func (t *TxRelayerImpl) waitForReceipt(hash ethgo.Hash) (*ethgo.Receipt, error) {
count := uint(0)
// A negative numRetries means we don't want to receive the receipt after SendTransaction/SendTransactionLocal calls
if t.numRetries < 0 {
return nil, nil
}

for {
for count := 0; count < t.numRetries; count++ {
receipt, err := t.client.Eth().GetTransactionReceipt(hash)
if err != nil {
if err.Error() != "not found" {
Expand All @@ -256,13 +261,10 @@ func (t *TxRelayerImpl) waitForReceipt(hash ethgo.Hash) (*ethgo.Receipt, error)
return receipt, nil
}

if count > numRetries {
return nil, fmt.Errorf("timeout while waiting for transaction %s to be processed", hash)
}

time.Sleep(t.receiptTimeout)
count++
}

return nil, fmt.Errorf("timeout while waiting for transaction %s to be processed", hash)
}

// ConvertTxnToCallMsg converts txn instance to call message
Expand Down Expand Up @@ -302,3 +304,12 @@ func WithWriter(writer io.Writer) TxRelayerOption {
t.writer = writer
}
}

// WithNumRetries sets the maximum number of eth_getTransactionReceipt retries
// before considering the transaction sending as timed out. Set to -1 to disable
// waitForReceipt and not wait for the transaction receipt
func WithNumRetries(numRetries int) TxRelayerOption {
return func(t *TxRelayerImpl) {
t.numRetries = numRetries
}
}

0 comments on commit 5abd428

Please sign in to comment.