forked from nspcc-dev/dbft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
60 lines (51 loc) · 1.21 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
package dbft
import (
"github.com/nspcc-dev/dbft/payload"
)
type (
// inbox is a structure storing messages from a single epoch.
inbox struct {
prepare map[uint16]payload.ConsensusPayload
chViews map[uint16]payload.ConsensusPayload
commit map[uint16]payload.ConsensusPayload
}
// cache is an auxiliary structure storing messages
// from future epochs.
cache struct {
mail map[uint32]*inbox
}
)
func newInbox() *inbox {
return &inbox{
prepare: make(map[uint16]payload.ConsensusPayload),
chViews: make(map[uint16]payload.ConsensusPayload),
commit: make(map[uint16]payload.ConsensusPayload),
}
}
func newCache() cache {
return cache{
mail: make(map[uint32]*inbox),
}
}
func (c *cache) getHeight(h uint32) *inbox {
if m, ok := c.mail[h]; ok {
delete(c.mail, h)
return m
}
return nil
}
func (c *cache) addMessage(m payload.ConsensusPayload) {
msgs, ok := c.mail[m.Height()]
if !ok {
msgs = newInbox()
c.mail[m.Height()] = msgs
}
switch m.Type() {
case payload.PrepareRequestType, payload.PrepareResponseType:
msgs.prepare[m.ValidatorIndex()] = m
case payload.ChangeViewType:
msgs.chViews[m.ValidatorIndex()] = m
case payload.CommitType:
msgs.commit[m.ValidatorIndex()] = m
}
}