github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/jobqueue/finalized_block_reader_test.go (about) 1 package jobqueue_test 2 3 import ( 4 "testing" 5 6 "github.com/dgraph-io/badger/v2" 7 "github.com/stretchr/testify/require" 8 9 "github.com/onflow/flow-go/engine/testutil" 10 vertestutils "github.com/onflow/flow-go/engine/verification/utils/unittest" 11 "github.com/onflow/flow-go/model/flow" 12 "github.com/onflow/flow-go/model/flow/filter" 13 "github.com/onflow/flow-go/module/jobqueue" 14 "github.com/onflow/flow-go/module/metrics" 15 "github.com/onflow/flow-go/module/trace" 16 "github.com/onflow/flow-go/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 log := unittest.Logger() 55 participants := unittest.IdentityListFixture(5, unittest.WithAllRoles()) 56 rootSnapshot := unittest.RootSnapshotFixture(participants) 57 s := testutil.CompleteStateFixture(t, log, collector, tracer, rootSnapshot) 58 59 reader := jobqueue.NewFinalizedBlockReader(s.State, s.Storage.Blocks) 60 61 // generates a chain of blocks in the form of root <- R1 <- C1 <- R2 <- C2 <- ... where Rs are distinct reference 62 // blocks (i.e., containing guarantees), and Cs are container blocks for their preceding reference block, 63 // Container blocks only contain receipts of their preceding reference blocks. But they do not 64 // hold any guarantees. 65 root, err := s.State.Final().Head() 66 require.NoError(t, err) 67 protocolState, err := s.State.Final().ProtocolState() 68 require.NoError(t, err) 69 protocolStateID := protocolState.ID() 70 71 clusterCommittee := participants.Filter(filter.HasRole[flow.Identity](flow.RoleCollection)) 72 sources := unittest.RandomSourcesFixture(10) 73 results := vertestutils.CompleteExecutionReceiptChainFixture(t, root, protocolStateID, blockCount/2, sources, vertestutils.WithClusterCommittee(clusterCommittee)) 74 blocks := vertestutils.ExtendStateWithFinalizedBlocks(t, results, s.State) 75 76 withBlockReader(reader, blocks) 77 }) 78 }