github.com/koko1123/flow-go-1@v0.29.6/module/jobqueue/finalized_block_reader_test.go (about) 1 package jobqueue_test 2 3 import ( 4 "testing" 5 6 "github.com/dgraph-io/badger/v3" 7 "github.com/stretchr/testify/require" 8 9 "github.com/koko1123/flow-go-1/engine/testutil" 10 vertestutils "github.com/koko1123/flow-go-1/engine/verification/utils/unittest" 11 "github.com/koko1123/flow-go-1/model/flow" 12 "github.com/koko1123/flow-go-1/model/flow/filter" 13 "github.com/koko1123/flow-go-1/module/jobqueue" 14 "github.com/koko1123/flow-go-1/module/metrics" 15 "github.com/koko1123/flow-go-1/module/trace" 16 "github.com/koko1123/flow-go-1/utils/unittest" 17 ) 18 19 // TestBlockReader evaluates that block reader correctly reads stored finalized blocks from the blocks storage and 20 // protocol state. 21 func TestBlockReader(t *testing.T) { 22 withReader(t, 10, func(reader *jobqueue.FinalizedBlockReader, blocks []*flow.Block) { 23 // head of block reader should be the same height as the last block on the chain. 24 head, err := reader.Head() 25 require.NoError(t, err) 26 require.Equal(t, head, blocks[len(blocks)-1].Header.Height) 27 28 // retrieved blocks from block reader should be the same as the original blocks stored in it. 29 for _, actual := range blocks { 30 index := actual.Header.Height 31 job, err := reader.AtIndex(index) 32 require.NoError(t, err) 33 34 retrieved, err := jobqueue.JobToBlock(job) 35 require.NoError(t, err) 36 require.Equal(t, actual.ID(), retrieved.ID()) 37 } 38 }) 39 } 40 41 // withReader is a test helper that sets up a block reader. 42 // It also provides a chain of specified number of finalized blocks ready to read by block reader, i.e., the protocol state is extended with the 43 // chain of blocks and the blocks are stored in blocks storage. 44 func withReader( 45 t *testing.T, 46 blockCount int, 47 withBlockReader func(*jobqueue.FinalizedBlockReader, []*flow.Block), 48 ) { 49 require.Equal(t, blockCount%2, 0, "block count for this test should be even") 50 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 51 52 collector := &metrics.NoopCollector{} 53 tracer := trace.NewNoopTracer() 54 participants := unittest.IdentityListFixture(5, unittest.WithAllRoles()) 55 rootSnapshot := unittest.RootSnapshotFixture(participants) 56 s := testutil.CompleteStateFixture(t, collector, tracer, rootSnapshot) 57 58 reader := jobqueue.NewFinalizedBlockReader(s.State, s.Storage.Blocks) 59 60 // generates a chain of blocks in the form of root <- R1 <- C1 <- R2 <- C2 <- ... where Rs are distinct reference 61 // blocks (i.e., containing guarantees), and Cs are container blocks for their preceding reference block, 62 // Container blocks only contain receipts of their preceding reference blocks. But they do not 63 // hold any guarantees. 64 root, err := s.State.Params().Root() 65 require.NoError(t, err) 66 clusterCommittee := participants.Filter(filter.HasRole(flow.RoleCollection)) 67 results := vertestutils.CompleteExecutionReceiptChainFixture(t, root, blockCount/2, vertestutils.WithClusterCommittee(clusterCommittee)) 68 blocks := vertestutils.ExtendStateWithFinalizedBlocks(t, results, s.State) 69 70 withBlockReader(reader, blocks) 71 }) 72 }