2021-05-17 12:15:19 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
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 05:17:39 +00:00
|
|
|
"github.com/ledgerwatch/erigon/consensus/ethash"
|
2022-09-18 10:41:01 +00:00
|
|
|
"github.com/ledgerwatch/erigon/rpc/rpccfg"
|
|
|
|
|
2021-07-01 21:31:14 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/gointerfaces/txpool"
|
2021-09-29 01:36:25 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv/kvcache"
|
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/core/types"
|
|
|
|
"github.com/ledgerwatch/erigon/rlp"
|
2022-06-10 15:18:43 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/rpchelper"
|
2021-11-14 04:08:52 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/snapshotsync"
|
2021-06-27 06:41:21 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/stages"
|
2021-05-17 12:15:19 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPendingBlock(t *testing.T) {
|
2021-06-29 10:00:22 +00:00
|
|
|
ctx, conn := rpcdaemontest.CreateTestGrpcConn(t, stages.Mock(t))
|
2021-05-17 12:15:19 +00:00
|
|
|
mining := txpool.NewMiningClient(conn)
|
2022-06-10 15:18:43 +00:00
|
|
|
ff := rpchelper.New(ctx, nil, nil, mining, func() {})
|
2021-09-29 01:36:25 +00:00
|
|
|
stateCache := kvcache.New(kvcache.DefaultCoherentConfig)
|
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 05:17:39 +00:00
|
|
|
engine := ethash.NewFaker()
|
|
|
|
api := NewEthAPI(NewBaseApi(ff, stateCache, snapshotsync.NewBlockReader(), nil, false, rpccfg.DefaultEvmCallTimeout, engine), nil, nil, nil, mining, 5000000)
|
2021-05-17 12:15:19 +00:00
|
|
|
expect := uint64(12345)
|
|
|
|
b, err := rlp.EncodeToBytes(types.NewBlockWithHeader(&types.Header{Number: big.NewInt(int64(expect))}))
|
|
|
|
require.NoError(t, err)
|
|
|
|
ch := make(chan *types.Block, 1)
|
2021-06-29 10:00:22 +00:00
|
|
|
id := ff.SubscribePendingBlock(ch)
|
|
|
|
defer ff.UnsubscribePendingBlock(id)
|
2021-05-17 12:15:19 +00:00
|
|
|
|
2021-06-29 10:00:22 +00:00
|
|
|
ff.HandlePendingBlock(&txpool.OnPendingBlockReply{RplBlock: b})
|
2021-05-17 12:15:19 +00:00
|
|
|
block := api.pendingBlock()
|
|
|
|
|
2021-11-29 11:32:41 +00:00
|
|
|
require.Equal(t, block.NumberU64(), expect)
|
2021-05-17 12:15:19 +00:00
|
|
|
select {
|
|
|
|
case got := <-ch:
|
2021-11-29 11:32:41 +00:00
|
|
|
require.Equal(t, expect, got.NumberU64())
|
2021-05-17 12:15:19 +00:00
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
t.Fatalf("timeout waiting for expected notification")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPendingLogs(t *testing.T) {
|
2021-06-29 10:00:22 +00:00
|
|
|
ctx, conn := rpcdaemontest.CreateTestGrpcConn(t, stages.Mock(t))
|
2021-05-17 12:15:19 +00:00
|
|
|
mining := txpool.NewMiningClient(conn)
|
2022-06-10 15:18:43 +00:00
|
|
|
ff := rpchelper.New(ctx, nil, nil, mining, func() {})
|
2021-05-17 12:15:19 +00:00
|
|
|
expect := []byte{211}
|
|
|
|
|
|
|
|
ch := make(chan types.Logs, 1)
|
|
|
|
defer close(ch)
|
2021-06-29 10:00:22 +00:00
|
|
|
id := ff.SubscribePendingLogs(ch)
|
|
|
|
defer ff.UnsubscribePendingLogs(id)
|
2021-05-17 12:15:19 +00:00
|
|
|
|
|
|
|
b, err := rlp.EncodeToBytes([]*types.Log{{Data: expect}})
|
|
|
|
require.NoError(t, err)
|
2021-06-29 10:00:22 +00:00
|
|
|
ff.HandlePendingLogs(&txpool.OnPendingLogsReply{RplLogs: b})
|
2021-05-17 12:15:19 +00:00
|
|
|
select {
|
|
|
|
case logs := <-ch:
|
|
|
|
require.Equal(t, expect, logs[0].Data)
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
t.Fatalf("timeout waiting for expected notification")
|
|
|
|
}
|
|
|
|
}
|