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

     1  package operation
     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/model/flow"
    10  	"github.com/onflow/flow-go/storage"
    11  	"github.com/onflow/flow-go/utils/unittest"
    12  )
    13  
    14  func TestResults_IndexByServiceEvents(t *testing.T) {
    15  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    16  		height1 := uint64(21)
    17  		height2 := uint64(37)
    18  		height3 := uint64(55)
    19  		vb1 := flow.SealedVersionBeacon{
    20  			VersionBeacon: unittest.VersionBeaconFixture(
    21  				unittest.WithBoundaries(
    22  					flow.VersionBoundary{
    23  						Version:     "1.0.0",
    24  						BlockHeight: height1 + 5,
    25  					},
    26  				),
    27  			),
    28  			SealHeight: height1,
    29  		}
    30  		vb2 := flow.SealedVersionBeacon{
    31  			VersionBeacon: unittest.VersionBeaconFixture(
    32  				unittest.WithBoundaries(
    33  					flow.VersionBoundary{
    34  						Version:     "1.1.0",
    35  						BlockHeight: height2 + 5,
    36  					},
    37  				),
    38  			),
    39  			SealHeight: height2,
    40  		}
    41  		vb3 := flow.SealedVersionBeacon{
    42  			VersionBeacon: unittest.VersionBeaconFixture(
    43  				unittest.WithBoundaries(
    44  					flow.VersionBoundary{
    45  						Version:     "2.0.0",
    46  						BlockHeight: height3 + 5,
    47  					},
    48  				),
    49  			),
    50  			SealHeight: height3,
    51  		}
    52  
    53  		// indexing 3 version beacons at different heights
    54  		err := db.Update(IndexVersionBeaconByHeight(&vb1))
    55  		require.NoError(t, err)
    56  
    57  		err = db.Update(IndexVersionBeaconByHeight(&vb2))
    58  		require.NoError(t, err)
    59  
    60  		err = db.Update(IndexVersionBeaconByHeight(&vb3))
    61  		require.NoError(t, err)
    62  
    63  		// index version beacon 2 again to make sure we tolerate duplicates
    64  		// it is possible for two or more events of the same type to be from the same height
    65  		err = db.Update(IndexVersionBeaconByHeight(&vb2))
    66  		require.NoError(t, err)
    67  
    68  		t.Run("retrieve exact height match", func(t *testing.T) {
    69  			var actualVB flow.SealedVersionBeacon
    70  			err := db.View(LookupLastVersionBeaconByHeight(height1, &actualVB))
    71  			require.NoError(t, err)
    72  			require.Equal(t, vb1, actualVB)
    73  
    74  			err = db.View(LookupLastVersionBeaconByHeight(height2, &actualVB))
    75  			require.NoError(t, err)
    76  			require.Equal(t, vb2, actualVB)
    77  
    78  			err = db.View(LookupLastVersionBeaconByHeight(height3, &actualVB))
    79  			require.NoError(t, err)
    80  			require.Equal(t, vb3, actualVB)
    81  		})
    82  
    83  		t.Run("finds highest but not higher than given", func(t *testing.T) {
    84  			var actualVB flow.SealedVersionBeacon
    85  
    86  			err := db.View(LookupLastVersionBeaconByHeight(height3-1, &actualVB))
    87  			require.NoError(t, err)
    88  			require.Equal(t, vb2, actualVB)
    89  		})
    90  
    91  		t.Run("finds highest", func(t *testing.T) {
    92  			var actualVB flow.SealedVersionBeacon
    93  
    94  			err := db.View(LookupLastVersionBeaconByHeight(height3+1, &actualVB))
    95  			require.NoError(t, err)
    96  			require.Equal(t, vb3, actualVB)
    97  		})
    98  
    99  		t.Run("height below lowest entry returns nothing", func(t *testing.T) {
   100  			var actualVB flow.SealedVersionBeacon
   101  
   102  			err := db.View(LookupLastVersionBeaconByHeight(height1-1, &actualVB))
   103  			require.ErrorIs(t, err, storage.ErrNotFound)
   104  		})
   105  	})
   106  }