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. 8
      benchmark.go
  4. 24
      newnode/newnode.go
  5. 12
      utils/utils.go

@ -8,17 +8,20 @@ import (
"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
PubK []byte
}
//ResponseRandomNumber struct for exchanging random information
type ResponseRandomNumber struct {
NumberOfShards int
NumberOfNodesAdded int
Leaders []*NodeInfo
}
//SerializeNodeInfo
// SerializeNodeInfo is for serializing nodeinfo
func SerializeNodeInfo(nodeinfo *NodeInfo) []byte {
var result bytes.Buffer
encoder := gob.NewEncoder(&result)
@ -29,7 +32,7 @@ func SerializeNodeInfo(nodeinfo *NodeInfo) []byte {
return result.Bytes()
}
// DeserializeNodeInfo deserializes the node
// DeserializeNodeInfo deserializes the nodeinfo
func DeserializeNodeInfo(d []byte) *NodeInfo {
var wn NodeInfo
r := bytes.NewBuffer(d)
@ -41,7 +44,7 @@ func DeserializeNodeInfo(d []byte) *NodeInfo {
return &wn
}
//SerializeRandomInfo
// SerializeRandomInfo serializes random number informations
func SerializeRandomInfo(response ResponseRandomNumber) []byte {
//Needs to escape the serialization of unexported fields
var result bytes.Buffer
@ -54,7 +57,7 @@ func SerializeRandomInfo(response ResponseRandomNumber) []byte {
return result.Bytes()
}
// DeserializeRandomInfo deserializes the node
// DeserializeRandomInfo deserializes the random informations
func DeserializeRandomInfo(d []byte) ResponseRandomNumber {
var wn ResponseRandomNumber
r := bytes.NewBuffer(d)

@ -30,7 +30,7 @@ type BeaconChain struct {
Port string
}
//Init
//New beaconchain initialization
func New(numShards int, ip, port string) *BeaconChain {
bc := BeaconChain{}
bc.log = log.New()
@ -58,16 +58,6 @@ func (bc *BeaconChain) AcceptConnections(b []byte) {
if isLeader {
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}
msg := bcconn.SerializeRandomInfo(response)
msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.Acknowledge, msg)

@ -76,7 +76,7 @@ func loggingInit(logFolder, role, ip, port string, onlyLogTps bool) {
func main() {
accountModel := flag.Bool("account_model", true, "Whether to use account model")
//TODO: use http://getmyipaddress.org/ or http://www.get-myip.com/ to retrieve my IP address
// TODO: use http://getmyipaddress.org/ or http://www.get-myip.com/ to retrieve my IP address
ip := flag.String("ip", "127.0.0.1", "IP of the node")
port := flag.String("port", "9000", "port of the node.")
@ -95,7 +95,7 @@ func main() {
idcPort := flag.String("idc_port", "8081", "port of the identity chain")
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")
flag.Parse()
@ -135,11 +135,13 @@ func main() {
leader = distributionConfig.GetLeader(shardID)
selfPeer = distributionConfig.GetSelfPeer(*ip, *port, shardID)
_, pubKey := utils.GenKey(*ip, *port)
peers = distributionConfig.GetPeers(*ip, *port, shardID)
selfPeer.PubKey = pubKey
// Create client peer.
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
if leader.IP == *ip && leader.Port == *port {

@ -17,12 +17,13 @@ import (
"github.com/harmony-one/harmony/utils"
)
// An uninteresting service.
// Service is the server for listening.
type Service struct {
ch chan bool
waitGroup *sync.WaitGroup
}
//NewNode is ther struct for a candidate node
type NewNode struct {
Role string
ShardID int
@ -38,7 +39,7 @@ type NewNode struct {
Service *Service
}
//NewNode
// New candidatenode initialization
func New(ip string, port string) *NewNode {
priKey, pubKey := utils.GenKey(ip, port)
var node NewNode
@ -56,7 +57,7 @@ type registerResponseRandomNumber struct {
Leaders []*bcconn.NodeInfo
}
// NewService
// NewService starts a newservice in the candidate node
func (node *NewNode) NewService(ip, port string) *Service {
laddr, err := net.ResolveTCPAddr("tcp", ip+":"+port)
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.Debug("listening on", "address", laddr.String())
node.Service = &Service{
ch: make(chan bool),
waitGroup: &sync.WaitGroup{},
@ -115,7 +115,7 @@ func (node NewNode) String() string {
return fmt.Sprintf("idc: %v:%v and node infi %v", node.Self.IP, node.Self.Port, node.SetInfo)
}
//ConnectBeaconChain connects to beacon chain
// ConnectBeaconChain connects to beacon chain
func (node *NewNode) ConnectBeaconChain(BCPeer p2p.Peer) {
node.log.Info("connecting to beacon chain now ...")
pubk, err := node.PubK.MarshalBinary()
@ -128,7 +128,7 @@ func (node *NewNode) ConnectBeaconChain(BCPeer p2p.Peer) {
msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.Register, msg)
gotShardInfo := false
timeout := time.After(300 * time.Second)
tick := time.Tick(1 * time.Second)
tick := time.Tick(5 * time.Second)
checkLoop:
for {
select {
@ -150,14 +150,12 @@ checkLoop:
}
}
//ProcessShardInfo
// ProcessShardInfo
func (node *NewNode) processShardInfo(msgPayload []byte) bool {
leadersInfo := bcconn.DeserializeRandomInfo(msgPayload)
leaders := leadersInfo.Leaders
shardNum, isLeader := utils.AllocateShard(leadersInfo.NumberOfNodesAdded, leadersInfo.NumberOfShards)
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.PubKey = crypto.Ed25519Curve.Point()
err := leaderPeer.PubKey.UnmarshalBinary(leaderNode.PubK[:])
@ -166,26 +164,28 @@ func (node *NewNode) processShardInfo(msgPayload []byte) bool {
}
node.leader = leaderPeer
node.isLeader = isLeader
node.ShardID = shardNum
node.ShardID = shardNum - 1 //0 indexing.
node.SetInfo = true
node.log.Info("Shard information obtained ..")
return true
}
// GetShardID gives shardid of node
func (node *NewNode) GetShardID() string {
return strconv.Itoa(node.ShardID)
}
// GetLeader gives the leader of the node
func (node *NewNode) GetLeader() p2p.Peer {
return node.leader
}
// GetClientPeer gives the client of the node
func (node *NewNode) GetClientPeer() *p2p.Peer {
return nil
}
//GetSelfPeer
// GetSelfPeer gives the peer part of the node's own struct
func (node *NewNode) GetSelfPeer() p2p.Peer {
return node.Self
}

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

Loading…
Cancel
Save