-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcommittee.go
52 lines (45 loc) · 1.29 KB
/
committee.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
package gpbft
import (
"fmt"
"sync"
)
var _ CommitteeProvider = (*cachedCommitteeProvider)(nil)
type cachedCommitteeProvider struct {
delegate CommitteeProvider
// mu guards access to committees.
mu sync.Mutex
committees map[uint64]*Committee
}
func newCachedCommitteeProvider(delegate CommitteeProvider) *cachedCommitteeProvider {
return &cachedCommitteeProvider{
delegate: delegate,
committees: make(map[uint64]*Committee),
}
}
func (c *cachedCommitteeProvider) GetCommittee(instance uint64) (*Committee, error) {
c.mu.Lock()
defer c.mu.Unlock()
if committee, found := c.committees[instance]; found {
return committee, nil
}
switch committee, err := c.delegate.GetCommittee(instance); {
case err != nil:
return nil, fmt.Errorf("instance %d: %w: %w", instance, ErrValidationNoCommittee, err)
case committee == nil:
return nil, fmt.Errorf("unexpected nil committee for instance %d", instance)
default:
c.committees[instance] = committee
return committee, nil
}
}
// EvictCommitteesBefore evicts any cached committees that correspond to
// instances prior to the given instance.
func (c *cachedCommitteeProvider) EvictCommitteesBefore(instance uint64) {
c.mu.Lock()
defer c.mu.Unlock()
for i := range c.committees {
if i < instance {
delete(c.committees, i)
}
}
}