From 413ae2fd1ea0bc91986744f32fe62ee1ec7f0477 Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Thu, 28 Feb 2019 23:03:25 -0800 Subject: [PATCH] add bootnode parameter in networkinfo service instead of always using global bootnode, networkinfo now support a list of new bootnodes. This is to enable 2nd stage peer discovery, which can use existing beacon peers as the bootnode to discover shard peers. Signed-off-by: Leo Chen --- api/service/networkinfo/service.go | 10 ++++++-- api/service/networkinfo/service_test.go | 33 +++++++++++++++++++++++++ node/service_setup.go | 12 ++++----- 3 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 api/service/networkinfo/service_test.go diff --git a/api/service/networkinfo/service.go b/api/service/networkinfo/service.go index 336ecb813..59dd131fe 100644 --- a/api/service/networkinfo/service.go +++ b/api/service/networkinfo/service.go @@ -21,6 +21,7 @@ import ( type Service struct { Host p2p.Host Rendezvous p2p.GroupID + bootnodes utils.AddrList dht *libp2pdht.IpfsDHT ctx context.Context cancel context.CancelFunc @@ -33,7 +34,7 @@ type Service struct { } // New returns role conversion service. -func New(h p2p.Host, rendezvous p2p.GroupID, peerChan chan p2p.Peer) *Service { +func New(h p2p.Host, rendezvous p2p.GroupID, peerChan chan p2p.Peer, bootnodes utils.AddrList) *Service { timeout := 30 * time.Minute ctx, cancel := context.WithTimeout(context.Background(), timeout) dht, err := libp2pdht.New(ctx, h.GetP2PHost()) @@ -50,6 +51,7 @@ func New(h p2p.Host, rendezvous p2p.GroupID, peerChan chan p2p.Peer) *Service { stopChan: make(chan struct{}), stoppedChan: make(chan struct{}), peerChan: peerChan, + bootnodes: bootnodes, } } @@ -71,7 +73,11 @@ func (s *Service) Init() error { } var wg sync.WaitGroup - for _, peerAddr := range utils.BootNodes { + if s.bootnodes == nil { + s.bootnodes = utils.BootNodes + } + + for _, peerAddr := range s.bootnodes { peerinfo, _ := peerstore.InfoFromP2pAddr(peerAddr) wg.Add(1) go func() { diff --git a/api/service/networkinfo/service_test.go b/api/service/networkinfo/service_test.go new file mode 100644 index 000000000..b6a61f4d5 --- /dev/null +++ b/api/service/networkinfo/service_test.go @@ -0,0 +1,33 @@ +package networkinfo + +import ( + "testing" + "time" + + "github.com/harmony-one/harmony/internal/utils" + "github.com/harmony-one/harmony/p2p" + "github.com/harmony-one/harmony/p2p/p2pimpl" +) + +func TestService(t *testing.T) { + nodePriKey, _, err := utils.LoadKeyFromFile("/tmp/127.0.0.1.12345.key") + if err != nil { + t.Fatal(err) + } + peerPriKey, peerPubKey := utils.GenKey("127.0.0.1", "12345") + if peerPriKey == nil || peerPubKey == nil { + t.Fatal("generate key error") + } + selfPeer := p2p.Peer{IP: "127.0.0.1", Port: "12345", ValidatorID: -1, PubKey: peerPubKey} + + host, err := p2pimpl.NewHost(&selfPeer, nodePriKey) + if err != nil { + t.Fatal("unable to new host in harmony") + } + + s := New(host, "teststring", nil, nil) + s.StartService() + + time.Sleep(2 * time.Second) + s.StopService() +} diff --git a/node/service_setup.go b/node/service_setup.go index c432e31aa..08b8afb5a 100644 --- a/node/service_setup.go +++ b/node/service_setup.go @@ -28,7 +28,7 @@ func (node *Node) setupForShardLeader() { // Register peer discovery service. No need to do staking for beacon chain node. node.serviceManager.RegisterService(service.PeerDiscovery, discovery.New(node.host, nodeConfig, chanPeer, node.AddBeaconPeer)) // Register networkinfo service. "0" is the beacon shard ID - node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer)) + node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer, nil)) // Register explorer service. node.serviceManager.RegisterService(service.SupportExplorer, explorer.New(&node.SelfPeer)) @@ -48,7 +48,7 @@ func (node *Node) setupForShardValidator() { // Register peer discovery service. "0" is the beacon shard ID. No need to do staking for beacon chain node. node.serviceManager.RegisterService(service.PeerDiscovery, discovery.New(node.host, nodeConfig, chanPeer, node.AddBeaconPeer)) // Register networkinfo service. "0" is the beacon shard ID - node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer)) + node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer, nil)) } func (node *Node) setupForBeaconLeader() { @@ -57,7 +57,7 @@ func (node *Node) setupForBeaconLeader() { // Register peer discovery service. No need to do staking for beacon chain node. node.serviceManager.RegisterService(service.PeerDiscovery, discovery.New(node.host, nodeConfig, chanPeer, nil)) // Register networkinfo service. - node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer)) + node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer, nil)) // Register consensus service. node.serviceManager.RegisterService(service.Consensus, consensus.New(node.BlockChannel, node.Consensus, node.startConsensus)) // Register new block service. @@ -74,7 +74,7 @@ func (node *Node) setupForBeaconValidator() { // Register peer discovery service. No need to do staking for beacon chain node. node.serviceManager.RegisterService(service.PeerDiscovery, discovery.New(node.host, nodeConfig, chanPeer, nil)) // Register networkinfo service. - node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer)) + node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer, nil)) } func (node *Node) setupForNewNode() { @@ -85,7 +85,7 @@ func (node *Node) setupForNewNode() { // Register peer discovery service. "0" is the beacon shard ID node.serviceManager.RegisterService(service.PeerDiscovery, discovery.New(node.host, nodeConfig, chanPeer, node.AddBeaconPeer)) // Register networkinfo service. "0" is the beacon shard ID - node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer)) + node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer, nil)) // TODO: how to restart networkinfo and discovery service after receiving shard id info from beacon chain? } @@ -96,7 +96,7 @@ func (node *Node) setupForClientNode() { // Register peer discovery service. node.serviceManager.RegisterService(service.PeerDiscovery, discovery.New(node.host, nodeConfig, chanPeer, nil)) // Register networkinfo service. "0" is the beacon shard ID - node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer)) + node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer, nil)) } // AddBeaconChainDatabase adds database support for beaconchain blocks on normal sharding nodes (not BeaconChain node)