making changes leo requested, cleaningup fixing golint and gofmt

pull/126/head
ak 6 years ago
parent 3fe8432be3
commit 1610b70512
  1. 13
      bcconn/bcconn.go
  2. 12
      beaconchain/beaconchain.go
  3. 6
      benchmark.go
  4. 20
      newnode/newnode.go
  5. 12
      utils/utils.go

@ -8,17 +8,20 @@ import (
"github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/p2p"
) )
type NodeInfo struct { //TODO: to be merged with Leo's key. //NodeInfo struct exists to share information on the node
type NodeInfo struct { //TODO: to be merged with Leo's nodeinfo.
Self p2p.Peer Self p2p.Peer
PubK []byte PubK []byte
} }
//ResponseRandomNumber struct for exchanging random information
type ResponseRandomNumber struct { type ResponseRandomNumber struct {
NumberOfShards int NumberOfShards int
NumberOfNodesAdded int NumberOfNodesAdded int
Leaders []*NodeInfo Leaders []*NodeInfo
} }
//SerializeNodeInfo // SerializeNodeInfo is for serializing nodeinfo
func SerializeNodeInfo(nodeinfo *NodeInfo) []byte { func SerializeNodeInfo(nodeinfo *NodeInfo) []byte {
var result bytes.Buffer var result bytes.Buffer
encoder := gob.NewEncoder(&result) encoder := gob.NewEncoder(&result)
@ -29,7 +32,7 @@ func SerializeNodeInfo(nodeinfo *NodeInfo) []byte {
return result.Bytes() return result.Bytes()
} }
// DeserializeNodeInfo deserializes the node // DeserializeNodeInfo deserializes the nodeinfo
func DeserializeNodeInfo(d []byte) *NodeInfo { func DeserializeNodeInfo(d []byte) *NodeInfo {
var wn NodeInfo var wn NodeInfo
r := bytes.NewBuffer(d) r := bytes.NewBuffer(d)
@ -41,7 +44,7 @@ func DeserializeNodeInfo(d []byte) *NodeInfo {
return &wn return &wn
} }
//SerializeRandomInfo // SerializeRandomInfo serializes random number informations
func SerializeRandomInfo(response ResponseRandomNumber) []byte { func SerializeRandomInfo(response ResponseRandomNumber) []byte {
//Needs to escape the serialization of unexported fields //Needs to escape the serialization of unexported fields
var result bytes.Buffer var result bytes.Buffer
@ -54,7 +57,7 @@ func SerializeRandomInfo(response ResponseRandomNumber) []byte {
return result.Bytes() return result.Bytes()
} }
// DeserializeRandomInfo deserializes the node // DeserializeRandomInfo deserializes the random informations
func DeserializeRandomInfo(d []byte) ResponseRandomNumber { func DeserializeRandomInfo(d []byte) ResponseRandomNumber {
var wn ResponseRandomNumber var wn ResponseRandomNumber
r := bytes.NewBuffer(d) r := bytes.NewBuffer(d)

@ -30,7 +30,7 @@ type BeaconChain struct {
Port string Port string
} }
//Init //New beaconchain initialization
func New(numShards int, ip, port string) *BeaconChain { func New(numShards int, ip, port string) *BeaconChain {
bc := BeaconChain{} bc := BeaconChain{}
bc.log = log.New() bc.log = log.New()
@ -58,16 +58,6 @@ func (bc *BeaconChain) AcceptConnections(b []byte) {
if isLeader { if isLeader {
bc.Leaders = append(bc.Leaders, Node) bc.Leaders = append(bc.Leaders, Node)
} }
/**
**IMPORTANT**
Note that public key is not in kyber.Scalar form it is in byte form.
Use following conversion to get back the actual key
//peer.PubKey = crypto.Ed25519Curve.Point()
//err = peer.PubKey.UnmarshalBinary(p.PubKey[:])
**/
response := bcconn.ResponseRandomNumber{NumberOfShards: bc.NumberOfShards, NumberOfNodesAdded: bc.NumberOfNodesAdded, Leaders: bc.Leaders} response := bcconn.ResponseRandomNumber{NumberOfShards: bc.NumberOfShards, NumberOfNodesAdded: bc.NumberOfNodesAdded, Leaders: bc.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)

@ -95,7 +95,7 @@ func main() {
idcPort := flag.String("idc_port", "8081", "port of the identity chain") idcPort := flag.String("idc_port", "8081", "port of the identity chain")
peerDiscovery := flag.Bool("peer_discovery", true, "Enable Peer Discovery") peerDiscovery := flag.Bool("peer_discovery", true, "Enable Peer Discovery")
// // Leader needs to have a minimal number of peers to start consensus //Leader needs to have a minimal number of peers to start consensus
minPeers := flag.Int("min_peers", 100, "Minimal number of Peers in shard") minPeers := flag.Int("min_peers", 100, "Minimal number of Peers in shard")
flag.Parse() flag.Parse()
@ -135,11 +135,13 @@ func main() {
leader = distributionConfig.GetLeader(shardID) leader = distributionConfig.GetLeader(shardID)
selfPeer = distributionConfig.GetSelfPeer(*ip, *port, shardID) selfPeer = distributionConfig.GetSelfPeer(*ip, *port, shardID)
_, pubKey := utils.GenKey(*ip, *port) _, pubKey := utils.GenKey(*ip, *port)
peers = distributionConfig.GetPeers(*ip, *port, shardID)
selfPeer.PubKey = pubKey selfPeer.PubKey = pubKey
// Create client peer. // Create client peer.
clientPeer = distributionConfig.GetClientPeer() clientPeer = distributionConfig.GetClientPeer()
} }
fmt.Println(peers, leader, selfPeer, clientPeer, *logFolder, *minPeers)
fmt.Println(peers, leader, selfPeer, clientPeer, *logFolder, *minPeers) //TODO: to be replaced by a logger later: ak, rl
var role string var role string
if leader.IP == *ip && leader.Port == *port { if leader.IP == *ip && leader.Port == *port {

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

@ -74,19 +74,15 @@ func AllocateShard(numnode, numshards int) (int, bool) {
if numshards == 1 { if numshards == 1 {
if numnode == 1 { if numnode == 1 {
return 1, true return 1, true
} else {
return 1, false
} }
return 1, false
} }
if numnode <= numshards { if numnode > numshards {
return numnode, true
} else {
shardnum := numnode % numshards shardnum := numnode % numshards
if shardnum == 0 { if shardnum == 0 {
return numshards, false return numshards, false
} else {
return shardnum, false
} }
return shardnum, false
} }
return numnode, true
} }

Loading…
Cancel
Save