prysm-pulse/beacon-chain/sync/rpc_beacon_blocks_test.go
Preston Van Loon 3f0d1c1d41 Reg sync beacon blocks (#3218)
* checkpoint

* checkpoint

* varint prefix for ssz

* move the encoding API around a little bit to support reader writer

* add a simple test for the happy path subscribe

* move wait timeout to testutil

* Add inverted topic mapping

* Add varint prefixing to ssz network encoder

* fix spacing

* fix comments

* fix comments

* make anon methods more clear

* clean up log fields

* move topic mapping, reformat TODOs, get ready for brutal team review

* lint

* lint

* lint

* Update beacon-chain/p2p/gossip_topic_mappings.go

Co-Authored-By: Nishant Das <nishdas93@gmail.com>

* PR feedback

* PR feedback

* PR feedback

* PR feedback

* PR feedback

* basic test with a hardcoded fork choice

* updated beacon config to use genesis fork version

* checkpoint on hello handler

* create a testing db method that can be used with the new database interface

* lint

* lint

* PR feedback

* checkpoint

* passing tests

* comments, errors

* comments, errors

* remove swarm

* Add basic sanity test, naive implementation

* add test with real data
2019-08-20 14:06:49 -05:00

77 lines
2.0 KiB
Go

package sync
import (
"context"
"sync"
"testing"
"time"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/protocol"
db "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/testutil"
)
func TestBeaconBlocksRPCHandler_ReturnsBlocks(t *testing.T) {
p1 := p2ptest.NewTestP2P(t)
p2 := p2ptest.NewTestP2P(t)
p1.Connect(p2)
if len(p1.Host.Network().Peers()) != 1 {
t.Error("Expected peers to be connected")
}
d := db.SetupDB(t)
defer db.TeardownDB(t, d)
req := &pb.BeaconBlocksRequest{
HeadSlot: 100,
Step: 4,
Count: 100,
}
// Populate the database with blocks that would match the request.
for i := req.HeadSlot; i < req.HeadSlot+(req.Step*req.Count); i++ {
if err := d.SaveBlock(context.Background(), &ethpb.BeaconBlock{Slot: i}); err != nil {
t.Fatal(err)
}
}
r := &RegularSync{p2p: p1, db: d}
pcl := protocol.ID("/testing")
var wg sync.WaitGroup
wg.Add(1)
p2.Host.SetStreamHandler(pcl, func(stream network.Stream) {
defer wg.Done()
expectSuccess(t, r, stream)
res := &pb.BeaconBlocksResponse{}
if err := r.p2p.Encoding().Decode(stream, res); err != nil {
t.Error(err)
}
if uint64(len(res.Blocks)) != req.Count {
t.Errorf("Received only %d blocks, expected %d", len(res.Blocks), req.Count)
}
for _, blk := range res.Blocks {
if (blk.Slot-req.HeadSlot)%req.Step != 0 {
t.Errorf("Received unexpected block slot %d", blk.Slot)
}
}
})
stream1, err := p1.Host.NewStream(context.Background(), p2.Host.ID(), pcl)
if err != nil {
t.Fatal(err)
}
err = r.beaconBlocksRPCHandler(context.Background(), req, stream1)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if testutil.WaitTimeout(&wg, 1*time.Second) {
t.Fatal("Did not receive stream within 1 sec")
}
}