forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
accepted.go
72 lines (55 loc) · 1.77 KB
/
accepted.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package tracker
import (
"sync"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/snow/validators"
"github.com/MetalBlockchain/metalgo/utils/crypto/bls"
"github.com/MetalBlockchain/metalgo/utils/set"
)
var _ Accepted = (*accepted)(nil)
type Accepted interface {
validators.SetCallbackListener
// SetLastAccepted updates the latest accepted block for [nodeID] to
// [blockID]. If [nodeID] is not currently a validator, this is a noop.
SetLastAccepted(nodeID ids.NodeID, blockID ids.ID)
// LastAccepted returns the latest known accepted block of [nodeID]. If
// [nodeID]'s last accepted block was never unknown, false will be returned.
LastAccepted(nodeID ids.NodeID) (ids.ID, bool)
}
type accepted struct {
lock sync.RWMutex
validators set.Set[ids.NodeID]
frontier map[ids.NodeID]ids.ID
}
func NewAccepted() Accepted {
return &accepted{
frontier: make(map[ids.NodeID]ids.ID),
}
}
func (a *accepted) OnValidatorAdded(nodeID ids.NodeID, _ *bls.PublicKey, _ ids.ID, _ uint64) {
a.lock.Lock()
defer a.lock.Unlock()
a.validators.Add(nodeID)
}
func (a *accepted) OnValidatorRemoved(nodeID ids.NodeID, _ uint64) {
a.lock.Lock()
defer a.lock.Unlock()
a.validators.Remove(nodeID)
delete(a.frontier, nodeID)
}
func (*accepted) OnValidatorWeightChanged(_ ids.NodeID, _, _ uint64) {}
func (a *accepted) SetLastAccepted(nodeID ids.NodeID, frontier ids.ID) {
a.lock.Lock()
defer a.lock.Unlock()
if a.validators.Contains(nodeID) {
a.frontier[nodeID] = frontier
}
}
func (a *accepted) LastAccepted(nodeID ids.NodeID) (ids.ID, bool) {
a.lock.RLock()
defer a.lock.RUnlock()
acceptedID, ok := a.frontier[nodeID]
return acceptedID, ok
}