diff --git a/cmd/harmony/main.go b/cmd/harmony/main.go index 913dad052..97828163e 100644 --- a/cmd/harmony/main.go +++ b/cmd/harmony/main.go @@ -115,6 +115,10 @@ func initSetup() { func createGlobalConfig() *nodeconfig.ConfigType { var err error nodeConfig := nodeconfig.GetGlobalConfig() + + // Currently we hardcode only one shard. + nodeConfig.ShardIDString = "0" + nodeConfig.StakingPriKey = node.LoadStakingKeyFromFile(*stakingKeyFile, *accountIndex) nodeConfig.P2pPriKey, _, err = utils.LoadKeyFromFile(*keyFile) @@ -142,49 +146,28 @@ func createGlobalConfig() *nodeconfig.ConfigType { nodeConfig.StringRole = "validator" } - return nodeConfig -} - -func main() { - flag.Var(&utils.BootNodes, "bootnodes", "a list of bootnode multiaddress") - flag.Parse() - - initSetup() - nodeConfig := createGlobalConfig() - - var shardID = "0" - var peers []p2p.Peer - var clientPeer *p2p.Peer - - // Init logging. - loggingInit(*logFolder, nodeConfig.StringRole, *ip, *port, *onlyLogTps) - - host, err := p2pimpl.NewHost(&nodeConfig.SelfPeer, nodeConfig.P2pPriKey) + nodeConfig.Host, err = p2pimpl.NewHost(&nodeConfig.SelfPeer, nodeConfig.P2pPriKey) if *logConn { - host.GetP2PHost().Network().Notify(utils.ConnLogger) + nodeConfig.Host.GetP2PHost().Network().Notify(utils.ConnLogger) } if err != nil { panic("unable to new host in harmony") } - host.AddPeer(&nodeConfig.Leader) + nodeConfig.Host.AddPeer(&nodeConfig.Leader) + + return nodeConfig +} +func setUpConsensusAndNode(nodeConfig *nodeconfig.ConfigType) (*consensus.Consensus, *node.Node) { // Consensus object. // TODO: consensus object shouldn't start here - consensus := consensus.New(host, shardID, peers, nodeConfig.Leader) + // TODO(minhdoan): During refactoring, found out that the peers list is actually empty. Need to clean up the logic of consensus later. + consensus := consensus.New(nodeConfig.Host, nodeConfig.ShardIDString, []p2p.Peer{}, nodeConfig.Leader) consensus.MinPeers = *minPeers - // Start Profiler for leader if profile argument is on - if nodeConfig.StringRole == "leader" && (*profile || *metricsReportURL != "") { - prof := profiler.GetProfiler() - prof.Config(shardID, *metricsReportURL) - if *profile { - prof.Start() - } - } - // Current node. - currentNode := node.New(host, consensus, nodeConfig.MainDB) + currentNode := node.New(nodeConfig.Host, consensus, nodeConfig.MainDB) currentNode.Consensus.OfflinePeers = currentNode.OfflinePeers currentNode.NodeConfig.SetRole(nodeconfig.NewNode) currentNode.AccountKey = nodeConfig.StakingPriKey @@ -215,26 +198,45 @@ func main() { // Add randomness protocol // TODO: enable drand only for beacon chain // TODO: put this in a better place other than main. - dRand := drand.New(host, shardID, peers, nodeConfig.Leader, currentNode.ConfirmedBlockChannel, *isLeader) + // TODO(minhdoan): During refactoring, found out that the peers list is actually empty. Need to clean up the logic of drand later. + dRand := drand.New(nodeConfig.Host, nodeConfig.ShardIDString, []p2p.Peer{}, nodeConfig.Leader, currentNode.ConfirmedBlockChannel, *isLeader) currentNode.Consensus.RegisterPRndChannel(dRand.PRndChannel) currentNode.Consensus.RegisterRndChannel(dRand.RndChannel) currentNode.DRand = dRand - // If there is a client configured in the node list. - if clientPeer != nil { - currentNode.ClientPeer = clientPeer - } - // Assign closure functions to the consensus object consensus.BlockVerifier = currentNode.VerifyNewBlock consensus.OnConsensusDone = currentNode.PostConsensusProcessing currentNode.State = node.NodeWaitToJoin + return consensus, currentNode +} + +func main() { + flag.Var(&utils.BootNodes, "bootnodes", "a list of bootnode multiaddress") + flag.Parse() + + initSetup() + nodeConfig := createGlobalConfig() + + // Init logging. + loggingInit(*logFolder, nodeConfig.StringRole, *ip, *port, *onlyLogTps) + + // Start Profiler for leader if profile argument is on + if nodeConfig.StringRole == "leader" && (*profile || *metricsReportURL != "") { + prof := profiler.GetProfiler() + prof.Config(nodeConfig.ShardIDString, *metricsReportURL) + if *profile { + prof.Start() + } + } + + consensus, currentNode := setUpConsensusAndNode(nodeConfig) if consensus.IsLeader { go currentNode.SendPongMessage() } - log.Info("New Harmony Node ====", "Role", currentNode.NodeConfig.Role(), "multiaddress", fmt.Sprintf("/ip4/%s/tcp/%s/p2p/%s", *ip, *port, host.GetID().Pretty())) + log.Info("New Harmony Node ====", "Role", currentNode.NodeConfig.Role(), "multiaddress", fmt.Sprintf("/ip4/%s/tcp/%s/p2p/%s", *ip, *port, nodeConfig.Host.GetID().Pretty())) go currentNode.SupportSyncing() currentNode.ServiceManagerSetup() currentNode.RunServices() diff --git a/internal/configs/node/config.go b/internal/configs/node/config.go index 02e6a5d99..dc1696409 100644 --- a/internal/configs/node/config.go +++ b/internal/configs/node/config.go @@ -57,16 +57,18 @@ const ( // ConfigType is the structure of all node related configuration variables type ConfigType struct { // 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 - group p2p.GroupID // the 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 - isBeacon bool // whether this node is a beacon node or not - isLeader bool // whether this node is a leader or not - shardID uint32 // shardID of this node - role Role // Role of the node - StringRole string - + beacon p2p.GroupID // the beacon group ID + group p2p.GroupID // the 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 + isBeacon bool // whether this node is a beacon node or not + isLeader bool // whether this node is a leader or not + shardID uint32 // shardID of this node + role Role // Role of the node + + ShardIDString string + StringRole string + Host p2p.Host StakingPriKey *ecdsa.PrivateKey P2pPriKey p2p_crypto.PrivKey BlsPriKey *bls.SecretKey