11package storage_test
22
33import (
4- "fmt"
54 "testing"
65
76 pebble2 "github.com/cockroachdb/pebble"
@@ -58,6 +57,7 @@ func TestReceipts(t *testing.T) {
5857 require .NoError (t , err )
5958
6059 suite .Run (t , & ReceiptTestSuite {
60+ BlocksIndexer : bl ,
6161 ReceiptIndexer : pebble .NewReceipts (db ),
6262 DB : db ,
6363 })
@@ -277,16 +277,23 @@ func (b *BlockTestSuite) TestHeights() {
277277
278278type ReceiptTestSuite struct {
279279 suite.Suite
280+ BlocksIndexer storage.BlockIndexer
280281 ReceiptIndexer storage.ReceiptIndexer
281282 DB * pebble.Storage
282283}
283284
284285func (s * ReceiptTestSuite ) TestStoreReceipt () {
285286
286287 s .Run ("store receipt successfully" , func () {
287- receipt := mocks .NewReceipt (1 , common .HexToHash ("0xf1" ))
288288 batch := s .DB .NewBatch ()
289- err := s .ReceiptIndexer .Store ([]* models.Receipt {receipt }, batch )
289+ flowID := flow.Identifier {0x01 }
290+ block := mocks .NewBlock (1 )
291+
292+ err := s .BlocksIndexer .Store (block .Height + 1 , flowID , block , batch )
293+ s .Require ().NoError (err )
294+
295+ receipt := mocks .NewReceipt (block )
296+ err = s .ReceiptIndexer .Store ([]* models.Receipt {receipt }, batch )
290297 s .Require ().NoError (err )
291298
292299 err = batch .Commit (pebble2 .Sync )
@@ -295,10 +302,17 @@ func (s *ReceiptTestSuite) TestStoreReceipt() {
295302
296303 s .Run ("store multiple receipts at same height" , func () {
297304 const height = 5
305+ batch := s .DB .NewBatch ()
306+ flowID := flow.Identifier {0x01 }
307+ block := mocks .NewBlock (height )
308+
309+ err := s .BlocksIndexer .Store (height + 1 , flowID , block , batch )
310+ s .Require ().NoError (err )
311+
298312 receipts := []* models.Receipt {
299- mocks .NewReceipt (height , common . HexToHash ( "0x1" ) ),
300- mocks .NewReceipt (height , common . HexToHash ( "0x2" ) ),
301- mocks .NewReceipt (height , common . HexToHash ( "0x3" ) ),
313+ mocks .NewReceipt (block ),
314+ mocks .NewReceipt (block ),
315+ mocks .NewReceipt (block ),
302316 }
303317 // Log index field holds the index position in the entire block
304318 logIndex := uint (0 )
@@ -309,8 +323,7 @@ func (s *ReceiptTestSuite) TestStoreReceipt() {
309323 }
310324 }
311325
312- batch := s .DB .NewBatch ()
313- err := s .ReceiptIndexer .Store (receipts , batch )
326+ err = s .ReceiptIndexer .Store (receipts , batch )
314327 s .Require ().NoError (err )
315328
316329 err = batch .Commit (pebble2 .Sync )
@@ -325,22 +338,39 @@ func (s *ReceiptTestSuite) TestStoreReceipt() {
325338 })
326339
327340 s .Run ("fail to store multiple receipts with different heights" , func () {
341+ batch := s .DB .NewBatch ()
342+ flowID := flow.Identifier {0x01 }
343+ block1 := mocks .NewBlock (1 )
344+
345+ err := s .BlocksIndexer .Store (block1 .Height + 1 , flowID , block1 , batch )
346+ s .Require ().NoError (err )
347+
348+ block2 := mocks .NewBlock (2 )
349+
350+ err = s .BlocksIndexer .Store (block2 .Height + 1 , flowID , block2 , batch )
351+ s .Require ().NoError (err )
352+
328353 receipts := []* models.Receipt {
329- mocks .NewReceipt (1 , common . HexToHash ( "0x1" ) ),
330- mocks .NewReceipt (2 , common . HexToHash ( "0x2" ) ),
354+ mocks .NewReceipt (block1 ),
355+ mocks .NewReceipt (block2 ),
331356 }
332357
333- batch := s .DB .NewBatch ()
334- err := s .ReceiptIndexer .Store (receipts , batch )
358+ err = s .ReceiptIndexer .Store (receipts , batch )
335359 s .Require ().EqualError (err , "can't store receipts for multiple heights" )
336360 })
337361}
338362
339363func (s * ReceiptTestSuite ) TestGetReceiptByTransactionID () {
340364 s .Run ("existing transaction ID" , func () {
341- receipt := mocks .NewReceipt (2 , common .HexToHash ("0xf2" ))
342365 batch := s .DB .NewBatch ()
343- err := s .ReceiptIndexer .Store ([]* models.Receipt {receipt }, batch )
366+ flowID := flow.Identifier {0x01 }
367+ block := mocks .NewBlock (2 )
368+
369+ err := s .BlocksIndexer .Store (block .Height + 1 , flowID , block , batch )
370+ s .Require ().NoError (err )
371+
372+ receipt := mocks .NewReceipt (block )
373+ err = s .ReceiptIndexer .Store ([]* models.Receipt {receipt }, batch )
344374 s .Require ().NoError (err )
345375
346376 err = batch .Commit (pebble2 .Sync )
@@ -361,9 +391,15 @@ func (s *ReceiptTestSuite) TestGetReceiptByTransactionID() {
361391
362392func (s * ReceiptTestSuite ) TestGetReceiptByBlockHeight () {
363393 s .Run ("existing block height" , func () {
364- receipt := mocks .NewReceipt (3 , common .HexToHash ("0x1" ))
365394 batch := s .DB .NewBatch ()
366- err := s .ReceiptIndexer .Store ([]* models.Receipt {receipt }, batch )
395+ flowID := flow.Identifier {0x01 }
396+ block := mocks .NewBlock (3 )
397+
398+ err := s .BlocksIndexer .Store (block .Height + 1 , flowID , block , batch )
399+ s .Require ().NoError (err )
400+
401+ receipt := mocks .NewReceipt (block )
402+ err = s .ReceiptIndexer .Store ([]* models.Receipt {receipt }, batch )
367403 s .Require ().NoError (err )
368404
369405 err = batch .Commit (pebble2 .Sync )
@@ -372,7 +408,13 @@ func (s *ReceiptTestSuite) TestGetReceiptByBlockHeight() {
372408 batch = s .DB .NewBatch ()
373409
374410 // add one more receipt that shouldn't be retrieved
375- r := mocks .NewReceipt (4 , common .HexToHash ("0x2" ))
411+ flowID = flow.Identifier {0x04 }
412+ block4 := mocks .NewBlock (4 )
413+
414+ err = s .BlocksIndexer .Store (block4 .Height + 1 , flowID , block4 , batch )
415+ s .Require ().NoError (err )
416+
417+ r := mocks .NewReceipt (block4 )
376418 s .Require ().NoError (s .ReceiptIndexer .Store ([]* models.Receipt {r }, batch ))
377419
378420 err = batch .Commit (pebble2 .Sync )
@@ -399,11 +441,18 @@ func (s *ReceiptTestSuite) TestBloomsForBlockRange() {
399441 testHeights := make ([]uint64 , 0 )
400442
401443 for i := start ; i < end ; i ++ {
402- r := mocks .NewReceipt (i , common .HexToHash (fmt .Sprintf ("0xf1%d" , i )))
444+ batch := s .DB .NewBatch ()
445+ flowID := flow.Identifier {0x0i }
446+ block := mocks .NewBlock (i )
447+
448+ err := s .BlocksIndexer .Store (block .Height + 1 , flowID , block , batch )
449+ s .Require ().NoError (err )
450+
451+ r := mocks .NewReceipt (block )
403452 testBlooms = append (testBlooms , & r .Bloom )
404453 testHeights = append (testHeights , i )
405- batch : = s .DB .NewBatch ()
406- err : = s .ReceiptIndexer .Store ([]* models.Receipt {r }, batch )
454+ batch = s .DB .NewBatch ()
455+ err = s .ReceiptIndexer .Store ([]* models.Receipt {r }, batch )
407456 s .Require ().NoError (err )
408457
409458 err = batch .Commit (pebble2 .Sync )
@@ -441,13 +490,20 @@ func (s *ReceiptTestSuite) TestBloomsForBlockRange() {
441490 testHeights := make ([]uint64 , 0 )
442491
443492 for i := start ; i < end ; i ++ {
444- r1 := mocks .NewReceipt (i , common .HexToHash (fmt .Sprintf ("0x%d" , i )))
445- r2 := mocks .NewReceipt (i , common .HexToHash (fmt .Sprintf ("0x%d" , i )))
493+ batch := s .DB .NewBatch ()
494+ flowID := flow.Identifier {0x0i }
495+ block := mocks .NewBlock (i )
496+
497+ err := s .BlocksIndexer .Store (block .Height + 1 , flowID , block , batch )
498+ s .Require ().NoError (err )
499+
500+ r1 := mocks .NewReceipt (block )
501+ r2 := mocks .NewReceipt (block )
446502 receipts := []* models.Receipt {r1 , r2 }
447503
448- batch : = s .DB .NewBatch ()
504+ batch = s .DB .NewBatch ()
449505 s .Require ().NoError (s .ReceiptIndexer .Store (receipts , batch ))
450- err : = batch .Commit (pebble2 .Sync )
506+ err = batch .Commit (pebble2 .Sync )
451507 s .Require ().NoError (err )
452508
453509 testBlooms = append (testBlooms , & r1 .Bloom , & r2 .Bloom )
@@ -494,13 +550,20 @@ func (s *ReceiptTestSuite) TestBloomsForBlockRange() {
494550
495551 var expectedBloom * types.Bloom
496552 for i := start ; i < end ; i ++ {
497- r1 := mocks .NewReceipt (i , common .HexToHash (fmt .Sprintf ("0x%d" , i )))
553+ batch := s .DB .NewBatch ()
554+ flowID := flow.Identifier {0x0i }
555+ block := mocks .NewBlock (i )
556+
557+ err := s .BlocksIndexer .Store (block .Height + 1 , flowID , block , batch )
558+ s .Require ().NoError (err )
559+
560+ r1 := mocks .NewReceipt (block )
498561 receipts := []* models.Receipt {r1 }
499562
500- batch : = s .DB .NewBatch ()
563+ batch = s .DB .NewBatch ()
501564 s .Require ().NoError (s .ReceiptIndexer .Store (receipts , batch ))
502565
503- err : = batch .Commit (pebble2 .Sync )
566+ err = batch .Commit (pebble2 .Sync )
504567 s .Require ().NoError (err )
505568
506569 if i == specific {
0 commit comments