Skip to content

Commit

Permalink
add extract rpc error data helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Dec 19, 2024
1 parent 95e93ca commit 3ce932f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
1 change: 0 additions & 1 deletion bridgesync/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 4 additions & 13 deletions test/helpers/ethtxmanmock_e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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).
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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
}
14 changes: 14 additions & 0 deletions test/helpers/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helpers

import (
"context"
"errors"
"fmt"
"math/big"
"testing"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit 3ce932f

Please sign in to comment.