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.
197 lines
5.6 KiB
197 lines
5.6 KiB
6 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"flag"
|
||
6 years ago
|
"fmt"
|
||
6 years ago
|
"math/rand"
|
||
6 years ago
|
"os"
|
||
6 years ago
|
"path"
|
||
6 years ago
|
"runtime"
|
||
6 years ago
|
"time"
|
||
6 years ago
|
|
||
6 years ago
|
"github.com/harmony-one/harmony/consensus"
|
||
6 years ago
|
"github.com/harmony-one/harmony/internal/attack"
|
||
6 years ago
|
"github.com/harmony-one/harmony/internal/db"
|
||
6 years ago
|
pkg_newnode "github.com/harmony-one/harmony/internal/newnode"
|
||
6 years ago
|
"github.com/harmony-one/harmony/internal/profiler"
|
||
6 years ago
|
"github.com/harmony-one/harmony/log"
|
||
|
"github.com/harmony-one/harmony/node"
|
||
6 years ago
|
"github.com/harmony-one/harmony/p2p"
|
||
6 years ago
|
"github.com/harmony-one/harmony/p2p/p2pimpl"
|
||
6 years ago
|
)
|
||
|
|
||
6 years ago
|
var (
|
||
|
version string
|
||
|
builtBy string
|
||
|
builtAt string
|
||
|
commit string
|
||
|
)
|
||
|
|
||
6 years ago
|
// Constants used by the benchmark.
|
||
6 years ago
|
const (
|
||
|
AttackProbability = 20
|
||
|
)
|
||
|
|
||
|
func attackDetermination(attackedMode int) bool {
|
||
|
switch attackedMode {
|
||
|
case 0:
|
||
|
return false
|
||
|
case 1:
|
||
|
return true
|
||
|
case 2:
|
||
|
return rand.Intn(100) < AttackProbability
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
6 years ago
|
// InitLDBDatabase initializes a LDBDatabase.
|
||
6 years ago
|
func InitLDBDatabase(ip string, port string) (*db.LDBDatabase, error) {
|
||
|
// TODO(minhdoan): Refactor this.
|
||
|
dbFileName := "/tmp/harmony_" + ip + port + ".dat"
|
||
|
var err = os.RemoveAll(dbFileName)
|
||
|
if err != nil {
|
||
|
fmt.Println(err.Error())
|
||
|
}
|
||
|
return db.NewLDBDatabase(dbFileName, 0, 0)
|
||
|
}
|
||
|
|
||
6 years ago
|
func printVersion(me string) {
|
||
|
fmt.Fprintf(os.Stderr, "Harmony (C) 2018. %v, version %v-%v (%v %v)\n", path.Base(me), version, commit, builtBy, builtAt)
|
||
|
os.Exit(0)
|
||
|
}
|
||
|
|
||
6 years ago
|
func loggingInit(logFolder, role, ip, port string, onlyLogTps bool) {
|
||
|
// Setup a logger to stdout and log file.
|
||
|
logFileName := fmt.Sprintf("./%v/%s-%v-%v.log", logFolder, role, ip, port)
|
||
|
h := log.MultiHandler(
|
||
|
log.StdoutHandler,
|
||
|
log.Must.FileHandler(logFileName, log.JSONFormat()), // Log to file
|
||
|
)
|
||
|
if onlyLogTps {
|
||
|
h = log.MatchFilterHandler("msg", "TPS Report", h)
|
||
|
}
|
||
|
log.Root().SetHandler(h)
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
func main() {
|
||
6 years ago
|
// TODO: use http://getmyipaddress.org/ or http://www.get-myip.com/ to retrieve my IP address
|
||
6 years ago
|
ip := flag.String("ip", "127.0.0.1", "IP of the node")
|
||
|
port := flag.String("port", "9000", "port of the node.")
|
||
6 years ago
|
logFolder := flag.String("log_folder", "latest", "the folder collecting the logs of this execution")
|
||
6 years ago
|
attackedMode := flag.Int("attacked_mode", 0, "0 means not attacked, 1 means attacked, 2 means being open to be selected as attacked")
|
||
6 years ago
|
dbSupported := flag.Bool("db_supported", false, "false means not db_supported, true means db_supported")
|
||
6 years ago
|
profile := flag.Bool("profile", false, "Turn on profiling (CPU, Memory).")
|
||
6 years ago
|
metricsReportURL := flag.String("metrics_report_url", "", "If set, reports metrics to this URL.")
|
||
6 years ago
|
versionFlag := flag.Bool("version", false, "Output version info")
|
||
6 years ago
|
onlyLogTps := flag.Bool("only_log_tps", false, "Only log TPS if true")
|
||
6 years ago
|
|
||
6 years ago
|
//This IP belongs to jenkins.harmony.one
|
||
6 years ago
|
bcIP := flag.String("bc", "127.0.0.1", "IP of the identity chain")
|
||
|
bcPort := flag.String("bc_port", "8081", "port of the identity chain")
|
||
6 years ago
|
|
||
6 years ago
|
//Leader needs to have a minimal number of peers to start consensus
|
||
6 years ago
|
minPeers := flag.Int("min_peers", 100, "Minimal number of Peers in shard")
|
||
|
|
||
6 years ago
|
flag.Parse()
|
||
|
|
||
6 years ago
|
if *versionFlag {
|
||
|
printVersion(os.Args[0])
|
||
|
}
|
||
|
|
||
6 years ago
|
// Add GOMAXPROCS to achieve max performance.
|
||
|
runtime.GOMAXPROCS(1024)
|
||
|
|
||
6 years ago
|
// Set up randomization seed.
|
||
|
rand.Seed(int64(time.Now().Nanosecond()))
|
||
|
|
||
6 years ago
|
var shardID string
|
||
|
var peers []p2p.Peer
|
||
|
var leader p2p.Peer
|
||
|
var selfPeer p2p.Peer
|
||
|
var clientPeer *p2p.Peer
|
||
6 years ago
|
//Use Peer Discovery to get shard/leader/peer/...
|
||
6 years ago
|
candidateNode := pkg_newnode.New(*ip, *port)
|
||
|
BCPeer := p2p.Peer{IP: *bcIP, Port: *bcPort}
|
||
|
candidateNode.ContactBeaconChain(BCPeer)
|
||
|
shardID = candidateNode.GetShardID()
|
||
|
leader = candidateNode.GetLeader()
|
||
|
selfPeer = candidateNode.GetSelfPeer()
|
||
|
clientPeer = candidateNode.GetClientPeer()
|
||
|
selfPeer.PubKey = candidateNode.PubK
|
||
6 years ago
|
|
||
6 years ago
|
// fmt.Println(peers, leader, selfPeer, clientPeer, *logFolder, *minPeers) //TODO: to be replaced by a logger later: ak, rl
|
||
6 years ago
|
|
||
|
var role string
|
||
6 years ago
|
if leader.IP == *ip && leader.Port == *port {
|
||
6 years ago
|
role = "leader"
|
||
|
} else {
|
||
|
role = "validator"
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
if role == "validator" {
|
||
|
// Attack determination.
|
||
|
attack.GetInstance().SetAttackEnabled(attackDetermination(*attackedMode))
|
||
|
}
|
||
6 years ago
|
// Init logging.
|
||
|
loggingInit(*logFolder, role, *ip, *port, *onlyLogTps)
|
||
6 years ago
|
|
||
6 years ago
|
// Initialize leveldb if dbSupported.
|
||
6 years ago
|
var ldb *db.LDBDatabase
|
||
6 years ago
|
|
||
6 years ago
|
if *dbSupported {
|
||
6 years ago
|
ldb, _ = InitLDBDatabase(*ip, *port)
|
||
|
}
|
||
|
|
||
6 years ago
|
host := p2pimpl.NewHost(selfPeer)
|
||
6 years ago
|
// Consensus object.
|
||
6 years ago
|
consensus := consensus.New(host, shardID, peers, leader)
|
||
6 years ago
|
consensus.MinPeers = *minPeers
|
||
6 years ago
|
|
||
6 years ago
|
// Start Profiler for leader if profile argument is on
|
||
6 years ago
|
if role == "leader" && (*profile || *metricsReportURL != "") {
|
||
|
prof := profiler.GetProfiler()
|
||
|
prof.Config(consensus.Log, shardID, *metricsReportURL)
|
||
|
if *profile {
|
||
|
prof.Start()
|
||
|
}
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
6 years ago
|
// Set logger to attack model.
|
||
6 years ago
|
attack.GetInstance().SetLogger(consensus.Log)
|
||
6 years ago
|
// Current node.
|
||
6 years ago
|
currentNode := node.New(host, consensus, ldb)
|
||
6 years ago
|
currentNode.Consensus.OfflinePeers = currentNode.OfflinePeers
|
||
|
|
||
6 years ago
|
// If there is a client configured in the node list.
|
||
|
if clientPeer != nil {
|
||
6 years ago
|
currentNode.ClientPeer = clientPeer
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// Assign closure functions to the consensus object
|
||
6 years ago
|
consensus.BlockVerifier = currentNode.VerifyNewBlock
|
||
|
consensus.OnConsensusDone = currentNode.PostConsensusProcessing
|
||
6 years ago
|
|
||
6 years ago
|
currentNode.State = node.NodeWaitToJoin
|
||
6 years ago
|
|
||
6 years ago
|
if consensus.IsLeader {
|
||
6 years ago
|
currentNode.State = node.NodeLeader
|
||
6 years ago
|
// Let consensus run
|
||
|
go func() {
|
||
|
consensus.WaitForNewBlock(currentNode.BlockChannel)
|
||
|
}()
|
||
|
// Node waiting for consensus readiness to create new block
|
||
|
go func() {
|
||
|
currentNode.WaitForConsensusReady(consensus.ReadySignal)
|
||
|
}()
|
||
6 years ago
|
} else {
|
||
6 years ago
|
go currentNode.JoinShard(leader)
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
go currentNode.SupportSyncing()
|
||
6 years ago
|
if consensus.IsLeader {
|
||
|
go currentNode.SupportClient()
|
||
6 years ago
|
go currentNode.SupportExplorer()
|
||
6 years ago
|
}
|
||
6 years ago
|
currentNode.StartServer()
|
||
6 years ago
|
}
|