-
Notifications
You must be signed in to change notification settings - Fork 4
/
helpers.go
63 lines (55 loc) · 1.43 KB
/
helpers.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 dbft
type (
// inbox is a structure storing messages from a single epoch.
inbox[H Hash] struct {
prepare map[uint16]ConsensusPayload[H]
chViews map[uint16]ConsensusPayload[H]
preCommit map[uint16]ConsensusPayload[H]
commit map[uint16]ConsensusPayload[H]
}
// cache is an auxiliary structure storing messages
// from future epochs.
cache[H Hash] struct {
mail map[uint32]*inbox[H]
}
)
func newInbox[H Hash]() *inbox[H] {
return &inbox[H]{
prepare: make(map[uint16]ConsensusPayload[H]),
chViews: make(map[uint16]ConsensusPayload[H]),
preCommit: make(map[uint16]ConsensusPayload[H]),
commit: make(map[uint16]ConsensusPayload[H]),
}
}
func newCache[H Hash]() cache[H] {
return cache[H]{
mail: make(map[uint32]*inbox[H]),
}
}
func (c *cache[H]) getHeight(h uint32) *inbox[H] {
if m, ok := c.mail[h]; ok {
delete(c.mail, h)
return m
}
return nil
}
func (c *cache[H]) addMessage(m ConsensusPayload[H]) {
msgs, ok := c.mail[m.Height()]
if !ok {
msgs = newInbox[H]()
c.mail[m.Height()] = msgs
}
switch m.Type() {
case PrepareRequestType, PrepareResponseType:
msgs.prepare[m.ValidatorIndex()] = m
case ChangeViewType:
msgs.chViews[m.ValidatorIndex()] = m
case PreCommitType:
msgs.preCommit[m.ValidatorIndex()] = m
case CommitType:
msgs.commit[m.ValidatorIndex()] = m
default:
// Others are recoveries and we don't currently use them.
// Theoretically messages could be extracted.
}
}