Skip to content

Commit

Permalink
tests code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dessaya committed Oct 22, 2024
1 parent c3562a9 commit b4ee16a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 51 deletions.
21 changes: 21 additions & 0 deletions packages/evm/jsonrpc/jsonrpctest/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package jsonrpctest
import (
"context"
"crypto/ecdsa"
"encoding/json"
"errors"
"math"
"math/big"
Expand All @@ -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"
Expand Down Expand Up @@ -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))
Expand Down
64 changes: 13 additions & 51 deletions packages/evm/jsonrpc/jsonrpctest/jsonrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit b4ee16a

Please sign in to comment.