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