github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/model/verification/chunkDataPackRequest_test.go (about) 1 package verification_test 2 3 import ( 4 "bytes" 5 "sort" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 10 "github.com/onflow/flow-go/model/flow" 11 "github.com/onflow/flow-go/model/verification" 12 "github.com/onflow/flow-go/utils/unittest" 13 ) 14 15 // TestChunkDataPackRequestList_UniqueRequestInfo tests UniqueRequestInfo method of the ChunkDataPackRequest lists 16 // against combining and merging requests with duplicate chunk IDs. 17 func TestChunkDataPackRequestList_UniqueRequestInfo(t *testing.T) { 18 reqList := verification.ChunkDataPackRequestList{} 19 20 // adds two requests for same chunk ID 21 thisChunkID := unittest.IdentifierFixture() 22 thisReq1 := unittest.ChunkDataPackRequestFixture(unittest.WithChunkID(thisChunkID)) 23 thisReq2 := unittest.ChunkDataPackRequestFixture(unittest.WithChunkID(thisChunkID)) 24 require.NotEqual(t, thisReq1, thisReq2, "fixture request must be distinct") 25 reqList = append(reqList, thisReq1) 26 reqList = append(reqList, thisReq2) 27 28 // creates a distinct request for distinct chunk ID 29 otherChunkID := unittest.IdentifierFixture() 30 require.NotEqual(t, thisChunkID, otherChunkID) 31 otherReq := unittest.ChunkDataPackRequestFixture(unittest.WithChunkID(otherChunkID)) 32 reqList = append(reqList, otherReq) 33 34 // extracts unique request info, must be only two request info one for thisChunkID, and 35 // one for otherChunkID 36 uniqueReqInfo := reqList.UniqueRequestInfo() 37 require.Equal(t, 2, len(uniqueReqInfo)) // only two unique chunk IDs 38 39 // lays out the list into map for testing. 40 reqInfoMap := make(map[flow.Identifier]*verification.ChunkDataPackRequestInfo) 41 for _, reqInfo := range uniqueReqInfo { 42 reqInfoMap[reqInfo.ChunkID] = reqInfo 43 } 44 45 // since thisReq1 and thisReq2 share the same chunk ID, the request info must have union of their 46 // agrees, disagrees, and targets. 47 thisChunkIDReqInfo := reqInfoMap[thisChunkID] 48 49 // pre-sorting these because the call to 'Union' will sort them, and the 'Equal' test requires 50 // that the order be the same 51 sort.Slice(thisChunkIDReqInfo.Agrees, func(p, q int) bool { 52 return bytes.Compare(thisChunkIDReqInfo.Agrees[p][:], thisChunkIDReqInfo.Agrees[q][:]) < 0 53 }) 54 sort.Slice(thisChunkIDReqInfo.Disagrees, func(p, q int) bool { 55 return bytes.Compare(thisChunkIDReqInfo.Disagrees[p][:], thisChunkIDReqInfo.Disagrees[q][:]) < 0 56 }) 57 58 thisChunkIDReqInfo.Targets = thisChunkIDReqInfo.Targets.Sort(flow.Canonical[flow.Identity]) 59 60 require.Equal(t, thisChunkIDReqInfo.Agrees, thisReq1.Agrees.Union(thisReq2.Agrees)) 61 require.Equal(t, thisChunkIDReqInfo.Disagrees, thisReq1.Disagrees.Union(thisReq2.Disagrees)) 62 require.Equal(t, thisChunkIDReqInfo.Targets, thisReq1.Targets.Union(thisReq2.Targets)) 63 64 // there is only one request for otherChunkID, so its request info must be the same as the 65 // original request. 66 otherChunkIDReqInfo := reqInfoMap[otherChunkID] 67 require.Equal(t, *otherChunkIDReqInfo, otherReq.ChunkDataPackRequestInfo) 68 }