2019-08-13 21:12:00 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2019-08-21 06:08:30 +00:00
|
|
|
"context"
|
2019-08-22 15:23:16 +00:00
|
|
|
"crypto/ecdsa"
|
2019-08-21 06:08:30 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2019-08-13 21:12:00 +00:00
|
|
|
"testing"
|
2019-08-21 06:08:30 +00:00
|
|
|
"time"
|
2019-08-13 21:12:00 +00:00
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2020-05-31 20:08:36 +00:00
|
|
|
"github.com/libp2p/go-libp2p"
|
2022-10-07 07:24:51 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/host"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
|
|
noise "github.com/libp2p/go-libp2p/p2p/security/noise"
|
2020-05-31 20:08:36 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/async/event"
|
|
|
|
mock "github.com/prysmaticlabs/prysm/v4/beacon-chain/blockchain/testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/core/feed"
|
|
|
|
statefeed "github.com/prysmaticlabs/prysm/v4/beacon-chain/core/feed/state"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/p2p/encoder"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/p2p/peers"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/p2p/peers/scorers"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/config/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/network/forks"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/require"
|
|
|
|
prysmTime "github.com/prysmaticlabs/prysm/v4/time"
|
2019-08-13 21:12:00 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
2021-01-07 13:35:42 +00:00
|
|
|
type mockListener struct {
|
|
|
|
localNode *enode.LocalNode
|
|
|
|
}
|
2019-08-21 06:08:30 +00:00
|
|
|
|
2021-01-07 13:35:42 +00:00
|
|
|
func (m mockListener) Self() *enode.Node {
|
|
|
|
return m.localNode.Node()
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
func (mockListener) Close() {
|
2021-01-05 13:09:41 +00:00
|
|
|
// no-op
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
func (mockListener) Lookup(enode.ID) []*enode.Node {
|
2019-08-21 06:08:30 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2020-10-12 08:11:05 +00:00
|
|
|
func (mockListener) ReadRandomNodes(_ []*enode.Node) int {
|
2019-08-21 06:08:30 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
func (mockListener) Resolve(*enode.Node) *enode.Node {
|
2019-08-21 06:08:30 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
func (mockListener) Ping(*enode.Node) error {
|
2019-08-21 06:08:30 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
func (mockListener) RequestENR(*enode.Node) (*enode.Node, error) {
|
2019-08-21 06:08:30 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2020-03-23 14:41:47 +00:00
|
|
|
func (mockListener) LocalNode() *enode.LocalNode {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2020-06-01 22:03:33 +00:00
|
|
|
func (mockListener) RandomNodes() enode.Iterator {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2019-08-22 15:23:16 +00:00
|
|
|
func createHost(t *testing.T, port int) (host.Host, *ecdsa.PrivateKey, net.IP) {
|
2020-10-01 18:53:36 +00:00
|
|
|
_, pkey := createAddrAndPrivKey(t)
|
|
|
|
ipAddr := net.ParseIP("127.0.0.1")
|
2019-08-21 06:08:30 +00:00
|
|
|
listen, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ipAddr, port))
|
2020-08-25 15:23:06 +00:00
|
|
|
require.NoError(t, err, "Failed to p2p listen")
|
2022-01-14 14:06:20 +00:00
|
|
|
h, err := libp2p.New([]libp2p.Option{privKeyOption(pkey), libp2p.ListenAddrs(listen), libp2p.Security(noise.ID, noise.New)}...)
|
2020-08-25 15:23:06 +00:00
|
|
|
require.NoError(t, err)
|
2019-08-22 15:23:16 +00:00
|
|
|
return h, pkey, ipAddr
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 21:12:00 +00:00
|
|
|
func TestService_Stop_SetsStartedToFalse(t *testing.T) {
|
2022-05-20 07:16:53 +00:00
|
|
|
params.SetupTestConfigCleanup(t)
|
2021-02-12 17:45:22 +00:00
|
|
|
s, err := NewService(context.Background(), &Config{StateNotifier: &mock.MockStateNotifier{}})
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2019-08-13 21:12:00 +00:00
|
|
|
s.started = true
|
2019-08-21 06:08:30 +00:00
|
|
|
s.dv5Listener = &mockListener{}
|
2020-07-14 16:51:39 +00:00
|
|
|
assert.NoError(t, s.Stop())
|
|
|
|
assert.Equal(t, false, s.started)
|
2019-08-13 21:12:00 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 10:59:34 +00:00
|
|
|
func TestService_Stop_DontPanicIfDv5ListenerIsNotInited(t *testing.T) {
|
2022-05-20 07:16:53 +00:00
|
|
|
params.SetupTestConfigCleanup(t)
|
2021-02-12 17:45:22 +00:00
|
|
|
s, err := NewService(context.Background(), &Config{StateNotifier: &mock.MockStateNotifier{}})
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NoError(t, s.Stop())
|
2019-08-28 10:59:34 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 21:12:00 +00:00
|
|
|
func TestService_Start_OnlyStartsOnce(t *testing.T) {
|
2022-05-20 07:16:53 +00:00
|
|
|
params.SetupTestConfigCleanup(t)
|
2019-08-13 21:12:00 +00:00
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
|
2019-08-21 06:08:30 +00:00
|
|
|
cfg := &Config{
|
2020-10-16 07:05:40 +00:00
|
|
|
TCPPort: 2000,
|
|
|
|
UDPPort: 2000,
|
|
|
|
StateNotifier: &mock.MockStateNotifier{},
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
2021-02-12 17:45:22 +00:00
|
|
|
s, err := NewService(context.Background(), cfg)
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-14 20:27:03 +00:00
|
|
|
s.stateNotifier = &mock.MockStateNotifier{}
|
2019-08-21 06:08:30 +00:00
|
|
|
s.dv5Listener = &mockListener{}
|
2020-04-14 20:27:03 +00:00
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
s.Start()
|
|
|
|
<-exitRoutine
|
2020-04-14 16:41:09 +00:00
|
|
|
}()
|
2020-04-14 20:27:03 +00:00
|
|
|
// Send in a loop to ensure it is delivered (busy wait for the service to subscribe to the state feed).
|
|
|
|
for sent := 0; sent == 0; {
|
|
|
|
sent = s.stateNotifier.StateFeed().Send(&feed.Event{
|
|
|
|
Type: statefeed.Initialized,
|
|
|
|
Data: &statefeed.InitializedData{
|
|
|
|
StartTime: time.Now(),
|
|
|
|
GenesisValidatorsRoot: make([]byte, 32),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second * 2)
|
2020-07-14 16:51:39 +00:00
|
|
|
assert.Equal(t, true, s.started, "Expected service to be started")
|
2019-08-13 21:12:00 +00:00
|
|
|
s.Start()
|
2020-08-13 16:22:25 +00:00
|
|
|
require.LogsContain(t, hook, "Attempted to start p2p service when it was already started")
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, s.Stop())
|
2020-04-14 20:27:03 +00:00
|
|
|
exitRoutine <- true
|
2019-08-13 21:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_Status_NotRunning(t *testing.T) {
|
2022-05-20 07:16:53 +00:00
|
|
|
params.SetupTestConfigCleanup(t)
|
2019-08-13 21:12:00 +00:00
|
|
|
s := &Service{started: false}
|
2019-08-21 06:08:30 +00:00
|
|
|
s.dv5Listener = &mockListener{}
|
2020-07-14 16:51:39 +00:00
|
|
|
assert.ErrorContains(t, "not running", s.Status(), "Status returned wrong error")
|
2019-08-13 21:12:00 +00:00
|
|
|
}
|
2019-08-21 06:08:30 +00:00
|
|
|
|
2021-08-16 19:15:43 +00:00
|
|
|
func TestService_Status_NoGenesisTimeSet(t *testing.T) {
|
2022-05-20 07:16:53 +00:00
|
|
|
params.SetupTestConfigCleanup(t)
|
2021-08-16 19:15:43 +00:00
|
|
|
s := &Service{started: true}
|
|
|
|
s.dv5Listener = &mockListener{}
|
|
|
|
assert.ErrorContains(t, "no genesis time set", s.Status(), "Status returned wrong error")
|
|
|
|
|
|
|
|
s.genesisTime = time.Now()
|
|
|
|
|
|
|
|
assert.NoError(t, s.Status(), "Status returned error")
|
|
|
|
}
|
|
|
|
|
2023-02-22 06:53:09 +00:00
|
|
|
func TestService_Start_NoDiscoverFlag(t *testing.T) {
|
|
|
|
params.SetupTestConfigCleanup(t)
|
|
|
|
|
|
|
|
cfg := &Config{
|
|
|
|
TCPPort: 2000,
|
|
|
|
UDPPort: 2000,
|
|
|
|
StateNotifier: &mock.MockStateNotifier{},
|
|
|
|
NoDiscovery: true, // <-- no s.dv5Listener is created
|
|
|
|
}
|
|
|
|
s, err := NewService(context.Background(), cfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s.stateNotifier = &mock.MockStateNotifier{}
|
2023-02-23 11:04:53 +00:00
|
|
|
|
2023-02-22 06:53:09 +00:00
|
|
|
// required params to addForkEntry in s.forkWatcher
|
|
|
|
s.genesisTime = time.Now()
|
|
|
|
beaconCfg := params.BeaconConfig().Copy()
|
|
|
|
beaconCfg.AltairForkEpoch = 0
|
|
|
|
beaconCfg.BellatrixForkEpoch = 0
|
|
|
|
beaconCfg.CapellaForkEpoch = 0
|
|
|
|
beaconCfg.SecondsPerSlot = 1
|
|
|
|
params.OverrideBeaconConfig(beaconCfg)
|
|
|
|
|
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
s.Start()
|
|
|
|
<-exitRoutine
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Send in a loop to ensure it is delivered (busy wait for the service to subscribe to the state feed).
|
|
|
|
for sent := 0; sent == 0; {
|
|
|
|
sent = s.stateNotifier.StateFeed().Send(&feed.Event{
|
|
|
|
Type: statefeed.Initialized,
|
|
|
|
Data: &statefeed.InitializedData{
|
|
|
|
StartTime: time.Now(),
|
|
|
|
GenesisValidatorsRoot: make([]byte, 32),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
|
|
|
exitRoutine <- true
|
|
|
|
}
|
|
|
|
|
2019-08-21 06:08:30 +00:00
|
|
|
func TestListenForNewNodes(t *testing.T) {
|
2022-05-20 07:16:53 +00:00
|
|
|
params.SetupTestConfigCleanup(t)
|
2020-04-14 20:27:03 +00:00
|
|
|
// Setup bootnode.
|
2020-10-16 07:05:40 +00:00
|
|
|
notifier := &mock.MockStateNotifier{}
|
|
|
|
cfg := &Config{StateNotifier: notifier}
|
2019-08-21 06:08:30 +00:00
|
|
|
port := 2000
|
2019-09-12 04:52:27 +00:00
|
|
|
cfg.UDPPort = uint(port)
|
2019-08-21 06:08:30 +00:00
|
|
|
_, pkey := createAddrAndPrivKey(t)
|
|
|
|
ipAddr := net.ParseIP("127.0.0.1")
|
2021-09-15 00:09:04 +00:00
|
|
|
genesisTime := prysmTime.Now()
|
2020-04-14 20:27:03 +00:00
|
|
|
genesisValidatorsRoot := make([]byte, 32)
|
|
|
|
s := &Service{
|
|
|
|
cfg: cfg,
|
|
|
|
genesisTime: genesisTime,
|
|
|
|
genesisValidatorsRoot: genesisValidatorsRoot,
|
|
|
|
}
|
2020-07-09 17:38:15 +00:00
|
|
|
bootListener, err := s.createListener(ipAddr, pkey)
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2019-08-21 06:08:30 +00:00
|
|
|
defer bootListener.Close()
|
|
|
|
|
2020-03-10 02:53:37 +00:00
|
|
|
// Use shorter period for testing.
|
|
|
|
currentPeriod := pollingPeriod
|
|
|
|
pollingPeriod = 1 * time.Second
|
|
|
|
defer func() {
|
|
|
|
pollingPeriod = currentPeriod
|
|
|
|
}()
|
|
|
|
|
2019-08-21 06:08:30 +00:00
|
|
|
bootNode := bootListener.Self()
|
|
|
|
|
2020-04-14 20:27:03 +00:00
|
|
|
var listeners []*discover.UDPv5
|
|
|
|
var hosts []host.Host
|
|
|
|
// setup other nodes.
|
2019-09-06 19:20:20 +00:00
|
|
|
cfg = &Config{
|
2019-09-23 17:24:16 +00:00
|
|
|
BootstrapNodeAddr: []string{bootNode.String()},
|
|
|
|
Discv5BootStrapAddr: []string{bootNode.String()},
|
2020-03-10 02:53:37 +00:00
|
|
|
MaxPeers: 30,
|
2020-10-16 07:05:40 +00:00
|
|
|
StateNotifier: notifier,
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
|
|
|
for i := 1; i <= 5; i++ {
|
2020-04-14 20:27:03 +00:00
|
|
|
h, pkey, ipAddr := createHost(t, port+i)
|
|
|
|
cfg.UDPPort = uint(port + i)
|
|
|
|
cfg.TCPPort = uint(port + i)
|
|
|
|
s := &Service{
|
|
|
|
cfg: cfg,
|
|
|
|
genesisTime: genesisTime,
|
|
|
|
genesisValidatorsRoot: genesisValidatorsRoot,
|
|
|
|
}
|
|
|
|
listener, err := s.startDiscoveryV5(ipAddr, pkey)
|
2020-07-14 16:51:39 +00:00
|
|
|
assert.NoError(t, err, "Could not start discovery for node")
|
2020-04-14 20:27:03 +00:00
|
|
|
listeners = append(listeners, listener)
|
2019-08-22 15:23:16 +00:00
|
|
|
hosts = append(hosts, h)
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
2020-04-14 20:27:03 +00:00
|
|
|
defer func() {
|
|
|
|
// Close down all peers.
|
|
|
|
for _, listener := range listeners {
|
|
|
|
listener.Close()
|
|
|
|
}
|
|
|
|
}()
|
2019-08-21 06:08:30 +00:00
|
|
|
|
2019-08-22 15:23:16 +00:00
|
|
|
// close peers upon exit of test
|
|
|
|
defer func() {
|
|
|
|
for _, h := range hosts {
|
2020-04-14 16:41:09 +00:00
|
|
|
if err := h.Close(); err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
}
|
2019-08-22 15:23:16 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-08-30 20:15:40 +00:00
|
|
|
cfg.UDPPort = 14000
|
2020-04-14 20:27:03 +00:00
|
|
|
cfg.TCPPort = 14001
|
2019-08-21 06:08:30 +00:00
|
|
|
|
2021-02-12 17:45:22 +00:00
|
|
|
s, err = NewService(context.Background(), cfg)
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-14 20:27:03 +00:00
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
s.Start()
|
|
|
|
<-exitRoutine
|
2020-04-14 16:41:09 +00:00
|
|
|
}()
|
2020-10-16 07:05:40 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2020-04-14 20:27:03 +00:00
|
|
|
// Send in a loop to ensure it is delivered (busy wait for the service to subscribe to the state feed).
|
|
|
|
for sent := 0; sent == 0; {
|
|
|
|
sent = s.stateNotifier.StateFeed().Send(&feed.Event{
|
|
|
|
Type: statefeed.Initialized,
|
|
|
|
Data: &statefeed.InitializedData{
|
|
|
|
StartTime: genesisTime,
|
|
|
|
GenesisValidatorsRoot: genesisValidatorsRoot,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2019-09-06 19:20:20 +00:00
|
|
|
time.Sleep(4 * time.Second)
|
2021-01-07 16:42:03 +00:00
|
|
|
assert.Equal(t, 5, len(s.host.Network().Peers()), "Not all peers added to peerstore")
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, s.Stop())
|
2020-04-14 20:27:03 +00:00
|
|
|
exitRoutine <- true
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
2019-08-23 16:53:38 +00:00
|
|
|
|
|
|
|
func TestPeer_Disconnect(t *testing.T) {
|
2022-05-20 07:16:53 +00:00
|
|
|
params.SetupTestConfigCleanup(t)
|
2019-08-23 16:53:38 +00:00
|
|
|
h1, _, _ := createHost(t, 5000)
|
2020-04-14 16:41:09 +00:00
|
|
|
defer func() {
|
|
|
|
if err := h1.Close(); err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
}
|
|
|
|
}()
|
2019-08-23 16:53:38 +00:00
|
|
|
|
|
|
|
s := &Service{
|
|
|
|
host: h1,
|
|
|
|
}
|
|
|
|
|
|
|
|
h2, _, ipaddr := createHost(t, 5001)
|
2020-04-14 16:41:09 +00:00
|
|
|
defer func() {
|
|
|
|
if err := h2.Close(); err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
}
|
|
|
|
}()
|
2019-08-23 16:53:38 +00:00
|
|
|
|
|
|
|
h2Addr, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%s", ipaddr, 5001, h2.ID()))
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2019-08-23 16:53:38 +00:00
|
|
|
addrInfo, err := peer.AddrInfoFromP2pAddr(h2Addr)
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, s.host.Connect(context.Background(), *addrInfo))
|
|
|
|
assert.Equal(t, 1, len(s.host.Network().Peers()), "Invalid number of peers")
|
|
|
|
assert.Equal(t, 1, len(s.host.Network().Conns()), "Invalid number of connections")
|
|
|
|
require.NoError(t, s.Disconnect(h2.ID()))
|
|
|
|
assert.Equal(t, 0, len(s.host.Network().Conns()), "Invalid number of connections")
|
2019-08-23 16:53:38 +00:00
|
|
|
}
|
2020-07-13 02:28:40 +00:00
|
|
|
|
|
|
|
func TestService_JoinLeaveTopic(t *testing.T) {
|
2022-05-20 07:16:53 +00:00
|
|
|
params.SetupTestConfigCleanup(t)
|
2020-10-16 07:05:40 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancel()
|
2021-02-12 17:45:22 +00:00
|
|
|
s, err := NewService(ctx, &Config{StateNotifier: &mock.MockStateNotifier{}})
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-16 07:05:40 +00:00
|
|
|
|
|
|
|
go s.awaitStateInitialized()
|
|
|
|
fd := initializeStateWithForkDigest(ctx, t, s.stateNotifier.StateFeed())
|
|
|
|
|
2020-07-14 16:51:39 +00:00
|
|
|
assert.Equal(t, 0, len(s.joinedTopics))
|
2020-07-13 02:28:40 +00:00
|
|
|
|
2020-10-16 07:05:40 +00:00
|
|
|
topic := fmt.Sprintf(AttestationSubnetTopicFormat, fd, 42) + "/" + encoder.ProtocolSuffixSSZSnappy
|
2020-07-13 02:28:40 +00:00
|
|
|
topicHandle, err := s.JoinTopic(topic)
|
2020-07-14 16:51:39 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(s.joinedTopics))
|
2020-07-13 02:28:40 +00:00
|
|
|
|
2020-10-16 07:05:40 +00:00
|
|
|
if topicHandle == nil {
|
|
|
|
t.Fatal("topic is nil")
|
|
|
|
}
|
|
|
|
|
2020-07-13 02:28:40 +00:00
|
|
|
sub, err := topicHandle.Subscribe()
|
2020-07-14 16:51:39 +00:00
|
|
|
assert.NoError(t, err)
|
2020-07-13 02:28:40 +00:00
|
|
|
|
|
|
|
// Try leaving topic that has subscriptions.
|
|
|
|
want := "cannot close topic: outstanding event handlers or subscriptions"
|
2020-07-14 16:51:39 +00:00
|
|
|
assert.ErrorContains(t, want, s.LeaveTopic(topic))
|
2020-07-13 02:28:40 +00:00
|
|
|
|
|
|
|
// After subscription is cancelled, leaving topic should not result in error.
|
|
|
|
sub.Cancel()
|
2020-07-14 16:51:39 +00:00
|
|
|
assert.NoError(t, s.LeaveTopic(topic))
|
2020-07-13 02:28:40 +00:00
|
|
|
}
|
2020-10-16 07:05:40 +00:00
|
|
|
|
|
|
|
// initializeStateWithForkDigest sets up the state feed initialized event and returns the fork
|
|
|
|
// digest associated with that genesis event.
|
|
|
|
func initializeStateWithForkDigest(ctx context.Context, t *testing.T, ef *event.Feed) [4]byte {
|
2021-09-15 00:09:04 +00:00
|
|
|
gt := prysmTime.Now()
|
2022-02-14 13:34:38 +00:00
|
|
|
gvr := bytesutil.PadTo([]byte("genesis validators root"), 32)
|
2020-10-16 07:05:40 +00:00
|
|
|
for n := 0; n == 0; {
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
t.Fatal(ctx.Err())
|
|
|
|
}
|
|
|
|
n = ef.Send(&feed.Event{
|
|
|
|
Type: statefeed.Initialized,
|
|
|
|
Data: &statefeed.InitializedData{
|
|
|
|
StartTime: gt,
|
|
|
|
GenesisValidatorsRoot: gvr,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-17 19:20:50 +00:00
|
|
|
fd, err := forks.CreateForkDigest(gt, gvr)
|
2020-10-16 07:05:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
time.Sleep(50 * time.Millisecond) // wait for pubsub filter to initialize.
|
|
|
|
|
|
|
|
return fd
|
|
|
|
}
|
2020-11-21 20:09:07 +00:00
|
|
|
|
|
|
|
func TestService_connectWithPeer(t *testing.T) {
|
2022-05-20 07:16:53 +00:00
|
|
|
params.SetupTestConfigCleanup(t)
|
2020-11-21 20:09:07 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
peers *peers.Status
|
|
|
|
info peer.AddrInfo
|
|
|
|
wantErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "bad peer",
|
|
|
|
peers: func() *peers.Status {
|
|
|
|
ps := peers.NewStatus(context.Background(), &peers.StatusConfig{
|
|
|
|
ScorerParams: &scorers.Config{},
|
|
|
|
})
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
ps.Scorers().BadResponsesScorer().Increment("bad")
|
|
|
|
}
|
|
|
|
return ps
|
|
|
|
}(),
|
|
|
|
info: peer.AddrInfo{ID: "bad"},
|
|
|
|
wantErr: "refused to connect to bad peer",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
h, _, _ := createHost(t, 34567)
|
|
|
|
defer func() {
|
|
|
|
if err := h.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
ctx := context.Background()
|
|
|
|
s := &Service{
|
|
|
|
host: h,
|
|
|
|
peers: tt.peers,
|
|
|
|
}
|
|
|
|
err := s.connectWithPeer(ctx, tt.info)
|
|
|
|
if len(tt.wantErr) > 0 {
|
|
|
|
require.ErrorContains(t, tt.wantErr, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|