2023-07-08 17:01:26 +00:00
|
|
|
package jsonrpc
|
2021-02-21 20:18:59 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2022-09-18 10:41:01 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
2021-07-29 11:53:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2023-01-13 18:12:18 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2022-02-16 17:38:54 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/cli/httpcfg"
|
2021-06-29 10:00:22 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/rpcdaemontest"
|
2021-06-26 12:27:29 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common/hexutil"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/rpc"
|
2021-02-21 20:18:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEmptyQuery(t *testing.T) {
|
2022-09-18 10:41:01 +00:00
|
|
|
m, _, _ := rpcdaemontest.CreateTestSentry(t)
|
2023-06-15 06:11:51 +00:00
|
|
|
api := NewTraceAPI(newBaseApiForTest(m), m.DB, &httpcfg.HttpCfg{})
|
2021-02-21 20:18:59 +00:00
|
|
|
// Call GetTransactionReceipt for transaction which is not in the database
|
2021-05-01 07:42:23 +00:00
|
|
|
var latest = rpc.LatestBlockNumber
|
2021-02-21 20:18:59 +00:00
|
|
|
results, err := api.CallMany(context.Background(), json.RawMessage("[]"), &rpc.BlockNumberOrHash{BlockNumber: &latest})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("calling CallMany: %v", err)
|
|
|
|
}
|
|
|
|
if results == nil {
|
|
|
|
t.Errorf("expected empty array, got nil")
|
|
|
|
}
|
|
|
|
if len(results) > 0 {
|
|
|
|
t.Errorf("expected empty array, got %d elements", len(results))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestCoinbaseBalance(t *testing.T) {
|
2022-09-18 10:41:01 +00:00
|
|
|
m, _, _ := rpcdaemontest.CreateTestSentry(t)
|
2023-06-15 06:11:51 +00:00
|
|
|
api := NewTraceAPI(newBaseApiForTest(m), m.DB, &httpcfg.HttpCfg{})
|
2021-02-21 20:18:59 +00:00
|
|
|
// Call GetTransactionReceipt for transaction which is not in the database
|
2021-05-01 07:42:23 +00:00
|
|
|
var latest = rpc.LatestBlockNumber
|
2021-02-21 20:18:59 +00:00
|
|
|
results, err := api.CallMany(context.Background(), json.RawMessage(`
|
|
|
|
[
|
|
|
|
[{"from":"0x71562b71999873db5b286df957af199ec94617f7","to":"0x0d3ab14bbad3d99f4203bd7a11acb94882050e7e","gas":"0x15f90","gasPrice":"0x4a817c800","value":"0x1"},["trace", "stateDiff"]],
|
|
|
|
[{"from":"0x71562b71999873db5b286df957af199ec94617f7","to":"0x0d3ab14bbad3d99f4203bd7a11acb94882050e7e","gas":"0x15f90","gasPrice":"0x4a817c800","value":"0x1"},["trace", "stateDiff"]]
|
|
|
|
]
|
|
|
|
`), &rpc.BlockNumberOrHash{BlockNumber: &latest})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("calling CallMany: %v", err)
|
|
|
|
}
|
|
|
|
if results == nil {
|
|
|
|
t.Errorf("expected empty array, got nil")
|
|
|
|
}
|
|
|
|
if len(results) != 2 {
|
|
|
|
t.Errorf("expected array with 2 elements, got %d elements", len(results))
|
|
|
|
}
|
|
|
|
// Expect balance increase of the coinbase (zero address)
|
2023-01-13 18:12:18 +00:00
|
|
|
if _, ok := results[1].StateDiff[libcommon.Address{}]; !ok {
|
2021-02-21 20:18:59 +00:00
|
|
|
t.Errorf("expected balance increase for coinbase (zero address)")
|
|
|
|
}
|
2021-06-26 12:27:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReplayTransaction(t *testing.T) {
|
2022-09-18 10:41:01 +00:00
|
|
|
m, _, _ := rpcdaemontest.CreateTestSentry(t)
|
2023-06-15 06:11:51 +00:00
|
|
|
api := NewTraceAPI(newBaseApiForTest(m), m.DB, &httpcfg.HttpCfg{})
|
2023-01-13 18:12:18 +00:00
|
|
|
var txnHash libcommon.Hash
|
2022-09-18 10:41:01 +00:00
|
|
|
if err := m.DB.View(context.Background(), func(tx kv.Tx) error {
|
2023-06-15 06:11:51 +00:00
|
|
|
b, err := m.BlockReader.BlockByNumber(m.Ctx, tx, 6)
|
2021-06-26 12:27:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
txnHash = b.Transactions()[5].Hash()
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call GetTransactionReceipt for transaction which is not in the database
|
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
|
|
|
results, err := api.ReplayTransaction(context.Background(), txnHash, []string{"stateDiff"}, new(bool))
|
2021-06-26 12:27:29 +00:00
|
|
|
if err != nil {
|
2021-07-21 18:34:56 +00:00
|
|
|
t.Errorf("calling ReplayTransaction: %v", err)
|
2021-06-26 12:27:29 +00:00
|
|
|
}
|
|
|
|
require.NotNil(t, results)
|
|
|
|
require.NotNil(t, results.StateDiff)
|
2023-01-13 18:12:18 +00:00
|
|
|
addrDiff := results.StateDiff[libcommon.HexToAddress("0x0000000000000006000000000000000000000000")]
|
2021-06-26 12:27:29 +00:00
|
|
|
v := addrDiff.Balance.(map[string]*hexutil.Big)["+"].ToInt().Uint64()
|
|
|
|
require.Equal(t, uint64(1_000_000_000_000_000), v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReplayBlockTransactions(t *testing.T) {
|
2022-09-18 10:41:01 +00:00
|
|
|
m, _, _ := rpcdaemontest.CreateTestSentry(t)
|
2023-06-15 06:11:51 +00:00
|
|
|
api := NewTraceAPI(newBaseApiForTest(m), m.DB, &httpcfg.HttpCfg{})
|
2021-02-21 20:18:59 +00:00
|
|
|
|
2021-06-26 12:27:29 +00:00
|
|
|
// Call GetTransactionReceipt for transaction which is not in the database
|
|
|
|
n := rpc.BlockNumber(6)
|
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
|
|
|
results, err := api.ReplayBlockTransactions(m.Ctx, rpc.BlockNumberOrHash{BlockNumber: &n}, []string{"stateDiff"}, new(bool))
|
2021-06-26 12:27:29 +00:00
|
|
|
if err != nil {
|
2021-07-21 18:34:56 +00:00
|
|
|
t.Errorf("calling ReplayBlockTransactions: %v", err)
|
2021-06-26 12:27:29 +00:00
|
|
|
}
|
|
|
|
require.NotNil(t, results)
|
|
|
|
require.NotNil(t, results[0].StateDiff)
|
2023-01-13 18:12:18 +00:00
|
|
|
addrDiff := results[0].StateDiff[libcommon.HexToAddress("0x0000000000000001000000000000000000000000")]
|
2021-06-26 12:27:29 +00:00
|
|
|
v := addrDiff.Balance.(map[string]*hexutil.Big)["+"].ToInt().Uint64()
|
|
|
|
require.Equal(t, uint64(1_000_000_000_000_000), v)
|
2021-02-21 20:18:59 +00:00
|
|
|
}
|