use Info to replace nodeInfo in pingpong

remove redundant bcconn.NodeInfo

use node.Info

Signed-off-by: Leo Chen <leo@harmony.one>
pull/280/head
Leo Chen 6 years ago
parent f189ba0183
commit f8b14043eb
  1. 17
      api/proto/bcconn/bcconn.go
  2. 14
      api/proto/node/pingpong.go
  3. 24
      internal/beaconchain/libs/beaconchain.go
  4. 8
      internal/beaconchain/rpc/server.go
  5. 10
      internal/newnode/newnode.go

@ -5,24 +5,19 @@ import (
"encoding/gob" "encoding/gob"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/harmony/p2p"
)
//NodeInfo struct exists to share information on the node "github.com/harmony-one/harmony/api/proto/node"
type NodeInfo struct { //TODO: to be merged with Leo's nodeinfo. )
Self p2p.Peer
PubK []byte
}
//ResponseRandomNumber struct for exchanging random information //ResponseRandomNumber struct for exchanging random information
type ResponseRandomNumber struct { type ResponseRandomNumber struct {
NumberOfShards int NumberOfShards int
NumberOfNodesAdded int NumberOfNodesAdded int
Leaders []*NodeInfo Leaders []*node.Info
} }
// SerializeNodeInfo is for serializing nodeinfo // SerializeNodeInfo is for serializing nodeinfo
func SerializeNodeInfo(nodeinfo *NodeInfo) []byte { func SerializeNodeInfo(nodeinfo *node.Info) []byte {
var result bytes.Buffer var result bytes.Buffer
encoder := gob.NewEncoder(&result) encoder := gob.NewEncoder(&result)
err := encoder.Encode(nodeinfo) err := encoder.Encode(nodeinfo)
@ -33,8 +28,8 @@ func SerializeNodeInfo(nodeinfo *NodeInfo) []byte {
} }
// DeserializeNodeInfo deserializes the nodeinfo // DeserializeNodeInfo deserializes the nodeinfo
func DeserializeNodeInfo(d []byte) *NodeInfo { func DeserializeNodeInfo(d []byte) *node.Info {
var wn NodeInfo var wn node.Info
r := bytes.NewBuffer(d) r := bytes.NewBuffer(d)
decoder := gob.NewDecoder(r) decoder := gob.NewDecoder(r)
err := decoder.Decode(&wn) err := decoder.Decode(&wn)

@ -40,10 +40,10 @@ func (r RoleType) String() string {
return "Unknown" 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 // this is basically a simplified version of Peer
// for network transportation // for network transportation
type nodeInfo struct { type Info struct {
IP string IP string
Port string Port string
PubKey []byte PubKey []byte
@ -54,13 +54,13 @@ type nodeInfo struct {
// PingMessageType defines the data structure of the Ping message // PingMessageType defines the data structure of the Ping message
type PingMessageType struct { type PingMessageType struct {
Version uint16 // version of the protocol Version uint16 // version of the protocol
Node nodeInfo Node Info
} }
// PongMessageType defines the data structure of the Pong message // PongMessageType defines the data structure of the Pong message
type PongMessageType struct { type PongMessageType struct {
Version uint16 // version of the protocol Version uint16 // version of the protocol
Peers []nodeInfo Peers []Info
PubKeys [][]byte // list of publickKeys, has to be identical among all validators/leaders 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.PubKeys = make([][]byte, 0)
pong.Version = ProtocolVersion pong.Version = ProtocolVersion
pong.Peers = make([]nodeInfo, 0) pong.Peers = make([]Info, 0)
var err error var err error
for _, p := range peers { for _, p := range peers {
n := nodeInfo{} n := Info{}
n.IP = p.IP n.IP = p.IP
n.Port = p.Port n.Port = p.Port
n.ValidatorID = p.ValidatorID n.ValidatorID = p.ValidatorID
@ -146,7 +146,7 @@ func GetPingMessage(payload []byte) (*PingMessageType, error) {
// GetPongMessage deserializes the Pong Message from a list of byte // GetPongMessage deserializes the Pong Message from a list of byte
func GetPongMessage(payload []byte) (*PongMessageType, error) { func GetPongMessage(payload []byte) (*PongMessageType, error) {
pong := new(PongMessageType) pong := new(PongMessageType)
pong.Peers = make([]nodeInfo, 0) pong.Peers = make([]Info, 0)
pong.PubKeys = make([][]byte, 0) pong.PubKeys = make([][]byte, 0)
r := bytes.NewBuffer(payload) r := bytes.NewBuffer(payload)

@ -11,6 +11,7 @@ import (
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/harmony/api/proto/bcconn" "github.com/harmony-one/harmony/api/proto/bcconn"
proto_identity "github.com/harmony-one/harmony/api/proto/identity" 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/crypto/pki"
"github.com/harmony-one/harmony/internal/beaconchain/rpc" "github.com/harmony-one/harmony/internal/beaconchain/rpc"
"github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/internal/utils"
@ -30,8 +31,8 @@ const BeaconchainServicePortDiff = 4444
//BCInfo is the information that needs to be stored on the disk in order to allow for a restart. //BCInfo is the information that needs to be stored on the disk in order to allow for a restart.
type BCInfo struct { type BCInfo struct {
Leaders []*bcconn.NodeInfo `json:"leaders"` Leaders []*node.Info `json:"leaders"`
ShardLeaderMap map[int]*bcconn.NodeInfo `json:"shardLeaderMap"` ShardLeaderMap map[int]*node.Info `json:"shardLeaderMap"`
NumberOfShards int `json:"numShards"` NumberOfShards int `json:"numShards"`
NumberOfNodesAdded int `json:"numNodesAdded"` NumberOfNodesAdded int `json:"numNodesAdded"`
IP string `json:"ip"` IP string `json:"ip"`
@ -42,11 +43,12 @@ type BCInfo struct {
type BeaconChain struct { type BeaconChain struct {
BCInfo BCInfo BCInfo BCInfo
log log.Logger log log.Logger
ShardLeaderMap map[int]*bcconn.NodeInfo ShardLeaderMap map[int]*node.Info
PubKey kyber.Point PubKey kyber.Point
host host.Host host host.Host
state BCState state BCState
rpcServer *beaconchain.Server rpcServer *beaconchain.Server
Peer p2p.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.
@ -80,8 +82,8 @@ func (bc *BeaconChain) StartRPCServer() {
} }
// GetShardLeaderMap returns the map from shard id to leader. // GetShardLeaderMap returns the map from shard id to leader.
func (bc *BeaconChain) GetShardLeaderMap() map[int]*bcconn.NodeInfo { func (bc *BeaconChain) GetShardLeaderMap() map[int]*node.Info {
result := make(map[int]*bcconn.NodeInfo) result := make(map[int]*node.Info)
for i, leader := range bc.BCInfo.Leaders { for i, leader := range bc.BCInfo.Leaders {
result[i] = leader result[i] = leader
} }
@ -97,7 +99,7 @@ func New(numShards int, ip, port string) *BeaconChain {
bcinfo := &BCInfo{NumberOfShards: numShards, NumberOfNodesAdded: 0, bcinfo := &BCInfo{NumberOfShards: numShards, NumberOfNodesAdded: 0,
IP: ip, IP: ip,
Port: port, Port: port,
ShardLeaderMap: make(map[int]*bcconn.NodeInfo)} ShardLeaderMap: make(map[int]*node.Info)}
bc.BCInfo = *bcinfo bc.BCInfo = *bcinfo
return &bc return &bc
} }
@ -110,9 +112,11 @@ 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) *bcconn.NodeInfo { func (bc *BeaconChain) AcceptNodeInfo(b []byte) *node.Info {
Node := bcconn.DeserializeNodeInfo(b) 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 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 {
@ -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 //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 bci := bc.BCInfo
response := bcconn.ResponseRandomNumber{NumberOfShards: bci.NumberOfShards, NumberOfNodesAdded: bci.NumberOfNodesAdded, Leaders: bci.Leaders} response := bcconn.ResponseRandomNumber{NumberOfShards: bci.NumberOfShards, NumberOfNodesAdded: bci.NumberOfNodesAdded, Leaders: bci.Leaders}
msg := bcconn.SerializeRandomInfo(response) msg := bcconn.SerializeRandomInfo(response)
msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.Acknowledge, msg) msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.Acknowledge, msg)
bc.log.Info("Sent Out Msg", "# Nodes", response.NumberOfNodesAdded) 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 bc.state = RandomInfoSent
} }

@ -5,7 +5,7 @@ import (
"log" "log"
"net" "net"
"github.com/harmony-one/harmony/api/proto/bcconn" "github.com/harmony-one/harmony/api/proto/node"
"google.golang.org/grpc" "google.golang.org/grpc"
@ -14,7 +14,7 @@ import (
// Server is the Server struct for beacon chain package. // Server is the Server struct for beacon chain package.
type Server struct { type Server struct {
shardLeaderMap func() map[int]*bcconn.NodeInfo shardLeaderMap func() map[int]*node.Info
} }
// FetchLeaders implements the FetchLeaders interface to return current leaders. // 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{} leaders := []*proto.FetchLeadersResponse_Leader{}
for shardID, leader := range s.shardLeaderMap() { 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) log.Println(leaders)
return &proto.FetchLeadersResponse{Leaders: leaders}, nil 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. // 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} s := &Server{shardLeaderMap}
return s return s
} }

@ -12,6 +12,7 @@ import (
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/harmony/api/proto/bcconn" "github.com/harmony-one/harmony/api/proto/bcconn"
proto_identity "github.com/harmony-one/harmony/api/proto/identity" 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/crypto"
"github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/p2p"
@ -52,7 +53,7 @@ func New(ip string, port string) *NewNode {
type registerResponseRandomNumber struct { type registerResponseRandomNumber struct {
NumberOfShards int NumberOfShards int
NumberOfNodesAdded int NumberOfNodesAdded int
Leaders []*bcconn.NodeInfo Leaders []*proto_node.Info
} }
// ContactBeaconChain starts a newservice in the candidate node // ContactBeaconChain starts a newservice in the candidate node
@ -72,8 +73,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")
} }
p := p2p.Peer{IP: node.Self.IP, Port: node.Self.Port} nodeInfo := &proto_node.Info{IP: node.Self.IP, Port: node.Self.Port, PubKey: pubk}
nodeInfo := &bcconn.NodeInfo{Self: p, PubK: pubk}
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 +108,9 @@ 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.Self.IP, Port: v.Self.Port} leaderPeer := p2p.Peer{IP: v.IP, Port: v.Port}
leaderPeer.PubKey = crypto.Ed25519Curve.Point() leaderPeer.PubKey = crypto.Ed25519Curve.Point()
err := leaderPeer.PubKey.UnmarshalBinary(v.PubK[:]) 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")
} }

Loading…
Cancel
Save