From 5d4418c5487bf0371c25ca8cb0a6106d131dbb24 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Mon, 4 Feb 2019 14:04:54 -0800 Subject: [PATCH] add staking service and hook up with networkinfo service --- api/service/manager.go | 4 +-- api/service/staking/service.go | 54 +++++++++++++++++++++++++++++----- node/node.go | 4 +++ 3 files changed, 52 insertions(+), 10 deletions(-) diff --git a/api/service/manager.go b/api/service/manager.go index e81ebdb81..1080a7598 100644 --- a/api/service/manager.go +++ b/api/service/manager.go @@ -47,10 +47,10 @@ func (t Type) String() string { return "BlockProposal" case NetworkInfo: return "NetworkInfo" - case PeerDiscovery: - return "PeerDiscovery" case Staking: return "Staking" + case PeerDiscovery: + return "PeerDiscovery" case Test: return "Test" case Done: diff --git a/api/service/staking/service.go b/api/service/staking/service.go index bd9349f4d..73659e885 100644 --- a/api/service/staking/service.go +++ b/api/service/staking/service.go @@ -5,21 +5,59 @@ import ( "github.com/harmony-one/harmony/p2p" ) -// Service is the struct for staking service. +// Service is the role conversion service. type Service struct { - Host p2p.Host + stopChan chan struct{} + stoppedChan chan struct{} + peerChan chan *p2p.Peer } -//StartService starts the staking service. +// NewService returns role conversion service. +func NewService(peerChan chan *p2p.Peer) *Service { + return &Service{ + stopChan: make(chan struct{}), + stoppedChan: make(chan struct{}), + peerChan: peerChan, + } +} + +// StartService starts role conversion service. func (s *Service) StartService() { - utils.GetLogInstance().Info("Starting staking service.") + s.Init() + s.Run() +} + +// Init initializes role conversion service. +func (s *Service) Init() { +} + +// Run runs role conversion. +func (s *Service) Run() { + // Wait until peer info of beacon chain is ready. + peer := <-s.peerChan + go func() { + defer close(s.stoppedChan) + for { + select { + default: + utils.GetLogInstance().Info("Running role conversion") + // TODO: Write some logic here. + s.DoService(peer) + case <-s.stopChan: + return + } + } + }() } -func (s *Service) createStakingTransaction() { - //creates staking transaction. +// DoService does role conversion. +func (s *Service) DoService(peer *p2p.Peer) { } -// StopService shutdowns staking service. +// StopService stops role conversion service. func (s *Service) StopService() { - utils.GetLogInstance().Info("Shutting down staking service.") + utils.GetLogInstance().Info("Stopping role conversion service.") + s.stopChan <- struct{}{} + <-s.stoppedChan + utils.GetLogInstance().Info("Role conversion stopped.") } diff --git a/node/node.go b/node/node.go index 36edf3e62..0a6e16258 100644 --- a/node/node.go +++ b/node/node.go @@ -30,6 +30,7 @@ import ( consensus_service "github.com/harmony-one/harmony/api/service/consensus" "github.com/harmony-one/harmony/api/service/explorer" "github.com/harmony-one/harmony/api/service/networkinfo" + "github.com/harmony-one/harmony/api/service/staking" "github.com/harmony-one/harmony/api/service/syncing" "github.com/harmony-one/harmony/api/service/syncing/downloader" downloader_pb "github.com/harmony-one/harmony/api/service/syncing/downloader/proto" @@ -692,7 +693,10 @@ func (node *Node) setupForBeaconValidator() { func (node *Node) setupForNewNode() { chanPeer := make(chan *p2p.Peer) + // Add network info serivce. node.serviceManager.RegisterService(service_manager.NetworkInfo, networkinfo.NewService(chanPeer)) + // Add staking service. + node.serviceManager.RegisterService(service_manager.Staking, staking.NewService(chanPeer)) } // ServiceManagerSetup setups service store.