2023-07-08 17:01:26 +00:00
|
|
|
package jsonrpc
|
2021-10-09 09:04:54 +00:00
|
|
|
|
|
|
|
import (
|
2022-07-01 03:03:29 +00:00
|
|
|
"context"
|
2022-06-17 12:40:49 +00:00
|
|
|
"fmt"
|
2021-10-09 09:04:54 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
2022-07-01 03:03:29 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/direct"
|
2021-10-09 09:04:54 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/gointerfaces/sentry"
|
2024-01-09 01:26:26 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/wrap"
|
2023-01-13 18:12:18 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2022-05-26 03:31:06 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/rpcservices"
|
2021-10-09 09:04:54 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core"
|
|
|
|
"github.com/ledgerwatch/erigon/eth/protocols/eth"
|
2022-07-01 03:03:29 +00:00
|
|
|
"github.com/ledgerwatch/erigon/ethdb/privateapi"
|
2021-10-09 09:04:54 +00:00
|
|
|
"github.com/ledgerwatch/erigon/rlp"
|
2023-07-10 17:22:03 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/builder"
|
2022-06-10 15:18:43 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/rpchelper"
|
2021-10-09 09:04:54 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/stages"
|
2023-08-05 21:33:10 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/stages/mock"
|
2023-05-13 20:22:30 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2021-10-09 09:04:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEthSubscribe(t *testing.T) {
|
2023-08-05 21:33:10 +00:00
|
|
|
m, require := mock.Mock(t), require.New(t)
|
2022-06-29 07:22:47 +00:00
|
|
|
chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 7, func(i int, b *core.BlockGen) {
|
2023-01-13 18:12:18 +00:00
|
|
|
b.SetCoinbase(libcommon.Address{1})
|
2023-06-23 03:07:42 +00:00
|
|
|
})
|
2021-10-09 09:04:54 +00:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
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-10-09 09:04:54 +00:00
|
|
|
require.NoError(err)
|
|
|
|
}
|
|
|
|
m.ReceiveWg.Wait() // Wait for all messages to be processed before we proceeed
|
|
|
|
|
2022-07-01 03:03:29 +00:00
|
|
|
ctx := context.Background()
|
2023-05-16 09:53:50 +00:00
|
|
|
logger := log.New()
|
2023-07-10 17:22:03 +00:00
|
|
|
backendServer := privateapi.NewEthBackendServer(ctx, nil, m.DB, m.Notifications.Events, m.BlockReader, logger, builder.NewLatestBlockBuiltStore())
|
2022-07-01 03:03:29 +00:00
|
|
|
backendClient := direct.NewEthBackendClientDirect(backendServer)
|
2023-06-15 06:11:51 +00:00
|
|
|
backend := rpcservices.NewRemoteBackend(backendClient, m.DB, m.BlockReader)
|
2023-05-17 16:36:15 +00:00
|
|
|
ff := rpchelper.New(ctx, backend, nil, nil, func() {}, m.Log)
|
2021-10-09 09:04:54 +00:00
|
|
|
|
2023-01-02 04:42:40 +00:00
|
|
|
newHeads, id := ff.SubscribeNewHeads(16)
|
2021-10-09 09:04:54 +00:00
|
|
|
defer ff.UnsubscribeHeads(id)
|
|
|
|
|
2023-08-05 21:33:10 +00:00
|
|
|
initialCycle := mock.MockInsertAsInitialCycle
|
2021-10-09 09:04:54 +00:00
|
|
|
highestSeenHeader := chain.TopBlock.NumberU64()
|
2023-05-23 05:28:31 +00:00
|
|
|
|
2023-09-29 02:03:19 +00:00
|
|
|
hook := stages.NewHook(m.Ctx, m.DB, m.Notifications, m.Sync, m.BlockReader, m.ChainConfig, m.Log, m.UpdateHead)
|
2024-01-09 01:26:26 +00:00
|
|
|
if err := stages.StageLoopIteration(m.Ctx, m.DB, wrap.TxContainer{}, m.Sync, initialCycle, logger, m.BlockReader, hook, false); err != nil {
|
2021-10-09 09:04:54 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := uint64(1); i <= highestSeenHeader; i++ {
|
|
|
|
header := <-newHeads
|
2022-06-17 12:40:49 +00:00
|
|
|
fmt.Printf("Got header %d\n", header.Number.Uint64())
|
2022-02-10 01:25:58 +00:00
|
|
|
require.Equal(i, header.Number.Uint64())
|
2021-10-09 09:04:54 +00:00
|
|
|
}
|
|
|
|
}
|