github.com/koko1123/flow-go-1@v0.29.6/model/chunks/chunkassignment.go (about) 1 package chunks 2 3 import ( 4 "github.com/koko1123/flow-go-1/model/flow" 5 ) 6 7 // Assignment is assignment map of the chunks to the list of the verifier nodes 8 type Assignment struct { 9 // TODO: use a slice here instead of a map, which will be more performant 10 verifiersForChunk map[uint64]map[flow.Identifier]struct{} 11 } 12 13 func NewAssignment() *Assignment { 14 return &Assignment{ 15 verifiersForChunk: make(map[uint64]map[flow.Identifier]struct{}), 16 } 17 } 18 19 // Verifiers returns the list of verifier nodes assigned to a chunk 20 func (a *Assignment) Verifiers(chunk *flow.Chunk) flow.IdentifierList { 21 v := make([]flow.Identifier, 0) 22 for id := range a.verifiersForChunk[chunk.Index] { 23 v = append(v, id) 24 } 25 return v 26 } 27 28 // HasVerifier checks if a chunk is assigned to the given verifier 29 // TODO: method should probably error if chunk has unknown index 30 func (a *Assignment) HasVerifier(chunk *flow.Chunk, identifier flow.Identifier) bool { 31 assignedVerifiers, found := a.verifiersForChunk[chunk.Index] 32 if !found { 33 // is verifier assigned to this chunk? 34 // No, because we only assign verifiers to existing chunks 35 return false 36 } 37 _, isAssigned := assignedVerifiers[identifier] 38 return isAssigned 39 } 40 41 // Add records the list of verifier nodes as the assigned verifiers of the chunk 42 // it returns an error if the list of verifiers is empty or contains duplicate ids 43 func (a *Assignment) Add(chunk *flow.Chunk, verifiers flow.IdentifierList) { 44 // sorts verifiers list based on their identifier 45 v := make(map[flow.Identifier]struct{}) 46 for _, id := range verifiers { 47 v[id] = struct{}{} 48 } 49 a.verifiersForChunk[chunk.Index] = v 50 } 51 52 // ByNodeID returns the indices of all chunks assigned to the given verifierID 53 func (a *Assignment) ByNodeID(verifierID flow.Identifier) []uint64 { 54 var chunks []uint64 55 56 // iterates over pairs of (chunk index, assigned verifiers) 57 for chunkIdx, assignedVerifiers := range a.verifiersForChunk { 58 _, isAssigned := assignedVerifiers[verifierID] 59 if isAssigned { 60 chunks = append(chunks, chunkIdx) 61 } 62 } 63 return chunks 64 } 65 66 // Len returns the number of chunks in the assignment 67 func (a *Assignment) Len() int { 68 return len(a.verifiersForChunk) 69 } 70 71 // AssignmentDataPack 72 // 73 // AssignmentDataPack provides a storable representation of chunk assignments on 74 // mempool 75 type AssignmentDataPack struct { 76 assignment *Assignment 77 fingerprint flow.Identifier 78 } 79 80 // NewAssignmentDataPack casts an assignment and its fingerprint into an assignment data pack 81 func NewAssignmentDataPack(fingerprint flow.Identifier, assignment *Assignment) *AssignmentDataPack { 82 return &AssignmentDataPack{ 83 assignment: assignment, 84 fingerprint: fingerprint, 85 } 86 } 87 88 // ID returns the unique identifier for assignment data pack 89 func (a *AssignmentDataPack) ID() flow.Identifier { 90 return a.fingerprint 91 } 92 93 // Checksum returns the checksum of the assignment data pack 94 func (a *AssignmentDataPack) Checksum() flow.Identifier { 95 return flow.MakeID(a) 96 } 97 98 // Assignment returns the assignment part of the assignment data pack 99 func (a *AssignmentDataPack) Assignment() *Assignment { 100 return a.assignment 101 }