-
Notifications
You must be signed in to change notification settings - Fork 175
/
chunkassignment.go
101 lines (87 loc) · 3.01 KB
/
chunkassignment.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
101
package chunks
import (
"github.com/onflow/flow-go/model/flow"
)
// Assignment is assignment map of the chunks to the list of the verifier nodes
type Assignment struct {
// TODO: use a slice here instead of a map, which will be more performant
verifiersForChunk map[uint64]map[flow.Identifier]struct{}
}
func NewAssignment() *Assignment {
return &Assignment{
verifiersForChunk: make(map[uint64]map[flow.Identifier]struct{}),
}
}
// Verifiers returns the list of verifier nodes assigned to a chunk
func (a *Assignment) Verifiers(chunk *flow.Chunk) flow.IdentifierList {
v := make([]flow.Identifier, 0)
for id := range a.verifiersForChunk[chunk.Index] {
v = append(v, id)
}
return v
}
// HasVerifier checks if a chunk is assigned to the given verifier
// TODO: method should probably error if chunk has unknown index
func (a *Assignment) HasVerifier(chunk *flow.Chunk, identifier flow.Identifier) bool {
assignedVerifiers, found := a.verifiersForChunk[chunk.Index]
if !found {
// is verifier assigned to this chunk?
// No, because we only assign verifiers to existing chunks
return false
}
_, isAssigned := assignedVerifiers[identifier]
return isAssigned
}
// Add records the list of verifier nodes as the assigned verifiers of the chunk
// it returns an error if the list of verifiers is empty or contains duplicate ids
func (a *Assignment) Add(chunk *flow.Chunk, verifiers flow.IdentifierList) {
// sorts verifiers list based on their identifier
v := make(map[flow.Identifier]struct{})
for _, id := range verifiers {
v[id] = struct{}{}
}
a.verifiersForChunk[chunk.Index] = v
}
// ByNodeID returns the indices of all chunks assigned to the given verifierID
func (a *Assignment) ByNodeID(verifierID flow.Identifier) []uint64 {
var chunks []uint64
// iterates over pairs of (chunk index, assigned verifiers)
for chunkIdx, assignedVerifiers := range a.verifiersForChunk {
_, isAssigned := assignedVerifiers[verifierID]
if isAssigned {
chunks = append(chunks, chunkIdx)
}
}
return chunks
}
// Len returns the number of chunks in the assignment
func (a *Assignment) Len() int {
return len(a.verifiersForChunk)
}
// AssignmentDataPack
//
// AssignmentDataPack provides a storable representation of chunk assignments on
// mempool
type AssignmentDataPack struct {
assignment *Assignment
fingerprint flow.Identifier
}
// NewAssignmentDataPack casts an assignment and its fingerprint into an assignment data pack
func NewAssignmentDataPack(fingerprint flow.Identifier, assignment *Assignment) *AssignmentDataPack {
return &AssignmentDataPack{
assignment: assignment,
fingerprint: fingerprint,
}
}
// ID returns the unique identifier for assignment data pack
func (a *AssignmentDataPack) ID() flow.Identifier {
return a.fingerprint
}
// Checksum returns the checksum of the assignment data pack
func (a *AssignmentDataPack) Checksum() flow.Identifier {
return flow.MakeID(a)
}
// Assignment returns the assignment part of the assignment data pack
func (a *AssignmentDataPack) Assignment() *Assignment {
return a.assignment
}