2021-02-21 20:18:59 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
2021-07-29 11:53:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2021-09-29 01:36:25 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv/kvcache"
|
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-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
2021-06-26 12:27:29 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common/hexutil"
|
|
|
|
"github.com/ledgerwatch/erigon/core/rawdb"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/rpc"
|
2021-11-14 04:08:52 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/snapshotsync"
|
2021-06-26 12:27:29 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-02-21 20:18:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEmptyQuery(t *testing.T) {
|
2021-06-29 10:00:22 +00:00
|
|
|
db := rpcdaemontest.CreateTestKV(t)
|
2021-09-29 01:36:25 +00:00
|
|
|
stateCache := kvcache.New(kvcache.DefaultCoherentConfig)
|
2022-08-12 09:13:14 +00:00
|
|
|
api := NewTraceAPI(NewBaseApi(nil, stateCache, snapshotsync.NewBlockReader(), nil, nil, false), 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) {
|
2021-06-29 10:00:22 +00:00
|
|
|
db := rpcdaemontest.CreateTestKV(t)
|
2021-09-29 01:36:25 +00:00
|
|
|
stateCache := kvcache.New(kvcache.DefaultCoherentConfig)
|
2022-08-12 09:13:14 +00:00
|
|
|
api := NewTraceAPI(NewBaseApi(nil, stateCache, snapshotsync.NewBlockReader(), nil, nil, false), 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)
|
|
|
|
if _, ok := results[1].StateDiff[common.Address{}]; !ok {
|
|
|
|
t.Errorf("expected balance increase for coinbase (zero address)")
|
|
|
|
}
|
2021-06-26 12:27:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReplayTransaction(t *testing.T) {
|
2021-06-29 10:00:22 +00:00
|
|
|
db := rpcdaemontest.CreateTestKV(t)
|
2021-09-29 01:36:25 +00:00
|
|
|
stateCache := kvcache.New(kvcache.DefaultCoherentConfig)
|
2022-08-12 09:13:14 +00:00
|
|
|
api := NewTraceAPI(NewBaseApi(nil, stateCache, snapshotsync.NewBlockReader(), nil, nil, false), db, &httpcfg.HttpCfg{})
|
2021-06-26 12:27:29 +00:00
|
|
|
var txnHash common.Hash
|
2021-07-28 02:47:38 +00:00
|
|
|
if err := db.View(context.Background(), func(tx kv.Tx) error {
|
2021-06-26 12:27:29 +00:00
|
|
|
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 {
|
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)
|
|
|
|
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) {
|
2021-06-29 10:00:22 +00:00
|
|
|
db := rpcdaemontest.CreateTestKV(t)
|
2021-09-29 01:36:25 +00:00
|
|
|
stateCache := kvcache.New(kvcache.DefaultCoherentConfig)
|
2022-08-12 09:13:14 +00:00
|
|
|
api := NewTraceAPI(NewBaseApi(nil, stateCache, snapshotsync.NewBlockReader(), nil, nil, false), 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)
|
|
|
|
results, err := api.ReplayBlockTransactions(context.Background(), rpc.BlockNumberOrHash{BlockNumber: &n}, []string{"stateDiff"})
|
|
|
|
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)
|
2021-07-21 18:34:56 +00:00
|
|
|
addrDiff := results[0].StateDiff[common.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
|
|
|
}
|