/
consensusmanager.go
127 lines (95 loc) · 4.2 KB
/
consensusmanager.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package tangle
import (
"github.com/iotaledger/hive.go/events"
"github.com/iotaledger/goshimmer/packages/ledgerstate"
)
// region ConsensusManager /////////////////////////////////////////////////////////////////////////////////////////////
// ConsensusManager is the component in charge of forming opinions about timestamps and payloads.
type ConsensusManager struct {
Events *ConsensusManagerEvents
tangle *Tangle
}
// NewConsensusManager returns a new ConsensusManager.
func NewConsensusManager(tangle *Tangle) (opinionFormer *ConsensusManager) {
opinionFormer = &ConsensusManager{
Events: &ConsensusManagerEvents{
MessageOpinionFormed: events.NewEvent(MessageIDCaller),
StatementProcessed: events.NewEvent(MessageCaller),
},
tangle: tangle,
}
return
}
// Setup sets up the behavior of the component by making it attach to the relevant events of the other components.
func (o *ConsensusManager) Setup() {
if o.tangle.Options.ConsensusMechanism == nil {
o.tangle.Booker.Events.MessageBooked.Attach(events.NewClosure(func(messageID MessageID) {
o.Events.MessageOpinionFormed.Trigger(messageID)
}))
return
}
o.tangle.Options.ConsensusMechanism.Setup()
}
// Shutdown shuts down the component and persists its state.
func (o *ConsensusManager) Shutdown() {
if o.tangle.Options.ConsensusMechanism == nil {
return
}
o.tangle.Options.ConsensusMechanism.Shutdown()
}
// PayloadLiked returns the opinion of the given MessageID.
func (o *ConsensusManager) PayloadLiked(messageID MessageID) (liked bool) {
o.tangle.Storage.Message(messageID).Consume(func(message *Message) {
if message.Payload().Type() != ledgerstate.TransactionType {
liked = true
return
}
if o.tangle.Options.ConsensusMechanism == nil {
return
}
liked = o.tangle.Options.ConsensusMechanism.TransactionLiked(message.Payload().(*ledgerstate.Transaction).ID())
})
return
}
// MessageEligible returns whether the given messageID is marked as eligible.
func (o *ConsensusManager) MessageEligible(messageID MessageID) (eligible bool) {
if messageID == EmptyMessageID {
return true
}
o.tangle.Storage.MessageMetadata(messageID).Consume(func(messageMetadata *MessageMetadata) {
eligible = messageMetadata.IsEligible()
})
return
}
// SetTransactionLiked sets the transaction like status.
func (o *ConsensusManager) SetTransactionLiked(transactionID ledgerstate.TransactionID, liked bool) (modified bool) {
if o.tangle.Options.ConsensusMechanism == nil {
return
}
return o.tangle.Options.ConsensusMechanism.SetTransactionLiked(transactionID, liked)
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// region ConsensusManagerEvents ///////////////////////////////////////////////////////////////////////////////////////
// ConsensusManagerEvents defines all the events related to the opinion manager.
type ConsensusManagerEvents struct {
// Fired when an opinion of a message is formed.
MessageOpinionFormed *events.Event
// Fired when after a received statement is processed.
StatementProcessed *events.Event
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// region ConsensusMechanism ///////////////////////////////////////////////////////////////////////////////////////////
// ConsensusMechanism is a generic interface allowing the Tangle to use different methods to reach consensus.
type ConsensusMechanism interface {
// Init initializes the ConsensusMechanism by making the Tangle object available that is using it.
Init(tangle *Tangle)
// Setup sets up the behavior of the ConsensusMechanism by making it attach to the relevant events in the Tangle.
Setup()
// TransactionLiked returns a boolean value indicating whether the given Transaction is liked.
TransactionLiked(transactionID ledgerstate.TransactionID) (liked bool)
// Shutdown shuts down the ConsensusMechanism and persists its state.
Shutdown()
// SetTransactionLiked sets the transaction like status.
SetTransactionLiked(transactionID ledgerstate.TransactionID, liked bool) (modified bool)
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////