diff --git a/benchmark.go b/benchmark.go index 419d3d912..9e160a952 100644 --- a/benchmark.go +++ b/benchmark.go @@ -12,8 +12,8 @@ import ( "github.com/harmony-one/harmony/attack" "github.com/harmony-one/harmony/consensus" "github.com/harmony-one/harmony/db" - "github.com/harmony-one/harmony/discovery" "github.com/harmony-one/harmony/log" + pkg_newnode "github.com/harmony-one/harmony/newnode" "github.com/harmony-one/harmony/node" "github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/profiler" @@ -73,9 +73,11 @@ func loggingInit(logFolder, role, ip, port string, onlyLogTps bool) { log.Root().SetHandler(h) } + 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.") configFile := flag.String("config_file", "config.txt", "file containing all ip addresses") @@ -88,12 +90,12 @@ func main() { syncNode := flag.Bool("sync_node", false, "Whether this node is a new node joining blockchain and it needs to get synced before joining consensus.") onlyLogTps := flag.Bool("only_log_tps", false, "Only log TPS if true") - // This IP belongs to jenkins.harmony.one - idcIP := flag.String("idc", "54.183.5.66", "IP of the identity chain") - idcPort := flag.String("idc_port", "8080", "port of the identity chain") - peerDisvoery := flag.Bool("peer_discovery", false, "Enable Peer Discovery") + //This IP belongs to jenkins.harmony.one + idcIP := flag.String("idc", "127.0.0.1", "IP of the identity chain") + 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() @@ -113,37 +115,31 @@ func main() { var leader p2p.Peer var selfPeer p2p.Peer var clientPeer *p2p.Peer - // Use Peer Discovery to get shard/leader/peer/... - priKey, pubKey := utils.GenKey(*ip, *port) - if *peerDisvoery { - // Contact Identity Chain - // This is a blocking call - // Assume @ak has get it working - // TODO: this has to work with @ak's fix - discoveryConfig := discovery.New(priKey, pubKey) - - err := discoveryConfig.StartClientMode(*idcIP, *idcPort) - if err != nil { - fmt.Println("Unable to start peer discovery! ", err) - os.Exit(1) - } + //Use Peer Discovery to get shard/leader/peer/... + if *peerDiscovery { + candidateNode := pkg_newnode.New(*ip, *port) + BCPeer := p2p.Peer{IP: *idcIP, Port: *idcPort} + service := candidateNode.NewService(*ip, *port) + candidateNode.ConnectBeaconChain(BCPeer) + shardID = candidateNode.GetShardID() + leader = candidateNode.GetLeader() + selfPeer = candidateNode.GetSelfPeer() + clientPeer = candidateNode.GetClientPeer() + service.Stop() + selfPeer.PubKey = candidateNode.PubK - shardID = discoveryConfig.GetShardID() - leader = discoveryConfig.GetLeader() - peers = discoveryConfig.GetPeers() - selfPeer = discoveryConfig.GetSelfPeer() } else { distributionConfig := utils.NewDistributionConfig() distributionConfig.ReadConfigFile(*configFile) shardID = distributionConfig.GetShardID(*ip, *port) - peers = distributionConfig.GetPeers(*ip, *port, shardID) leader = distributionConfig.GetLeader(shardID) selfPeer = distributionConfig.GetSelfPeer(*ip, *port, shardID) - + _, pubKey := utils.GenKey(*ip, *port) + selfPeer.PubKey = pubKey // Create client peer. clientPeer = distributionConfig.GetClientPeer() } - selfPeer.PubKey = pubKey + fmt.Println(peers, leader, selfPeer, clientPeer, *logFolder, *minPeers) var role string if leader.IP == *ip && leader.Port == *port { @@ -156,7 +152,6 @@ func main() { // Attack determination. attack.GetInstance().SetAttackEnabled(attackDetermination(*attackedMode)) } - // Init logging. loggingInit(*logFolder, role, *ip, *port, *onlyLogTps) @@ -223,7 +218,7 @@ func main() { }() } } else { - if *peerDisvoery { + if *peerDiscovery { go currentNode.JoinShard(leader) } } diff --git a/deploy.sh b/deploy.sh index 19529ef3b..02165e707 100755 --- a/deploy.sh +++ b/deploy.sh @@ -73,7 +73,8 @@ while IFS='' read -r line || [[ -n "$line" ]]; do IFS=' ' read ip port mode shardID <<< $line #echo $ip $port $mode if [ "$mode" != "client" ]; then - ./bin/benchmark -ip $ip -port $port -config_file $config -log_folder $log_folder $DB $PEER -min_peers $MIN & + ./bin/benchmark -ip $ip -port $port -log_folder $log_folder $DB $PEER -min_peers $MIN & + sleep 10 fi done < $config diff --git a/utils/utils.go b/utils/utils.go index d6ba9d03f..1009e4270 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -67,3 +67,26 @@ func GenKey(ip, port string) (kyber.Scalar, kyber.Point) { return priKey, pubKey } + +// AllocateShard uses the number of current nodes and number of shards +// to return the shardnum a new node belongs to, it also tells whether the node is a leader +func AllocateShard(numnode, numshards int) (int, bool) { + if numshards == 1 { + if numnode == 1 { + return 1, true + } else { + return 1, false + } + } + if numnode <= numshards { + return numnode, true + } else { + shardnum := numnode % numshards + if shardnum == 0 { + return numshards, false + } else { + return shardnum, false + } + + } +}