2022-07-01 11:12:01 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-18 10:41:01 +00:00
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
2022-07-01 11:12:01 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/gointerfaces/txpool"
|
|
|
|
"github.com/ledgerwatch/erigon-lib/kv/kvcache"
|
|
|
|
"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/core/types"
|
|
|
|
"github.com/ledgerwatch/erigon/rlp"
|
|
|
|
"github.com/ledgerwatch/erigon/rpc"
|
2022-09-17 06:25:27 +00:00
|
|
|
"github.com/ledgerwatch/erigon/rpc/rpccfg"
|
2022-07-01 11:12:01 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/rpchelper"
|
|
|
|
"github.com/ledgerwatch/erigon/turbo/snapshotsync"
|
|
|
|
"github.com/ledgerwatch/erigon/turbo/stages"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Gets the latest block number with the latest tag
|
|
|
|
func TestGetBlockByNumberWithLatestTag(t *testing.T) {
|
2022-09-18 10:41:01 +00:00
|
|
|
m, _, _ := rpcdaemontest.CreateTestSentry(t)
|
2022-09-26 03:54:42 +00:00
|
|
|
agg := m.HistoryV3Components()
|
2022-10-18 04:53:54 +00:00
|
|
|
br := snapshotsync.NewBlockReaderWithSnapshots(m.BlockSnapshots)
|
2022-07-01 11:12:01 +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
|
|
|
api := NewEthAPI(NewBaseApi(nil, stateCache, br, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine), m.DB, nil, nil, nil, 5000000)
|
2022-07-01 11:12:01 +00:00
|
|
|
b, err := api.GetBlockByNumber(context.Background(), rpc.LatestBlockNumber, false)
|
|
|
|
expected := common.HexToHash("0x6804117de2f3e6ee32953e78ced1db7b20214e0d8c745a03b8fecf7cc8ee76ef")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error getting block number with latest tag: %s", err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, expected, b["hash"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBlockByNumberWithLatestTag_WithHeadHashInDb(t *testing.T) {
|
2022-09-18 10:41:01 +00:00
|
|
|
m, _, _ := rpcdaemontest.CreateTestSentry(t)
|
2022-09-26 03:54:42 +00:00
|
|
|
agg := m.HistoryV3Components()
|
2022-10-18 04:53:54 +00:00
|
|
|
br := snapshotsync.NewBlockReaderWithSnapshots(m.BlockSnapshots)
|
2022-07-01 11:12:01 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
stateCache := kvcache.New(kvcache.DefaultCoherentConfig)
|
2022-09-18 10:41:01 +00:00
|
|
|
tx, err := m.DB.BeginRw(ctx)
|
2022-07-01 11:12:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not begin read write transaction: %s", err)
|
|
|
|
}
|
|
|
|
latestBlockHash := common.HexToHash("0x6804117de2f3e6ee32953e78ced1db7b20214e0d8c745a03b8fecf7cc8ee76ef")
|
|
|
|
latestBlock, err := rawdb.ReadBlockByHash(tx, latestBlockHash)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
t.Errorf("couldn't retrieve latest block")
|
|
|
|
}
|
|
|
|
rawdb.WriteHeaderNumber(tx, latestBlockHash, latestBlock.NonceU64())
|
|
|
|
rawdb.WriteForkchoiceHead(tx, latestBlockHash)
|
|
|
|
if safedHeadBlock := rawdb.ReadForkchoiceHead(tx); safedHeadBlock == (common.Hash{}) {
|
|
|
|
tx.Rollback()
|
|
|
|
t.Error("didn't find forkchoice head hash")
|
|
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
|
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
|
|
|
api := NewEthAPI(NewBaseApi(nil, stateCache, br, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine), m.DB, nil, nil, nil, 5000000)
|
2022-07-01 11:12:01 +00:00
|
|
|
block, err := api.GetBlockByNumber(ctx, rpc.LatestBlockNumber, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error retrieving block by number: %s", err)
|
|
|
|
}
|
|
|
|
expectedHash := common.HexToHash("0x71b89b6ca7b65debfd2fbb01e4f07de7bba343e6617559fa81df19b605f84662")
|
|
|
|
assert.Equal(t, expectedHash, block["hash"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBlockByNumberWithPendingTag(t *testing.T) {
|
|
|
|
m := stages.MockWithTxPool(t)
|
2022-09-26 03:54:42 +00:00
|
|
|
agg := m.HistoryV3Components()
|
2022-10-18 04:53:54 +00:00
|
|
|
br := snapshotsync.NewBlockReaderWithSnapshots(m.BlockSnapshots)
|
2022-07-01 11:12:01 +00:00
|
|
|
stateCache := kvcache.New(kvcache.DefaultCoherentConfig)
|
|
|
|
|
|
|
|
ctx, conn := rpcdaemontest.CreateTestGrpcConn(t, m)
|
|
|
|
txPool := txpool.NewTxpoolClient(conn)
|
|
|
|
ff := rpchelper.New(ctx, nil, txPool, txpool.NewMiningClient(conn), func() {})
|
|
|
|
|
|
|
|
expected := 1
|
|
|
|
header := &types.Header{
|
|
|
|
Number: big.NewInt(int64(expected)),
|
|
|
|
}
|
|
|
|
|
|
|
|
rlpBlock, err := rlp.EncodeToBytes(types.NewBlockWithHeader(header))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed encoding the block: %s", err)
|
|
|
|
}
|
|
|
|
ff.HandlePendingBlock(&txpool.OnPendingBlockReply{
|
|
|
|
RplBlock: rlpBlock,
|
|
|
|
})
|
|
|
|
|
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
|
|
|
api := NewEthAPI(NewBaseApi(ff, stateCache, br, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine), m.DB, nil, nil, nil, 5000000)
|
2022-07-01 11:12:01 +00:00
|
|
|
b, err := api.GetBlockByNumber(context.Background(), rpc.PendingBlockNumber, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error getting block number with pending tag: %s", err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, (*hexutil.Big)(big.NewInt(int64(expected))), b["number"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBlockByNumber_WithFinalizedTag_NoFinalizedBlockInDb(t *testing.T) {
|
2022-09-18 10:41:01 +00:00
|
|
|
m, _, _ := rpcdaemontest.CreateTestSentry(t)
|
2022-09-26 03:54:42 +00:00
|
|
|
agg := m.HistoryV3Components()
|
2022-10-18 04:53:54 +00:00
|
|
|
br := snapshotsync.NewBlockReaderWithSnapshots(m.BlockSnapshots)
|
2022-07-01 11:12:01 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
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
|
|
|
api := NewEthAPI(NewBaseApi(nil, stateCache, br, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine), m.DB, nil, nil, nil, 5000000)
|
2022-07-01 11:12:01 +00:00
|
|
|
if _, err := api.GetBlockByNumber(ctx, rpc.FinalizedBlockNumber, false); err != nil {
|
|
|
|
assert.ErrorIs(t, rpchelper.UnknownBlockError, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBlockByNumber_WithFinalizedTag_WithFinalizedBlockInDb(t *testing.T) {
|
2022-09-18 10:41:01 +00:00
|
|
|
m, _, _ := rpcdaemontest.CreateTestSentry(t)
|
2022-09-26 03:54:42 +00:00
|
|
|
agg := m.HistoryV3Components()
|
2022-10-18 04:53:54 +00:00
|
|
|
br := snapshotsync.NewBlockReaderWithSnapshots(m.BlockSnapshots)
|
2022-07-01 11:12:01 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
stateCache := kvcache.New(kvcache.DefaultCoherentConfig)
|
2022-09-18 10:41:01 +00:00
|
|
|
tx, err := m.DB.BeginRw(ctx)
|
2022-07-01 11:12:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not begin read write transaction: %s", err)
|
|
|
|
}
|
|
|
|
latestBlockHash := common.HexToHash("0x6804117de2f3e6ee32953e78ced1db7b20214e0d8c745a03b8fecf7cc8ee76ef")
|
|
|
|
latestBlock, err := rawdb.ReadBlockByHash(tx, latestBlockHash)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
t.Errorf("couldn't retrieve latest block")
|
|
|
|
}
|
|
|
|
rawdb.WriteHeaderNumber(tx, latestBlockHash, latestBlock.NonceU64())
|
|
|
|
rawdb.WriteForkchoiceFinalized(tx, latestBlockHash)
|
|
|
|
if safedFinalizedBlock := rawdb.ReadForkchoiceFinalized(tx); safedFinalizedBlock == (common.Hash{}) {
|
|
|
|
tx.Rollback()
|
|
|
|
t.Error("didn't find forkchoice finalized hash")
|
|
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
|
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
|
|
|
api := NewEthAPI(NewBaseApi(nil, stateCache, br, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine), m.DB, nil, nil, nil, 5000000)
|
2022-07-01 11:12:01 +00:00
|
|
|
block, err := api.GetBlockByNumber(ctx, rpc.FinalizedBlockNumber, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error retrieving block by number: %s", err)
|
|
|
|
}
|
|
|
|
expectedHash := common.HexToHash("0x71b89b6ca7b65debfd2fbb01e4f07de7bba343e6617559fa81df19b605f84662")
|
|
|
|
assert.Equal(t, expectedHash, block["hash"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBlockByNumber_WithSafeTag_NoSafeBlockInDb(t *testing.T) {
|
2022-09-18 10:41:01 +00:00
|
|
|
m, _, _ := rpcdaemontest.CreateTestSentry(t)
|
2022-09-26 03:54:42 +00:00
|
|
|
agg := m.HistoryV3Components()
|
2022-10-18 04:53:54 +00:00
|
|
|
br := snapshotsync.NewBlockReaderWithSnapshots(m.BlockSnapshots)
|
2022-07-01 11:12:01 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
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
|
|
|
api := NewEthAPI(NewBaseApi(nil, stateCache, br, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine), m.DB, nil, nil, nil, 5000000)
|
2022-07-01 11:12:01 +00:00
|
|
|
if _, err := api.GetBlockByNumber(ctx, rpc.SafeBlockNumber, false); err != nil {
|
|
|
|
assert.ErrorIs(t, rpchelper.UnknownBlockError, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBlockByNumber_WithSafeTag_WithSafeBlockInDb(t *testing.T) {
|
2022-09-18 10:41:01 +00:00
|
|
|
m, _, _ := rpcdaemontest.CreateTestSentry(t)
|
2022-09-26 03:54:42 +00:00
|
|
|
agg := m.HistoryV3Components()
|
2022-10-18 04:53:54 +00:00
|
|
|
br := snapshotsync.NewBlockReaderWithSnapshots(m.BlockSnapshots)
|
2022-07-01 11:12:01 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
stateCache := kvcache.New(kvcache.DefaultCoherentConfig)
|
2022-09-18 10:41:01 +00:00
|
|
|
tx, err := m.DB.BeginRw(ctx)
|
2022-07-01 11:12:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not begin read write transaction: %s", err)
|
|
|
|
}
|
|
|
|
latestBlockHash := common.HexToHash("0x6804117de2f3e6ee32953e78ced1db7b20214e0d8c745a03b8fecf7cc8ee76ef")
|
|
|
|
latestBlock, err := rawdb.ReadBlockByHash(tx, latestBlockHash)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
t.Errorf("couldn't retrieve latest block")
|
|
|
|
}
|
|
|
|
rawdb.WriteHeaderNumber(tx, latestBlockHash, latestBlock.NonceU64())
|
|
|
|
rawdb.WriteForkchoiceSafe(tx, latestBlockHash)
|
|
|
|
if safedSafeBlock := rawdb.ReadForkchoiceSafe(tx); safedSafeBlock == (common.Hash{}) {
|
|
|
|
tx.Rollback()
|
|
|
|
t.Error("didn't find forkchoice safe block hash")
|
|
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
|
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
|
|
|
api := NewEthAPI(NewBaseApi(nil, stateCache, br, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine), m.DB, nil, nil, nil, 5000000)
|
2022-07-01 11:12:01 +00:00
|
|
|
block, err := api.GetBlockByNumber(ctx, rpc.SafeBlockNumber, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error retrieving block by number: %s", err)
|
|
|
|
}
|
|
|
|
expectedHash := common.HexToHash("0x71b89b6ca7b65debfd2fbb01e4f07de7bba343e6617559fa81df19b605f84662")
|
|
|
|
assert.Equal(t, expectedHash, block["hash"])
|
|
|
|
}
|
2022-08-04 11:49:53 +00:00
|
|
|
|
|
|
|
func TestGetBlockTransactionCountByHash(t *testing.T) {
|
2022-09-18 10:41:01 +00:00
|
|
|
m, _, _ := rpcdaemontest.CreateTestSentry(t)
|
2022-09-26 03:54:42 +00:00
|
|
|
agg := m.HistoryV3Components()
|
2022-10-18 04:53:54 +00:00
|
|
|
br := snapshotsync.NewBlockReaderWithSnapshots(m.BlockSnapshots)
|
2022-08-04 11:49:53 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
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
|
|
|
api := NewEthAPI(NewBaseApi(nil, stateCache, br, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine), m.DB, nil, nil, nil, 5000000)
|
2022-08-04 11:49:53 +00:00
|
|
|
blockHash := common.HexToHash("0x6804117de2f3e6ee32953e78ced1db7b20214e0d8c745a03b8fecf7cc8ee76ef")
|
|
|
|
|
2022-09-18 10:41:01 +00:00
|
|
|
tx, err := m.DB.BeginRw(ctx)
|
2022-08-04 11:49:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not begin read write transaction: %s", err)
|
|
|
|
}
|
|
|
|
header, err := rawdb.ReadHeaderByHash(tx, blockHash)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
t.Errorf("failed reading block by hash: %s", err)
|
|
|
|
}
|
|
|
|
bodyWithTx, err := rawdb.ReadBodyWithTransactions(tx, blockHash, header.Number.Uint64())
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
t.Errorf("failed getting body with transactions: %s", err)
|
|
|
|
}
|
|
|
|
tx.Rollback()
|
|
|
|
|
|
|
|
expectedAmount := hexutil.Uint(len(bodyWithTx.Transactions))
|
|
|
|
|
|
|
|
txAmount, err := api.GetBlockTransactionCountByHash(ctx, blockHash)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed getting the transaction count, err=%s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expectedAmount, *txAmount)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBlockTransactionCountByNumber(t *testing.T) {
|
2022-09-18 10:41:01 +00:00
|
|
|
m, _, _ := rpcdaemontest.CreateTestSentry(t)
|
2022-09-26 03:54:42 +00:00
|
|
|
agg := m.HistoryV3Components()
|
2022-10-18 04:53:54 +00:00
|
|
|
br := snapshotsync.NewBlockReaderWithSnapshots(m.BlockSnapshots)
|
2022-08-04 11:49:53 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
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
|
|
|
api := NewEthAPI(NewBaseApi(nil, stateCache, br, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine), m.DB, nil, nil, nil, 5000000)
|
2022-08-04 11:49:53 +00:00
|
|
|
blockHash := common.HexToHash("0x6804117de2f3e6ee32953e78ced1db7b20214e0d8c745a03b8fecf7cc8ee76ef")
|
|
|
|
|
2022-09-18 10:41:01 +00:00
|
|
|
tx, err := m.DB.BeginRw(ctx)
|
2022-08-04 11:49:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not begin read write transaction: %s", err)
|
|
|
|
}
|
|
|
|
header, err := rawdb.ReadHeaderByHash(tx, blockHash)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
t.Errorf("failed reading block by hash: %s", err)
|
|
|
|
}
|
|
|
|
bodyWithTx, err := rawdb.ReadBodyWithTransactions(tx, blockHash, header.Number.Uint64())
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
t.Errorf("failed getting body with transactions: %s", err)
|
|
|
|
}
|
|
|
|
tx.Rollback()
|
|
|
|
|
|
|
|
expectedAmount := hexutil.Uint(len(bodyWithTx.Transactions))
|
|
|
|
|
|
|
|
txAmount, err := api.GetBlockTransactionCountByNumber(ctx, rpc.BlockNumber(header.Number.Uint64()))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed getting the transaction count, err=%s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expectedAmount, *txAmount)
|
|
|
|
}
|