From 47f7ab15251e444a0c0e65d7443ad2b58370d35a Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Tue, 29 Jan 2019 08:21:55 -0800 Subject: [PATCH] add node.Role, AddAndRunServices method for Node, clean up code --- api/services/explorer/service.go | 2 ++ cmd/harmony.go | 7 ++++- node/node.go | 44 +++++++++++++++++++++++--------- node/service.go | 4 +-- 4 files changed, 42 insertions(+), 15 deletions(-) diff --git a/api/services/explorer/service.go b/api/services/explorer/service.go index 831011864..d393a7f52 100644 --- a/api/services/explorer/service.go +++ b/api/services/explorer/service.go @@ -40,12 +40,14 @@ func New(selfPeer *p2p.Peer) *Service { // Start starts explorer service. func (s *Service) Start() { + utils.GetLogInstance().Info("Starting explorer service.") s.Init(true) s.server = s.Run() } // Stop shutdowns explorer service. func (s *Service) Stop() { + utils.GetLogInstance().Info("Shutting down explorer service.") if err := s.server.Shutdown(context.Background()); err != nil { utils.GetLogInstance().Error("Error when shutting down explorer server", "error", err) } else { diff --git a/cmd/harmony.go b/cmd/harmony.go index df52b0d70..ff8919405 100644 --- a/cmd/harmony.go +++ b/cmd/harmony.go @@ -196,6 +196,11 @@ func main() { // Current node. currentNode := node.New(host, consensus, ldb) currentNode.Consensus.OfflinePeers = currentNode.OfflinePeers + if role == "leader" { + currentNode.Role = node.ShardLeader + } else { + currentNode.Role = node.ShardValidator + } // If there is a client configured in the node list. if clientPeer != nil { @@ -225,7 +230,7 @@ func main() { go currentNode.SupportSyncing() if consensus.IsLeader { go currentNode.SupportClient() - go currentNode.SupportExplorer() } + currentNode.AddAndRunServices() currentNode.StartServer() } diff --git a/node/node.go b/node/node.go index 6abb31507..24b3147d7 100644 --- a/node/node.go +++ b/node/node.go @@ -52,6 +52,18 @@ const ( NodeLeader // Node is the leader of some shard. ) +// Role defines a role of a node. +type Role byte + +// All constants for different node roles. +const ( + Unknown Role = iota + ShardLeader + ShardValidator + BeaconLeader + BeaconValidator +) + func (state State) String() string { switch state { case NodeInit: @@ -143,6 +155,9 @@ type Node struct { actionChannel chan *Action serviceStore *ServiceStore + // Node Role. + Role Role + // For test only TestBankKeys []*ecdsa.PrivateKey ContractKeys []*ecdsa.PrivateKey @@ -308,6 +323,7 @@ func (node *Node) DoSyncing() { node.stateMutex.Unlock() continue case consensusBlock := <-node.Consensus.ConsensusBlock: + // never reached from chao if !node.IsOutOfSync(consensusBlock) { if node.State == NodeNotInSync { utils.GetLogInstance().Info("[SYNC] Node is now IN SYNC!") @@ -457,16 +473,6 @@ func (node *Node) SupportClient() { node.StartClientServer() } -// SupportExplorer initializes and starts the client service -func (node *Node) SupportExplorer() { - es := explorer.Service{ - IP: node.SelfPeer.IP, - Port: node.SelfPeer.Port, - } - es.Init(true) - es.Run() -} - // InitClientServer initializes client server. func (node *Node) InitClientServer() { node.clientServer = clientService.NewServer(node.blockchain.State, node.CallFaucetContract) @@ -483,6 +489,7 @@ func (node *Node) StartClientServer() { func (node *Node) SupportSyncing() { node.InitSyncingServer() node.StartSyncingServer() + go node.DoSyncing() go node.SendNewBlockToUnsync() } @@ -494,8 +501,7 @@ func (node *Node) InitSyncingServer() { // StartSyncingServer starts syncing server. func (node *Node) StartSyncingServer() { - port := GetSyncingPort(node.SelfPeer.Port) - utils.GetLogInstance().Info("support_sycning: StartSyncingServer on port:", "port", port) + utils.GetLogInstance().Info("support_sycning: StartSyncingServer") node.downloaderServer.Start(node.SelfPeer.IP, GetSyncingPort(node.SelfPeer.Port)) } @@ -625,3 +631,17 @@ func (node *Node) RemovePeersHandler() { } } } + +// AddAndRunServices manually adds certain services and start them. +func (node *Node) AddAndRunServices() { + node.SetupServiceManager() + + switch node.Role { + case ShardLeader: + // Add and run explorer. + node.RegisterService(SupportExplorer, explorer.New(&node.SelfPeer)) + node.actionChannel <- &Action{action: Start, serviceType: SupportExplorer} + case Unknown: + utils.GetLogInstance().Info("Running node services") + } +} diff --git a/node/service.go b/node/service.go index c7d8c2e32..9c487a8bd 100644 --- a/node/service.go +++ b/node/service.go @@ -48,7 +48,7 @@ func (t ServiceType) String() string { // Constants for timing. const ( // WaitForStatusUpdate is the delay time to update new status. Currently set 1 second for development. Should be 30 minutes for production. - WaitForStatusUpdate = time.Second * 1 + WaitForStatusUpdate = time.Minute * 1 ) // Action is type of service action. @@ -88,7 +88,7 @@ func (node *Node) RegisterService(t ServiceType, service ServiceInterface) { node.serviceStore.Register(t, service) } -// RegisterServices registers all service for a node with its role. +// InitServiceMap initializes service map. func (node *Node) InitServiceMap() { node.serviceStore = &ServiceStore{services: make(map[ServiceType]ServiceInterface)} }