|
|
@ -17,12 +17,13 @@ import ( |
|
|
|
"github.com/harmony-one/harmony/utils" |
|
|
|
"github.com/harmony-one/harmony/utils" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// An uninteresting service.
|
|
|
|
// Service is the server for listening.
|
|
|
|
type Service struct { |
|
|
|
type Service struct { |
|
|
|
ch chan bool |
|
|
|
ch chan bool |
|
|
|
waitGroup *sync.WaitGroup |
|
|
|
waitGroup *sync.WaitGroup |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//NewNode is ther struct for a candidate node
|
|
|
|
type NewNode struct { |
|
|
|
type NewNode struct { |
|
|
|
Role string |
|
|
|
Role string |
|
|
|
ShardID int |
|
|
|
ShardID int |
|
|
@ -38,7 +39,7 @@ type NewNode struct { |
|
|
|
Service *Service |
|
|
|
Service *Service |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//NewNode
|
|
|
|
// New candidatenode initialization
|
|
|
|
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 |
|
|
@ -56,7 +57,7 @@ type registerResponseRandomNumber struct { |
|
|
|
Leaders []*bcconn.NodeInfo |
|
|
|
Leaders []*bcconn.NodeInfo |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// NewService
|
|
|
|
// NewService starts a newservice in the candidate node
|
|
|
|
func (node *NewNode) NewService(ip, port string) *Service { |
|
|
|
func (node *NewNode) NewService(ip, port string) *Service { |
|
|
|
laddr, err := net.ResolveTCPAddr("tcp", ip+":"+port) |
|
|
|
laddr, err := net.ResolveTCPAddr("tcp", ip+":"+port) |
|
|
|
if nil != err { |
|
|
|
if nil != err { |
|
|
@ -67,7 +68,6 @@ func (node *NewNode) NewService(ip, port string) *Service { |
|
|
|
node.log.Crit("cannot start a listener for new node", err) |
|
|
|
node.log.Crit("cannot start a listener for new node", err) |
|
|
|
} |
|
|
|
} |
|
|
|
node.log.Debug("listening on", "address", laddr.String()) |
|
|
|
node.log.Debug("listening on", "address", laddr.String()) |
|
|
|
|
|
|
|
|
|
|
|
node.Service = &Service{ |
|
|
|
node.Service = &Service{ |
|
|
|
ch: make(chan bool), |
|
|
|
ch: make(chan bool), |
|
|
|
waitGroup: &sync.WaitGroup{}, |
|
|
|
waitGroup: &sync.WaitGroup{}, |
|
|
@ -128,7 +128,7 @@ func (node *NewNode) ConnectBeaconChain(BCPeer p2p.Peer) { |
|
|
|
msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.Register, msg) |
|
|
|
msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.Register, msg) |
|
|
|
gotShardInfo := false |
|
|
|
gotShardInfo := false |
|
|
|
timeout := time.After(300 * time.Second) |
|
|
|
timeout := time.After(300 * time.Second) |
|
|
|
tick := time.Tick(1 * time.Second) |
|
|
|
tick := time.Tick(5 * time.Second) |
|
|
|
checkLoop: |
|
|
|
checkLoop: |
|
|
|
for { |
|
|
|
for { |
|
|
|
select { |
|
|
|
select { |
|
|
@ -156,8 +156,6 @@ 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) |
|
|
|
leaderNode := leaders[shardNum-1] //0 indexing.
|
|
|
|
leaderNode := leaders[shardNum-1] //0 indexing.
|
|
|
|
//node.leader = leaderNode.Self //Does not have public key.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
leaderPeer := p2p.Peer{IP: leaderNode.Self.IP, Port: leaderNode.Self.Port} |
|
|
|
leaderPeer := p2p.Peer{IP: leaderNode.Self.IP, Port: leaderNode.Self.Port} |
|
|
|
leaderPeer.PubKey = crypto.Ed25519Curve.Point() |
|
|
|
leaderPeer.PubKey = crypto.Ed25519Curve.Point() |
|
|
|
err := leaderPeer.PubKey.UnmarshalBinary(leaderNode.PubK[:]) |
|
|
|
err := leaderPeer.PubKey.UnmarshalBinary(leaderNode.PubK[:]) |
|
|
@ -166,26 +164,28 @@ func (node *NewNode) processShardInfo(msgPayload []byte) bool { |
|
|
|
} |
|
|
|
} |
|
|
|
node.leader = leaderPeer |
|
|
|
node.leader = leaderPeer |
|
|
|
node.isLeader = isLeader |
|
|
|
node.isLeader = isLeader |
|
|
|
node.ShardID = shardNum |
|
|
|
node.ShardID = shardNum - 1 //0 indexing.
|
|
|
|
node.SetInfo = true |
|
|
|
node.SetInfo = true |
|
|
|
node.log.Info("Shard information obtained ..") |
|
|
|
node.log.Info("Shard information obtained ..") |
|
|
|
return true |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GetShardID gives shardid of node
|
|
|
|
func (node *NewNode) GetShardID() string { |
|
|
|
func (node *NewNode) GetShardID() string { |
|
|
|
return strconv.Itoa(node.ShardID) |
|
|
|
return strconv.Itoa(node.ShardID) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GetLeader gives the leader of the node
|
|
|
|
func (node *NewNode) GetLeader() p2p.Peer { |
|
|
|
func (node *NewNode) GetLeader() p2p.Peer { |
|
|
|
|
|
|
|
|
|
|
|
return node.leader |
|
|
|
return node.leader |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GetClientPeer gives the client of the node
|
|
|
|
func (node *NewNode) GetClientPeer() *p2p.Peer { |
|
|
|
func (node *NewNode) GetClientPeer() *p2p.Peer { |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//GetSelfPeer
|
|
|
|
// GetSelfPeer gives the peer part of the node's own struct
|
|
|
|
func (node *NewNode) GetSelfPeer() p2p.Peer { |
|
|
|
func (node *NewNode) GetSelfPeer() p2p.Peer { |
|
|
|
return node.Self |
|
|
|
return node.Self |
|
|
|
} |
|
|
|
} |
|
|
|