diff --git a/api/proto/bcconn/bcconn.go b/api/proto/bcconn/bcconn.go index adace3247..d96e1f933 100644 --- a/api/proto/bcconn/bcconn.go +++ b/api/proto/bcconn/bcconn.go @@ -5,24 +5,19 @@ import ( "encoding/gob" "github.com/ethereum/go-ethereum/log" - "github.com/harmony-one/harmony/p2p" -) -//NodeInfo struct exists to share information on the node -type NodeInfo struct { //TODO: to be merged with Leo's nodeinfo. - Self p2p.Peer - PubK []byte -} + "github.com/harmony-one/harmony/api/proto/node" +) //ResponseRandomNumber struct for exchanging random information type ResponseRandomNumber struct { NumberOfShards int NumberOfNodesAdded int - Leaders []*NodeInfo + Leaders []*node.Info } // SerializeNodeInfo is for serializing nodeinfo -func SerializeNodeInfo(nodeinfo *NodeInfo) []byte { +func SerializeNodeInfo(nodeinfo *node.Info) []byte { var result bytes.Buffer encoder := gob.NewEncoder(&result) err := encoder.Encode(nodeinfo) @@ -33,8 +28,8 @@ func SerializeNodeInfo(nodeinfo *NodeInfo) []byte { } // DeserializeNodeInfo deserializes the nodeinfo -func DeserializeNodeInfo(d []byte) *NodeInfo { - var wn NodeInfo +func DeserializeNodeInfo(d []byte) *node.Info { + var wn node.Info r := bytes.NewBuffer(d) decoder := gob.NewDecoder(r) err := decoder.Decode(&wn) diff --git a/api/proto/node/pingpong.go b/api/proto/node/pingpong.go index 9fa45c093..2986982d1 100644 --- a/api/proto/node/pingpong.go +++ b/api/proto/node/pingpong.go @@ -40,10 +40,10 @@ func (r RoleType) String() string { return "Unknown" } -// refer to Peer struct in p2p/peer.go +// Info refers to Peer struct in p2p/peer.go // this is basically a simplified version of Peer // for network transportation -type nodeInfo struct { +type Info struct { IP string Port string PubKey []byte @@ -54,13 +54,13 @@ type nodeInfo struct { // PingMessageType defines the data structure of the Ping message type PingMessageType struct { Version uint16 // version of the protocol - Node nodeInfo + Node Info } // PongMessageType defines the data structure of the Pong message type PongMessageType struct { Version uint16 // version of the protocol - Peers []nodeInfo + Peers []Info PubKeys [][]byte // list of publickKeys, has to be identical among all validators/leaders } @@ -99,11 +99,11 @@ func NewPongMessage(peers []p2p.Peer, pubKeys []kyber.Point) *PongMessageType { pong.PubKeys = make([][]byte, 0) pong.Version = ProtocolVersion - pong.Peers = make([]nodeInfo, 0) + pong.Peers = make([]Info, 0) var err error for _, p := range peers { - n := nodeInfo{} + n := Info{} n.IP = p.IP n.Port = p.Port n.ValidatorID = p.ValidatorID @@ -146,7 +146,7 @@ func GetPingMessage(payload []byte) (*PingMessageType, error) { // GetPongMessage deserializes the Pong Message from a list of byte func GetPongMessage(payload []byte) (*PongMessageType, error) { pong := new(PongMessageType) - pong.Peers = make([]nodeInfo, 0) + pong.Peers = make([]Info, 0) pong.PubKeys = make([][]byte, 0) r := bytes.NewBuffer(payload) diff --git a/internal/beaconchain/libs/beaconchain.go b/internal/beaconchain/libs/beaconchain.go index c2f5e2940..b74c29f87 100644 --- a/internal/beaconchain/libs/beaconchain.go +++ b/internal/beaconchain/libs/beaconchain.go @@ -11,6 +11,7 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/harmony-one/harmony/api/proto/bcconn" proto_identity "github.com/harmony-one/harmony/api/proto/identity" + "github.com/harmony-one/harmony/api/proto/node" "github.com/harmony-one/harmony/crypto/pki" "github.com/harmony-one/harmony/internal/beaconchain/rpc" "github.com/harmony-one/harmony/internal/utils" @@ -30,23 +31,24 @@ const BeaconchainServicePortDiff = 4444 //BCInfo is the information that needs to be stored on the disk in order to allow for a restart. type BCInfo struct { - Leaders []*bcconn.NodeInfo `json:"leaders"` - ShardLeaderMap map[int]*bcconn.NodeInfo `json:"shardLeaderMap"` - NumberOfShards int `json:"numShards"` - NumberOfNodesAdded int `json:"numNodesAdded"` - IP string `json:"ip"` - Port string `json:"port"` + Leaders []*node.Info `json:"leaders"` + ShardLeaderMap map[int]*node.Info `json:"shardLeaderMap"` + NumberOfShards int `json:"numShards"` + NumberOfNodesAdded int `json:"numNodesAdded"` + IP string `json:"ip"` + Port string `json:"port"` } // BeaconChain (Blockchain) keeps Identities per epoch, currently centralized! type BeaconChain struct { BCInfo BCInfo log log.Logger - ShardLeaderMap map[int]*bcconn.NodeInfo + ShardLeaderMap map[int]*node.Info PubKey kyber.Point host host.Host state BCState rpcServer *beaconchain.Server + Peer p2p.Peer } //SaveFile is to store the file in which beaconchain info will be stored. @@ -80,8 +82,8 @@ func (bc *BeaconChain) StartRPCServer() { } // GetShardLeaderMap returns the map from shard id to leader. -func (bc *BeaconChain) GetShardLeaderMap() map[int]*bcconn.NodeInfo { - result := make(map[int]*bcconn.NodeInfo) +func (bc *BeaconChain) GetShardLeaderMap() map[int]*node.Info { + result := make(map[int]*node.Info) for i, leader := range bc.BCInfo.Leaders { result[i] = leader } @@ -97,7 +99,7 @@ func New(numShards int, ip, port string) *BeaconChain { bcinfo := &BCInfo{NumberOfShards: numShards, NumberOfNodesAdded: 0, IP: ip, Port: port, - ShardLeaderMap: make(map[int]*bcconn.NodeInfo)} + ShardLeaderMap: make(map[int]*node.Info)} bc.BCInfo = *bcinfo return &bc } @@ -110,9 +112,11 @@ func generateBCKey() kyber.Point { } //AcceptNodeInfo deserializes node information received via beaconchain handler -func (bc *BeaconChain) AcceptNodeInfo(b []byte) *bcconn.NodeInfo { +func (bc *BeaconChain) AcceptNodeInfo(b []byte) *node.Info { Node := bcconn.DeserializeNodeInfo(b) - bc.log.Info("New Node Connection", "IP", Node.Self.IP, "Port", Node.Self.Port) + bc.log.Info("New Node Connection", "IP", Node.IP, "Port", Node.Port) + bc.Peer = p2p.Peer{IP: Node.IP, Port: Node.Port} + bc.host.AddPeer(&bc.Peer) bc.BCInfo.NumberOfNodesAdded = bc.BCInfo.NumberOfNodesAdded + 1 shardNum, isLeader := utils.AllocateShard(bc.BCInfo.NumberOfNodesAdded, bc.BCInfo.NumberOfShards) if isLeader { @@ -125,13 +129,13 @@ func (bc *BeaconChain) AcceptNodeInfo(b []byte) *bcconn.NodeInfo { } //RespondRandomness sends a randomness beacon to the node inorder for it process what shard it will be in -func (bc *BeaconChain) RespondRandomness(Node *bcconn.NodeInfo) { +func (bc *BeaconChain) RespondRandomness(Node *node.Info) { bci := bc.BCInfo response := bcconn.ResponseRandomNumber{NumberOfShards: bci.NumberOfShards, NumberOfNodesAdded: bci.NumberOfNodesAdded, Leaders: bci.Leaders} msg := bcconn.SerializeRandomInfo(response) msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.Acknowledge, msg) bc.log.Info("Sent Out Msg", "# Nodes", response.NumberOfNodesAdded) - host.SendMessage(bc.host, Node.Self, msgToSend, nil) + host.SendMessage(bc.host, bc.Peer, msgToSend, nil) bc.state = RandomInfoSent } diff --git a/internal/beaconchain/rpc/server.go b/internal/beaconchain/rpc/server.go index 8aba0ccfe..1ae12c2f4 100644 --- a/internal/beaconchain/rpc/server.go +++ b/internal/beaconchain/rpc/server.go @@ -5,7 +5,7 @@ import ( "log" "net" - "github.com/harmony-one/harmony/api/proto/bcconn" + "github.com/harmony-one/harmony/api/proto/node" "google.golang.org/grpc" @@ -14,7 +14,7 @@ import ( // Server is the Server struct for beacon chain package. type Server struct { - shardLeaderMap func() map[int]*bcconn.NodeInfo + shardLeaderMap func() map[int]*node.Info } // FetchLeaders implements the FetchLeaders interface to return current leaders. @@ -23,7 +23,7 @@ func (s *Server) FetchLeaders(ctx context.Context, request *proto.FetchLeadersRe leaders := []*proto.FetchLeadersResponse_Leader{} for shardID, leader := range s.shardLeaderMap() { - leaders = append(leaders, &proto.FetchLeadersResponse_Leader{Ip: leader.Self.IP, Port: leader.Self.Port, ShardId: uint32(shardID)}) + leaders = append(leaders, &proto.FetchLeadersResponse_Leader{Ip: leader.IP, Port: leader.Port, ShardId: uint32(shardID)}) } log.Println(leaders) return &proto.FetchLeadersResponse{Leaders: leaders}, nil @@ -45,7 +45,7 @@ func (s *Server) Start(ip, port string) (*grpc.Server, error) { } // NewServer creates new Server which implements BeaconChainServiceServer interface. -func NewServer(shardLeaderMap func() map[int]*bcconn.NodeInfo) *Server { +func NewServer(shardLeaderMap func() map[int]*node.Info) *Server { s := &Server{shardLeaderMap} return s } diff --git a/internal/newnode/newnode.go b/internal/newnode/newnode.go index f9dd00a31..2cc6d67be 100644 --- a/internal/newnode/newnode.go +++ b/internal/newnode/newnode.go @@ -12,6 +12,7 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/harmony-one/harmony/api/proto/bcconn" proto_identity "github.com/harmony-one/harmony/api/proto/identity" + proto_node "github.com/harmony-one/harmony/api/proto/node" "github.com/harmony-one/harmony/crypto" "github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/p2p" @@ -52,7 +53,7 @@ func New(ip string, port string) *NewNode { type registerResponseRandomNumber struct { NumberOfShards int NumberOfNodesAdded int - Leaders []*bcconn.NodeInfo + Leaders []*proto_node.Info } // ContactBeaconChain starts a newservice in the candidate node @@ -72,8 +73,7 @@ func (node *NewNode) requestBeaconChain(BCPeer p2p.Peer) (err error) { if err != nil { node.log.Error("Could not Marshall public key into binary") } - p := p2p.Peer{IP: node.Self.IP, Port: node.Self.Port} - nodeInfo := &bcconn.NodeInfo{Self: p, PubK: pubk} + nodeInfo := &proto_node.Info{IP: node.Self.IP, Port: node.Self.Port, PubKey: pubk} msg := bcconn.SerializeNodeInfo(nodeInfo) msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.Register, msg) gotShardInfo := false @@ -108,9 +108,9 @@ func (node *NewNode) processShardInfo(msgPayload []byte) bool { leaders := leadersInfo.Leaders shardNum, isLeader := utils.AllocateShard(leadersInfo.NumberOfNodesAdded, leadersInfo.NumberOfShards) for n, v := range leaders { - leaderPeer := p2p.Peer{IP: v.Self.IP, Port: v.Self.Port} + leaderPeer := p2p.Peer{IP: v.IP, Port: v.Port} leaderPeer.PubKey = crypto.Ed25519Curve.Point() - err := leaderPeer.PubKey.UnmarshalBinary(v.PubK[:]) + err := leaderPeer.PubKey.UnmarshalBinary(v.PubKey[:]) if err != nil { node.log.Error("Could not unmarshall leaders public key from binary to kyber.point") }