From 3ce932fcb81924ff2de483b982b3588d4d35cbd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Negovanovi=C4=87?= Date: Thu, 19 Dec 2024 08:53:41 +0100 Subject: [PATCH] add extract rpc error data helper function --- bridgesync/e2e_test.go | 1 - test/helpers/ethtxmanmock_e2e.go | 17 ++++------------- test/helpers/simulated.go | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/bridgesync/e2e_test.go b/bridgesync/e2e_test.go index be63c980..61e45674 100644 --- a/bridgesync/e2e_test.go +++ b/bridgesync/e2e_test.go @@ -47,7 +47,6 @@ func TestBridgeEventE2E(t *testing.T) { bridge.OriginAddress, true, nil, ) - t.Logf("BridgeAsset err: %+v", err) require.NoError(t, err) helpers.CommitBlocks(t, env.L1Client, 1, blockTime) bn, err := env.L1Client.Client().BlockNumber(ctx) diff --git a/test/helpers/ethtxmanmock_e2e.go b/test/helpers/ethtxmanmock_e2e.go index 8952e72d..1a42ea17 100644 --- a/test/helpers/ethtxmanmock_e2e.go +++ b/test/helpers/ethtxmanmock_e2e.go @@ -56,11 +56,11 @@ func NewEthTxManMock( _, err := client.Client().EstimateGas(ctx, msg) if err != nil { - log.Errorf("eth_estimateGas invocation failed: %+v", err) + log.Errorf("eth_estimateGas invocation failed: %w", ExtractRPCErrorData(err)) res, err := client.Client().CallContract(ctx, msg, nil) if err != nil { - log.Errorf("eth_call invocation failed: %+v", err) + log.Errorf("eth_call invocation failed: %w", ExtractRPCErrorData(err)) } else { log.Debugf("contract call result: %s", hex.EncodeToString(res)) } @@ -69,11 +69,9 @@ func NewEthTxManMock( err = SendTx(ctx, client, auth, to, data, common.Big0) if err != nil { - log.Errorf("failed to send transaction: %s", err) + log.Errorf("failed to send transaction: %w", err) return } - - client.Commit() }). Return(common.Hash{}, nil) ethTxMock.On("Result", mock.Anything, mock.Anything). @@ -102,7 +100,7 @@ func SendTx(ctx context.Context, client *simulated.Backend, auth *bind.TransactO gas, err = client.Client().EstimateGas(ctx, msg) if err != nil { - return err + return ExtractRPCErrorData(err) } } @@ -142,12 +140,5 @@ func SendTx(ctx context.Context, client *simulated.Backend, auth *bind.TransactO client.Commit() - receipt, err := client.Client().TransactionReceipt(ctx, signedTx.Hash()) - if err != nil { - return fmt.Errorf("transaction failed: %w", err) - } - - fmt.Printf("Transaction status: %d\n", receipt.Status) - return nil } diff --git a/test/helpers/simulated.go b/test/helpers/simulated.go index e9f0a555..4c283bf3 100644 --- a/test/helpers/simulated.go +++ b/test/helpers/simulated.go @@ -2,6 +2,7 @@ package helpers import ( "context" + "errors" "fmt" "math/big" "testing" @@ -157,6 +158,7 @@ func NewSimulatedBackend(t *testing.T, return client, setup } +// CreateAccount creates new private key and corresponding transaction signer func CreateAccount(chainID *big.Int) (*bind.TransactOpts, error) { privateKey, err := crypto.GenerateKey() if err != nil { @@ -165,3 +167,15 @@ func CreateAccount(chainID *big.Int) (*bind.TransactOpts, error) { return bind.NewKeyedTransactorWithChainID(privateKey, chainID) } + +// ExtractRPCErrorData tries to extract the error data from the provided error +func ExtractRPCErrorData(err error) error { + var ed rpc.DataError + if errors.As(err, &ed) { + if eds, ok := ed.ErrorData().(string); ok { + return fmt.Errorf("%w (error data: %s)", err, eds) + } + } + + return err +}