add node.Role, AddAndRunServices method for Node, clean up code

pull/366/head
Minh Doan 6 years ago committed by Minh Doan
parent 7f3e151c8b
commit 47f7ab1525
  1. 2
      api/services/explorer/service.go
  2. 7
      cmd/harmony.go
  3. 44
      node/node.go
  4. 4
      node/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 {

@ -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()
}

@ -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")
}
}

@ -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)}
}

Loading…
Cancel
Save