support the peerstore in beacon chain node

Signed-off-by: Leo Chen <leo@harmony.one>
pull/280/head
Leo Chen 6 years ago
parent 5822ec3c40
commit ccd9728069
  1. 15
      internal/beaconchain/libs/beaconchain.go
  2. 29
      internal/newnode/newnode.go

@ -18,6 +18,7 @@ import (
"github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/p2p"
"github.com/harmony-one/harmony/p2p/host" "github.com/harmony-one/harmony/p2p/host"
"github.com/harmony-one/harmony/p2p/p2pimpl" "github.com/harmony-one/harmony/p2p/p2pimpl"
peer "github.com/libp2p/go-libp2p-peer"
) )
//BCState keeps track of the state the beaconchain is in //BCState keeps track of the state the beaconchain is in
@ -49,6 +50,7 @@ type BeaconChain struct {
state BCState state BCState
rpcServer *beaconchain.Server rpcServer *beaconchain.Server
Peer p2p.Peer Peer p2p.Peer
Self p2p.Peer // self Peer
} }
//SaveFile is to store the file in which beaconchain info will be stored. //SaveFile is to store the file in which beaconchain info will be stored.
@ -95,7 +97,8 @@ func New(numShards int, ip, port string) *BeaconChain {
bc := BeaconChain{} bc := BeaconChain{}
bc.log = log.New() bc.log = log.New()
bc.PubKey = generateBCKey() bc.PubKey = generateBCKey()
bc.host = p2pimpl.NewHost(p2p.Peer{IP: ip, Port: port}) bc.Self = p2p.Peer{IP: ip, Port: port}
bc.host, _ = p2pimpl.NewHost(&bc.Self)
bcinfo := &BCInfo{NumberOfShards: numShards, NumberOfNodesAdded: 0, bcinfo := &BCInfo{NumberOfShards: numShards, NumberOfNodesAdded: 0,
IP: ip, IP: ip,
Port: port, Port: port,
@ -114,9 +117,10 @@ func generateBCKey() kyber.Point {
//AcceptNodeInfo deserializes node information received via beaconchain handler //AcceptNodeInfo deserializes node information received via beaconchain handler
func (bc *BeaconChain) AcceptNodeInfo(b []byte) *node.Info { func (bc *BeaconChain) AcceptNodeInfo(b []byte) *node.Info {
Node := bcconn.DeserializeNodeInfo(b) Node := bcconn.DeserializeNodeInfo(b)
bc.log.Info("New Node Connection", "IP", Node.IP, "Port", Node.Port) bc.log.Info("New Node Connection", "IP", Node.IP, "Port", Node.Port, "PeerID", Node.PeerID)
bc.Peer = p2p.Peer{IP: Node.IP, Port: Node.Port} bc.Peer = p2p.Peer{IP: Node.IP, Port: Node.Port, PeerID: Node.PeerID}
bc.host.AddPeer(&bc.Peer) bc.host.AddPeer(&bc.Peer)
bc.BCInfo.NumberOfNodesAdded = bc.BCInfo.NumberOfNodesAdded + 1 bc.BCInfo.NumberOfNodesAdded = bc.BCInfo.NumberOfNodesAdded + 1
shardNum, isLeader := utils.AllocateShard(bc.BCInfo.NumberOfNodesAdded, bc.BCInfo.NumberOfShards) shardNum, isLeader := utils.AllocateShard(bc.BCInfo.NumberOfNodesAdded, bc.BCInfo.NumberOfShards)
if isLeader { if isLeader {
@ -189,3 +193,8 @@ func BCItoBC(bci *BCInfo) *BeaconChain {
func SetSaveFile(path string) { func SetSaveFile(path string) {
SaveFile = path SaveFile = path
} }
//GetID return ID
func (bc *BeaconChain) GetID() peer.ID {
return bc.host.GetID()
}

@ -18,6 +18,8 @@ import (
"github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/p2p"
"github.com/harmony-one/harmony/p2p/host" "github.com/harmony-one/harmony/p2p/host"
"github.com/harmony-one/harmony/p2p/p2pimpl" "github.com/harmony-one/harmony/p2p/p2pimpl"
multiaddr "github.com/multiformats/go-multiaddr"
) )
//NewNode is ther struct for a candidate node //NewNode is ther struct for a candidate node
@ -40,12 +42,17 @@ type NewNode struct {
func New(ip string, port string) *NewNode { func New(ip string, port string) *NewNode {
priKey, pubKey := utils.GenKey(ip, port) priKey, pubKey := utils.GenKey(ip, port)
var node NewNode var node NewNode
var err error
node.PubK = pubKey node.PubK = pubKey
node.priK = priKey node.priK = priKey
node.Self = p2p.Peer{IP: ip, Port: port, PubKey: pubKey, ValidatorID: -1} node.Self = p2p.Peer{IP: ip, Port: port, PubKey: pubKey, ValidatorID: -1}
node.log = log.New() node.log = log.New()
node.SetInfo = false node.SetInfo = false
node.host = p2pimpl.NewHost(node.Self) node.host, err = p2pimpl.NewHost(&node.Self)
if err != nil {
node.log.Error("failed to create new host", "msg", err)
return nil
}
node.Leaders = map[uint32]p2p.Peer{} node.Leaders = map[uint32]p2p.Peer{}
return &node return &node
} }
@ -73,7 +80,7 @@ func (node *NewNode) requestBeaconChain(BCPeer p2p.Peer) (err error) {
if err != nil { if err != nil {
node.log.Error("Could not Marshall public key into binary") node.log.Error("Could not Marshall public key into binary")
} }
nodeInfo := &proto_node.Info{IP: node.Self.IP, Port: node.Self.Port, PubKey: pubk} nodeInfo := &proto_node.Info{IP: node.Self.IP, Port: node.Self.Port, PubKey: pubk, PeerID: node.host.GetID()}
msg := bcconn.SerializeNodeInfo(nodeInfo) msg := bcconn.SerializeNodeInfo(nodeInfo)
msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.Register, msg) msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.Register, msg)
gotShardInfo := false gotShardInfo := false
@ -108,9 +115,18 @@ func (node *NewNode) processShardInfo(msgPayload []byte) bool {
leaders := leadersInfo.Leaders leaders := leadersInfo.Leaders
shardNum, isLeader := utils.AllocateShard(leadersInfo.NumberOfNodesAdded, leadersInfo.NumberOfShards) shardNum, isLeader := utils.AllocateShard(leadersInfo.NumberOfNodesAdded, leadersInfo.NumberOfShards)
for n, v := range leaders { for n, v := range leaders {
leaderPeer := p2p.Peer{IP: v.IP, Port: v.Port} leaderPeer := p2p.Peer{IP: v.IP, Port: v.Port, PeerID: v.PeerID}
addr := fmt.Sprintf("/ip4/%s/tcp/%s", leaderPeer.IP, leaderPeer.Port)
targetAddr, err := multiaddr.NewMultiaddr(addr)
if err != nil {
log.Error("processShardInfo NewMultiaddr error", "error", err)
return false
}
leaderPeer.Addrs = append(leaderPeer.Addrs, targetAddr)
leaderPeer.PubKey = crypto.Ed25519Curve.Point() leaderPeer.PubKey = crypto.Ed25519Curve.Point()
err := leaderPeer.PubKey.UnmarshalBinary(v.PubKey[:]) err = leaderPeer.PubKey.UnmarshalBinary(v.PubKey[:])
if err != nil { if err != nil {
node.log.Error("Could not unmarshall leaders public key from binary to kyber.point") node.log.Error("Could not unmarshall leaders public key from binary to kyber.point")
} }
@ -144,3 +160,8 @@ func (node *NewNode) GetClientPeer() *p2p.Peer {
func (node *NewNode) GetSelfPeer() p2p.Peer { func (node *NewNode) GetSelfPeer() p2p.Peer {
return node.Self return node.Self
} }
// AddPeer add new peer for newnode
func (node *NewNode) AddPeer(p *p2p.Peer) error {
return node.host.AddPeer(p)
}

Loading…
Cancel
Save