-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
42 lines (37 loc) · 1.35 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
package consensus
import (
msg_pb "github.com/Timestopeofficial/feechain/api/proto/message"
"github.com/Timestopeofficial/feechain/consensus"
"github.com/Timestopeofficial/feechain/core/types"
"github.com/Timestopeofficial/feechain/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{}
startChan chan struct{}
messageChan chan *msg_pb.Message
}
// New returns consensus service.
func New(blockChannel chan *types.Block, consensus *consensus.Consensus, startChan chan struct{}) *Service {
return &Service{blockChannel: blockChannel, consensus: consensus, startChan: startChan}
}
// Start starts consensus service.
func (s *Service) Start() error {
utils.Logger().Info().Msg("[consensus/service] Starting consensus service.")
s.stopChan = make(chan struct{})
s.stoppedChan = make(chan struct{})
s.consensus.Start(s.blockChannel, s.stopChan, s.stoppedChan, s.startChan)
s.consensus.WaitForNewRandomness()
return nil
}
// Stop stops consensus service.
func (s *Service) Stop() error {
utils.Logger().Info().Msg("Stopping consensus service.")
s.stopChan <- struct{}{}
<-s.stoppedChan
utils.Logger().Info().Msg("Consensus service stopped.")
return s.consensus.Close()
}