diff --git a/api/service/consensus/consensus.go b/api/service/consensus/consensus.go new file mode 100644 index 000000000..1d87a785f --- /dev/null +++ b/api/service/consensus/consensus.go @@ -0,0 +1,35 @@ +package consensus + +import ( + "github.com/harmony-one/harmony/consensus" + "github.com/harmony-one/harmony/core/types" + "github.com/harmony-one/harmony/internal/utils" +) + +// Service is the consensus service. +type Service struct { + blockChannel chan *types.Block // The channel to receive new blocks from Node + consensus *consensus.Consensus + stopChan chan struct{} + stoppedChan chan struct{} +} + +// NewService returns consensus service. +func NewService(blockChannel chan *types.Block, consensus *consensus.Consensus) *Service { + return &Service{blockChannel: blockChannel, consensus: consensus} +} + +// StartService starts service. +func (s *Service) StartService() { + s.stopChan = make(chan struct{}) + s.stoppedChan = make(chan struct{}) + s.consensus.WaitForNewBlock(s.blockChannel, s.stopChan, s.stoppedChan) +} + +// StopService stops service. +func (s *Service) StopService() { + utils.GetLogInstance().Info("Stopping consensus service.") + s.stopChan <- struct{}{} + <-s.stoppedChan + utils.GetLogInstance().Info("Consensus service stopped.") +} diff --git a/node/service/random_generation.go b/api/service/rgen/service.go similarity index 50% rename from node/service/random_generation.go rename to api/service/rgen/service.go index e40ad4373..2043234ca 100644 --- a/node/service/random_generation.go +++ b/api/service/rgen/service.go @@ -1,35 +1,35 @@ -package service +package rgen import ( "github.com/harmony-one/harmony/internal/utils" ) -// RandomGeneration is the consensus service. -type RandomGeneration struct { +// Service is the random generation service. +type Service struct { stopChan chan struct{} stoppedChan chan struct{} } -// NewRandomGeneration returns random generation service. -func NewRandomGeneration() *RandomGeneration { - return &RandomGeneration{} +// NewService returns random generation service. +func NewService() *Service { + return &Service{} } // StartService starts random generation service. -func (cs *RandomGeneration) StartService() { - cs.stopChan = make(chan struct{}) - cs.stoppedChan = make(chan struct{}) +func (s *Service) StartService() { + s.stopChan = make(chan struct{}) + s.stoppedChan = make(chan struct{}) - cs.Init() - cs.Run(cs.stopChan, cs.stoppedChan) + s.Init() + s.Run(s.stopChan, s.stoppedChan) } // Init initializes random generation. -func (cs *RandomGeneration) Init() { +func (s *Service) Init() { } // Run runs random generation. -func (cs *RandomGeneration) Run(stopChan chan struct{}, stoppedChan chan struct{}) { +func (s *Service) Run(stopChan chan struct{}, stoppedChan chan struct{}) { go func() { defer close(stoppedChan) for { @@ -37,7 +37,7 @@ func (cs *RandomGeneration) Run(stopChan chan struct{}, stoppedChan chan struct{ default: utils.GetLogInstance().Info("Running random generation") // Write some logic here. - cs.DoRandomGeneration() + s.DoRandomGeneration() case <-stopChan: return } @@ -46,14 +46,14 @@ func (cs *RandomGeneration) Run(stopChan chan struct{}, stoppedChan chan struct{ } // DoRandomGeneration does random generation. -func (cs *RandomGeneration) DoRandomGeneration() { +func (s *Service) DoRandomGeneration() { } // StopService stops random generation service. -func (cs *RandomGeneration) StopService() { +func (s *Service) StopService() { utils.GetLogInstance().Info("Stopping random generation service.") - cs.stopChan <- struct{}{} - <-cs.stoppedChan + s.stopChan <- struct{}{} + <-s.stoppedChan utils.GetLogInstance().Info("Random generation stopped.") }