From b4ee16a8416a9d47c02a139131230f160d9b8dc4 Mon Sep 17 00:00:00 2001 From: Diego Essaya Date: Tue, 22 Oct 2024 14:44:15 -0300 Subject: [PATCH] tests code cleanup --- packages/evm/jsonrpc/jsonrpctest/env.go | 21 ++++++ .../evm/jsonrpc/jsonrpctest/jsonrpc_test.go | 64 ++++--------------- 2 files changed, 34 insertions(+), 51 deletions(-) diff --git a/packages/evm/jsonrpc/jsonrpctest/env.go b/packages/evm/jsonrpc/jsonrpctest/env.go index ca4a97e4a7..0d24c26124 100644 --- a/packages/evm/jsonrpc/jsonrpctest/env.go +++ b/packages/evm/jsonrpc/jsonrpctest/env.go @@ -6,6 +6,7 @@ package jsonrpctest import ( "context" "crypto/ecdsa" + "encoding/json" "errors" "math" "math/big" @@ -18,6 +19,7 @@ import ( "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/eth/tracers" "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/rpc" "github.com/stretchr/testify/require" @@ -288,6 +290,25 @@ func (e *Env) getLogs(q ethereum.FilterQuery) []types.Log { return logs } +func (e *Env) traceTransactionWithCallTracer(txHash common.Hash) (jsonrpc.CallFrame, error) { + var res json.RawMessage + // we have to use the raw client, because the normal client does not support debug methods + err := e.RawClient.CallContext( + context.Background(), + &res, + "debug_traceTransaction", + txHash, + tracers.TraceConfig{TracerConfig: []byte(`{"tracer": "callTracer"}`)}, + ) + if err != nil { + return jsonrpc.CallFrame{}, err + } + trace := jsonrpc.CallFrame{} + err = json.Unmarshal(res, &trace) + require.NoError(e.T, err) + return trace, nil +} + func (e *Env) TestRPCGetLogs() { creator, creatorAddress := e.NewAccountWithL2Funds() contractABI, err := abi.JSON(strings.NewReader(evmtest.ERC20ContractABI)) diff --git a/packages/evm/jsonrpc/jsonrpctest/jsonrpc_test.go b/packages/evm/jsonrpc/jsonrpctest/jsonrpc_test.go index afa7190ec7..c8b61d6324 100644 --- a/packages/evm/jsonrpc/jsonrpctest/jsonrpc_test.go +++ b/packages/evm/jsonrpc/jsonrpctest/jsonrpc_test.go @@ -557,32 +557,10 @@ func TestRPCTraceTx(t *testing.T) { bi := env.soloChain.GetLatestBlockInfo() require.EqualValues(t, 2, bi.NumSuccessfulRequests) - // assert each tx only has internal txs that belong to their execution - var res1 json.RawMessage - // we have to use the raw client, because the normal client does not support debug methods - err = env.RawClient.CallContext( - context.Background(), - &res1, - "debug_traceTransaction", - tx1.Hash().Hex(), - tracers.TraceConfig{TracerConfig: []byte(`{"tracer": "callTracer"}`)}, - ) - require.NoError(t, err) - - // assert each tx only has internal txs that belong to their execution - var res2 json.RawMessage - // we have to use the raw client, because the normal client does not support debug methods - err = env.RawClient.CallContext( - context.Background(), - &res2, - "debug_traceTransaction", - tx2.Hash().Hex(), - tracers.TraceConfig{TracerConfig: []byte(`{"tracer": "callTracer"}`)}, - ) + // assert each tx can be individually traced + trace1, err := env.traceTransactionWithCallTracer(tx1.Hash()) require.NoError(t, err) - - trace1 := jsonrpc.CallFrame{} - err = json.Unmarshal(res1, &trace1) + _, err = env.traceTransactionWithCallTracer(tx2.Hash()) require.NoError(t, err) require.Equal(t, creatorAddress, trace1.From) @@ -610,7 +588,7 @@ func TestRPCTraceTx(t *testing.T) { // Transfer calls produce "fake" Transactions to simulate EVM behavior. // They are not real in the sense of being persisted to the blockchain, therefore requires additional checks. -func TestRPCTraceEvmDeposit(t *testing.T) { +func TestRPCTraceEVMDeposit(t *testing.T) { env := newSoloTestEnv(t) wallet, _ := env.solo.NewKeyPairWithFunds() _, evmAddr := env.soloChain.NewEthereumAccountWithL2Funds() @@ -631,24 +609,13 @@ func TestRPCTraceEvmDeposit(t *testing.T) { require.NoError(t, err) require.EqualValues(t, types.ReceiptStatusSuccessful, rc.Status) - var res1 json.RawMessage - err = env.RawClient.CallContext( - context.Background(), - &res1, - "debug_traceTransaction", - tx.Hash().Hex(), - tracers.TraceConfig{TracerConfig: []byte(`{"tracer": "callTracer"}`)}, - ) - require.NoError(t, err) - - var trace1 jsonrpc.SendTxArgs - err = json.Unmarshal(res1, &trace1) + trace, err := env.traceTransactionWithCallTracer(tx.Hash()) require.NoError(t, err) fmt.Print(hexutil.EncodeUint64(isc.NewAssetsBaseTokens(1000).BaseTokens)) - require.Equal(t, evmAddr.String(), trace1.To.String()) - require.Equal(t, hexutil.EncodeUint64(isc.NewAssetsBaseTokens(1000).BaseTokens*1e12), trace1.Value.String()) + require.Equal(t, evmAddr.String(), trace.To.String()) + require.Equal(t, hexutil.EncodeUint64(isc.NewAssetsBaseTokens(1000).BaseTokens*1e12), trace.Value.String()) } func TestRPCTraceBlock(t *testing.T) { @@ -898,14 +865,10 @@ func TestRPCBlockReceipt(t *testing.T) { bi := env.soloChain.GetLatestBlockInfo() require.EqualValues(t, 2, bi.NumSuccessfulRequests) - var receipts []*types.Receipt - err = env.RawClient.CallContext( + receipts, err := env.Client.BlockReceipts( context.Background(), - &receipts, - "eth_getBlockReceipts", - hexutil.EncodeUint64(env.BlockNumber())) - require.NoError(t, err) - + rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(env.BlockNumber())), + ) require.Len(t, receipts, 2) r1 := receipts[slices.IndexFunc(receipts, func(v *types.Receipt) bool { @@ -923,11 +886,10 @@ func TestRPCBlockReceipt(t *testing.T) { // Test the same block with its hash. block := env.BlockByNumber(new(big.Int).SetUint64(env.BlockNumber())) - err = env.RawClient.CallContext( + receipts, err = env.Client.BlockReceipts( context.Background(), - &receipts, - "eth_getBlockReceipts", - block.Hash().String()) + rpc.BlockNumberOrHashWithHash(block.Hash(), false), + ) require.NoError(t, err) require.Len(t, receipts, 2)