Re-enable non-EIP155 unprotected txns (#8400)

Refer to linked issue https://github.com/ledgerwatch/erigon/issues/8381
This commit is contained in:
Somnath 2023-10-08 06:48:14 +05:30 committed by GitHub
parent f7adc2374d
commit 66fe74dd12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 154 additions and 106 deletions

View File

@ -126,7 +126,7 @@ func RootCommand() (*cobra.Command, *httpcfg.HttpCfg) {
rootCmd.PersistentFlags().DurationVar(&cfg.EvmCallTimeout, "rpc.evmtimeout", rpccfg.DefaultEvmCallTimeout, "Maximum amount of time to wait for the answer from EVM call.") rootCmd.PersistentFlags().DurationVar(&cfg.EvmCallTimeout, "rpc.evmtimeout", rpccfg.DefaultEvmCallTimeout, "Maximum amount of time to wait for the answer from EVM call.")
rootCmd.PersistentFlags().IntVar(&cfg.BatchLimit, utils.RpcBatchLimit.Name, utils.RpcBatchLimit.Value, utils.RpcBatchLimit.Usage) rootCmd.PersistentFlags().IntVar(&cfg.BatchLimit, utils.RpcBatchLimit.Name, utils.RpcBatchLimit.Value, utils.RpcBatchLimit.Usage)
rootCmd.PersistentFlags().IntVar(&cfg.ReturnDataLimit, utils.RpcReturnDataLimit.Name, utils.RpcReturnDataLimit.Value, utils.RpcReturnDataLimit.Usage) rootCmd.PersistentFlags().IntVar(&cfg.ReturnDataLimit, utils.RpcReturnDataLimit.Name, utils.RpcReturnDataLimit.Value, utils.RpcReturnDataLimit.Usage)
rootCmd.PersistentFlags().BoolVar(&cfg.AllowUnprotectedTxs, utils.AllowUnprotectedTxs.Name, utils.AllowUnprotectedTxs.Value, utils.AllowUnprotectedTxs.Usage)
rootCmd.PersistentFlags().Uint64Var(&cfg.OtsMaxPageSize, utils.OtsSearchMaxCapFlag.Name, utils.OtsSearchMaxCapFlag.Value, utils.OtsSearchMaxCapFlag.Usage) rootCmd.PersistentFlags().Uint64Var(&cfg.OtsMaxPageSize, utils.OtsSearchMaxCapFlag.Name, utils.OtsSearchMaxCapFlag.Value, utils.OtsSearchMaxCapFlag.Usage)
if err := rootCmd.MarkPersistentFlagFilename("rpc.accessList", "json"); err != nil { if err := rootCmd.MarkPersistentFlagFilename("rpc.accessList", "json"); err != nil {

View File

@ -61,8 +61,9 @@ type HttpCfg struct {
LogDirVerbosity string LogDirVerbosity string
LogDirPath string LogDirPath string
BatchLimit int // Maximum number of requests in a batch BatchLimit int // Maximum number of requests in a batch
ReturnDataLimit int // Maximum number of bytes returned from calls (like eth_call) ReturnDataLimit int // Maximum number of bytes returned from calls (like eth_call)
AllowUnprotectedTxs bool // Whether to allow non EIP-155 protected transactions txs over RPC
// Ots API // Ots API
OtsMaxPageSize uint64 OtsMaxPageSize uint64

View File

@ -478,7 +478,7 @@ var (
} }
AllowUnprotectedTxs = cli.BoolFlag{ AllowUnprotectedTxs = cli.BoolFlag{
Name: "rpc.allow-unprotected-txs", Name: "rpc.allow-unprotected-txs",
Usage: "Allow for unprotected (non EIP155 signed) transactions to be submitted via RPC", Usage: "Allow for unprotected (non-EIP155 signed) transactions to be submitted via RPC",
} }
StateCacheFlag = cli.StringFlag{ StateCacheFlag = cli.StringFlag{
Name: "state.cache", Name: "state.cache",

View File

@ -155,10 +155,9 @@ type Config struct {
TLSConnection bool TLSConnection bool
TLSCertFile string TLSCertFile string
// AllowUnprotectedTxs allows non EIP-155 protected transactions to be send over RPC.
AllowUnprotectedTxs bool `toml:",omitempty"` TLSKeyFile string
TLSKeyFile string TLSCACert string
TLSCACert string
MdbxPageSize datasize.ByteSize MdbxPageSize datasize.ByteSize
MdbxDBSizeLimit datasize.ByteSize MdbxDBSizeLimit datasize.ByteSize

View File

@ -74,6 +74,7 @@ var DefaultFlags = []cli.Flag{
&utils.RpcGasCapFlag, &utils.RpcGasCapFlag,
&utils.RpcBatchLimit, &utils.RpcBatchLimit,
&utils.RpcReturnDataLimit, &utils.RpcReturnDataLimit,
&utils.AllowUnprotectedTxs,
&utils.RPCGlobalTxFeeCapFlag, &utils.RPCGlobalTxFeeCapFlag,
&utils.TxpoolApiAddrFlag, &utils.TxpoolApiAddrFlag,
&utils.TraceMaxtracesFlag, &utils.TraceMaxtracesFlag,

View File

@ -396,6 +396,7 @@ func setEmbeddedRpcDaemon(ctx *cli.Context, cfg *nodecfg.Config, logger log.Logg
TraceCompatibility: ctx.Bool(utils.RpcTraceCompatFlag.Name), TraceCompatibility: ctx.Bool(utils.RpcTraceCompatFlag.Name),
BatchLimit: ctx.Int(utils.RpcBatchLimit.Name), BatchLimit: ctx.Int(utils.RpcBatchLimit.Name),
ReturnDataLimit: ctx.Int(utils.RpcReturnDataLimit.Name), ReturnDataLimit: ctx.Int(utils.RpcReturnDataLimit.Name),
AllowUnprotectedTxs: ctx.Bool(utils.AllowUnprotectedTxs.Name),
OtsMaxPageSize: ctx.Uint64(utils.OtsSearchMaxCapFlag.Name), OtsMaxPageSize: ctx.Uint64(utils.OtsSearchMaxCapFlag.Name),

View File

@ -81,7 +81,7 @@ func (e *EngineServer) Start(httpConfig httpcfg.HttpCfg, db kv.RoDB, blockReader
eth rpchelper.ApiBackend, txPool txpool.TxpoolClient, mining txpool.MiningClient) { eth rpchelper.ApiBackend, txPool txpool.TxpoolClient, mining txpool.MiningClient) {
base := jsonrpc.NewBaseApi(filters, stateCache, blockReader, agg, httpConfig.WithDatadir, httpConfig.EvmCallTimeout, engineReader, httpConfig.Dirs) base := jsonrpc.NewBaseApi(filters, stateCache, blockReader, agg, httpConfig.WithDatadir, httpConfig.EvmCallTimeout, engineReader, httpConfig.Dirs)
ethImpl := jsonrpc.NewEthAPI(base, db, eth, txPool, mining, httpConfig.Gascap, httpConfig.ReturnDataLimit, e.logger) ethImpl := jsonrpc.NewEthAPI(base, db, eth, txPool, mining, httpConfig.Gascap, httpConfig.ReturnDataLimit, httpConfig.AllowUnprotectedTxs, e.logger)
// engineImpl := NewEngineAPI(base, db, engineBackend) // engineImpl := NewEngineAPI(base, db, engineBackend)
// e.startEngineMessageHandler() // e.startEngineMessageHandler()

View File

@ -18,7 +18,7 @@ func TestNotFoundMustReturnNil(t *testing.T) {
require := require.New(t) require := require.New(t)
m, _, _ := rpcdaemontest.CreateTestSentry(t) m, _, _ := rpcdaemontest.CreateTestSentry(t)
api := NewEthAPI(newBaseApiForTest(m), api := NewEthAPI(newBaseApiForTest(m),
m.DB, nil, nil, nil, 5000000, 100_000, log.New()) m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
ctx := context.Background() ctx := context.Background()
a, err := api.GetTransactionByBlockNumberAndIndex(ctx, 10_000, 1) a, err := api.GetTransactionByBlockNumberAndIndex(ctx, 10_000, 1)

View File

@ -22,7 +22,7 @@ func APIList(db kv.RoDB, eth rpchelper.ApiBackend, txPool txpool.TxpoolClient, m
logger log.Logger, logger log.Logger,
) (list []rpc.API) { ) (list []rpc.API) {
base := NewBaseApi(filters, stateCache, blockReader, agg, cfg.WithDatadir, cfg.EvmCallTimeout, engine, cfg.Dirs) base := NewBaseApi(filters, stateCache, blockReader, agg, cfg.WithDatadir, cfg.EvmCallTimeout, engine, cfg.Dirs)
ethImpl := NewEthAPI(base, db, eth, txPool, mining, cfg.Gascap, cfg.ReturnDataLimit, logger) ethImpl := NewEthAPI(base, db, eth, txPool, mining, cfg.Gascap, cfg.ReturnDataLimit, cfg.AllowUnprotectedTxs, logger)
erigonImpl := NewErigonAPI(base, db, eth) erigonImpl := NewErigonAPI(base, db, eth)
txpoolImpl := NewTxPoolAPI(base, db, txPool) txpoolImpl := NewTxPoolAPI(base, db, txPool)
netImpl := NewNetAPIImpl(eth) netImpl := NewNetAPIImpl(eth)

View File

@ -53,7 +53,7 @@ func TestTraceBlockByNumber(t *testing.T) {
agg := m.HistoryV3Components() agg := m.HistoryV3Components()
stateCache := kvcache.New(kvcache.DefaultCoherentConfig) stateCache := kvcache.New(kvcache.DefaultCoherentConfig)
baseApi := NewBaseApi(nil, stateCache, m.BlockReader, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine, m.Dirs) baseApi := NewBaseApi(nil, stateCache, m.BlockReader, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine, m.Dirs)
ethApi := NewEthAPI(baseApi, m.DB, nil, nil, nil, 5000000, 100_000, log.New()) ethApi := NewEthAPI(baseApi, m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
api := NewPrivateDebugAPI(baseApi, m.DB, 0) api := NewPrivateDebugAPI(baseApi, m.DB, 0)
for _, tt := range debugTraceTransactionTests { for _, tt := range debugTraceTransactionTests {
var buf bytes.Buffer var buf bytes.Buffer
@ -98,7 +98,7 @@ func TestTraceBlockByNumber(t *testing.T) {
func TestTraceBlockByHash(t *testing.T) { func TestTraceBlockByHash(t *testing.T) {
m, _, _ := rpcdaemontest.CreateTestSentry(t) m, _, _ := rpcdaemontest.CreateTestSentry(t)
ethApi := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) ethApi := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
api := NewPrivateDebugAPI(newBaseApiForTest(m), m.DB, 0) api := NewPrivateDebugAPI(newBaseApiForTest(m), m.DB, 0)
for _, tt := range debugTraceTransactionTests { for _, tt := range debugTraceTransactionTests {
var buf bytes.Buffer var buf bytes.Buffer

View File

@ -29,7 +29,7 @@ func TestGetLogs(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
m, _, _ := rpcdaemontest.CreateTestSentry(t) m, _, _ := rpcdaemontest.CreateTestSentry(t)
{ {
ethApi := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) ethApi := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
logs, err := ethApi.GetLogs(context.Background(), filters.FilterCriteria{FromBlock: big.NewInt(0), ToBlock: big.NewInt(10)}) logs, err := ethApi.GetLogs(context.Background(), filters.FilterCriteria{FromBlock: big.NewInt(0), ToBlock: big.NewInt(10)})
assert.NoError(err) assert.NoError(err)

View File

@ -318,32 +318,34 @@ func (api *BaseAPI) pruneMode(tx kv.Tx) (*prune.Mode, error) {
// APIImpl is implementation of the EthAPI interface based on remote Db access // APIImpl is implementation of the EthAPI interface based on remote Db access
type APIImpl struct { type APIImpl struct {
*BaseAPI *BaseAPI
ethBackend rpchelper.ApiBackend ethBackend rpchelper.ApiBackend
txPool txpool.TxpoolClient txPool txpool.TxpoolClient
mining txpool.MiningClient mining txpool.MiningClient
gasCache *GasPriceCache gasCache *GasPriceCache
db kv.RoDB db kv.RoDB
GasCap uint64 GasCap uint64
ReturnDataLimit int ReturnDataLimit int
logger log.Logger AllowUnprotectedTxs bool
logger log.Logger
} }
// NewEthAPI returns APIImpl instance // NewEthAPI returns APIImpl instance
func NewEthAPI(base *BaseAPI, db kv.RoDB, eth rpchelper.ApiBackend, txPool txpool.TxpoolClient, mining txpool.MiningClient, gascap uint64, returnDataLimit int, logger log.Logger) *APIImpl { func NewEthAPI(base *BaseAPI, db kv.RoDB, eth rpchelper.ApiBackend, txPool txpool.TxpoolClient, mining txpool.MiningClient, gascap uint64, returnDataLimit int, allowUnprotectedTxs bool, logger log.Logger) *APIImpl {
if gascap == 0 { if gascap == 0 {
gascap = uint64(math.MaxUint64 / 2) gascap = uint64(math.MaxUint64 / 2)
} }
return &APIImpl{ return &APIImpl{
BaseAPI: base, BaseAPI: base,
db: db, db: db,
ethBackend: eth, ethBackend: eth,
txPool: txPool, txPool: txPool,
mining: mining, mining: mining,
gasCache: NewGasPriceCache(), gasCache: NewGasPriceCache(),
GasCap: gascap, GasCap: gascap,
ReturnDataLimit: returnDataLimit, AllowUnprotectedTxs: allowUnprotectedTxs,
logger: logger, ReturnDataLimit: returnDataLimit,
logger: logger,
} }
} }

View File

@ -54,7 +54,7 @@ func TestGetTransactionReceipt(t *testing.T) {
db := m.DB db := m.DB
agg := m.HistoryV3Components() agg := m.HistoryV3Components()
stateCache := kvcache.New(kvcache.DefaultCoherentConfig) stateCache := kvcache.New(kvcache.DefaultCoherentConfig)
api := NewEthAPI(NewBaseApi(nil, stateCache, m.BlockReader, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine, m.Dirs), db, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(NewBaseApi(nil, stateCache, m.BlockReader, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine, m.Dirs), db, nil, nil, nil, 5000000, 100_000, false, log.New())
// Call GetTransactionReceipt for transaction which is not in the database // Call GetTransactionReceipt for transaction which is not in the database
if _, err := api.GetTransactionReceipt(context.Background(), common.Hash{}); err != nil { if _, err := api.GetTransactionReceipt(context.Background(), common.Hash{}); err != nil {
t.Errorf("calling GetTransactionReceipt with empty hash: %v", err) t.Errorf("calling GetTransactionReceipt with empty hash: %v", err)
@ -63,7 +63,7 @@ func TestGetTransactionReceipt(t *testing.T) {
func TestGetTransactionReceiptUnprotected(t *testing.T) { func TestGetTransactionReceiptUnprotected(t *testing.T) {
m, _, _ := rpcdaemontest.CreateTestSentry(t) m, _, _ := rpcdaemontest.CreateTestSentry(t)
api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
// Call GetTransactionReceipt for un-protected transaction // Call GetTransactionReceipt for un-protected transaction
if _, err := api.GetTransactionReceipt(context.Background(), common.HexToHash("0x3f3cb8a0e13ed2481f97f53f7095b9cbc78b6ffb779f2d3e565146371a8830ea")); err != nil { if _, err := api.GetTransactionReceipt(context.Background(), common.HexToHash("0x3f3cb8a0e13ed2481f97f53f7095b9cbc78b6ffb779f2d3e565146371a8830ea")); err != nil {
t.Errorf("calling GetTransactionReceipt for unprotected tx: %v", err) t.Errorf("calling GetTransactionReceipt for unprotected tx: %v", err)
@ -75,7 +75,7 @@ func TestGetTransactionReceiptUnprotected(t *testing.T) {
func TestGetStorageAt_ByBlockNumber_WithRequireCanonicalDefault(t *testing.T) { func TestGetStorageAt_ByBlockNumber_WithRequireCanonicalDefault(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
m, _, _ := rpcdaemontest.CreateTestSentry(t) m, _, _ := rpcdaemontest.CreateTestSentry(t)
api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
addr := common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7") addr := common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7")
result, err := api.GetStorageAt(context.Background(), addr, "0x0", rpc.BlockNumberOrHashWithNumber(0)) result, err := api.GetStorageAt(context.Background(), addr, "0x0", rpc.BlockNumberOrHashWithNumber(0))
@ -89,7 +89,7 @@ func TestGetStorageAt_ByBlockNumber_WithRequireCanonicalDefault(t *testing.T) {
func TestGetStorageAt_ByBlockHash_WithRequireCanonicalDefault(t *testing.T) { func TestGetStorageAt_ByBlockHash_WithRequireCanonicalDefault(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
m, _, _ := rpcdaemontest.CreateTestSentry(t) m, _, _ := rpcdaemontest.CreateTestSentry(t)
api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
addr := common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7") addr := common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7")
result, err := api.GetStorageAt(context.Background(), addr, "0x0", rpc.BlockNumberOrHashWithHash(m.Genesis.Hash(), false)) result, err := api.GetStorageAt(context.Background(), addr, "0x0", rpc.BlockNumberOrHashWithHash(m.Genesis.Hash(), false))
@ -103,7 +103,7 @@ func TestGetStorageAt_ByBlockHash_WithRequireCanonicalDefault(t *testing.T) {
func TestGetStorageAt_ByBlockHash_WithRequireCanonicalTrue(t *testing.T) { func TestGetStorageAt_ByBlockHash_WithRequireCanonicalTrue(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
m, _, _ := rpcdaemontest.CreateTestSentry(t) m, _, _ := rpcdaemontest.CreateTestSentry(t)
api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
addr := common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7") addr := common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7")
result, err := api.GetStorageAt(context.Background(), addr, "0x0", rpc.BlockNumberOrHashWithHash(m.Genesis.Hash(), true)) result, err := api.GetStorageAt(context.Background(), addr, "0x0", rpc.BlockNumberOrHashWithHash(m.Genesis.Hash(), true))
@ -116,7 +116,7 @@ func TestGetStorageAt_ByBlockHash_WithRequireCanonicalTrue(t *testing.T) {
func TestGetStorageAt_ByBlockHash_WithRequireCanonicalDefault_BlockNotFoundError(t *testing.T) { func TestGetStorageAt_ByBlockHash_WithRequireCanonicalDefault_BlockNotFoundError(t *testing.T) {
m, _, _ := rpcdaemontest.CreateTestSentry(t) m, _, _ := rpcdaemontest.CreateTestSentry(t)
api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
addr := common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7") addr := common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7")
offChain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 1, func(i int, block *core.BlockGen) { offChain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 1, func(i int, block *core.BlockGen) {
@ -137,7 +137,7 @@ func TestGetStorageAt_ByBlockHash_WithRequireCanonicalDefault_BlockNotFoundError
func TestGetStorageAt_ByBlockHash_WithRequireCanonicalTrue_BlockNotFoundError(t *testing.T) { func TestGetStorageAt_ByBlockHash_WithRequireCanonicalTrue_BlockNotFoundError(t *testing.T) {
m, _, _ := rpcdaemontest.CreateTestSentry(t) m, _, _ := rpcdaemontest.CreateTestSentry(t)
api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
addr := common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7") addr := common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7")
offChain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 1, func(i int, block *core.BlockGen) { offChain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 1, func(i int, block *core.BlockGen) {
@ -159,7 +159,7 @@ func TestGetStorageAt_ByBlockHash_WithRequireCanonicalTrue_BlockNotFoundError(t
func TestGetStorageAt_ByBlockHash_WithRequireCanonicalDefault_NonCanonicalBlock(t *testing.T) { func TestGetStorageAt_ByBlockHash_WithRequireCanonicalDefault_NonCanonicalBlock(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
m, _, orphanedChain := rpcdaemontest.CreateTestSentry(t) m, _, orphanedChain := rpcdaemontest.CreateTestSentry(t)
api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
addr := common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7") addr := common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7")
orphanedBlock := orphanedChain[0].Blocks[0] orphanedBlock := orphanedChain[0].Blocks[0]
@ -178,7 +178,7 @@ func TestGetStorageAt_ByBlockHash_WithRequireCanonicalDefault_NonCanonicalBlock(
func TestGetStorageAt_ByBlockHash_WithRequireCanonicalTrue_NonCanonicalBlock(t *testing.T) { func TestGetStorageAt_ByBlockHash_WithRequireCanonicalTrue_NonCanonicalBlock(t *testing.T) {
m, _, orphanedChain := rpcdaemontest.CreateTestSentry(t) m, _, orphanedChain := rpcdaemontest.CreateTestSentry(t)
api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
addr := common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7") addr := common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7")
orphanedBlock := orphanedChain[0].Blocks[0] orphanedBlock := orphanedChain[0].Blocks[0]
@ -194,7 +194,7 @@ func TestGetStorageAt_ByBlockHash_WithRequireCanonicalTrue_NonCanonicalBlock(t *
func TestCall_ByBlockHash_WithRequireCanonicalDefault_NonCanonicalBlock(t *testing.T) { func TestCall_ByBlockHash_WithRequireCanonicalDefault_NonCanonicalBlock(t *testing.T) {
m, _, orphanedChain := rpcdaemontest.CreateTestSentry(t) m, _, orphanedChain := rpcdaemontest.CreateTestSentry(t)
api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
from := common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7") from := common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7")
to := common.HexToAddress("0x0d3ab14bbad3d99f4203bd7a11acb94882050e7e") to := common.HexToAddress("0x0d3ab14bbad3d99f4203bd7a11acb94882050e7e")
@ -217,7 +217,7 @@ func TestCall_ByBlockHash_WithRequireCanonicalDefault_NonCanonicalBlock(t *testi
func TestCall_ByBlockHash_WithRequireCanonicalTrue_NonCanonicalBlock(t *testing.T) { func TestCall_ByBlockHash_WithRequireCanonicalTrue_NonCanonicalBlock(t *testing.T) {
m, _, orphanedChain := rpcdaemontest.CreateTestSentry(t) m, _, orphanedChain := rpcdaemontest.CreateTestSentry(t)
api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
from := common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7") from := common.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7")
to := common.HexToAddress("0x0d3ab14bbad3d99f4203bd7a11acb94882050e7e") to := common.HexToAddress("0x0d3ab14bbad3d99f4203bd7a11acb94882050e7e")

View File

@ -25,7 +25,7 @@ import (
// Gets the latest block number with the latest tag // Gets the latest block number with the latest tag
func TestGetBlockByNumberWithLatestTag(t *testing.T) { func TestGetBlockByNumberWithLatestTag(t *testing.T) {
m, _, _ := rpcdaemontest.CreateTestSentry(t) m, _, _ := rpcdaemontest.CreateTestSentry(t)
api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
b, err := api.GetBlockByNumber(context.Background(), rpc.LatestBlockNumber, false) b, err := api.GetBlockByNumber(context.Background(), rpc.LatestBlockNumber, false)
expected := common.HexToHash("0x5883164d4100b95e1d8e931b8b9574586a1dea7507941e6ad3c1e3a2591485fd") expected := common.HexToHash("0x5883164d4100b95e1d8e931b8b9574586a1dea7507941e6ad3c1e3a2591485fd")
if err != nil { if err != nil {
@ -55,7 +55,7 @@ func TestGetBlockByNumberWithLatestTag_WithHeadHashInDb(t *testing.T) {
} }
tx.Commit() tx.Commit()
api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
block, err := api.GetBlockByNumber(ctx, rpc.LatestBlockNumber, false) block, err := api.GetBlockByNumber(ctx, rpc.LatestBlockNumber, false)
if err != nil { if err != nil {
t.Errorf("error retrieving block by number: %s", err) t.Errorf("error retrieving block by number: %s", err)
@ -86,7 +86,7 @@ func TestGetBlockByNumberWithPendingTag(t *testing.T) {
RplBlock: rlpBlock, RplBlock: rlpBlock,
}) })
api := NewEthAPI(NewBaseApi(ff, stateCache, m.BlockReader, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine, m.Dirs), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(NewBaseApi(ff, stateCache, m.BlockReader, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine, m.Dirs), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
b, err := api.GetBlockByNumber(context.Background(), rpc.PendingBlockNumber, false) b, err := api.GetBlockByNumber(context.Background(), rpc.PendingBlockNumber, false)
if err != nil { if err != nil {
t.Errorf("error getting block number with pending tag: %s", err) t.Errorf("error getting block number with pending tag: %s", err)
@ -97,7 +97,7 @@ func TestGetBlockByNumberWithPendingTag(t *testing.T) {
func TestGetBlockByNumber_WithFinalizedTag_NoFinalizedBlockInDb(t *testing.T) { func TestGetBlockByNumber_WithFinalizedTag_NoFinalizedBlockInDb(t *testing.T) {
m, _, _ := rpcdaemontest.CreateTestSentry(t) m, _, _ := rpcdaemontest.CreateTestSentry(t)
ctx := context.Background() ctx := context.Background()
api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
if _, err := api.GetBlockByNumber(ctx, rpc.FinalizedBlockNumber, false); err != nil { if _, err := api.GetBlockByNumber(ctx, rpc.FinalizedBlockNumber, false); err != nil {
assert.ErrorIs(t, rpchelper.UnknownBlockError, err) assert.ErrorIs(t, rpchelper.UnknownBlockError, err)
} }
@ -124,7 +124,7 @@ func TestGetBlockByNumber_WithFinalizedTag_WithFinalizedBlockInDb(t *testing.T)
} }
tx.Commit() tx.Commit()
api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
block, err := api.GetBlockByNumber(ctx, rpc.FinalizedBlockNumber, false) block, err := api.GetBlockByNumber(ctx, rpc.FinalizedBlockNumber, false)
if err != nil { if err != nil {
t.Errorf("error retrieving block by number: %s", err) t.Errorf("error retrieving block by number: %s", err)
@ -136,7 +136,7 @@ func TestGetBlockByNumber_WithFinalizedTag_WithFinalizedBlockInDb(t *testing.T)
func TestGetBlockByNumber_WithSafeTag_NoSafeBlockInDb(t *testing.T) { func TestGetBlockByNumber_WithSafeTag_NoSafeBlockInDb(t *testing.T) {
m, _, _ := rpcdaemontest.CreateTestSentry(t) m, _, _ := rpcdaemontest.CreateTestSentry(t)
ctx := context.Background() ctx := context.Background()
api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
if _, err := api.GetBlockByNumber(ctx, rpc.SafeBlockNumber, false); err != nil { if _, err := api.GetBlockByNumber(ctx, rpc.SafeBlockNumber, false); err != nil {
assert.ErrorIs(t, rpchelper.UnknownBlockError, err) assert.ErrorIs(t, rpchelper.UnknownBlockError, err)
} }
@ -163,7 +163,7 @@ func TestGetBlockByNumber_WithSafeTag_WithSafeBlockInDb(t *testing.T) {
} }
tx.Commit() tx.Commit()
api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
block, err := api.GetBlockByNumber(ctx, rpc.SafeBlockNumber, false) block, err := api.GetBlockByNumber(ctx, rpc.SafeBlockNumber, false)
if err != nil { if err != nil {
t.Errorf("error retrieving block by number: %s", err) t.Errorf("error retrieving block by number: %s", err)
@ -176,7 +176,7 @@ func TestGetBlockTransactionCountByHash(t *testing.T) {
m, _, _ := rpcdaemontest.CreateTestSentry(t) m, _, _ := rpcdaemontest.CreateTestSentry(t)
ctx := context.Background() ctx := context.Background()
api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
blockHash := common.HexToHash("0x6804117de2f3e6ee32953e78ced1db7b20214e0d8c745a03b8fecf7cc8ee76ef") blockHash := common.HexToHash("0x6804117de2f3e6ee32953e78ced1db7b20214e0d8c745a03b8fecf7cc8ee76ef")
tx, err := m.DB.BeginRw(ctx) tx, err := m.DB.BeginRw(ctx)
@ -208,7 +208,7 @@ func TestGetBlockTransactionCountByHash(t *testing.T) {
func TestGetBlockTransactionCountByHash_ZeroTx(t *testing.T) { func TestGetBlockTransactionCountByHash_ZeroTx(t *testing.T) {
m, _, _ := rpcdaemontest.CreateTestSentry(t) m, _, _ := rpcdaemontest.CreateTestSentry(t)
ctx := context.Background() ctx := context.Background()
api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
blockHash := common.HexToHash("0x5883164d4100b95e1d8e931b8b9574586a1dea7507941e6ad3c1e3a2591485fd") blockHash := common.HexToHash("0x5883164d4100b95e1d8e931b8b9574586a1dea7507941e6ad3c1e3a2591485fd")
tx, err := m.DB.BeginRw(ctx) tx, err := m.DB.BeginRw(ctx)
@ -240,7 +240,7 @@ func TestGetBlockTransactionCountByHash_ZeroTx(t *testing.T) {
func TestGetBlockTransactionCountByNumber(t *testing.T) { func TestGetBlockTransactionCountByNumber(t *testing.T) {
m, _, _ := rpcdaemontest.CreateTestSentry(t) m, _, _ := rpcdaemontest.CreateTestSentry(t)
ctx := context.Background() ctx := context.Background()
api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
blockHash := common.HexToHash("0x6804117de2f3e6ee32953e78ced1db7b20214e0d8c745a03b8fecf7cc8ee76ef") blockHash := common.HexToHash("0x6804117de2f3e6ee32953e78ced1db7b20214e0d8c745a03b8fecf7cc8ee76ef")
tx, err := m.DB.BeginRw(ctx) tx, err := m.DB.BeginRw(ctx)
@ -272,7 +272,7 @@ func TestGetBlockTransactionCountByNumber(t *testing.T) {
func TestGetBlockTransactionCountByNumber_ZeroTx(t *testing.T) { func TestGetBlockTransactionCountByNumber_ZeroTx(t *testing.T) {
m, _, _ := rpcdaemontest.CreateTestSentry(t) m, _, _ := rpcdaemontest.CreateTestSentry(t)
ctx := context.Background() ctx := context.Background()
api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
blockHash := common.HexToHash("0x5883164d4100b95e1d8e931b8b9574586a1dea7507941e6ad3c1e3a2591485fd") blockHash := common.HexToHash("0x5883164d4100b95e1d8e931b8b9574586a1dea7507941e6ad3c1e3a2591485fd")

View File

@ -84,7 +84,7 @@ func TestCallMany(t *testing.T) {
db := contractBackend.DB() db := contractBackend.DB()
engine := contractBackend.Engine() engine := contractBackend.Engine()
api := NewEthAPI(NewBaseApi(nil, stateCache, contractBackend.BlockReader(), contractBackend.Agg(), false, rpccfg.DefaultEvmCallTimeout, engine, api := NewEthAPI(NewBaseApi(nil, stateCache, contractBackend.BlockReader(), contractBackend.Agg(), false, rpccfg.DefaultEvmCallTimeout, engine,
datadir.New(t.TempDir())), db, nil, nil, nil, 5000000, 100_000, log.New()) datadir.New(t.TempDir())), db, nil, nil, nil, 5000000, 100_000, false, log.New())
callArgAddr1 := ethapi.CallArgs{From: &address, To: &tokenAddr, Nonce: &nonce, callArgAddr1 := ethapi.CallArgs{From: &address, To: &tokenAddr, Nonce: &nonce,
MaxPriorityFeePerGas: (*hexutil.Big)(big.NewInt(1e9)), MaxPriorityFeePerGas: (*hexutil.Big)(big.NewInt(1e9)),

View File

@ -42,7 +42,7 @@ func TestEstimateGas(t *testing.T) {
ctx, conn := rpcdaemontest.CreateTestGrpcConn(t, mock.Mock(t)) ctx, conn := rpcdaemontest.CreateTestGrpcConn(t, mock.Mock(t))
mining := txpool.NewMiningClient(conn) mining := txpool.NewMiningClient(conn)
ff := rpchelper.New(ctx, nil, nil, mining, func() {}, m.Log) ff := rpchelper.New(ctx, nil, nil, mining, func() {}, m.Log)
api := NewEthAPI(NewBaseApi(ff, stateCache, m.BlockReader, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine, m.Dirs), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(NewBaseApi(ff, stateCache, m.BlockReader, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine, m.Dirs), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
var from = libcommon.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7") var from = libcommon.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7")
var to = libcommon.HexToAddress("0x0d3ab14bbad3d99f4203bd7a11acb94882050e7e") var to = libcommon.HexToAddress("0x0d3ab14bbad3d99f4203bd7a11acb94882050e7e")
if _, err := api.EstimateGas(context.Background(), &ethapi.CallArgs{ if _, err := api.EstimateGas(context.Background(), &ethapi.CallArgs{
@ -57,7 +57,7 @@ func TestEthCallNonCanonical(t *testing.T) {
m, _, _ := rpcdaemontest.CreateTestSentry(t) m, _, _ := rpcdaemontest.CreateTestSentry(t)
agg := m.HistoryV3Components() agg := m.HistoryV3Components()
stateCache := kvcache.New(kvcache.DefaultCoherentConfig) stateCache := kvcache.New(kvcache.DefaultCoherentConfig)
api := NewEthAPI(NewBaseApi(nil, stateCache, m.BlockReader, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine, m.Dirs), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(NewBaseApi(nil, stateCache, m.BlockReader, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine, m.Dirs), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
var from = libcommon.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7") var from = libcommon.HexToAddress("0x71562b71999873db5b286df957af199ec94617f7")
var to = libcommon.HexToAddress("0x0d3ab14bbad3d99f4203bd7a11acb94882050e7e") var to = libcommon.HexToAddress("0x0d3ab14bbad3d99f4203bd7a11acb94882050e7e")
if _, err := api.Call(context.Background(), ethapi.CallArgs{ if _, err := api.Call(context.Background(), ethapi.CallArgs{
@ -76,7 +76,7 @@ func TestEthCallToPrunedBlock(t *testing.T) {
m, bankAddress, contractAddress := chainWithDeployedContract(t) m, bankAddress, contractAddress := chainWithDeployedContract(t)
doPrune(t, m.DB, pruneTo) doPrune(t, m.DB, pruneTo)
api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
callData := hexutil.MustDecode("0x2e64cec1") callData := hexutil.MustDecode("0x2e64cec1")
callDataBytes := hexutility.Bytes(callData) callDataBytes := hexutility.Bytes(callData)
@ -97,7 +97,7 @@ func TestGetProof(t *testing.T) {
if m.HistoryV3 { if m.HistoryV3 {
t.Skip("not supported by Erigon3") t.Skip("not supported by Erigon3")
} }
api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
key := func(b byte) libcommon.Hash { key := func(b byte) libcommon.Hash {
result := libcommon.Hash{} result := libcommon.Hash{}

View File

@ -30,7 +30,7 @@ func TestNewFilters(t *testing.T) {
ctx, conn := rpcdaemontest.CreateTestGrpcConn(t, mock.Mock(t)) ctx, conn := rpcdaemontest.CreateTestGrpcConn(t, mock.Mock(t))
mining := txpool.NewMiningClient(conn) mining := txpool.NewMiningClient(conn)
ff := rpchelper.New(ctx, nil, nil, mining, func() {}, m.Log) ff := rpchelper.New(ctx, nil, nil, mining, func() {}, m.Log)
api := NewEthAPI(NewBaseApi(ff, stateCache, m.BlockReader, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine, m.Dirs), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) api := NewEthAPI(NewBaseApi(ff, stateCache, m.BlockReader, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine, m.Dirs), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
ptf, err := api.NewPendingTransactionFilter(ctx) ptf, err := api.NewPendingTransactionFilter(ctx)
assert.Nil(err) assert.Nil(err)

View File

@ -27,7 +27,7 @@ func TestPendingBlock(t *testing.T) {
stateCache := kvcache.New(kvcache.DefaultCoherentConfig) stateCache := kvcache.New(kvcache.DefaultCoherentConfig)
engine := ethash.NewFaker() engine := ethash.NewFaker()
api := NewEthAPI(NewBaseApi(ff, stateCache, m.BlockReader, nil, false, rpccfg.DefaultEvmCallTimeout, engine, api := NewEthAPI(NewBaseApi(ff, stateCache, m.BlockReader, nil, false, rpccfg.DefaultEvmCallTimeout, engine,
m.Dirs), nil, nil, nil, mining, 5000000, 100_000, log.New()) m.Dirs), nil, nil, nil, mining, 5000000, 100_000, false, log.New())
expect := uint64(12345) expect := uint64(12345)
b, err := rlp.EncodeToBytes(types.NewBlockWithHeader(&types.Header{Number: big.NewInt(int64(expect))})) b, err := rlp.EncodeToBytes(types.NewBlockWithHeader(&types.Header{Number: big.NewInt(int64(expect))}))
require.NoError(t, err) require.NoError(t, err)

View File

@ -40,7 +40,7 @@ func TestGasPrice(t *testing.T) {
t.Run(testCase.description, func(t *testing.T) { t.Run(testCase.description, func(t *testing.T) {
m := createGasPriceTestKV(t, testCase.chainSize) m := createGasPriceTestKV(t, testCase.chainSize)
defer m.DB.Close() defer m.DB.Close()
eth := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, log.New()) eth := NewEthAPI(newBaseApiForTest(m), m.DB, nil, nil, nil, 5000000, 100_000, false, log.New())
ctx := context.Background() ctx := context.Background()
result, err := eth.GasPrice(ctx) result, err := eth.GasPrice(ctx)

View File

@ -27,7 +27,7 @@ func (api *APIImpl) SendRawTransaction(ctx context.Context, encodedTx hexutility
if err := checkTxFee(txn.GetPrice().ToBig(), txn.GetGas(), ethconfig.Defaults.RPCTxFeeCap); err != nil { if err := checkTxFee(txn.GetPrice().ToBig(), txn.GetGas(), ethconfig.Defaults.RPCTxFeeCap); err != nil {
return common.Hash{}, err return common.Hash{}, err
} }
if !txn.Protected() { if !txn.Protected() && !api.AllowUnprotectedTxs {
return common.Hash{}, errors.New("only replay-protected (EIP-155) transactions allowed over RPC") return common.Hash{}, errors.New("only replay-protected (EIP-155) transactions allowed over RPC")
} }
@ -48,7 +48,7 @@ func (api *APIImpl) SendRawTransaction(ctx context.Context, encodedTx hexutility
txnChainId := txn.GetChainID() txnChainId := txn.GetChainID()
chainId := cc.ChainID chainId := cc.ChainID
if chainId.Cmp(txnChainId.ToBig()) != 0 { if (txn.Protected() || !api.AllowUnprotectedTxs) && chainId.Cmp(txnChainId.ToBig()) != 0 {
return common.Hash{}, fmt.Errorf("invalid chain id, expected: %d got: %d", chainId, *txnChainId) return common.Hash{}, fmt.Errorf("invalid chain id, expected: %d got: %d", chainId, *txnChainId)
} }

View File

@ -8,6 +8,8 @@ import (
"github.com/holiman/uint256" "github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/txpool/txpoolcfg"
"github.com/ledgerwatch/erigon-lib/gointerfaces/sentry" "github.com/ledgerwatch/erigon-lib/gointerfaces/sentry"
"github.com/ledgerwatch/erigon-lib/gointerfaces/txpool" "github.com/ledgerwatch/erigon-lib/gointerfaces/txpool"
"github.com/ledgerwatch/erigon-lib/kv/kvcache" "github.com/ledgerwatch/erigon-lib/kv/kvcache"
@ -16,6 +18,8 @@ import (
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/rpcdaemontest" "github.com/ledgerwatch/erigon/cmd/rpcdaemon/rpcdaemontest"
"github.com/ledgerwatch/erigon/common/u256" "github.com/ledgerwatch/erigon/common/u256"
txpool_proto "github.com/ledgerwatch/erigon-lib/gointerfaces/txpool"
"github.com/ledgerwatch/erigon/core" "github.com/ledgerwatch/erigon/core"
"github.com/ledgerwatch/erigon/core/types" "github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/eth/protocols/eth" "github.com/ledgerwatch/erigon/eth/protocols/eth"
@ -34,53 +38,56 @@ func newBaseApiForTest(m *mock.MockSentry) *jsonrpc.BaseAPI {
return jsonrpc.NewBaseApi(nil, stateCache, m.BlockReader, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine, m.Dirs) return jsonrpc.NewBaseApi(nil, stateCache, m.BlockReader, agg, false, rpccfg.DefaultEvmCallTimeout, m.Engine, m.Dirs)
} }
func TestSendRawTransaction(t *testing.T) { // Do 1 step to start txPool
t.Skip("Flaky test") func oneBlockStep(mockSentry *mock.MockSentry, require *require.Assertions, t *testing.T) {
m, require := mock.Mock(t), require.New(t) chain, err := core.GenerateChain(mockSentry.ChainConfig, mockSentry.Genesis, mockSentry.Engine, mockSentry.DB, 1 /*number of blocks:*/, func(i int, b *core.BlockGen) {
logger := log.New()
chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 1, func(i int, b *core.BlockGen) {
b.SetCoinbase(common.Address{1}) b.SetCoinbase(common.Address{1})
}) })
require.NoError(err) require.NoError(err)
{ // Do 1 step to start txPool
// Send NewBlock message // Send NewBlock message
b, err := rlp.EncodeToBytes(&eth.NewBlockPacket{ b, err := rlp.EncodeToBytes(&eth.NewBlockPacket{
Block: chain.TopBlock, Block: chain.TopBlock,
TD: big.NewInt(1), // This is ignored anyway TD: big.NewInt(1), // This is ignored anyway
}) })
require.NoError(err)
m.ReceiveWg.Add(1)
for _, err = range m.Send(&sentry.InboundMessage{Id: sentry.MessageId_NEW_BLOCK_66, Data: b, PeerId: m.PeerId}) {
require.NoError(err)
}
// Send all the headers
b, err = rlp.EncodeToBytes(&eth.BlockHeadersPacket66{
RequestId: 1,
BlockHeadersPacket: chain.Headers,
})
require.NoError(err)
m.ReceiveWg.Add(1)
for _, err = range m.Send(&sentry.InboundMessage{Id: sentry.MessageId_BLOCK_HEADERS_66, Data: b, PeerId: m.PeerId}) {
require.NoError(err)
}
m.ReceiveWg.Wait() // Wait for all messages to be processed before we proceed
initialCycle := mock.MockInsertAsInitialCycle
if err := stages.StageLoopIteration(m.Ctx, m.DB, nil, m.Sync, initialCycle, logger, m.BlockReader, nil, false); err != nil {
t.Fatal(err)
}
}
expectValue := uint64(1234)
txn, err := types.SignTx(types.NewTransaction(0, common.Address{1}, uint256.NewInt(expectValue), params.TxGas, uint256.NewInt(10*params.GWei), nil), *types.LatestSignerForChainID(m.ChainConfig.ChainID), m.Key)
require.NoError(err) require.NoError(err)
ctx, conn := rpcdaemontest.CreateTestGrpcConn(t, m) mockSentry.ReceiveWg.Add(1)
for _, err = range mockSentry.Send(&sentry.InboundMessage{Id: sentry.MessageId_NEW_BLOCK_66, Data: b, PeerId: mockSentry.PeerId}) {
require.NoError(err)
}
// Send all the headers
b, err = rlp.EncodeToBytes(&eth.BlockHeadersPacket66{
RequestId: 1,
BlockHeadersPacket: chain.Headers,
})
require.NoError(err)
mockSentry.ReceiveWg.Add(1)
for _, err = range mockSentry.Send(&sentry.InboundMessage{Id: sentry.MessageId_BLOCK_HEADERS_66, Data: b, PeerId: mockSentry.PeerId}) {
require.NoError(err)
}
mockSentry.ReceiveWg.Wait() // Wait for all messages to be processed before we proceed
initialCycle := mock.MockInsertAsInitialCycle
if err := stages.StageLoopIteration(mockSentry.Ctx, mockSentry.DB, nil, mockSentry.Sync, initialCycle, log.New(), mockSentry.BlockReader, nil, false); err != nil {
t.Fatal(err)
}
}
func TestSendRawTransaction(t *testing.T) {
mockSentry, require := mock.MockWithTxPool(t), require.New(t)
logger := log.New()
oneBlockStep(mockSentry, require, t)
expectValue := uint64(1234)
txn, err := types.SignTx(types.NewTransaction(0, common.Address{1}, uint256.NewInt(expectValue), params.TxGas, uint256.NewInt(10*params.GWei), nil), *types.LatestSignerForChainID(mockSentry.ChainConfig.ChainID), mockSentry.Key)
require.NoError(err)
ctx, conn := rpcdaemontest.CreateTestGrpcConn(t, mockSentry)
txPool := txpool.NewTxpoolClient(conn) txPool := txpool.NewTxpoolClient(conn)
ff := rpchelper.New(ctx, nil, txPool, txpool.NewMiningClient(conn), func() {}, m.Log) ff := rpchelper.New(ctx, nil, txPool, txpool.NewMiningClient(conn), func() {}, mockSentry.Log)
api := jsonrpc.NewEthAPI(newBaseApiForTest(m), m.DB, nil, txPool, nil, 5000000, 100_000, logger) api := jsonrpc.NewEthAPI(newBaseApiForTest(mockSentry), mockSentry.DB, nil, txPool, nil, 5000000, 100_000, false, logger)
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
err = txn.MarshalBinary(buf) err = txn.MarshalBinary(buf)
@ -98,8 +105,9 @@ func TestSendRawTransaction(t *testing.T) {
//send same tx second time and expect error //send same tx second time and expect error
_, err = api.SendRawTransaction(ctx, buf.Bytes()) _, err = api.SendRawTransaction(ctx, buf.Bytes())
require.NotNil(err) require.NotNil(err)
require.Equal("ALREADY_EXISTS: already known", err.Error()) expectedErr := txpool_proto.ImportResult_name[int32(txpool_proto.ImportResult_ALREADY_EXISTS)] + ": " + txpoolcfg.AlreadyKnown.String()
m.ReceiveWg.Wait() require.Equal(expectedErr, err.Error())
mockSentry.ReceiveWg.Wait()
//TODO: make propagation easy to test - now race //TODO: make propagation easy to test - now race
//time.Sleep(time.Second) //time.Sleep(time.Second)
@ -107,6 +115,42 @@ func TestSendRawTransaction(t *testing.T) {
//require.Equal(eth.ToProto[m.MultiClient.Protocol()][eth.NewPooledTransactionHashesMsg], sent.Id) //require.Equal(eth.ToProto[m.MultiClient.Protocol()][eth.NewPooledTransactionHashesMsg], sent.Id)
} }
func TestSendRawTransactionUnprotected(t *testing.T) {
mockSentry, require := mock.MockWithTxPool(t), require.New(t)
logger := log.New()
oneBlockStep(mockSentry, require, t)
expectedTxValue := uint64(4444)
// Create a legacy signer pre-155
unprotectedSigner := types.MakeFrontierSigner()
txn, err := types.SignTx(types.NewTransaction(0, common.Address{1}, uint256.NewInt(expectedTxValue), params.TxGas, uint256.NewInt(10*params.GWei), nil), *unprotectedSigner, mockSentry.Key)
require.NoError(err)
ctx, conn := rpcdaemontest.CreateTestGrpcConn(t, mockSentry)
txPool := txpool.NewTxpoolClient(conn)
ff := rpchelper.New(ctx, nil, txPool, txpool.NewMiningClient(conn), func() {}, mockSentry.Log)
api := jsonrpc.NewEthAPI(newBaseApiForTest(mockSentry), mockSentry.DB, nil, txPool, nil, 5000000, 100_000, false, logger)
// Enable unproteced txs flag
api.AllowUnprotectedTxs = true
buf := bytes.NewBuffer(nil)
err = txn.MarshalBinary(buf)
require.NoError(err)
txsCh, id := ff.SubscribePendingTxs(1)
defer ff.UnsubscribePendingTxs(id)
_, err = api.SendRawTransaction(ctx, buf.Bytes())
require.NoError(err)
got := <-txsCh
require.Equal(expectedTxValue, got[0].GetValue().Uint64())
}
func transaction(nonce uint64, gaslimit uint64, key *ecdsa.PrivateKey) types.Transaction { func transaction(nonce uint64, gaslimit uint64, key *ecdsa.PrivateKey) types.Transaction {
return pricedTransaction(nonce, gaslimit, u256.Num1, key) return pricedTransaction(nonce, gaslimit, u256.Num1, key)
} }