erigon-pulse/turbo/jsonrpc/gen_traces_test.go

341 lines
9.4 KiB
Go
Raw Normal View History

package jsonrpc
import (
"bytes"
"context"
"encoding/json"
"testing"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
2023-03-01 07:59:56 +00:00
"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv/kvcache"
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/cli/httpcfg"
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/rpcdaemontest"
"github.com/ledgerwatch/erigon/eth/tracers"
"github.com/ledgerwatch/erigon/rpc"
"github.com/ledgerwatch/erigon/rpc/rpccfg"
// Force-load native and js packages, to trigger registration
_ "github.com/ledgerwatch/erigon/eth/tracers/js"
_ "github.com/ledgerwatch/erigon/eth/tracers/native"
)
/*
Testing tracing RPC API by generating patters of contracts invoking one another based on the input
*/
func TestGeneratedDebugApi(t *testing.T) {
m := rpcdaemontest.CreateTestSentryForTraces(t)
agg := m.HistoryV3Components()
stateCache := kvcache.New(kvcache.DefaultCoherentConfig)
baseApi := NewBaseApi(nil, stateCache, m.BlockReader, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine, m.Dirs)
api := NewPrivateDebugAPI(baseApi, m.DB, 0)
var buf bytes.Buffer
stream := jsoniter.NewStream(jsoniter.ConfigDefault, &buf, 4096)
callTracer := "callTracer"
err := api.TraceBlockByNumber(context.Background(), rpc.BlockNumber(1), &tracers.TraceConfig{Tracer: &callTracer}, stream)
if err != nil {
t.Errorf("debug_traceBlock %d: %v", 0, err)
}
if err = stream.Flush(); err != nil {
t.Fatalf("error flusing: %v", err)
}
var result interface{}
if err = json.Unmarshal(buf.Bytes(), &result); err != nil {
t.Fatalf("parsing result: %v", err)
}
expectedJSON := `
[
{
"result": {
"calls": [
{
"calls": [
{
"from": "0x00000000000000000000000000000000000001ff",
"gas": "0x595a",
"gasUsed": "0x16",
"input": "0x0100",
"output": "0x0100",
"to": "0x00000000000000000000000000000000000000ff",
"type": "CALL",
"value": "0x0"
}
],
"from": "0x00000000000000000000000000000000000002ff",
"gas": "0x6525",
"gasUsed": "0xa7b",
"input": "0x000100",
"output": "0x0100",
"to": "0x00000000000000000000000000000000000001ff",
"type": "CALL",
"value": "0x0"
},
{
"calls": [
{
"from": "0x00000000000000000000000000000000000001ff",
"gas": "0x584a",
"gasUsed": "0x10",
"input": "0x",
"to": "0x00000000000000000000000000000000000000ff",
"type": "CALL",
"value": "0x0"
}
],
"from": "0x00000000000000000000000000000000000002ff",
"gas": "0x5a4c",
"gasUsed": "0xb1",
"input": "0x00",
"to": "0x00000000000000000000000000000000000001ff",
"type": "CALL",
"value": "0x0"
}
],
"from": "0x71562b71999873db5b286df957af199ec94617f7",
"gas": "0x7120",
"gasUsed": "0x684c",
"input": "0x01000100",
"to": "0x00000000000000000000000000000000000002ff",
"type": "CALL",
"value": "0x0"
}
}
]`
var expected interface{}
if err = json.Unmarshal([]byte(expectedJSON), &expected); err != nil {
t.Fatalf("parsing expected: %v", err)
}
if !assert.Equal(t, expected, result) {
t.Fatalf("not equal")
}
}
func TestGeneratedTraceApi(t *testing.T) {
m := rpcdaemontest.CreateTestSentryForTraces(t)
agg := m.HistoryV3Components()
stateCache := kvcache.New(kvcache.DefaultCoherentConfig)
baseApi := NewBaseApi(nil, stateCache, m.BlockReader, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine, m.Dirs)
api := NewTraceAPI(baseApi, m.DB, &httpcfg.HttpCfg{})
trace_block: add new `gasBailOut` parameter to request (default: false) (#7326) **Problem** While tracing the block with Parity tracer(`trace_block`) it returns error for a few blocks. <details><summary>Example of blocks that couldn't be traced</summary> These are the example of payloads with the errors occured during tracing the blocks: 1. `{"method":"trace_block","params":["0x24165bd"],"id":0}` - `first run for txIndex 32 error: insufficient funds for gas * price + value: address 0x000005D814d5abD6e0F9345c9b1f37C82Eaf1EBb have 547961731687102852 want 675024999764675175` 2. `{"method":"trace_block","params":["0x2658753"],"id":0}` - `first run for txIndex 45 error: insufficient funds for gas * price + value: address 0x000004BeDC012a5D043270AF67de24c20a3b8aeB have 211658993830905595 want 235579381558610848` 3. `{"method":"trace_block","params":["0x258b00c"],"id":0} -first run for txIndex 274 error: insufficient funds for gas * price + value: address 0xA3a762006D22806B35895f4C0599bAe3adF1B349 have 342048586356638524 want 386865441635818752` </details> After looking through the [trace_filtering.go](https://github.com/ledgerwatch/erigon/blob/devel/cmd/rpcdaemon/commands/trace_filtering.go#L1006) and [trace_adhoc.go](https://github.com/ledgerwatch/erigon/blob/devel/cmd/rpcdaemon/commands/trace_adhoc.go#L1072) noticed that `doCallMany` called with `gasBailOut = true` in [one](https://github.com/ledgerwatch/erigon/blob/devel/cmd/rpcdaemon/commands/trace_adhoc.go#L1072) place and `gasBailOut = false` in [another](https://github.com/ledgerwatch/erigon/blob/devel/cmd/rpcdaemon/commands/trace_filtering.go#L1006) (actually the part of the code that is called by `trace_block`. Changing its value to `true` fixed the problem and traces are successful with this change. Attaching the partial result of the successful trace below. <details><summary>{"method":"trace_block","params":["0x24165bd"],"id":0}</summary> ```json { "jsonrpc": "2.0", "id": 0, "result": [ { "action": { "from": "0x3eaf7de168a79c1d6a1aab8c106e42b6f4e0a7c8", "callType": "call", "gas": "0x30464", "input": "0x0000000100000000000000000000000000000000000000000000000000000001dcbde73f0000000000000000000000000000000000000000000002086b35cd47c9189dc02791bca1f2de4661ed88a30c99a7a9449aa841740d500b1d8e8ef31e21c99d1db9a6444d3adf12700001f4000000000000000000000f490d8e000000000000", "to": "0x1d36f9688cceafee9d7df45fe8e24884ed0d6730", "value": "0x0" }, "blockHash": "0x96f0792349a67b7995dda853df79bd5fa9d2926eb2ac8a07ac46e4df429632af", "blockNumber": 37840317, "error": "Reverted", "result": { "gasUsed": "0x1d2d", "output": "0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000056572723332000000000000000000000000000000000000000000000000000000" }, "subtraces": 1, "traceAddress": [], "transactionHash": "0x1417cab7ae635884117909cc828474df0121d4a2a0ad033f462e6fa84bfab176", "transactionPosition": 0, "type": "call" }, { "action": { "from": "0x1d36f9688cceafee9d7df45fe8e24884ed0d6730", "callType": "staticcall", "gas": "0x2e9b7", "input": "0x3850c7bd", "to": "0xa374094527e1673a86de625aa59517c5de346d32", "value": "0x0" }, "blockHash": "0x96f0792349a67b7995dda853df79bd5fa9d2926eb2ac8a07ac46e4df429632af", "blockNumber": 37840317, "result": { "gasUsed": "0xa88", "output": "0x000000000000000000000000000000000000000000000f49c33e3991750050fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffbc158000000000000000000000000000000000000000000000000000000000000096d00000000000000000000000000000000000000000000000000000000000009c400000000000000000000000000000000000000000000000000000000000009c400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001" }, "subtraces": 0, "traceAddress": [ 0 ], ..... }, "blockHash": "0x96f0792349a67b7995dda853df79bd5fa9d2926eb2ac8a07ac46e4df429632af", "blockNumber": 37840317, "result": null, "subtraces": 0, "traceAddress": [], "type": "reward" } ] } ``` </details> --------- Co-authored-by: alexqrid <>
2023-04-21 02:28:04 +00:00
traces, err := api.Block(context.Background(), rpc.BlockNumber(1), new(bool))
if err != nil {
t.Errorf("trace_block %d: %v", 0, err)
}
buf, err := json.Marshal(traces)
if err != nil {
t.Errorf("marshall result into JSON: %v", err)
}
var result interface{}
if err = json.Unmarshal(buf, &result); err != nil {
t.Fatalf("parsing result: %v", err)
}
expectedJSON := `
[
{
"action": {
"from": "0x71562b71999873db5b286df957af199ec94617f7",
"callType": "call",
"gas": "0x7120",
"input": "0x01000100",
"to": "0x00000000000000000000000000000000000002ff",
"value": "0x0"
},
"blockHash": "0x2c7ee9236a9eb58cbaf6473f458ddb41716c6735f4a63eacf0f8b759685f1dbc",
"blockNumber": 1,
"result": {
"gasUsed": "0x161c",
"output": "0x"
},
"subtraces": 2,
"traceAddress": [],
"transactionHash": "0xb42edc1d46932ef34be0ba49402dc94e3d2319c066f02945f6828cd344fcfa7b",
"transactionPosition": 0,
"type": "call"
},
{
"action": {
"from": "0x00000000000000000000000000000000000002ff",
"callType": "call",
"gas": "0x6525",
"input": "0x000100",
"to": "0x00000000000000000000000000000000000001ff",
"value": "0x0"
},
"blockHash": "0x2c7ee9236a9eb58cbaf6473f458ddb41716c6735f4a63eacf0f8b759685f1dbc",
"blockNumber": 1,
"result": {
"gasUsed": "0xa7b",
"output": "0x0100"
},
"subtraces": 1,
"traceAddress": [
0
],
"transactionHash": "0xb42edc1d46932ef34be0ba49402dc94e3d2319c066f02945f6828cd344fcfa7b",
"transactionPosition": 0,
"type": "call"
},
{
"action": {
"from": "0x00000000000000000000000000000000000001ff",
"callType": "call",
"gas": "0x595a",
"input": "0x0100",
"to": "0x00000000000000000000000000000000000000ff",
"value": "0x0"
},
"blockHash": "0x2c7ee9236a9eb58cbaf6473f458ddb41716c6735f4a63eacf0f8b759685f1dbc",
"blockNumber": 1,
"result": {
"gasUsed": "0x16",
"output": "0x0100"
},
"subtraces": 0,
"traceAddress": [
0,
0
],
"transactionHash": "0xb42edc1d46932ef34be0ba49402dc94e3d2319c066f02945f6828cd344fcfa7b",
"transactionPosition": 0,
"type": "call"
},
{
"action": {
"from": "0x00000000000000000000000000000000000002ff",
"callType": "call",
"gas": "0x5a4c",
"input": "0x00",
"to": "0x00000000000000000000000000000000000001ff",
"value": "0x0"
},
"blockHash": "0x2c7ee9236a9eb58cbaf6473f458ddb41716c6735f4a63eacf0f8b759685f1dbc",
"blockNumber": 1,
"result": {
"gasUsed": "0xb1",
"output": "0x"
},
"subtraces": 1,
"traceAddress": [
1
],
"transactionHash": "0xb42edc1d46932ef34be0ba49402dc94e3d2319c066f02945f6828cd344fcfa7b",
"transactionPosition": 0,
"type": "call"
},
{
"action": {
"from": "0x00000000000000000000000000000000000001ff",
"callType": "call",
"gas": "0x584a",
"input": "0x",
"to": "0x00000000000000000000000000000000000000ff",
"value": "0x0"
},
"blockHash": "0x2c7ee9236a9eb58cbaf6473f458ddb41716c6735f4a63eacf0f8b759685f1dbc",
"blockNumber": 1,
"result": {
"gasUsed": "0x10",
"output": "0x"
},
"subtraces": 0,
"traceAddress": [
1,
0
],
"transactionHash": "0xb42edc1d46932ef34be0ba49402dc94e3d2319c066f02945f6828cd344fcfa7b",
"transactionPosition": 0,
"type": "call"
},
{
"action": {
"author": "0x0100000000000000000000000000000000000000",
"rewardType": "block",
"value": "0x1bc16d674ec80000"
},
"blockHash": "0x2c7ee9236a9eb58cbaf6473f458ddb41716c6735f4a63eacf0f8b759685f1dbc",
"blockNumber": 1,
"result": null,
"subtraces": 0,
"traceAddress": [],
"type": "reward"
}
]`
var expected interface{}
if err = json.Unmarshal([]byte(expectedJSON), &expected); err != nil {
t.Fatalf("parsing expected: %v", err)
}
if !assert.Equal(t, expected, result) {
t.Fatalf("not equal")
}
}
func TestGeneratedTraceApiCollision(t *testing.T) {
m := rpcdaemontest.CreateTestSentryForTracesCollision(t)
api := NewTraceAPI(newBaseApiForTest(m), m.DB, &httpcfg.HttpCfg{})
trace_block: add new `gasBailOut` parameter to request (default: false) (#7326) **Problem** While tracing the block with Parity tracer(`trace_block`) it returns error for a few blocks. <details><summary>Example of blocks that couldn't be traced</summary> These are the example of payloads with the errors occured during tracing the blocks: 1. `{"method":"trace_block","params":["0x24165bd"],"id":0}` - `first run for txIndex 32 error: insufficient funds for gas * price + value: address 0x000005D814d5abD6e0F9345c9b1f37C82Eaf1EBb have 547961731687102852 want 675024999764675175` 2. `{"method":"trace_block","params":["0x2658753"],"id":0}` - `first run for txIndex 45 error: insufficient funds for gas * price + value: address 0x000004BeDC012a5D043270AF67de24c20a3b8aeB have 211658993830905595 want 235579381558610848` 3. `{"method":"trace_block","params":["0x258b00c"],"id":0} -first run for txIndex 274 error: insufficient funds for gas * price + value: address 0xA3a762006D22806B35895f4C0599bAe3adF1B349 have 342048586356638524 want 386865441635818752` </details> After looking through the [trace_filtering.go](https://github.com/ledgerwatch/erigon/blob/devel/cmd/rpcdaemon/commands/trace_filtering.go#L1006) and [trace_adhoc.go](https://github.com/ledgerwatch/erigon/blob/devel/cmd/rpcdaemon/commands/trace_adhoc.go#L1072) noticed that `doCallMany` called with `gasBailOut = true` in [one](https://github.com/ledgerwatch/erigon/blob/devel/cmd/rpcdaemon/commands/trace_adhoc.go#L1072) place and `gasBailOut = false` in [another](https://github.com/ledgerwatch/erigon/blob/devel/cmd/rpcdaemon/commands/trace_filtering.go#L1006) (actually the part of the code that is called by `trace_block`. Changing its value to `true` fixed the problem and traces are successful with this change. Attaching the partial result of the successful trace below. <details><summary>{"method":"trace_block","params":["0x24165bd"],"id":0}</summary> ```json { "jsonrpc": "2.0", "id": 0, "result": [ { "action": { "from": "0x3eaf7de168a79c1d6a1aab8c106e42b6f4e0a7c8", "callType": "call", "gas": "0x30464", "input": "0x0000000100000000000000000000000000000000000000000000000000000001dcbde73f0000000000000000000000000000000000000000000002086b35cd47c9189dc02791bca1f2de4661ed88a30c99a7a9449aa841740d500b1d8e8ef31e21c99d1db9a6444d3adf12700001f4000000000000000000000f490d8e000000000000", "to": "0x1d36f9688cceafee9d7df45fe8e24884ed0d6730", "value": "0x0" }, "blockHash": "0x96f0792349a67b7995dda853df79bd5fa9d2926eb2ac8a07ac46e4df429632af", "blockNumber": 37840317, "error": "Reverted", "result": { "gasUsed": "0x1d2d", "output": "0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000056572723332000000000000000000000000000000000000000000000000000000" }, "subtraces": 1, "traceAddress": [], "transactionHash": "0x1417cab7ae635884117909cc828474df0121d4a2a0ad033f462e6fa84bfab176", "transactionPosition": 0, "type": "call" }, { "action": { "from": "0x1d36f9688cceafee9d7df45fe8e24884ed0d6730", "callType": "staticcall", "gas": "0x2e9b7", "input": "0x3850c7bd", "to": "0xa374094527e1673a86de625aa59517c5de346d32", "value": "0x0" }, "blockHash": "0x96f0792349a67b7995dda853df79bd5fa9d2926eb2ac8a07ac46e4df429632af", "blockNumber": 37840317, "result": { "gasUsed": "0xa88", "output": "0x000000000000000000000000000000000000000000000f49c33e3991750050fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffbc158000000000000000000000000000000000000000000000000000000000000096d00000000000000000000000000000000000000000000000000000000000009c400000000000000000000000000000000000000000000000000000000000009c400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001" }, "subtraces": 0, "traceAddress": [ 0 ], ..... }, "blockHash": "0x96f0792349a67b7995dda853df79bd5fa9d2926eb2ac8a07ac46e4df429632af", "blockNumber": 37840317, "result": null, "subtraces": 0, "traceAddress": [], "type": "reward" } ] } ``` </details> --------- Co-authored-by: alexqrid <>
2023-04-21 02:28:04 +00:00
traces, err := api.Transaction(context.Background(), common.HexToHash("0xb2b9fa4c999c1c8370ce1fbd1c4315a9ce7f8421fe2ebed8a9051ff2e4e7e3da"), new(bool))
if err != nil {
t.Errorf("trace_block %d: %v", 0, err)
}
buf, err := json.Marshal(traces)
if err != nil {
t.Errorf("marshall result into JSON: %v", err)
}
var result interface{}
if err = json.Unmarshal(buf, &result); err != nil {
t.Fatalf("parsing result: %v", err)
}
expectedJSON := `
[
{
"action": {
"from": "0x71562b71999873db5b286df957af199ec94617f7",
"callType": "call",
"gas": "0x13498",
"input": "0x",
"to": "0x000000000000000000000000000000000000bbbb",
"value": "0x0"
},
"blockHash": "0xc78e9674685b04e1300d62cafdae0708d030a9bc0ff7aa9eb9315da23de650dc",
"blockNumber": 1,
"result": {
"gasUsed": "0x131bb",
"output": "0x"
},
"subtraces": 1,
"traceAddress": [],
"transactionHash": "0xb2b9fa4c999c1c8370ce1fbd1c4315a9ce7f8421fe2ebed8a9051ff2e4e7e3da",
"transactionPosition": 2,
"type": "call"
},
{
"action": {
"from": "0x000000000000000000000000000000000000bbbb",
"gas": "0xb49d",
"init": "0x600360035560046004556158ff6000526002601ef3",
"value": "0x0"
},
"blockHash": "0xc78e9674685b04e1300d62cafdae0708d030a9bc0ff7aa9eb9315da23de650dc",
"blockNumber": 1,
"error": "contract address collision",
"result": null,
"subtraces": 0,
"traceAddress": [
0
],
"transactionHash": "0xb2b9fa4c999c1c8370ce1fbd1c4315a9ce7f8421fe2ebed8a9051ff2e4e7e3da",
"transactionPosition": 2,
"type": "create"
}
]
`
var expected interface{}
if err = json.Unmarshal([]byte(expectedJSON), &expected); err != nil {
t.Fatalf("parsing expected: %v", err)
}
if !assert.Equal(t, expected, result) {
t.Fatalf("not equal")
}
}