github.com/koko1123/flow-go-1@v0.29.6/storage/badger/receipts_test.go (about)

     1  package badger_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/model/flow"
    10  	"github.com/koko1123/flow-go-1/module/metrics"
    11  	bstorage "github.com/koko1123/flow-go-1/storage/badger"
    12  	"github.com/koko1123/flow-go-1/utils/unittest"
    13  )
    14  
    15  func TestExecutionReceiptsStorage(t *testing.T) {
    16  	withStore := func(t *testing.T, f func(store *bstorage.ExecutionReceipts)) {
    17  		unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    18  			metrics := metrics.NewNoopCollector()
    19  			results := bstorage.NewExecutionResults(metrics, db)
    20  			store := bstorage.NewExecutionReceipts(metrics, db, results, bstorage.DefaultCacheSize)
    21  			f(store)
    22  		})
    23  	}
    24  
    25  	t.Run("get empty", func(t *testing.T) {
    26  		withStore(t, func(store *bstorage.ExecutionReceipts) {
    27  			block := unittest.BlockFixture()
    28  			receipts, err := store.ByBlockID(block.ID())
    29  			require.NoError(t, err)
    30  			require.Equal(t, 0, len(receipts))
    31  		})
    32  	})
    33  
    34  	t.Run("store one get one", func(t *testing.T) {
    35  		withStore(t, func(store *bstorage.ExecutionReceipts) {
    36  			block := unittest.BlockFixture()
    37  			receipt1 := unittest.ReceiptForBlockFixture(&block)
    38  
    39  			err := store.Store(receipt1)
    40  			require.NoError(t, err)
    41  
    42  			actual, err := store.ByID(receipt1.ID())
    43  			require.NoError(t, err)
    44  
    45  			require.Equal(t, receipt1, actual)
    46  
    47  			receipts, err := store.ByBlockID(block.ID())
    48  			require.NoError(t, err)
    49  
    50  			require.Equal(t, flow.ExecutionReceiptList{receipt1}, receipts)
    51  		})
    52  	})
    53  
    54  	t.Run("store two for the same block", func(t *testing.T) {
    55  		withStore(t, func(store *bstorage.ExecutionReceipts) {
    56  			block := unittest.BlockFixture()
    57  
    58  			executor1 := unittest.IdentifierFixture()
    59  			executor2 := unittest.IdentifierFixture()
    60  
    61  			receipt1 := unittest.ReceiptForBlockExecutorFixture(&block, executor1)
    62  			receipt2 := unittest.ReceiptForBlockExecutorFixture(&block, executor2)
    63  
    64  			err := store.Store(receipt1)
    65  			require.NoError(t, err)
    66  
    67  			err = store.Store(receipt2)
    68  			require.NoError(t, err)
    69  
    70  			receipts, err := store.ByBlockID(block.ID())
    71  			require.NoError(t, err)
    72  
    73  			require.ElementsMatch(t, []*flow.ExecutionReceipt{receipt1, receipt2}, receipts)
    74  		})
    75  	})
    76  
    77  	t.Run("store two for different blocks", func(t *testing.T) {
    78  		withStore(t, func(store *bstorage.ExecutionReceipts) {
    79  			block1 := unittest.BlockFixture()
    80  			block2 := unittest.BlockFixture()
    81  
    82  			executor1 := unittest.IdentifierFixture()
    83  			executor2 := unittest.IdentifierFixture()
    84  
    85  			receipt1 := unittest.ReceiptForBlockExecutorFixture(&block1, executor1)
    86  			receipt2 := unittest.ReceiptForBlockExecutorFixture(&block2, executor2)
    87  
    88  			err := store.Store(receipt1)
    89  			require.NoError(t, err)
    90  
    91  			err = store.Store(receipt2)
    92  			require.NoError(t, err)
    93  
    94  			receipts1, err := store.ByBlockID(block1.ID())
    95  			require.NoError(t, err)
    96  
    97  			receipts2, err := store.ByBlockID(block2.ID())
    98  			require.NoError(t, err)
    99  
   100  			require.ElementsMatch(t, []*flow.ExecutionReceipt{receipt1}, receipts1)
   101  			require.ElementsMatch(t, []*flow.ExecutionReceipt{receipt2}, receipts2)
   102  		})
   103  	})
   104  
   105  	t.Run("indexing duplicated receipts should be ok", func(t *testing.T) {
   106  		withStore(t, func(store *bstorage.ExecutionReceipts) {
   107  			block1 := unittest.BlockFixture()
   108  
   109  			executor1 := unittest.IdentifierFixture()
   110  			receipt1 := unittest.ReceiptForBlockExecutorFixture(&block1, executor1)
   111  
   112  			err := store.Store(receipt1)
   113  			require.NoError(t, err)
   114  
   115  			err = store.Store(receipt1)
   116  			require.NoError(t, err)
   117  
   118  			receipts, err := store.ByBlockID(block1.ID())
   119  			require.NoError(t, err)
   120  
   121  			require.ElementsMatch(t, []*flow.ExecutionReceipt{receipt1}, receipts)
   122  		})
   123  	})
   124  
   125  	t.Run("indexing receipt from the same executor for same block should succeed", func(t *testing.T) {
   126  		withStore(t, func(store *bstorage.ExecutionReceipts) {
   127  			block1 := unittest.BlockFixture()
   128  
   129  			executor1 := unittest.IdentifierFixture()
   130  
   131  			receipt1 := unittest.ReceiptForBlockExecutorFixture(&block1, executor1)
   132  			receipt2 := unittest.ReceiptForBlockExecutorFixture(&block1, executor1)
   133  
   134  			err := store.Store(receipt1)
   135  			require.NoError(t, err)
   136  
   137  			err = store.Store(receipt2)
   138  			require.NoError(t, err)
   139  
   140  			receipts, err := store.ByBlockID(block1.ID())
   141  			require.NoError(t, err)
   142  
   143  			require.ElementsMatch(t, []*flow.ExecutionReceipt{receipt1, receipt2}, receipts)
   144  		})
   145  	})
   146  }