github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/model/flow/chunk_test.go (about) 1 package flow_test 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 "github.com/stretchr/testify/require" 8 9 "github.com/onflow/flow-go/model/flow" 10 "github.com/onflow/flow-go/utils/rand" 11 "github.com/onflow/flow-go/utils/unittest" 12 ) 13 14 // TestChunkList_ByIndex evaluates reliability of ByIndex method against within and 15 // out of range indices 16 func TestChunkList_ByIndex(t *testing.T) { 17 // creates a chunk list with the size of 10 18 var chunkList flow.ChunkList = make([]*flow.Chunk, 10) 19 20 // an out of index chunk by index 21 _, ok := chunkList.ByIndex(11) 22 require.False(t, ok) 23 24 // a within range chunk by index 25 _, ok = chunkList.ByIndex(1) 26 require.True(t, ok) 27 } 28 29 // TestDistinctChunkIDs_EmptyChunks evaluates that two empty chunks 30 // with the distinct block ids would have distinct chunk ids. 31 func TestDistinctChunkIDs_EmptyChunks(t *testing.T) { 32 // generates two random block ids and requires them 33 // being distinct 34 blockIdA := unittest.IdentifierFixture() 35 blockIdB := unittest.IdentifierFixture() 36 require.NotEqual(t, blockIdA, blockIdB) 37 38 // generates a chunk associated with each block id 39 chunkA := &flow.Chunk{ 40 ChunkBody: flow.ChunkBody{ 41 BlockID: blockIdA, 42 }, 43 } 44 45 chunkB := &flow.Chunk{ 46 ChunkBody: flow.ChunkBody{ 47 BlockID: blockIdB, 48 }, 49 } 50 51 require.NotEqual(t, chunkA.ID(), chunkB.ID()) 52 } 53 54 // TestDistinctChunkIDs_FullChunks evaluates that two full chunks 55 // with completely identical fields but distinct block ids have 56 // distinct chunk ids. 57 func TestDistinctChunkIDs_FullChunks(t *testing.T) { 58 // generates two random block ids and requires them 59 // being distinct 60 blockIdA := unittest.IdentifierFixture() 61 blockIdB := unittest.IdentifierFixture() 62 require.NotEqual(t, blockIdA, blockIdB) 63 64 // generates a chunk associated with blockA 65 chunkA := unittest.ChunkFixture(blockIdA, 42) 66 67 // generates a deep copy of chunkA in chunkB 68 chunkB := *chunkA 69 70 // since chunkB is a deep copy of chunkA their 71 // chunk ids should be the same 72 require.Equal(t, chunkA.ID(), chunkB.ID()) 73 74 // changes block id in chunkB 75 chunkB.BlockID = blockIdB 76 77 // chunks with distinct block ids should have distinct chunk ids 78 require.NotEqual(t, chunkA.ID(), chunkB.ID()) 79 } 80 81 // TestChunkList_Indices evaluates the Indices method of ChunkList on lists of different sizes. 82 func TestChunkList_Indices(t *testing.T) { 83 cl := unittest.ChunkListFixture(5, unittest.IdentifierFixture()) 84 t.Run("empty chunk subset indices", func(t *testing.T) { 85 // subset of chunk list that is empty should return an empty list 86 subset := flow.ChunkList{} 87 indices := subset.Indices() 88 require.Len(t, indices, 0) 89 }) 90 91 t.Run("single chunk subset indices", func(t *testing.T) { 92 // subset of chunk list that contains chunk index of zero, should 93 // return a uint64 slice that only contains chunk index of zero. 94 subset := cl[:1] 95 indices := subset.Indices() 96 require.Len(t, indices, 1) 97 require.Contains(t, indices, uint64(0)) 98 }) 99 100 t.Run("multiple chunk subset indices", func(t *testing.T) { 101 // subset that only contains even chunk indices, should return 102 // a uint64 slice that only contains even chunk indices 103 subset := flow.ChunkList{cl[0], cl[2], cl[4]} 104 indices := subset.Indices() 105 require.Len(t, indices, 3) 106 require.Contains(t, indices, uint64(0), uint64(2), uint64(4)) 107 }) 108 } 109 110 func TestChunkIndexIsSet(t *testing.T) { 111 112 i, err := rand.Uint() 113 require.NoError(t, err) 114 chunk := flow.NewChunk( 115 unittest.IdentifierFixture(), 116 int(i), 117 unittest.StateCommitmentFixture(), 118 21, 119 unittest.IdentifierFixture(), 120 unittest.StateCommitmentFixture(), 121 17995, 122 ) 123 124 assert.Equal(t, i, uint(chunk.Index)) 125 assert.Equal(t, i, uint(chunk.CollectionIndex)) 126 } 127 128 func TestChunkNumberOfTxsIsSet(t *testing.T) { 129 130 i, err := rand.Uint32() 131 require.NoError(t, err) 132 chunk := flow.NewChunk( 133 unittest.IdentifierFixture(), 134 3, 135 unittest.StateCommitmentFixture(), 136 int(i), 137 unittest.IdentifierFixture(), 138 unittest.StateCommitmentFixture(), 139 17995, 140 ) 141 142 assert.Equal(t, i, uint32(chunk.NumberOfTransactions)) 143 } 144 145 func TestChunkTotalComputationUsedIsSet(t *testing.T) { 146 147 i, err := rand.Uint64() 148 require.NoError(t, err) 149 chunk := flow.NewChunk( 150 unittest.IdentifierFixture(), 151 3, 152 unittest.StateCommitmentFixture(), 153 21, 154 unittest.IdentifierFixture(), 155 unittest.StateCommitmentFixture(), 156 i, 157 ) 158 159 assert.Equal(t, i, chunk.TotalComputationUsed) 160 }