github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/badger/events_test.go (about)

     1  package badger_test
     2  
     3  import (
     4  	"math/rand"
     5  	"testing"
     6  
     7  	"github.com/dgraph-io/badger/v2"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/onflow/flow-go/fvm/systemcontracts"
    11  	"github.com/onflow/flow-go/model/flow"
    12  	"github.com/onflow/flow-go/module/metrics"
    13  	badgerstorage "github.com/onflow/flow-go/storage/badger"
    14  	"github.com/onflow/flow-go/utils/unittest"
    15  )
    16  
    17  func TestEventStoreRetrieve(t *testing.T) {
    18  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    19  		metrics := metrics.NewNoopCollector()
    20  		store := badgerstorage.NewEvents(metrics, db)
    21  
    22  		blockID := unittest.IdentifierFixture()
    23  		tx1ID := unittest.IdentifierFixture()
    24  		tx2ID := unittest.IdentifierFixture()
    25  		evt1_1 := unittest.EventFixture(flow.EventAccountCreated, 0, 0, tx1ID, 0)
    26  		evt1_2 := unittest.EventFixture(flow.EventAccountCreated, 1, 1, tx2ID, 0)
    27  
    28  		evt2_1 := unittest.EventFixture(flow.EventAccountUpdated, 2, 2, tx2ID, 0)
    29  
    30  		expected := []flow.EventsList{
    31  			{evt1_1, evt1_2},
    32  			{evt2_1},
    33  		}
    34  
    35  		batch := badgerstorage.NewBatch(db)
    36  		// store event
    37  		err := store.BatchStore(blockID, expected, batch)
    38  		require.NoError(t, err)
    39  
    40  		err = batch.Flush()
    41  		require.NoError(t, err)
    42  
    43  		// retrieve by blockID
    44  		actual, err := store.ByBlockID(blockID)
    45  		require.NoError(t, err)
    46  		require.Len(t, actual, 3)
    47  		require.Contains(t, actual, evt1_1)
    48  		require.Contains(t, actual, evt1_2)
    49  		require.Contains(t, actual, evt2_1)
    50  
    51  		// retrieve by blockID and event type
    52  		actual, err = store.ByBlockIDEventType(blockID, flow.EventAccountCreated)
    53  		require.NoError(t, err)
    54  		require.Len(t, actual, 2)
    55  		require.Contains(t, actual, evt1_1)
    56  		require.Contains(t, actual, evt1_2)
    57  
    58  		actual, err = store.ByBlockIDEventType(blockID, flow.EventAccountUpdated)
    59  		require.NoError(t, err)
    60  		require.Len(t, actual, 1)
    61  		require.Contains(t, actual, evt2_1)
    62  
    63  		events := systemcontracts.ServiceEventsForChain(flow.Emulator)
    64  
    65  		actual, err = store.ByBlockIDEventType(blockID, events.EpochSetup.EventType())
    66  		require.NoError(t, err)
    67  		require.Len(t, actual, 0)
    68  
    69  		// retrieve by blockID and transaction id
    70  		actual, err = store.ByBlockIDTransactionID(blockID, tx1ID)
    71  		require.NoError(t, err)
    72  		require.Len(t, actual, 1)
    73  		require.Contains(t, actual, evt1_1)
    74  
    75  		// retrieve by blockID and transaction index
    76  		actual, err = store.ByBlockIDTransactionIndex(blockID, 1)
    77  		require.NoError(t, err)
    78  		require.Len(t, actual, 1)
    79  		require.Contains(t, actual, evt1_2)
    80  
    81  		// test loading from database
    82  
    83  		newStore := badgerstorage.NewEvents(metrics, db)
    84  		actual, err = newStore.ByBlockID(blockID)
    85  		require.NoError(t, err)
    86  		require.Len(t, actual, 3)
    87  		require.Contains(t, actual, evt1_1)
    88  		require.Contains(t, actual, evt1_2)
    89  		require.Contains(t, actual, evt2_1)
    90  	})
    91  }
    92  
    93  func TestEventRetrieveWithoutStore(t *testing.T) {
    94  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    95  		metrics := metrics.NewNoopCollector()
    96  		store := badgerstorage.NewEvents(metrics, db)
    97  
    98  		blockID := unittest.IdentifierFixture()
    99  		txID := unittest.IdentifierFixture()
   100  		txIndex := rand.Uint32()
   101  
   102  		// retrieve by blockID
   103  		events, err := store.ByBlockID(blockID)
   104  		require.NoError(t, err)
   105  		require.True(t, len(events) == 0)
   106  
   107  		// retrieve by blockID and event type
   108  		events, err = store.ByBlockIDEventType(blockID, flow.EventAccountCreated)
   109  		require.NoError(t, err)
   110  		require.True(t, len(events) == 0)
   111  
   112  		// retrieve by blockID and transaction id
   113  		events, err = store.ByBlockIDTransactionID(blockID, txID)
   114  		require.NoError(t, err)
   115  		require.True(t, len(events) == 0)
   116  
   117  		// retrieve by blockID and transaction id
   118  		events, err = store.ByBlockIDTransactionIndex(blockID, txIndex)
   119  		require.NoError(t, err)
   120  		require.True(t, len(events) == 0)
   121  
   122  	})
   123  }