2021-06-11 08:34:47 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-06-27 06:41:21 +00:00
|
|
|
"math/big"
|
2021-06-11 08:34:47 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-06-27 06:41:21 +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-06-11 08:34:47 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/filters"
|
2021-06-29 10:00:22 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/rpcdaemontest"
|
2021-06-11 08:34:47 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
2021-08-06 02:45:44 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common/hexutil"
|
2021-06-27 06:41:21 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common/u256"
|
|
|
|
"github.com/ledgerwatch/erigon/core"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
|
|
"github.com/ledgerwatch/erigon/eth/protocols/eth"
|
|
|
|
"github.com/ledgerwatch/erigon/params"
|
|
|
|
"github.com/ledgerwatch/erigon/rlp"
|
|
|
|
"github.com/ledgerwatch/erigon/turbo/stages"
|
2021-06-11 08:34:47 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTxPoolContent(t *testing.T) {
|
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})
|
|
|
|
}, false /* intemediateHashes */)
|
|
|
|
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)
|
|
|
|
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(ð.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 proceeed
|
|
|
|
|
|
|
|
initialCycle := true
|
|
|
|
highestSeenHeader := chain.TopBlock.NumberU64()
|
2021-07-28 02:47:38 +00:00
|
|
|
if err := stages.StageLoopStep(m.Ctx, m.Log, m.DB, m.Sync, highestSeenHeader, m.Notifications, initialCycle, m.UpdateHead, nil); err != nil {
|
2021-06-27 06:41:21 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 10:00:22 +00:00
|
|
|
ctx, conn := rpcdaemontest.CreateTestGrpcConn(t, m)
|
2021-06-11 08:34:47 +00:00
|
|
|
txPool := txpool.NewTxpoolClient(conn)
|
|
|
|
ff := filters.New(ctx, nil, txPool, txpool.NewMiningClient(conn))
|
2021-06-27 06:41:21 +00:00
|
|
|
api := NewTxPoolAPI(NewBaseApi(ff), m.DB, txPool)
|
|
|
|
|
|
|
|
expectValue := uint64(1234)
|
|
|
|
txn, err := types.SignTx(types.NewTransaction(0, common.Address{1}, uint256.NewInt(expectValue), params.TxGas, u256.Num1, nil), *types.LatestSignerForChainID(m.ChainConfig.ChainID), m.Key)
|
|
|
|
require.NoError(err)
|
2021-06-11 08:34:47 +00:00
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
2021-06-27 06:41:21 +00:00
|
|
|
err = txn.MarshalBinary(buf)
|
|
|
|
require.NoError(err)
|
2021-06-11 08:34:47 +00:00
|
|
|
|
|
|
|
_, err = txPool.Add(ctx, &txpool.AddRequest{RlpTxs: [][]byte{buf.Bytes()}})
|
2021-06-27 06:41:21 +00:00
|
|
|
require.NoError(err)
|
2021-06-11 08:34:47 +00:00
|
|
|
content, err := api.Content(ctx)
|
2021-06-27 06:41:21 +00:00
|
|
|
require.NoError(err)
|
2021-06-11 08:34:47 +00:00
|
|
|
|
2021-06-27 06:41:21 +00:00
|
|
|
sender := m.Address.String()
|
|
|
|
require.Equal(1, len(content["pending"][sender]))
|
|
|
|
require.Equal(expectValue, content["pending"][sender]["0"].Value.ToInt().Uint64())
|
2021-08-06 02:45:44 +00:00
|
|
|
|
|
|
|
status, err := api.Status(ctx)
|
|
|
|
require.NoError(err)
|
|
|
|
require.Len(status, 2)
|
|
|
|
require.Equal(status["pending"], hexutil.Uint(1))
|
|
|
|
require.Equal(status["queued"], hexutil.Uint(0))
|
2021-06-11 08:34:47 +00:00
|
|
|
}
|