2021-06-29 10:00:22 +00:00
|
|
|
package commands_test
|
2021-05-04 01:37:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/ecdsa"
|
2022-09-18 10:41:01 +00:00
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
2021-05-04 01:37:17 +00:00
|
|
|
"github.com/holiman/uint256"
|
2021-07-01 21:31:14 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/gointerfaces/sentry"
|
|
|
|
"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/commands"
|
|
|
|
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/rpcdaemontest"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/common/u256"
|
2021-06-27 06:41:21 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
2021-06-27 06:41:21 +00:00
|
|
|
"github.com/ledgerwatch/erigon/eth/protocols/eth"
|
|
|
|
"github.com/ledgerwatch/erigon/params"
|
|
|
|
"github.com/ledgerwatch/erigon/rlp"
|
2022-09-17 06:25:27 +00:00
|
|
|
"github.com/ledgerwatch/erigon/rpc/rpccfg"
|
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-04 01:37:17 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSendRawTransaction(t *testing.T) {
|
2021-07-04 07:49:31 +00:00
|
|
|
t.Skip("Flaky test")
|
2021-06-27 06:41:21 +00:00
|
|
|
m, require := stages.Mock(t), require.New(t)
|
|
|
|
|
|
|
|
chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 1, func(i int, b *core.BlockGen) {
|
|
|
|
b.SetCoinbase(common.Address{1})
|
2022-03-03 15:09:03 +00:00
|
|
|
}, false /* intermediateHashes */)
|
2021-06-27 06:41:21 +00:00
|
|
|
require.NoError(err)
|
|
|
|
{ // Do 1 step to start txPool
|
|
|
|
|
|
|
|
// Send NewBlock message
|
|
|
|
b, err := rlp.EncodeToBytes(ð.NewBlockPacket{
|
|
|
|
Block: chain.TopBlock,
|
|
|
|
TD: big.NewInt(1), // This is ignored anyway
|
|
|
|
})
|
|
|
|
require.NoError(err)
|
|
|
|
m.ReceiveWg.Add(1)
|
2022-06-28 11:42:35 +00:00
|
|
|
for _, err = range m.Send(&sentry.InboundMessage{Id: sentry.MessageId_NEW_BLOCK_66, Data: b, PeerId: m.PeerId}) {
|
2021-06-27 06:41:21 +00:00
|
|
|
require.NoError(err)
|
|
|
|
}
|
|
|
|
// Send all the headers
|
|
|
|
b, err = rlp.EncodeToBytes(ð.BlockHeadersPacket66{
|
|
|
|
RequestId: 1,
|
|
|
|
BlockHeadersPacket: chain.Headers,
|
|
|
|
})
|
|
|
|
require.NoError(err)
|
|
|
|
m.ReceiveWg.Add(1)
|
2022-06-28 11:42:35 +00:00
|
|
|
for _, err = range m.Send(&sentry.InboundMessage{Id: sentry.MessageId_BLOCK_HEADERS_66, Data: b, PeerId: m.PeerId}) {
|
2021-06-27 06:41:21 +00:00
|
|
|
require.NoError(err)
|
|
|
|
}
|
2022-06-28 11:42:35 +00:00
|
|
|
m.ReceiveWg.Wait() // Wait for all messages to be processed before we proceeed
|
2021-06-27 06:41:21 +00:00
|
|
|
|
|
|
|
initialCycle := true
|
2022-12-21 07:39:19 +00:00
|
|
|
if _, err := stages.StageLoopStep(m.Ctx, m.ChainConfig, m.DB, m.Sync, m.Notifications, initialCycle, m.UpdateHead); err != nil {
|
2021-06-27 06:41:21 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expectValue := uint64(1234)
|
2021-10-28 14:18:34 +00:00
|
|
|
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)
|
2021-06-27 06:41:21 +00:00
|
|
|
require.NoError(err)
|
|
|
|
|
2021-06-29 10:00:22 +00:00
|
|
|
ctx, conn := rpcdaemontest.CreateTestGrpcConn(t, m)
|
2021-05-04 01:37:17 +00:00
|
|
|
txPool := txpool.NewTxpoolClient(conn)
|
2022-06-10 15:18:43 +00:00
|
|
|
ff := rpchelper.New(ctx, nil, txPool, txpool.NewMiningClient(conn), func() {})
|
2021-09-29 01:36:25 +00:00
|
|
|
stateCache := kvcache.New(kvcache.DefaultCoherentConfig)
|
2022-10-18 04:53:54 +00:00
|
|
|
br := snapshotsync.NewBlockReaderWithSnapshots(m.BlockSnapshots)
|
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 := commands.NewEthAPI(commands.NewBaseApi(ff, stateCache, br, nil, false, rpccfg.DefaultEvmCallTimeout, m.Engine), m.DB, nil, txPool, nil, 5000000)
|
2021-05-04 01:37:17 +00:00
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
2021-06-27 06:41:21 +00:00
|
|
|
err = txn.MarshalBinary(buf)
|
|
|
|
require.NoError(err)
|
2021-05-04 01:37:17 +00:00
|
|
|
|
|
|
|
txsCh := make(chan []types.Transaction, 1)
|
2021-06-29 10:00:22 +00:00
|
|
|
id := ff.SubscribePendingTxs(txsCh)
|
|
|
|
defer ff.UnsubscribePendingTxs(id)
|
2021-05-04 01:37:17 +00:00
|
|
|
|
2021-06-11 08:34:47 +00:00
|
|
|
_, err = api.SendRawTransaction(ctx, buf.Bytes())
|
2021-06-27 06:41:21 +00:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
got := <-txsCh
|
|
|
|
require.Equal(expectValue, got[0].GetValue().Uint64())
|
|
|
|
|
|
|
|
//send same tx second time and expect error
|
|
|
|
_, err = api.SendRawTransaction(ctx, buf.Bytes())
|
|
|
|
require.NotNil(err)
|
|
|
|
require.Equal("ALREADY_EXISTS: already known", err.Error())
|
2021-06-29 10:00:22 +00:00
|
|
|
m.ReceiveWg.Wait()
|
|
|
|
|
|
|
|
//TODO: make propagation easy to test - now race
|
|
|
|
//time.Sleep(time.Second)
|
|
|
|
//sent := m.SentMessage(0)
|
2022-05-26 03:45:35 +00:00
|
|
|
//require.Equal(eth.ToProto[m.MultiClient.Protocol()][eth.NewPooledTransactionHashesMsg], sent.Id)
|
2021-05-04 01:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func transaction(nonce uint64, gaslimit uint64, key *ecdsa.PrivateKey) types.Transaction {
|
|
|
|
return pricedTransaction(nonce, gaslimit, u256.Num1, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func pricedTransaction(nonce uint64, gaslimit uint64, gasprice *uint256.Int, key *ecdsa.PrivateKey) types.Transaction {
|
2021-06-04 16:25:28 +00:00
|
|
|
tx, _ := types.SignTx(types.NewTransaction(nonce, common.Address{}, uint256.NewInt(100), gaslimit, gasprice, nil), *types.LatestSignerForChainID(big.NewInt(1337)), key)
|
2021-05-04 01:37:17 +00:00
|
|
|
return tx
|
|
|
|
}
|