From e33fd4c73651e03cfc607ff7754631b28b7513ea Mon Sep 17 00:00:00 2001 From: StefanIliev545 Date: Tue, 3 Oct 2023 12:01:08 +0300 Subject: [PATCH] Waiting for receipt instead of random wait. --- integration/common/utils.go | 24 ++++++++++++++++++++++++ integration/simulation/simulation.go | 12 +++++++----- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/integration/common/utils.go b/integration/common/utils.go index 24df954384..4d5ca021af 100644 --- a/integration/common/utils.go +++ b/integration/common/utils.go @@ -10,11 +10,13 @@ import ( "time" "github.com/obscuronet/go-obscuro/go/common/retry" + "github.com/obscuronet/go-obscuro/go/ethadapter" "github.com/obscuronet/go-obscuro/go/obsclient" "github.com/obscuronet/go-obscuro/go/wallet" + "github.com/ethereum/go-ethereum" gethcommon "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/obscuronet/go-obscuro/go/rpc" @@ -36,6 +38,28 @@ func RndBtwTime(min time.Duration, max time.Duration) time.Duration { return time.Duration(RndBtw(uint64(min.Nanoseconds()), uint64(max.Nanoseconds()))) * time.Nanosecond } +func AwaitReceiptEth(ctx context.Context, client ethadapter.EthClient, txHash gethcommon.Hash, timeout time.Duration) error { + var receipt *types.Receipt + var err error + err = retry.Do(func() error { + receipt, err = client.TransactionReceipt(txHash) + if err != nil && !errors.Is(err, rpc.ErrNilResponse) && !errors.Is(err, ethereum.NotFound) { + // we only retry for a nil "not found" response. This is a different error, so we bail out of the retry loop + return retry.FailFast(err) + } + return err + }, retry.NewTimeoutStrategy(timeout, _awaitReceiptPollingInterval)) + if err != nil { + return fmt.Errorf("could not retrieve receipt for transaction %s - %w", txHash.Hex(), err) + } + + if receipt.Status == types.ReceiptStatusFailed { + return fmt.Errorf("receipt had status failed for transaction %s", txHash.Hex()) + } + + return nil +} + // AwaitReceipt blocks until the receipt for the transaction with the given hash has been received. Errors if the // transaction is unsuccessful or times out. func AwaitReceipt(ctx context.Context, client *obsclient.AuthObsClient, txHash gethcommon.Hash, timeout time.Duration) error { diff --git a/integration/simulation/simulation.go b/integration/simulation/simulation.go index 376d20c0ea..04ece6fc6c 100644 --- a/integration/simulation/simulation.go +++ b/integration/simulation/simulation.go @@ -146,6 +146,7 @@ func (s *Simulation) bridgeFundingToObscuro() { panic(err) } + transactions := make(types.Transactions, 0) for idx, wallet := range wallets { opts, err := bind.NewKeyedTransactorWithChainID(wallet.PrivateKey(), wallet.ChainID()) if err != nil { @@ -153,27 +154,28 @@ func (s *Simulation) bridgeFundingToObscuro() { } opts.Value = value - _, err = busCtr.SendValueToL2(opts, receivers[idx], value) + tx, err := busCtr.SendValueToL2(opts, receivers[idx], value) if err != nil { panic(err) } + transactions = append(transactions, tx) } - time.Sleep(3 * time.Second) + //time.Sleep(3 * time.Second) // todo - fix the wait group, for whatever reason it does not find a receipt... - /*wg := sync.WaitGroup{} + wg := sync.WaitGroup{} for _, tx := range transactions { wg.Add(1) transaction := tx go func() { defer wg.Done() - err := testcommon.AwaitReceiptEth(s.ctx, s.RPCHandles.RndEthClient(), transaction.Hash(), 20*time.Second) + err := testcommon.AwaitReceiptEth(s.ctx, s.RPCHandles.RndEthClient(), transaction.Hash(), 2*time.Minute) if err != nil { panic(err) } }() } - wg.Wait()*/ + wg.Wait() } // We subscribe to logs on every client for every wallet.