diff --git a/api/proto/node/pingpong.go b/api/proto/node/pingpong.go index 18d626380..a461f3d5d 100644 --- a/api/proto/node/pingpong.go +++ b/api/proto/node/pingpong.go @@ -54,6 +54,10 @@ type Info struct { PeerID peer.ID // Peerstore ID } +func (info Info) String() string { + return fmt.Sprintf("Info:%v/%v=>%v/%v", info.IP, info.Port, info.ValidatorID, info.PeerID) +} + // PingMessageType defines the data structure of the Ping message type PingMessageType struct { Version uint16 // version of the protocol diff --git a/cmd/beaconchain/main.go b/cmd/beaconchain/main.go index f44c65df0..7a502a2ce 100644 --- a/cmd/beaconchain/main.go +++ b/cmd/beaconchain/main.go @@ -8,6 +8,7 @@ import ( "github.com/ethereum/go-ethereum/log" beaconchain "github.com/harmony-one/harmony/internal/beaconchain/libs" + "github.com/harmony-one/harmony/internal/utils" ) var ( @@ -46,7 +47,9 @@ func main() { } else { fmt.Printf("Starting new beaconchain\n") beaconchain.SetSaveFile(*resetFlag) - bc = beaconchain.New(*numShards, *ip, *port) + + priKey, _, _ := utils.GenKeyP2P(*ip, *port) + bc = beaconchain.New(*numShards, *ip, *port, priKey) } fmt.Printf("Beacon Chain Started: /ip4/%s/tcp/%v/ipfs/%s\n", *ip, *port, bc.GetID().Pretty()) diff --git a/cmd/client/txgen/main.go b/cmd/client/txgen/main.go index 97a9e6e56..7efe4cd31 100644 --- a/cmd/client/txgen/main.go +++ b/cmd/client/txgen/main.go @@ -16,6 +16,7 @@ import ( "github.com/harmony-one/harmony/consensus" "github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/internal/newnode" + "github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/node" "github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/p2p/p2pimpl" @@ -65,6 +66,7 @@ func main() { var bcPeer *p2p.Peer var shardIDLeaderMap map[uint32]p2p.Peer + priKey, _, err := utils.GenKeyP2P(*ip, *port) if *bcAddr != "" { // Turn the destination into a multiaddr. @@ -84,7 +86,7 @@ func main() { bcPeer = &p2p.Peer{IP: *bcIP, Port: *bcPort} } - candidateNode := newnode.New(*ip, *port) + candidateNode := newnode.New(*ip, *port, priKey) candidateNode.AddPeer(bcPeer) candidateNode.ContactBeaconChain(*bcPeer) selfPeer := candidateNode.GetSelfPeer() @@ -113,7 +115,7 @@ func main() { // Nodes containing blockchain data to mirror the shards' data in the network nodes := []*node.Node{} - host, err := p2pimpl.NewHost(&selfPeer) + host, err := p2pimpl.NewHost(&selfPeer, priKey) if err != nil { panic("unable to new host in txgen") } diff --git a/cmd/client/wallet/main.go b/cmd/client/wallet/main.go index fd3c8adc1..7c697a208 100644 --- a/cmd/client/wallet/main.go +++ b/cmd/client/wallet/main.go @@ -24,6 +24,7 @@ import ( "github.com/harmony-one/harmony/core/types" libs "github.com/harmony-one/harmony/internal/beaconchain/libs" beaconchain "github.com/harmony-one/harmony/internal/beaconchain/rpc" + "github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/node" "github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/p2p/p2pimpl" @@ -299,7 +300,8 @@ func CreateWalletNode() *node.Node { // dummy host for wallet self := p2p.Peer{IP: "127.0.0.1", Port: "6789"} - host, _ := p2pimpl.NewHost(&self) + priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "6789") + host, _ := p2pimpl.NewHost(&self, priKey) walletNode := node.New(host, nil, nil) walletNode.Client = client.NewClient(walletNode.GetHost(), &shardIDLeaderMap) return walletNode diff --git a/cmd/harmony.go b/cmd/harmony.go index eac75505d..b8a2b4dcd 100644 --- a/cmd/harmony.go +++ b/cmd/harmony.go @@ -119,6 +119,7 @@ func main() { var selfPeer p2p.Peer var clientPeer *p2p.Peer var BCPeer *p2p.Peer + priKey, _, err := utils.GenKeyP2P(*ip, *port) if *bcAddr != "" { // Turn the destination into a multiaddr. @@ -139,7 +140,7 @@ func main() { } //Use Peer Discovery to get shard/leader/peer/... - candidateNode := pkg_newnode.New(*ip, *port) + candidateNode := pkg_newnode.New(*ip, *port, priKey) candidateNode.AddPeer(BCPeer) candidateNode.ContactBeaconChain(*BCPeer) @@ -149,7 +150,7 @@ func main() { clientPeer = candidateNode.GetClientPeer() selfPeer.PubKey = candidateNode.PubK - // fmt.Println(peers, leader, selfPeer, clientPeer, *logFolder, *minPeers) //TODO: to be replaced by a logger later: ak, rl + fmt.Println("Harmnoy", leader, selfPeer) var role string if leader.IP == *ip && leader.Port == *port { @@ -172,7 +173,7 @@ func main() { ldb, _ = InitLDBDatabase(*ip, *port, *freshDB) } - host, err := p2pimpl.NewHost(&selfPeer) + host, err := p2pimpl.NewHost(&selfPeer, priKey) if err != nil { panic("unable to new host in harmony") } diff --git a/internal/beaconchain/libs/beaconchain.go b/internal/beaconchain/libs/beaconchain.go index 6f6ee8bb6..65bb63297 100644 --- a/internal/beaconchain/libs/beaconchain.go +++ b/internal/beaconchain/libs/beaconchain.go @@ -18,6 +18,7 @@ import ( "github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/p2p/host" "github.com/harmony-one/harmony/p2p/p2pimpl" + p2p_crypto "github.com/libp2p/go-libp2p-crypto" peer "github.com/libp2p/go-libp2p-peer" ) @@ -93,12 +94,12 @@ func (bc *BeaconChain) GetShardLeaderMap() map[int]*node.Info { } //New beaconchain initialization -func New(numShards int, ip, port string) *BeaconChain { +func New(numShards int, ip, port string, key p2p_crypto.PrivKey) *BeaconChain { bc := BeaconChain{} bc.log = log.New() bc.PubKey = generateBCKey() bc.Self = p2p.Peer{IP: ip, Port: port} - bc.host, _ = p2pimpl.NewHost(&bc.Self) + bc.host, _ = p2pimpl.NewHost(&bc.Self, key) bcinfo := &BCInfo{NumberOfShards: numShards, NumberOfNodesAdded: 0, IP: ip, Port: port, diff --git a/internal/newnode/newnode.go b/internal/newnode/newnode.go index 42947120e..72e5f8687 100644 --- a/internal/newnode/newnode.go +++ b/internal/newnode/newnode.go @@ -19,6 +19,7 @@ import ( "github.com/harmony-one/harmony/p2p/host" "github.com/harmony-one/harmony/p2p/p2pimpl" + p2p_crypto "github.com/libp2p/go-libp2p-crypto" multiaddr "github.com/multiformats/go-multiaddr" ) @@ -39,7 +40,7 @@ type NewNode struct { } // New candidatenode initialization -func New(ip string, port string) *NewNode { +func New(ip string, port string, nodePk p2p_crypto.PrivKey) *NewNode { priKey, pubKey := utils.GenKey(ip, port) var node NewNode var err error @@ -48,8 +49,7 @@ func New(ip string, port string) *NewNode { node.Self = p2p.Peer{IP: ip, Port: port, PubKey: pubKey, ValidatorID: -1} node.log = utils.GetLogInstance() node.SetInfo = make(chan bool) - node.host, err = p2pimpl.NewHost(&node.Self) - node.log.Info("NewNode New", "Self", node.Self) + node.host, err = p2pimpl.NewHost(&node.Self, nodePk) if err != nil { node.log.Error("failed to create new host", "msg", err) return nil @@ -81,6 +81,7 @@ func (node *NewNode) requestBeaconChain(BCPeer p2p.Peer) (err error) { if err != nil { node.log.Error("Could not Marshall public key into binary") } + fmt.Printf("[New Node]: %v\n", *node) nodeInfo := &proto_node.Info{IP: node.Self.IP, Port: node.Self.Port, PubKey: pubk, PeerID: node.Self.PeerID} msg := bcconn.SerializeNodeInfo(nodeInfo) msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.Register, msg) diff --git a/p2p/host/hostv2/hostv2.go b/p2p/host/hostv2/hostv2.go index 80f9157ed..b081c9ec6 100644 --- a/p2p/host/hostv2/hostv2.go +++ b/p2p/host/hostv2/hostv2.go @@ -13,7 +13,7 @@ import ( net "github.com/libp2p/go-libp2p-net" peer "github.com/libp2p/go-libp2p-peer" peerstore "github.com/libp2p/go-libp2p-peerstore" - multiaddr "github.com/multiformats/go-multiaddr" + ma "github.com/multiformats/go-multiaddr" ) const ( @@ -45,7 +45,7 @@ func (host *HostV2) AddPeer(p *p2p.Peer) error { // reconstruct the multiaddress based on ip/port // PeerID has to be known for the ip/port addr := fmt.Sprintf("/ip4/%s/tcp/%s", p.IP, p.Port) - targetAddr, err := multiaddr.NewMultiaddr(addr) + targetAddr, err := ma.NewMultiaddr(addr) if err != nil { log.Error("AddPeer NewMultiaddr error", "error", err) return err @@ -65,23 +65,29 @@ func (host *HostV2) Peerstore() peerstore.Peerstore { } // New creates a host for p2p communication -func New(self p2p.Peer, priKey p2p_crypto.PrivKey) *HostV2 { - +func New(self *p2p.Peer, priKey p2p_crypto.PrivKey) *HostV2 { + listenAddr, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/%s", self.Port)) + if err != nil { + log.Error("New MA Error", "IP", self.IP, "Port", self.Port) + return nil + } // TODO (leo), use the [0] of Addrs for now, need to find a reliable way of using listenAddr p2pHost, err := libp2p.New(context.Background(), - libp2p.ListenAddrs(self.Addrs[0]), + libp2p.ListenAddrs(listenAddr), libp2p.Identity(priKey), // TODO(ricl): Other features to probe // libp2p.EnableRelay; libp2p.Routing; ) + self.PeerID = p2pHost.ID() + catchError(err) - log.Debug("HostV2 is up!", "port", self.Port, "id", p2pHost.ID().Pretty(), "addr", self.Addrs) + log.Debug("HostV2 is up!", "port", self.Port, "id", p2pHost.ID().Pretty(), "addr", listenAddr) // has to save the private key for host h := &HostV2{ h: p2pHost, - self: self, + self: *self, priKey: priKey, } diff --git a/p2p/p2pimpl/p2pimpl.go b/p2p/p2pimpl/p2pimpl.go index 5c589c86a..ec6cc4b7f 100644 --- a/p2p/p2pimpl/p2pimpl.go +++ b/p2p/p2pimpl/p2pimpl.go @@ -1,7 +1,6 @@ package p2pimpl import ( - "fmt" "net" "github.com/harmony-one/harmony/p2p" @@ -9,8 +8,7 @@ import ( "github.com/harmony-one/harmony/p2p/host/hostv2" "github.com/harmony-one/harmony/internal/utils" - peer "github.com/libp2p/go-libp2p-peer" - ma "github.com/multiformats/go-multiaddr" + p2p_crypto "github.com/libp2p/go-libp2p-crypto" ) // Version The version number of p2p library @@ -22,33 +20,13 @@ const Version = 2 // for hostv2, it generates multiaddress, keypair and add PeerID to peer, add priKey to host // TODO (leo) the PriKey of the host has to be persistent in disk, so that we don't need to regenerate it // on the same host if the node software restarted. The peerstore has to be persistent as well. -func NewHost(self *p2p.Peer) (p2p.Host, error) { +func NewHost(self *p2p.Peer, key p2p_crypto.PrivKey) (p2p.Host, error) { if Version == 1 { h := hostv1.New(self) return h, nil } - selfAddr, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/%s", self.Port)) - if err != nil { - return nil, err - } - self.Addrs = append(self.Addrs, selfAddr) - - // TODO (leo), change to GenKeyP2PRand() to generate random key. Right now, the key is predictable as the - // seed is fixed. - priKey, pubKey, err := utils.GenKeyP2P(self.IP, self.Port) - if err != nil { - return nil, err - } - - peerID, err := peer.IDFromPublicKey(pubKey) - - if err != nil { - return nil, err - } - - self.PeerID = peerID - h := hostv2.New(*self, priKey) + h := hostv2.New(self, key) utils.GetLogInstance().Info("NewHost", "self", net.JoinHostPort(self.IP, self.Port), "PeerID", self.PeerID)