-
Notifications
You must be signed in to change notification settings - Fork 14
/
consensus.go
63 lines (52 loc) · 1.17 KB
/
consensus.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package consensus
import (
"github.com/qlcchain/go-qlc/common/event"
"github.com/qlcchain/go-qlc/config"
"github.com/qlcchain/go-qlc/ledger"
"github.com/qlcchain/go-qlc/ledger/process"
"github.com/qlcchain/go-qlc/log"
"go.uber.org/atomic"
"go.uber.org/zap"
)
type ConsensusAlgorithm interface {
Init()
Start()
Stop()
ProcessMsg(bs *BlockSource)
}
type Consensus struct {
ca ConsensusAlgorithm
recv *Receiver
logger *zap.SugaredLogger
ledger *ledger.Ledger
verifier *process.LedgerVerifier
}
var GlobalUncheckedBlockNum atomic.Uint64
func NewConsensus(ca ConsensusAlgorithm, cfg *config.Config, eb event.EventBus) *Consensus {
l := ledger.NewLedger(cfg.LedgerDir())
return &Consensus{
ca: ca,
recv: NewReceiver(eb),
logger: log.NewLogger("consensus"),
ledger: l,
verifier: process.NewLedgerVerifier(l),
}
}
func (c *Consensus) Init() {
c.ca.Init()
c.recv.init(c)
}
func (c *Consensus) Start() {
go c.ca.Start()
err := c.recv.start()
if err != nil {
c.logger.Error("receiver start failed")
}
}
func (c *Consensus) Stop() {
err := c.recv.stop()
if err != nil {
c.logger.Error("receiver stop failed")
}
c.ca.Stop()
}