package commands import ( "bytes" "context" "encoding/json" "testing" jsoniter "github.com/json-iterator/go" libcommon "github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon-lib/kv/kvcache" "github.com/ledgerwatch/erigon/cmd/rpcdaemon/rpcdaemontest" "github.com/ledgerwatch/erigon/eth/tracers" "github.com/ledgerwatch/erigon/rpc" "github.com/ledgerwatch/erigon/rpc/rpccfg" "github.com/ledgerwatch/erigon/turbo/adapter/ethapi" "github.com/ledgerwatch/erigon/turbo/snapshotsync" ) var debugTraceTransactionTests = []struct { txHash string gas uint64 failed bool returnValue string }{ {"3f3cb8a0e13ed2481f97f53f7095b9cbc78b6ffb779f2d3e565146371a8830ea", 21000, false, ""}, {"f588c6426861d9ad25d5ccc12324a8d213f35ef1ed4153193f0c13eb81ca7f4a", 49189, false, "0000000000000000000000000000000000000000000000000000000000000001"}, {"b6449d8e167a8826d050afe4c9f07095236ff769a985f02649b1023c2ded2059", 38899, false, ""}, } var debugTraceTransactionNoRefundTests = []struct { txHash string gas uint64 failed bool returnValue string }{ {"3f3cb8a0e13ed2481f97f53f7095b9cbc78b6ffb779f2d3e565146371a8830ea", 21000, false, ""}, {"f588c6426861d9ad25d5ccc12324a8d213f35ef1ed4153193f0c13eb81ca7f4a", 49189, false, "0000000000000000000000000000000000000000000000000000000000000001"}, {"b6449d8e167a8826d050afe4c9f07095236ff769a985f02649b1023c2ded2059", 62899, false, ""}, } func TestTraceBlockByNumber(t *testing.T) { m, _, _ := rpcdaemontest.CreateTestSentry(t) agg := m.HistoryV3Components() br := snapshotsync.NewBlockReaderWithSnapshots(m.BlockSnapshots) stateCache := kvcache.New(kvcache.DefaultCoherentConfig) baseApi := NewBaseApi(nil, stateCache, br, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine) ethApi := NewEthAPI(baseApi, m.DB, nil, nil, nil, 5000000, 100_000) api := NewPrivateDebugAPI(baseApi, m.DB, 0) for _, tt := range debugTraceTransactionTests { var buf bytes.Buffer stream := jsoniter.NewStream(jsoniter.ConfigDefault, &buf, 4096) tx, err := ethApi.GetTransactionByHash(context.Background(), libcommon.HexToHash(tt.txHash)) if err != nil { t.Errorf("traceBlock %s: %v", tt.txHash, err) } txcount, err := ethApi.GetBlockTransactionCountByHash(context.Background(), *tx.BlockHash) if err != nil { t.Errorf("traceBlock %s: %v", tt.txHash, err) } err = api.TraceBlockByNumber(context.Background(), rpc.BlockNumber(tx.BlockNumber.ToInt().Uint64()), &tracers.TraceConfig{}, stream) if err != nil { t.Errorf("traceBlock %s: %v", tt.txHash, err) } if err = stream.Flush(); err != nil { t.Fatalf("error flusing: %v", err) } var er []ethapi.ExecutionResult if err = json.Unmarshal(buf.Bytes(), &er); err != nil { t.Fatalf("parsing result: %v", err) } if len(er) != int(*txcount) { t.Fatalf("incorrect length: %v", err) } } var buf bytes.Buffer stream := jsoniter.NewStream(jsoniter.ConfigDefault, &buf, 4096) err := api.TraceBlockByNumber(context.Background(), rpc.LatestBlockNumber, &tracers.TraceConfig{}, stream) if err != nil { t.Errorf("traceBlock %v: %v", rpc.LatestBlockNumber, err) } if err = stream.Flush(); err != nil { t.Fatalf("error flusing: %v", err) } var er []ethapi.ExecutionResult if err = json.Unmarshal(buf.Bytes(), &er); err != nil { t.Fatalf("parsing result: %v", err) } } func TestTraceBlockByHash(t *testing.T) { m, _, _ := rpcdaemontest.CreateTestSentry(t) agg := m.HistoryV3Components() br := snapshotsync.NewBlockReaderWithSnapshots(m.BlockSnapshots) stateCache := kvcache.New(kvcache.DefaultCoherentConfig) baseApi := NewBaseApi(nil, stateCache, br, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine) ethApi := NewEthAPI(baseApi, m.DB, nil, nil, nil, 5000000, 100_000) api := NewPrivateDebugAPI(baseApi, m.DB, 0) for _, tt := range debugTraceTransactionTests { var buf bytes.Buffer stream := jsoniter.NewStream(jsoniter.ConfigDefault, &buf, 4096) tx, err := ethApi.GetTransactionByHash(m.Ctx, libcommon.HexToHash(tt.txHash)) if err != nil { t.Errorf("traceBlock %s: %v", tt.txHash, err) } txcount, err := ethApi.GetBlockTransactionCountByHash(m.Ctx, *tx.BlockHash) if err != nil { t.Errorf("traceBlock %s: %v", tt.txHash, err) } err = api.TraceBlockByHash(m.Ctx, *tx.BlockHash, &tracers.TraceConfig{}, stream) if err != nil { t.Errorf("traceBlock %s: %v", tt.txHash, err) } if err = stream.Flush(); err != nil { t.Fatalf("error flusing: %v", err) } var er []ethapi.ExecutionResult if err = json.Unmarshal(buf.Bytes(), &er); err != nil { t.Fatalf("parsing result: %v", err) } if len(er) != int(*txcount) { t.Fatalf("incorrect length: %v", err) } } } func TestTraceTransaction(t *testing.T) { m, _, _ := rpcdaemontest.CreateTestSentry(t) agg := m.HistoryV3Components() br := snapshotsync.NewBlockReaderWithSnapshots(m.BlockSnapshots) stateCache := kvcache.New(kvcache.DefaultCoherentConfig) api := NewPrivateDebugAPI( NewBaseApi(nil, stateCache, br, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine), m.DB, 0) for _, tt := range debugTraceTransactionTests { var buf bytes.Buffer stream := jsoniter.NewStream(jsoniter.ConfigDefault, &buf, 4096) err := api.TraceTransaction(context.Background(), libcommon.HexToHash(tt.txHash), &tracers.TraceConfig{}, stream) if err != nil { t.Errorf("traceTransaction %s: %v", tt.txHash, err) } if err = stream.Flush(); err != nil { t.Fatalf("error flusing: %v", err) } var er ethapi.ExecutionResult if err = json.Unmarshal(buf.Bytes(), &er); err != nil { t.Fatalf("parsing result: %v", err) } if er.Gas != tt.gas { t.Errorf("wrong gas for transaction %s, got %d, expected %d", tt.txHash, er.Gas, tt.gas) } if er.Failed != tt.failed { t.Errorf("wrong failed flag for transaction %s, got %t, expected %t", tt.txHash, er.Failed, tt.failed) } if er.ReturnValue != tt.returnValue { t.Errorf("wrong return value for transaction %s, got %s, expected %s", tt.txHash, er.ReturnValue, tt.returnValue) } } } func TestTraceTransactionNoRefund(t *testing.T) { m, _, _ := rpcdaemontest.CreateTestSentry(t) br := snapshotsync.NewBlockReaderWithSnapshots(m.BlockSnapshots) agg := m.HistoryV3Components() stateCache := kvcache.New(kvcache.DefaultCoherentConfig) api := NewPrivateDebugAPI( NewBaseApi(nil, stateCache, br, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine), m.DB, 0) for _, tt := range debugTraceTransactionNoRefundTests { var buf bytes.Buffer stream := jsoniter.NewStream(jsoniter.ConfigDefault, &buf, 4096) var norefunds = true err := api.TraceTransaction(context.Background(), libcommon.HexToHash(tt.txHash), &tracers.TraceConfig{NoRefunds: &norefunds}, stream) if err != nil { t.Errorf("traceTransaction %s: %v", tt.txHash, err) } if err = stream.Flush(); err != nil { t.Fatalf("error flusing: %v", err) } var er ethapi.ExecutionResult if err = json.Unmarshal(buf.Bytes(), &er); err != nil { t.Fatalf("parsing result: %v", err) } if er.Gas != tt.gas { t.Errorf("wrong gas for transaction %s, got %d, expected %d", tt.txHash, er.Gas, tt.gas) } if er.Failed != tt.failed { t.Errorf("wrong failed flag for transaction %s, got %t, expected %t", tt.txHash, er.Failed, tt.failed) } if er.ReturnValue != tt.returnValue { t.Errorf("wrong return value for transaction %s, got %s, expected %s", tt.txHash, er.ReturnValue, tt.returnValue) } } } /* func TestStorageRangeAt(t *testing.T) { t.Parallel() // Create a state where account 0x010000... has a few storage entries. var ( //state, _ = state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) addr = common.Address{0x01} keys = []common.Hash{ // hashes of Keys of storage common.HexToHash("340dd630ad21bf010b4e676dbfa9ba9a02175262d1fa356232cfde6cb5b47ef2"), common.HexToHash("426fcb404ab2d5d8e61a3d918108006bbb0a9be65e92235bb10eefbdb6dcd053"), common.HexToHash("48078cfed56339ea54962e72c37c7f588fc4f8e5bc173827ba75cb10a63a96a5"), common.HexToHash("5723d2c3a83af9b735e3b7f21531e5623d183a9095a56604ead41f3582fdfb75"), } storage = storageMap{ keys[0]: {Key: &common.Hash{0x02}, Value: common.Hash{0x01}}, keys[1]: {Key: &common.Hash{0x04}, Value: common.Hash{0x02}}, keys[2]: {Key: &common.Hash{0x01}, Value: common.Hash{0x03}}, keys[3]: {Key: &common.Hash{0x03}, Value: common.Hash{0x04}}, } ) //for _, entry := range storage { // state.SetState(addr, *entry.Key, entry.Value) //} // Check a few combinations of limit and start/end. tests := []struct { start []byte limit int want StorageRangeResult }{ { start: []byte{}, limit: 0, want: StorageRangeResult{storageMap{}, &keys[0]}, }, { start: []byte{}, limit: 100, want: StorageRangeResult{storage, nil}, }, { start: []byte{}, limit: 2, want: StorageRangeResult{storageMap{keys[0]: storage[keys[0]], keys[1]: storage[keys[1]]}, &keys[2]}, }, { start: []byte{0x00}, limit: 4, want: StorageRangeResult{storage, nil}, }, { start: []byte{0x40}, limit: 2, want: StorageRangeResult{storageMap{keys[1]: storage[keys[1]], keys[2]: storage[keys[2]]}, &keys[3]}, }, } for _, test := range tests { //tr, err := state.StorageTrie(addr) //if err != nil { // t.Error(err) //} result, err := storageRangeAt(stateReader.(*state.PlainState), test.start, test.limit) if err != nil { t.Error(err) } require.EqualValues(t, test.want, result) } } */