2021-10-09 09:04:54 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
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"
|
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/common"
|
|
|
|
"github.com/ledgerwatch/erigon/core"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
|
|
"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"
|
2022-06-10 15:18:43 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/rpchelper"
|
2021-11-21 03:32:14 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/snapshotsync"
|
2021-10-09 09:04:54 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/stages"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEthSubscribe(t *testing.T) {
|
|
|
|
m, require := stages.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) {
|
2021-10-09 09:04:54 +00:00
|
|
|
b.SetCoinbase(common.Address{1})
|
2022-03-03 15:09:03 +00:00
|
|
|
}, false /* intermediateHashes */)
|
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()
|
2022-07-23 16:57:23 +00:00
|
|
|
backendServer := privateapi.NewEthBackendServer(ctx, nil, m.DB, m.Notifications.Events, snapshotsync.NewBlockReader(), nil, nil, nil, false)
|
2022-07-01 03:03:29 +00:00
|
|
|
backendClient := direct.NewEthBackendClientDirect(backendServer)
|
|
|
|
backend := rpcservices.NewRemoteBackend(backendClient, m.DB, snapshotsync.NewBlockReader())
|
2022-06-10 15:18:43 +00:00
|
|
|
ff := rpchelper.New(ctx, backend, nil, nil, func() {})
|
2021-10-09 09:04:54 +00:00
|
|
|
|
|
|
|
newHeads := make(chan *types.Header)
|
|
|
|
id := ff.SubscribeNewHeads(newHeads)
|
|
|
|
defer ff.UnsubscribeHeads(id)
|
|
|
|
|
|
|
|
initialCycle := true
|
|
|
|
highestSeenHeader := chain.TopBlock.NumberU64()
|
2022-01-07 15:54:26 +00:00
|
|
|
if _, err := stages.StageLoopStep(m.Ctx, m.DB, m.Sync, highestSeenHeader, m.Notifications, initialCycle, m.UpdateHead, nil); 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
|
|
|
}
|
|
|
|
}
|