erigon-pulse/cmd/rpcdaemon/commands/trace_adhoc_test.go
Nicolas Gotchac 961a0070cc
Fix trace error in Polygon | Pass Engin to the Base API (#6131)
So there is an issue with tracing certain blocks/transactions on
Polygon, for example:
```
> '{"method": "trace_transaction","params":["0xb198d93f640343a98f90d93aa2b74b4fc5c64f3a649f1608d2bfd1004f9dee0e"],"id":1,"jsonrpc":"2.0"}'
```
gives the error `first run for txIndex 1 error: insufficient funds for
gas * price + value: address 0x10AD27A96CDBffC90ab3b83bF695911426A69f5E
have 16927727762862809 want 17594166808296934`

The reason is that this transaction is from the author of the block,
which doesn't have enough ETH to pay for the gas fee + tx value if he's
not the block author receiving transactions fees.

The issue is that currently the APIs are using `ethash.NewFaker()`
Engine for running traces, etc. which doesn't know how to get the author
for a specific block (which is consensus dependant); as it was noting in
several TODO comments.

The fix is to pass the Engine to the BaseAPI, which can then be used to
create the right Block Context. I chose to split the current Engine
interface in 2, with Reader and Writer, so that the BaseAPI only
receives the Reader one, which might be safer (even though it's only
used for getting the block Author).
2022-12-04 12:17:39 +07:00

122 lines
4.8 KiB
Go

package commands
import (
"context"
"encoding/json"
"testing"
"github.com/ledgerwatch/erigon-lib/kv"
"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/common"
"github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/rpc"
"github.com/ledgerwatch/erigon/rpc/rpccfg"
"github.com/ledgerwatch/erigon/turbo/snapshotsync"
"github.com/stretchr/testify/require"
)
func TestEmptyQuery(t *testing.T) {
m, _, _ := rpcdaemontest.CreateTestSentry(t)
agg := m.HistoryV3Components()
stateCache := kvcache.New(kvcache.DefaultCoherentConfig)
br := snapshotsync.NewBlockReaderWithSnapshots(m.BlockSnapshots)
api := NewTraceAPI(NewBaseApi(nil, stateCache, br, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine), m.DB, &httpcfg.HttpCfg{})
// Call GetTransactionReceipt for transaction which is not in the database
var latest = rpc.LatestBlockNumber
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) {
m, _, _ := rpcdaemontest.CreateTestSentry(t)
agg := m.HistoryV3Components()
stateCache := kvcache.New(kvcache.DefaultCoherentConfig)
br := snapshotsync.NewBlockReaderWithSnapshots(m.BlockSnapshots)
api := NewTraceAPI(NewBaseApi(nil, stateCache, br, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine), m.DB, &httpcfg.HttpCfg{})
// Call GetTransactionReceipt for transaction which is not in the database
var latest = rpc.LatestBlockNumber
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)
if _, ok := results[1].StateDiff[common.Address{}]; !ok {
t.Errorf("expected balance increase for coinbase (zero address)")
}
}
func TestReplayTransaction(t *testing.T) {
m, _, _ := rpcdaemontest.CreateTestSentry(t)
agg := m.HistoryV3Components()
stateCache := kvcache.New(kvcache.DefaultCoherentConfig)
br := snapshotsync.NewBlockReaderWithSnapshots(m.BlockSnapshots)
api := NewTraceAPI(NewBaseApi(nil, stateCache, br, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine), m.DB, &httpcfg.HttpCfg{})
var txnHash common.Hash
if err := m.DB.View(context.Background(), func(tx kv.Tx) error {
b, err := rawdb.ReadBlockByNumber(tx, 6)
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
results, err := api.ReplayTransaction(context.Background(), txnHash, []string{"stateDiff"})
if err != nil {
t.Errorf("calling ReplayTransaction: %v", err)
}
require.NotNil(t, results)
require.NotNil(t, results.StateDiff)
addrDiff := results.StateDiff[common.HexToAddress("0x0000000000000006000000000000000000000000")]
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) {
m, _, _ := rpcdaemontest.CreateTestSentry(t)
agg := m.HistoryV3Components()
br := snapshotsync.NewBlockReaderWithSnapshots(m.BlockSnapshots)
stateCache := kvcache.New(kvcache.DefaultCoherentConfig)
api := NewTraceAPI(NewBaseApi(nil, stateCache, br, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine), m.DB, &httpcfg.HttpCfg{})
// Call GetTransactionReceipt for transaction which is not in the database
n := rpc.BlockNumber(6)
results, err := api.ReplayBlockTransactions(context.Background(), rpc.BlockNumberOrHash{BlockNumber: &n}, []string{"stateDiff"})
if err != nil {
t.Errorf("calling ReplayBlockTransactions: %v", err)
}
require.NotNil(t, results)
require.NotNil(t, results[0].StateDiff)
addrDiff := results[0].StateDiff[common.HexToAddress("0x0000000000000001000000000000000000000000")]
v := addrDiff.Balance.(map[string]*hexutil.Big)["+"].ToInt().Uint64()
require.Equal(t, uint64(1_000_000_000_000_000), v)
}