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