erigon-pulse/cmd/rpcdaemon/commands/txpool_api_test.go

90 lines
3.1 KiB
Go
Raw Normal View History

package commands
import (
"bytes"
2021-06-27 06:41:21 +00:00
"math/big"
"testing"
2021-06-27 06:41:21 +00:00
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/gointerfaces/sentry"
"github.com/ledgerwatch/erigon-lib/gointerfaces/txpool"
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/filters"
2021-06-29 10:00:22 +00:00
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/rpcdaemontest"
"github.com/ledgerwatch/erigon/common"
"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"
"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(&eth.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(&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 proceeed
initialCycle := true
highestSeenHeader := chain.TopBlock.NumberU64()
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)
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)
buf := bytes.NewBuffer(nil)
2021-06-27 06:41:21 +00:00
err = txn.MarshalBinary(buf)
require.NoError(err)
_, err = txPool.Add(ctx, &txpool.AddRequest{RlpTxs: [][]byte{buf.Bytes()}})
2021-06-27 06:41:21 +00:00
require.NoError(err)
content, err := api.Content(ctx)
2021-06-27 06:41:21 +00:00
require.NoError(err)
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())
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))
}