You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.6 KiB
65 lines
1.6 KiB
package main
|
|
|
|
import (
|
|
"flag"
|
|
"harmony-benchmark/consensus"
|
|
"harmony-benchmark/p2p"
|
|
"harmony-benchmark/node"
|
|
"log"
|
|
"os"
|
|
"bufio"
|
|
"strings"
|
|
)
|
|
|
|
func getLeader(iplist string) p2p.Peer {
|
|
file, _ := os.Open(iplist)
|
|
fscanner := bufio.NewScanner(file)
|
|
var leaderPeer p2p.Peer
|
|
for fscanner.Scan() {
|
|
p := strings.Split(fscanner.Text(), " ")
|
|
ip, port, status := p[0], p[1], p[2]
|
|
if status == "leader" {
|
|
leaderPeer.Ip = ip
|
|
leaderPeer.Port = port
|
|
}
|
|
}
|
|
return leaderPeer
|
|
}
|
|
|
|
func getPeers(Ip, Port, iplist string) []p2p.Peer {
|
|
file, _ := os.Open(iplist)
|
|
fscanner := bufio.NewScanner(file)
|
|
var peerList []p2p.Peer
|
|
for fscanner.Scan() {
|
|
p := strings.Split(fscanner.Text(), " ")
|
|
ip, port, status := p[0], p[1], p[2]
|
|
if status == "leader" || ip == Ip && port == Port {
|
|
continue
|
|
}
|
|
peer := p2p.Peer{Port: port, Ip: ip}
|
|
peerList = append(peerList, peer)
|
|
}
|
|
return peerList
|
|
}
|
|
|
|
func main() {
|
|
ip := flag.String("ip", "127.0.0.1", "IP of the node")
|
|
port := flag.String("port", "9000", "port of the node.")
|
|
ipfile := flag.String("ipfile", "iplist.txt", "file containing all ip addresses")
|
|
flag.Parse()
|
|
|
|
consensus := consensus.NewConsensus(*ip, *port, getPeers(*ip, *port, *ipfile), getLeader(*ipfile))
|
|
var nodeStatus string
|
|
if consensus.IsLeader {
|
|
nodeStatus = "leader"
|
|
} else {
|
|
nodeStatus = "validator"
|
|
}
|
|
|
|
log.Println("======================================")
|
|
log.Printf("This node is a %s node listening on ip: %s and port: %s\n", nodeStatus, *ip, *port)
|
|
log.Println("======================================")
|
|
|
|
node := node.NewNode(&consensus)
|
|
node.StartServer(*port)
|
|
}
|
|
|