-
Notifications
You must be signed in to change notification settings - Fork 3
/
service.go
44 lines (37 loc) · 1.59 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package blockproposal
import (
msg_pb "github.com/PositionExchange/posichain/api/proto/message"
"github.com/PositionExchange/posichain/consensus"
"github.com/PositionExchange/posichain/internal/utils"
)
// Service is a block proposal service.
type Service struct {
stopChan chan struct{}
stoppedChan chan struct{}
readySignal chan consensus.ProposalType
commitSigsChan chan []byte
messageChan chan *msg_pb.Message
waitForConsensusReady func(readySignal chan consensus.ProposalType, commitSigsChan chan []byte, stopChan chan struct{}, stoppedChan chan struct{})
}
// New returns a block proposal service.
func New(readySignal chan consensus.ProposalType, commitSigsChan chan []byte, waitForConsensusReady func(readySignal chan consensus.ProposalType, commitSigsChan chan []byte, stopChan chan struct{}, stoppedChan chan struct{})) *Service {
return &Service{readySignal: readySignal, commitSigsChan: commitSigsChan, waitForConsensusReady: waitForConsensusReady}
}
// Start starts block proposal service.
func (s *Service) Start() error {
s.stopChan = make(chan struct{})
s.stoppedChan = make(chan struct{})
s.run(s.stopChan, s.stoppedChan)
return nil
}
func (s *Service) run(stopChan chan struct{}, stoppedChan chan struct{}) {
s.waitForConsensusReady(s.readySignal, s.commitSigsChan, s.stopChan, s.stoppedChan)
}
// Stop stops block proposal service.
func (s *Service) Stop() error {
utils.Logger().Info().Msg("Stopping block proposal service.")
s.stopChan <- struct{}{}
<-s.stoppedChan
utils.Logger().Info().Msg("Role conversion stopped.")
return nil
}