finish logic of new block service

pull/375/head
Minh Doan 6 years ago committed by Minh Doan
parent a3bd76e9cf
commit fb630dbcca
  1. 24
      api/service/newblock/service.go
  2. 9
      node/node_newblock.go

@ -8,11 +8,13 @@ import (
type Service struct { type Service struct {
stopChan chan struct{} stopChan chan struct{}
stoppedChan chan struct{} stoppedChan chan struct{}
readySignal chan struct{}
waitForConsensusReady func(readySignal chan struct{}, stopChan chan struct{}, stoppedChan chan struct{})
} }
// NewService returns new block service. // NewService returns new block service.
func NewService() *Service { func NewService(readySignal chan struct{}, waitForConsensusReady func(readySignal chan struct{}, stopChan chan struct{}, stoppedChan chan struct{})) *Service {
return &Service{} return &Service{readySignal: readySignal, waitForConsensusReady: waitForConsensusReady}
} }
// StartService starts new block service. // StartService starts new block service.
@ -30,23 +32,7 @@ func (s *Service) Init() {
// Run runs new block. // Run runs new block.
func (s *Service) Run(stopChan chan struct{}, stoppedChan chan struct{}) { func (s *Service) Run(stopChan chan struct{}, stoppedChan chan struct{}) {
go func() { s.waitForConsensusReady(s.readySignal, s.stopChan, s.stoppedChan)
defer close(stoppedChan)
for {
select {
default:
utils.GetLogInstance().Info("Running new block")
// TODO: Write some logic here.
s.DoService()
case <-stopChan:
return
}
}
}()
}
// DoService does new block.
func (s *Service) DoService() {
} }
// StopService stops new block service. // StopService stops new block service.

@ -8,7 +8,11 @@ import (
) )
// WaitForConsensusReady listen for the readiness signal from consensus and generate new block for consensus. // WaitForConsensusReady listen for the readiness signal from consensus and generate new block for consensus.
func (node *Node) WaitForConsensusReady(readySignal chan struct{}) { func (node *Node) WaitForConsensusReady(readySignal chan struct{}, stopChan chan struct{}, stoppedChan chan struct{}) {
go func() {
// Setup stoppedChan
defer close(stoppedChan)
utils.GetLogInstance().Debug("Waiting for Consensus ready", "node", node) utils.GetLogInstance().Debug("Waiting for Consensus ready", "node", node)
time.Sleep(15 * time.Second) // Wait for other nodes to be ready (test-only) time.Sleep(15 * time.Second) // Wait for other nodes to be ready (test-only)
@ -24,6 +28,8 @@ func (node *Node) WaitForConsensusReady(readySignal chan struct{}) {
node.Consensus.ResetState() node.Consensus.ResetState()
timeoutCount++ timeoutCount++
utils.GetLogInstance().Debug("Consensus timeout, retry!", "count", timeoutCount, "node", node) utils.GetLogInstance().Debug("Consensus timeout, retry!", "count", timeoutCount, "node", node)
case <-stopChan:
return
} }
for { for {
@ -57,4 +63,5 @@ func (node *Node) WaitForConsensusReady(readySignal chan struct{}) {
node.BlockChannel <- newBlock node.BlockChannel <- newBlock
} }
} }
}()
} }

Loading…
Cancel
Save