-
Notifications
You must be signed in to change notification settings - Fork 178
/
util.go
100 lines (88 loc) · 3.51 KB
/
util.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
package protocol
import (
"fmt"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/model/flow/filter"
"github.com/onflow/flow-go/module/signature"
)
// IsNodeAuthorizedAt returns whether the node with the given ID is a valid
// un-ejected network participant as of the given state snapshot.
func IsNodeAuthorizedAt(snapshot Snapshot, id flow.Identifier) (bool, error) {
return CheckNodeStatusAt(
snapshot,
id,
filter.HasWeight(true),
filter.Not(filter.Ejected),
)
}
// IsNodeAuthorizedWithRoleAt returns whether the node with the given ID is a valid
// un-ejected network participant with the specified role as of the given state snapshot.
// Expected errors during normal operations:
// - storage.ErrNotFound if snapshot references an unknown block
//
// All other errors are unexpected and potential symptoms of internal state corruption.
func IsNodeAuthorizedWithRoleAt(snapshot Snapshot, id flow.Identifier, role flow.Role) (bool, error) {
return CheckNodeStatusAt(
snapshot,
id,
filter.HasWeight(true),
filter.Not(filter.Ejected),
filter.HasRole(role),
)
}
// CheckNodeStatusAt returns whether the node with the given ID is a valid identity at the given
// state snapshot, and satisfies all checks.
// Expected errors during normal operations:
// - storage.ErrNotFound if snapshot references an unknown block
//
// All other errors are unexpected and potential symptoms of internal state corruption.
func CheckNodeStatusAt(snapshot Snapshot, id flow.Identifier, checks ...flow.IdentityFilter) (bool, error) {
identity, err := snapshot.Identity(id)
if IsIdentityNotFound(err) {
return false, nil
}
if err != nil {
return false, fmt.Errorf("could not retrieve node identity (id=%x): %w)", id, err)
}
for _, check := range checks {
if !check(identity) {
return false, nil
}
}
return true, nil
}
// IsSporkRootSnapshot returns whether the given snapshot is the state snapshot
// representing the initial state for a spork.
func IsSporkRootSnapshot(snapshot Snapshot) (bool, error) {
segment, err := snapshot.SealingSegment()
if err != nil {
return false, fmt.Errorf("could not get snapshot head: %w", err)
}
if len(segment.Blocks) > 1 {
// spork root snapshots uniquely have only one block in the sealing segment
return false, nil
}
return true, nil
}
// FindGuarantors decodes the signer indices from the guarantee, and finds the guarantor identifiers from protocol state
// Expected Error returns during normal operations:
// - signature.InvalidSignerIndicesError if `signerIndices` does not encode a valid set of collection guarantors
// - storage.ErrNotFound if the guarantee's ReferenceBlockID is not found
// - protocol.ErrEpochNotCommitted if epoch has not been committed yet
// - protocol.ErrClusterNotFound if cluster is not found by the given chainID
func FindGuarantors(state State, guarantee *flow.CollectionGuarantee) ([]flow.Identifier, error) {
snapshot := state.AtBlockID(guarantee.ReferenceBlockID)
epochs := snapshot.Epochs()
epoch := epochs.Current()
cluster, err := epoch.ClusterByChainID(guarantee.ChainID)
if err != nil {
return nil, fmt.Errorf(
"fail to retrieve collector clusters for guarantee (ReferenceBlockID: %v, ChainID: %v): %w",
guarantee.ReferenceBlockID, guarantee.ChainID, err)
}
guarantorIDs, err := signature.DecodeSignerIndicesToIdentifiers(cluster.Members().NodeIDs(), guarantee.SignerIndices)
if err != nil {
return nil, fmt.Errorf("could not decode signer indices for guarantee %v: %w", guarantee.ID(), err)
}
return guarantorIDs, nil
}