clean & refactor. move beacondb to nodeconfig

pull/539/head
Minh Doan 6 years ago committed by Minh Doan
parent 15d134e280
commit e399e2a31d
  1. 90
      cmd/harmony/main.go
  2. 26
      internal/configs/node/config.go

@ -28,23 +28,6 @@ var (
commit string commit string
) )
// Constants used by the harmony.
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
}
// InitLDBDatabase initializes a LDBDatabase. isBeacon=true will return the beacon chain database for normal shard nodes // InitLDBDatabase initializes a LDBDatabase. isBeacon=true will return the beacon chain database for normal shard nodes
func InitLDBDatabase(ip string, port string, freshDB bool, isBeacon bool) (*ethdb.LDBDatabase, error) { func InitLDBDatabase(ip string, port string, freshDB bool, isBeacon bool) (*ethdb.LDBDatabase, error) {
var dbFileName string var dbFileName string
@ -81,7 +64,6 @@ func loggingInit(logFolder, role, ip, port string, onlyLogTps bool) {
} }
var ( var (
// 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") ip = flag.String("ip", "127.0.0.1", "IP of the node")
port = flag.String("port", "9000", "port of the node.") port = flag.String("port", "9000", "port of the node.")
logFolder = flag.String("log_folder", "latest", "the folder collecting the logs of this execution") logFolder = flag.String("log_folder", "latest", "the folder collecting the logs of this execution")
@ -90,26 +72,19 @@ var (
metricsReportURL = flag.String("metrics_report_url", "", "If set, reports metrics to this URL.") metricsReportURL = flag.String("metrics_report_url", "", "If set, reports metrics to this URL.")
versionFlag = flag.Bool("version", false, "Output version info") versionFlag = flag.Bool("version", false, "Output version info")
onlyLogTps = flag.Bool("only_log_tps", false, "Only log TPS if true") onlyLogTps = flag.Bool("only_log_tps", false, "Only log TPS if true")
//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")
// Key file to store the private key of staking account. // Key file to store the private key of staking account.
stakingKeyFile = flag.String("staking_key", "./.stakingkey", "the private key file of the harmony node") stakingKeyFile = flag.String("staking_key", "./.stakingkey", "the private key file of the harmony node")
// Key file to store the private key // Key file to store the private key
keyFile = flag.String("key", "./.hmykey", "the private key file of the harmony node") keyFile = flag.String("key", "./.hmykey", "the private key file of the harmony node")
// isBeacon indicates this node is a beacon chain node // isBeacon indicates this node is a beacon chain node
isBeacon = flag.Bool("is_beacon", false, "true means this node is a beacon chain node") isBeacon = flag.Bool("is_beacon", false, "true means this node is a beacon chain node")
// isNewNode indicates this node is a new node // isNewNode indicates this node is a new node
isNewNode = flag.Bool("is_newnode", false, "true means this node is a new node") isNewNode = flag.Bool("is_newnode", false, "true means this node is a new node")
accountIndex = flag.Int("account_index", 0, "the index of the staking account to use") accountIndex = flag.Int("account_index", 0, "the index of the staking account to use")
// isLeader indicates this node is a beacon chain leader node during the bootstrap process // isLeader indicates this node is a beacon chain leader node during the bootstrap process
isLeader = flag.Bool("is_leader", false, "true means this node is a beacon chain leader node") isLeader = flag.Bool("is_leader", false, "true means this node is a beacon chain leader node")
// logConn logs incoming/outgoing connections // logConn logs incoming/outgoing connections
logConn = flag.Bool("log_conn", false, "log incoming/outgoing connections") logConn = flag.Bool("log_conn", false, "log incoming/outgoing connections")
) )
@ -138,11 +113,34 @@ func initSetup() {
} }
func createGlobalConfig() *nodeconfig.ConfigType { func createGlobalConfig() *nodeconfig.ConfigType {
var err error
nodeConfig := nodeconfig.GetGlobalConfig() nodeConfig := nodeconfig.GetGlobalConfig()
nodeConfig.StakingPriKey = node.LoadStakingKeyFromFile(*stakingKeyFile, *accountIndex) nodeConfig.StakingPriKey = node.LoadStakingKeyFromFile(*stakingKeyFile, *accountIndex)
// Initialize leveldb if dbSupported. nodeConfig.P2pPriKey, _, err = utils.LoadKeyFromFile(*keyFile)
if err != nil {
panic(err)
}
// Setup Bls keys
nodeConfig.BlsPriKey, nodeConfig.BlsPubKey = utils.GenKey(*ip, *port)
if nodeConfig.BlsPriKey == nil || nodeConfig.BlsPubKey == nil {
panic(fmt.Errorf("generate key error"))
}
// Initialize leveldb for main blockchain and beacon.
nodeConfig.MainDB, _ = InitLDBDatabase(*ip, *port, *freshDB, false) nodeConfig.MainDB, _ = InitLDBDatabase(*ip, *port, *freshDB, false)
if !*isBeacon {
nodeConfig.BeaconDB, _ = InitLDBDatabase(*ip, *port, *freshDB, true)
}
nodeConfig.SelfPeer = p2p.Peer{IP: *ip, Port: *port, ValidatorID: -1, BlsPubKey: nodeConfig.BlsPubKey}
if *isLeader {
nodeConfig.StringRole = "leader"
nodeConfig.Leader = nodeConfig.SelfPeer
} else {
nodeConfig.StringRole = "validator"
}
return nodeConfig return nodeConfig
} }
@ -156,33 +154,12 @@ func main() {
var shardID = "0" var shardID = "0"
var peers []p2p.Peer var peers []p2p.Peer
var leader p2p.Peer
var selfPeer p2p.Peer
var clientPeer *p2p.Peer var clientPeer *p2p.Peer
var role string
p2pPriKey, _, err := utils.LoadKeyFromFile(*keyFile)
if err != nil {
panic(err)
}
peerPriKey, peerPubKey := utils.GenKey(*ip, *port)
if peerPriKey == nil || peerPubKey == nil {
panic(fmt.Errorf("generate key error"))
}
selfPeer = p2p.Peer{IP: *ip, Port: *port, ValidatorID: -1, BlsPubKey: peerPubKey}
if *isLeader {
role = "leader"
leader = selfPeer
} else {
role = "validator"
}
// Init logging. // Init logging.
loggingInit(*logFolder, role, *ip, *port, *onlyLogTps) loggingInit(*logFolder, nodeConfig.StringRole, *ip, *port, *onlyLogTps)
host, err := p2pimpl.NewHost(&selfPeer, p2pPriKey) host, err := p2pimpl.NewHost(&nodeConfig.SelfPeer, nodeConfig.P2pPriKey)
if *logConn { if *logConn {
host.GetP2PHost().Network().Notify(utils.ConnLogger) host.GetP2PHost().Network().Notify(utils.ConnLogger)
} }
@ -190,15 +167,15 @@ func main() {
panic("unable to new host in harmony") panic("unable to new host in harmony")
} }
host.AddPeer(&leader) host.AddPeer(&nodeConfig.Leader)
// Consensus object. // Consensus object.
// TODO: consensus object shouldn't start here // TODO: consensus object shouldn't start here
consensus := consensus.New(host, shardID, peers, leader) consensus := consensus.New(host, shardID, peers, nodeConfig.Leader)
consensus.MinPeers = *minPeers consensus.MinPeers = *minPeers
// Start Profiler for leader if profile argument is on // Start Profiler for leader if profile argument is on
if role == "leader" && (*profile || *metricsReportURL != "") { if nodeConfig.StringRole == "leader" && (*profile || *metricsReportURL != "") {
prof := profiler.GetProfiler() prof := profiler.GetProfiler()
prof.Config(shardID, *metricsReportURL) prof.Config(shardID, *metricsReportURL)
if *profile { if *profile {
@ -216,19 +193,18 @@ func main() {
consensus.ChainReader = currentNode.Blockchain() consensus.ChainReader = currentNode.Blockchain()
if *isBeacon { if *isBeacon {
if role == "leader" { if nodeConfig.StringRole == "leader" {
currentNode.NodeConfig.SetRole(nodeconfig.BeaconLeader) currentNode.NodeConfig.SetRole(nodeconfig.BeaconLeader)
} else { } else {
currentNode.NodeConfig.SetRole(nodeconfig.BeaconValidator) currentNode.NodeConfig.SetRole(nodeconfig.BeaconValidator)
} }
currentNode.NodeConfig.SetShardGroupID(p2p.GroupIDBeacon) currentNode.NodeConfig.SetShardGroupID(p2p.GroupIDBeacon)
} else { } else {
beacondb, _ := InitLDBDatabase(*ip, *port, *freshDB, true) currentNode.AddBeaconChainDatabase(nodeConfig.BeaconDB)
currentNode.AddBeaconChainDatabase(beacondb)
if *isNewNode { if *isNewNode {
currentNode.NodeConfig.SetRole(nodeconfig.NewNode) currentNode.NodeConfig.SetRole(nodeconfig.NewNode)
} else if role == "leader" { } else if nodeConfig.StringRole == "leader" {
currentNode.NodeConfig.SetRole(nodeconfig.ShardLeader) currentNode.NodeConfig.SetRole(nodeconfig.ShardLeader)
} else { } else {
currentNode.NodeConfig.SetRole(nodeconfig.ShardValidator) currentNode.NodeConfig.SetRole(nodeconfig.ShardValidator)
@ -239,7 +215,7 @@ func main() {
// Add randomness protocol // Add randomness protocol
// TODO: enable drand only for beacon chain // TODO: enable drand only for beacon chain
// TODO: put this in a better place other than main. // TODO: put this in a better place other than main.
dRand := drand.New(host, shardID, peers, leader, currentNode.ConfirmedBlockChannel, *isLeader) dRand := drand.New(host, shardID, peers, nodeConfig.Leader, currentNode.ConfirmedBlockChannel, *isLeader)
currentNode.Consensus.RegisterPRndChannel(dRand.PRndChannel) currentNode.Consensus.RegisterPRndChannel(dRand.PRndChannel)
currentNode.Consensus.RegisterRndChannel(dRand.RndChannel) currentNode.Consensus.RegisterRndChannel(dRand.RndChannel)
currentNode.DRand = dRand currentNode.DRand = dRand

@ -9,7 +9,9 @@ import (
"sync" "sync"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/p2p"
p2p_crypto "github.com/libp2p/go-libp2p-crypto"
) )
// Role defines a role of a node. // Role defines a role of a node.
@ -55,17 +57,25 @@ const (
// ConfigType is the structure of all node related configuration variables // ConfigType is the structure of all node related configuration variables
type ConfigType struct { type ConfigType struct {
// The three groupID design, please refer to https://github.com/harmony-one/harmony/blob/master/node/node.md#libp2p-integration // The three groupID design, please refer to https://github.com/harmony-one/harmony/blob/master/node/node.md#libp2p-integration
beacon p2p.GroupID // the beacon group ID beacon p2p.GroupID // the beacon group ID
group p2p.GroupID // the group ID of the shard group p2p.GroupID // the group ID of the shard
client p2p.GroupID // the client group ID of the shard client p2p.GroupID // the client group ID of the shard
isClient bool // whether this node is a client node, such as wallet/txgen isClient bool // whether this node is a client node, such as wallet/txgen
isBeacon bool // whether this node is a beacon node or not isBeacon bool // whether this node is a beacon node or not
isLeader bool // whether this node is a leader or not isLeader bool // whether this node is a leader or not
shardID uint32 // shardID of this node shardID uint32 // shardID of this node
role Role // Role of the node role Role // Role of the node
StringRole string
StakingPriKey *ecdsa.PrivateKey StakingPriKey *ecdsa.PrivateKey
P2pPriKey p2p_crypto.PrivKey
BlsPriKey *bls.SecretKey
BlsPubKey *bls.PublicKey
MainDB *ethdb.LDBDatabase MainDB *ethdb.LDBDatabase
BeaconDB *ethdb.LDBDatabase
SelfPeer p2p.Peer
Leader p2p.Peer
} }
// configs is a list of node configuration. // configs is a list of node configuration.

Loading…
Cancel
Save