erigon-pulse/cmd/rpcdaemon/commands/call_traces_test.go
alexqrid db51dd5f02
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 09:28:04 +07:00

275 lines
9.6 KiB
Go

package commands
import (
"context"
"sync"
"testing"
"github.com/holiman/uint256"
jsoniter "github.com/json-iterator/go"
"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv/kvcache"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/valyala/fastjson"
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/cli/httpcfg"
"github.com/ledgerwatch/erigon/rpc/rpccfg"
"github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/core"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/turbo/snapshotsync"
"github.com/ledgerwatch/erigon/turbo/stages"
)
func blockNumbersFromTraces(t *testing.T, b []byte) []int {
t.Helper()
var err error
var p fastjson.Parser
response := b
var v *fastjson.Value
if v, err = p.ParseBytes(response); err != nil {
t.Fatalf("parsing response: %v", err)
}
var elems []*fastjson.Value
if elems, err = v.Array(); err != nil {
t.Fatalf("expected array in the response: %v", err)
}
numbers := make([]int, 0, len(elems))
for _, elem := range elems {
bn := elem.GetInt("blockNumber")
numbers = append(numbers, bn)
}
return numbers
}
func TestCallTraceOneByOne(t *testing.T) {
m := stages.Mock(t)
chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 10, func(i int, gen *core.BlockGen) {
gen.SetCoinbase(common.Address{1})
}, false /* intermediateHashes */)
if err != nil {
t.Fatalf("generate chain: %v", err)
}
agg := m.HistoryV3Components()
br := snapshotsync.NewBlockReaderWithSnapshots(m.BlockSnapshots, m.TransactionsV3)
api := NewTraceAPI(
NewBaseApi(nil, kvcache.New(kvcache.DefaultCoherentConfig), br, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine, m.Dirs),
m.DB, &httpcfg.HttpCfg{})
// Insert blocks 1 by 1, to tirgget possible "off by one" errors
for i := 0; i < chain.Length(); i++ {
if err = m.InsertChain(chain.Slice(i, i+1)); err != nil {
t.Fatalf("inserting chain: %v", err)
}
}
stream := jsoniter.ConfigDefault.BorrowStream(nil)
defer jsoniter.ConfigDefault.ReturnStream(stream)
var fromBlock, toBlock uint64
fromBlock = 1
toBlock = 10
toAddress1 := common.Address{1}
traceReq1 := TraceFilterRequest{
FromBlock: (*hexutil.Uint64)(&fromBlock),
ToBlock: (*hexutil.Uint64)(&toBlock),
ToAddress: []*common.Address{&toAddress1},
}
if err = api.Filter(context.Background(), traceReq1, stream, new(bool)); err != nil {
t.Fatalf("trace_filter failed: %v", err)
}
assert.Equal(t, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, blockNumbersFromTraces(t, stream.Buffer()))
}
func TestCallTraceUnwind(t *testing.T) {
m := stages.Mock(t)
var chainA, chainB *core.ChainPack
var err error
chainA, err = core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 10, func(i int, gen *core.BlockGen) {
gen.SetCoinbase(common.Address{1})
}, false /* intermediateHashes */)
if err != nil {
t.Fatalf("generate chainA: %v", err)
}
chainB, err = core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 20, func(i int, gen *core.BlockGen) {
if i < 5 || i >= 10 {
gen.SetCoinbase(common.Address{1})
} else {
gen.SetCoinbase(common.Address{2})
}
}, false /* intermediateHashes */)
if err != nil {
t.Fatalf("generate chainB: %v", err)
}
agg := m.HistoryV3Components()
br := snapshotsync.NewBlockReaderWithSnapshots(m.BlockSnapshots, m.TransactionsV3)
api := NewTraceAPI(NewBaseApi(nil, kvcache.New(kvcache.DefaultCoherentConfig), br, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine, m.Dirs), m.DB, &httpcfg.HttpCfg{})
if err = m.InsertChain(chainA); err != nil {
t.Fatalf("inserting chainA: %v", err)
}
stream := jsoniter.ConfigDefault.BorrowStream(nil)
defer jsoniter.ConfigDefault.ReturnStream(stream)
var fromBlock, toBlock uint64
fromBlock = 1
toBlock = 10
toAddress1 := common.Address{1}
traceReq1 := TraceFilterRequest{
FromBlock: (*hexutil.Uint64)(&fromBlock),
ToBlock: (*hexutil.Uint64)(&toBlock),
ToAddress: []*common.Address{&toAddress1},
}
if err = api.Filter(context.Background(), traceReq1, stream, new(bool)); err != nil {
t.Fatalf("trace_filter failed: %v", err)
}
assert.Equal(t, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, blockNumbersFromTraces(t, stream.Buffer()))
if err = m.InsertChain(chainB.Slice(0, 12)); err != nil {
t.Fatalf("inserting chainB: %v", err)
}
stream.Reset(nil)
toBlock = 12
traceReq2 := TraceFilterRequest{
FromBlock: (*hexutil.Uint64)(&fromBlock),
ToBlock: (*hexutil.Uint64)(&toBlock),
ToAddress: []*common.Address{&toAddress1},
}
if err = api.Filter(context.Background(), traceReq2, stream, new(bool)); err != nil {
t.Fatalf("trace_filter failed: %v", err)
}
assert.Equal(t, []int{1, 2, 3, 4, 5, 11, 12}, blockNumbersFromTraces(t, stream.Buffer()))
if err = m.InsertChain(chainB.Slice(12, 20)); err != nil {
t.Fatalf("inserting chainB: %v", err)
}
stream.Reset(nil)
fromBlock = 12
toBlock = 20
traceReq3 := TraceFilterRequest{
FromBlock: (*hexutil.Uint64)(&fromBlock),
ToBlock: (*hexutil.Uint64)(&toBlock),
ToAddress: []*common.Address{&toAddress1},
}
if err = api.Filter(context.Background(), traceReq3, stream, new(bool)); err != nil {
t.Fatalf("trace_filter failed: %v", err)
}
assert.Equal(t, []int{12, 13, 14, 15, 16, 17, 18, 19, 20}, blockNumbersFromTraces(t, stream.Buffer()))
}
func TestFilterNoAddresses(t *testing.T) {
m := stages.Mock(t)
chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 10, func(i int, gen *core.BlockGen) {
gen.SetCoinbase(common.Address{1})
}, false /* intermediateHashes */)
if err != nil {
t.Fatalf("generate chain: %v", err)
}
agg := m.HistoryV3Components()
br := snapshotsync.NewBlockReaderWithSnapshots(m.BlockSnapshots, m.TransactionsV3)
api := NewTraceAPI(NewBaseApi(nil, kvcache.New(kvcache.DefaultCoherentConfig), br, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine, m.Dirs), m.DB, &httpcfg.HttpCfg{})
// Insert blocks 1 by 1, to tirgget possible "off by one" errors
for i := 0; i < chain.Length(); i++ {
if err = m.InsertChain(chain.Slice(i, i+1)); err != nil {
t.Fatalf("inserting chain: %v", err)
}
}
stream := jsoniter.ConfigDefault.BorrowStream(nil)
defer jsoniter.ConfigDefault.ReturnStream(stream)
var fromBlock, toBlock uint64
fromBlock = 1
toBlock = 10
traceReq1 := TraceFilterRequest{
FromBlock: (*hexutil.Uint64)(&fromBlock),
ToBlock: (*hexutil.Uint64)(&toBlock),
}
if err = api.Filter(context.Background(), traceReq1, stream, new(bool)); err != nil {
t.Fatalf("trace_filter failed: %v", err)
}
assert.Equal(t, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, blockNumbersFromTraces(t, stream.Buffer()))
}
func TestFilterAddressIntersection(t *testing.T) {
m := stages.Mock(t)
agg := m.HistoryV3Components()
br := snapshotsync.NewBlockReaderWithSnapshots(m.BlockSnapshots, m.TransactionsV3)
api := NewTraceAPI(NewBaseApi(nil, kvcache.New(kvcache.DefaultCoherentConfig), br, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine, m.Dirs), m.DB, &httpcfg.HttpCfg{})
toAddress1, toAddress2, other := common.Address{1}, common.Address{2}, common.Address{3}
once := new(sync.Once)
chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 15, func(i int, block *core.BlockGen) {
once.Do(func() { block.SetCoinbase(common.Address{4}) })
var rcv common.Address
if i < 5 {
rcv = toAddress1
} else if i < 10 {
rcv = toAddress2
} else {
rcv = other
}
signer := types.LatestSigner(m.ChainConfig)
txn, err := types.SignTx(types.NewTransaction(block.TxNonce(m.Address), rcv, new(uint256.Int), 21000, new(uint256.Int), nil), *signer, m.Key)
if err != nil {
t.Fatal(err)
}
block.AddTx(txn)
}, false /* intermediateHashes */)
require.NoError(t, err, "generate chain")
err = m.InsertChain(chain)
require.NoError(t, err, "inserting chain")
fromBlock, toBlock := uint64(1), uint64(15)
t.Run("second", func(t *testing.T) {
stream := jsoniter.ConfigDefault.BorrowStream(nil)
defer jsoniter.ConfigDefault.ReturnStream(stream)
traceReq1 := TraceFilterRequest{
FromBlock: (*hexutil.Uint64)(&fromBlock),
ToBlock: (*hexutil.Uint64)(&toBlock),
FromAddress: []*common.Address{&m.Address, &other},
ToAddress: []*common.Address{&m.Address, &toAddress2},
Mode: TraceFilterModeIntersection,
}
if err = api.Filter(context.Background(), traceReq1, stream, new(bool)); err != nil {
t.Fatalf("trace_filter failed: %v", err)
}
assert.Equal(t, []int{6, 7, 8, 9, 10}, blockNumbersFromTraces(t, stream.Buffer()))
})
t.Run("first", func(t *testing.T) {
stream := jsoniter.ConfigDefault.BorrowStream(nil)
defer jsoniter.ConfigDefault.ReturnStream(stream)
traceReq1 := TraceFilterRequest{
FromBlock: (*hexutil.Uint64)(&fromBlock),
ToBlock: (*hexutil.Uint64)(&toBlock),
FromAddress: []*common.Address{&m.Address, &other},
ToAddress: []*common.Address{&toAddress1, &m.Address},
Mode: TraceFilterModeIntersection,
}
if err = api.Filter(context.Background(), traceReq1, stream, new(bool)); err != nil {
t.Fatalf("trace_filter failed: %v", err)
}
assert.Equal(t, []int{1, 2, 3, 4, 5}, blockNumbersFromTraces(t, stream.Buffer()))
})
t.Run("empty", func(t *testing.T) {
stream := jsoniter.ConfigDefault.BorrowStream(nil)
defer jsoniter.ConfigDefault.ReturnStream(stream)
traceReq1 := TraceFilterRequest{
FromBlock: (*hexutil.Uint64)(&fromBlock),
ToBlock: (*hexutil.Uint64)(&toBlock),
ToAddress: []*common.Address{&other},
FromAddress: []*common.Address{&toAddress2, &toAddress1, &other},
Mode: TraceFilterModeIntersection,
}
if err = api.Filter(context.Background(), traceReq1, stream, new(bool)); err != nil {
t.Fatalf("trace_filter failed: %v", err)
}
require.Empty(t, blockNumbersFromTraces(t, stream.Buffer()))
})
}