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 <leo@harmony.one>
pull/519/head
Leo Chen 6 years ago
parent 60a10200c1
commit 413ae2fd1e
  1. 10
      api/service/networkinfo/service.go
  2. 33
      api/service/networkinfo/service_test.go
  3. 12
      node/service_setup.go

@ -21,6 +21,7 @@ import (
type Service struct { type Service struct {
Host p2p.Host Host p2p.Host
Rendezvous p2p.GroupID Rendezvous p2p.GroupID
bootnodes utils.AddrList
dht *libp2pdht.IpfsDHT dht *libp2pdht.IpfsDHT
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
@ -33,7 +34,7 @@ type Service struct {
} }
// New returns role conversion service. // 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 timeout := 30 * time.Minute
ctx, cancel := context.WithTimeout(context.Background(), timeout) ctx, cancel := context.WithTimeout(context.Background(), timeout)
dht, err := libp2pdht.New(ctx, h.GetP2PHost()) 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{}), stopChan: make(chan struct{}),
stoppedChan: make(chan struct{}), stoppedChan: make(chan struct{}),
peerChan: peerChan, peerChan: peerChan,
bootnodes: bootnodes,
} }
} }
@ -71,7 +73,11 @@ func (s *Service) Init() error {
} }
var wg sync.WaitGroup 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) peerinfo, _ := peerstore.InfoFromP2pAddr(peerAddr)
wg.Add(1) wg.Add(1)
go func() { go func() {

@ -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()
}

@ -28,7 +28,7 @@ func (node *Node) setupForShardLeader() {
// Register peer discovery service. No need to do staking for beacon chain node. // 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)) node.serviceManager.RegisterService(service.PeerDiscovery, discovery.New(node.host, nodeConfig, chanPeer, node.AddBeaconPeer))
// Register networkinfo service. "0" is the beacon shard ID // 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. // Register explorer service.
node.serviceManager.RegisterService(service.SupportExplorer, explorer.New(&node.SelfPeer)) 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. // 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)) node.serviceManager.RegisterService(service.PeerDiscovery, discovery.New(node.host, nodeConfig, chanPeer, node.AddBeaconPeer))
// Register networkinfo service. "0" is the beacon shard ID // 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() { 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. // 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)) node.serviceManager.RegisterService(service.PeerDiscovery, discovery.New(node.host, nodeConfig, chanPeer, nil))
// Register networkinfo service. // 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. // Register consensus service.
node.serviceManager.RegisterService(service.Consensus, consensus.New(node.BlockChannel, node.Consensus, node.startConsensus)) node.serviceManager.RegisterService(service.Consensus, consensus.New(node.BlockChannel, node.Consensus, node.startConsensus))
// Register new block service. // 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. // 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)) node.serviceManager.RegisterService(service.PeerDiscovery, discovery.New(node.host, nodeConfig, chanPeer, nil))
// Register networkinfo service. // 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() { func (node *Node) setupForNewNode() {
@ -85,7 +85,7 @@ func (node *Node) setupForNewNode() {
// Register peer discovery service. "0" is the beacon shard ID // Register peer discovery service. "0" is the beacon shard ID
node.serviceManager.RegisterService(service.PeerDiscovery, discovery.New(node.host, nodeConfig, chanPeer, node.AddBeaconPeer)) node.serviceManager.RegisterService(service.PeerDiscovery, discovery.New(node.host, nodeConfig, chanPeer, node.AddBeaconPeer))
// Register networkinfo service. "0" is the beacon shard ID // 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? // 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. // Register peer discovery service.
node.serviceManager.RegisterService(service.PeerDiscovery, discovery.New(node.host, nodeConfig, chanPeer, nil)) node.serviceManager.RegisterService(service.PeerDiscovery, discovery.New(node.host, nodeConfig, chanPeer, nil))
// Register networkinfo service. "0" is the beacon shard ID // 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) // AddBeaconChainDatabase adds database support for beaconchain blocks on normal sharding nodes (not BeaconChain node)

Loading…
Cancel
Save