github.com/koko1123/flow-go-1@v0.29.6/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/v3"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/koko1123/flow-go-1/fvm/systemcontracts"
    11  	"github.com/koko1123/flow-go-1/model/flow"
    12  	"github.com/koko1123/flow-go-1/module/metrics"
    13  	badgerstorage "github.com/koko1123/flow-go-1/storage/badger"
    14  	"github.com/koko1123/flow-go-1/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, err := systemcontracts.ServiceEventsForChain(flow.Emulator)
    64  		require.NoError(t, err)
    65  
    66  		actual, err = store.ByBlockIDEventType(blockID, events.EpochSetup.EventType())
    67  		require.NoError(t, err)
    68  		require.Len(t, actual, 0)
    69  
    70  		// retrieve by blockID and transaction id
    71  		actual, err = store.ByBlockIDTransactionID(blockID, tx1ID)
    72  		require.NoError(t, err)
    73  		require.Len(t, actual, 1)
    74  		require.Contains(t, actual, evt1_1)
    75  
    76  		// retrieve by blockID and transaction index
    77  		actual, err = store.ByBlockIDTransactionIndex(blockID, 1)
    78  		require.NoError(t, err)
    79  		require.Len(t, actual, 1)
    80  		require.Contains(t, actual, evt1_2)
    81  
    82  		// test loading from database
    83  
    84  		newStore := badgerstorage.NewEvents(metrics, db)
    85  		actual, err = newStore.ByBlockID(blockID)
    86  		require.NoError(t, err)
    87  		require.Len(t, actual, 3)
    88  		require.Contains(t, actual, evt1_1)
    89  		require.Contains(t, actual, evt1_2)
    90  		require.Contains(t, actual, evt2_1)
    91  	})
    92  }
    93  
    94  func TestEventRetrieveWithoutStore(t *testing.T) {
    95  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    96  		metrics := metrics.NewNoopCollector()
    97  		store := badgerstorage.NewEvents(metrics, db)
    98  
    99  		blockID := unittest.IdentifierFixture()
   100  		txID := unittest.IdentifierFixture()
   101  		txIndex := rand.Uint32()
   102  
   103  		// retrieve by blockID
   104  		events, err := store.ByBlockID(blockID)
   105  		require.NoError(t, err)
   106  		require.True(t, len(events) == 0)
   107  
   108  		// retrieve by blockID and event type
   109  		events, err = store.ByBlockIDEventType(blockID, flow.EventAccountCreated)
   110  		require.NoError(t, err)
   111  		require.True(t, len(events) == 0)
   112  
   113  		// retrieve by blockID and transaction id
   114  		events, err = store.ByBlockIDTransactionID(blockID, txID)
   115  		require.NoError(t, err)
   116  		require.True(t, len(events) == 0)
   117  
   118  		// retrieve by blockID and transaction id
   119  		events, err = store.ByBlockIDTransactionIndex(blockID, txIndex)
   120  		require.NoError(t, err)
   121  		require.True(t, len(events) == 0)
   122  
   123  	})
   124  }