Skip to content

Commit

Permalink
Move TestPrestateTracerTransfer to e2e test to avoid circular import
Browse files Browse the repository at this point in the history
  • Loading branch information
Eela Nagaraj committed May 3, 2023
1 parent a79463c commit e82bdb2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 85 deletions.
36 changes: 36 additions & 0 deletions e2e_test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,42 @@ func TestCallTraceTransactionNativeTransfer(t *testing.T) {
require.JSONEq(t, expectedTraceStr, string(res_json))
}

// Moved from API tests because registering the prestateTracer (necessary after the
// go native tracer refactor) causes a circular import.
// Use the prestateTracer to trace a native CELO transfer.
func TestPrestateTransactionNativeTransfer(t *testing.T) {
ac := test.AccountConfig(1, 2)
gc, ec, err := test.BuildConfig(ac)
require.NoError(t, err)
network, shutdown, err := test.NewNetwork(ac, gc, ec)
require.NoError(t, err)
defer shutdown()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

accounts := test.Accounts(ac.DeveloperAccounts(), gc.ChainConfig())

// Send one celo from external account 0 to 1 via node 0.
tx, err := accounts[0].SendCelo(ctx, accounts[1].Address, 1, network[0])
require.NoError(t, err)

// Wait for the whole network to process the transaction.
err = network.AwaitTransactions(ctx, tx)
require.NoError(t, err)
c, err := rpc.DialContext(ctx, network[0].WSEndpoint())
require.NoError(t, err)

var result map[string]interface{}
tracerStr := "prestateTracer"
err = c.CallContext(ctx, &result, "debug_traceTransaction", tx.Hash().String(), tracers.TraceConfig{Tracer: &tracerStr})
require.NoError(t, err)

toAddrLowercase := strings.ToLower(accounts[1].Address.String())
if _, has := result[toAddrLowercase]; !has {
t.Fatalf("Expected %s in result", toAddrLowercase)
}
}

// This test verifies correct behavior in a network of size one, in the case that
// this fails we know that the problem does not lie with our network code.
func TestSingleNodeNetworkManyTxs(t *testing.T) {
Expand Down
85 changes: 0 additions & 85 deletions eth/tracers/tracers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@
package tracers

import (
"crypto/ecdsa"
"crypto/rand"
"encoding/json"
"math/big"
"testing"

"github.com/celo-org/celo-blockchain/common"
"github.com/celo-org/celo-blockchain/common/hexutil"
"github.com/celo-org/celo-blockchain/contracts/testutil"
"github.com/celo-org/celo-blockchain/core"
"github.com/celo-org/celo-blockchain/core/rawdb"
Expand All @@ -35,87 +31,6 @@ import (
"github.com/celo-org/celo-blockchain/tests"
)

// callTrace is the result of a callTracer run.
type callTrace struct {
Type string `json:"type"`
From common.Address `json:"from"`
To common.Address `json:"to"`
Input hexutil.Bytes `json:"input"`
Output hexutil.Bytes `json:"output"`
Gas *hexutil.Uint64 `json:"gas,omitempty"`
GasUsed *hexutil.Uint64 `json:"gasUsed,omitempty"`
Value *hexutil.Big `json:"value,omitempty"`
Error string `json:"error,omitempty"`
Calls []callTrace `json:"calls,omitempty"`
}

func TestPrestateTracerTransfer(t *testing.T) {
celoMock := testutil.NewCeloMock()

toAddr := "0x00000000000000000000000000000000deadbeef"
unsignedTx := types.NewTransaction(1, common.HexToAddress(toAddr), new(big.Int), 5000000, big.NewInt(1), nil, nil, nil, []byte{})

privateKeyECDSA, err := ecdsa.GenerateKey(crypto.S256(), rand.Reader)
if err != nil {
t.Fatalf("err %v", err)
}
signer := types.NewEIP155Signer(big.NewInt(1))
tx, err := types.SignTx(unsignedTx, signer, privateKeyECDSA)
if err != nil {
t.Fatalf("err %v", err)
}
origin, _ := signer.Sender(tx)
txContext := vm.TxContext{
Origin: origin,
GasPrice: big.NewInt(1),
}
context := vm.BlockContext{
CanTransfer: vmcontext.CanTransfer,
Transfer: vmcontext.TobinTransfer,
Coinbase: common.Address{},
BlockNumber: new(big.Int).SetUint64(8000000),
Time: new(big.Int).SetUint64(5),
GetRegisteredAddress: vmcontext.GetRegisteredAddress,
}
alloc := core.GenesisAlloc{}
alloc[origin] = core.GenesisAccount{
Nonce: 1,
Code: []byte{},
Balance: big.NewInt(500000000000000),
}
_, statedb := tests.MakePreState(rawdb.NewMemoryDatabase(), alloc, false)

// Create the tracer, the EVM environment and run it
tracer, err := New("prestateTracer", new(Context))
if err != nil {
t.Fatalf("failed to create prestate tracer: %v", err)
}
vmConfig := vm.Config{Debug: true, Tracer: tracer}
evm := vm.NewEVM(context, txContext, statedb, params.MainnetChainConfig, vmConfig)

msg, err := tx.AsMessage(signer, nil)
if err != nil {
t.Fatalf("failed to prepare transaction for tracing: %v", err)
}

st := core.NewStateTransition(evm, msg, new(core.GasPool).AddGas(tx.Gas()), celoMock.Runner, nil)
if _, err = st.TransitionDb(); err != nil {
t.Fatalf("failed to execute transaction: %v", err)
}
// Retrieve the trace result and compare against the etalon
res, err := tracer.GetResult()
if err != nil {
t.Fatalf("failed to retrieve trace result: %v", err)
}
ret := make(map[string]interface{})
if err := json.Unmarshal(res, &ret); err != nil {
t.Fatalf("failed to unmarshal trace result: %v", err)
}
if _, has := ret[toAddr]; !has {
t.Fatalf("Expected %s in result", toAddr)
}
}

func BenchmarkTransactionTrace(b *testing.B) {
celoMock := testutil.NewCeloMock()
key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
Expand Down

0 comments on commit e82bdb2

Please sign in to comment.