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

     1  package procedure
     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/storage/badger/operation"
    11  	"github.com/koko1123/flow-go-1/utils/unittest"
    12  )
    13  
    14  func TestInsertExecuted(t *testing.T) {
    15  	chain, _, _ := unittest.ChainFixture(6)
    16  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    17  		t.Run("setup and bootstrap", func(t *testing.T) {
    18  			for _, block := range chain {
    19  				require.NoError(t, db.Update(operation.InsertHeader(block.Header.ID(), block.Header)))
    20  			}
    21  
    22  			root := chain[0].Header
    23  			require.NoError(t,
    24  				db.Update(operation.InsertExecutedBlock(root.ID())),
    25  			)
    26  
    27  			var height uint64
    28  			var blockID flow.Identifier
    29  			require.NoError(t,
    30  				db.View(GetHighestExecutedBlock(&height, &blockID)),
    31  			)
    32  
    33  			require.Equal(t, root.ID(), blockID)
    34  			require.Equal(t, root.Height, height)
    35  		})
    36  
    37  		t.Run("insert and get", func(t *testing.T) {
    38  			header1 := chain[1].Header
    39  			require.NoError(t,
    40  				db.Update(UpdateHighestExecutedBlockIfHigher(header1)),
    41  			)
    42  
    43  			var height uint64
    44  			var blockID flow.Identifier
    45  			require.NoError(t,
    46  				db.View(GetHighestExecutedBlock(&height, &blockID)),
    47  			)
    48  
    49  			require.Equal(t, header1.ID(), blockID)
    50  			require.Equal(t, header1.Height, height)
    51  		})
    52  
    53  		t.Run("insert more and get highest", func(t *testing.T) {
    54  			header2 := chain[2].Header
    55  			header3 := chain[3].Header
    56  			require.NoError(t,
    57  				db.Update(UpdateHighestExecutedBlockIfHigher(header2)),
    58  			)
    59  			require.NoError(t,
    60  				db.Update(UpdateHighestExecutedBlockIfHigher(header3)),
    61  			)
    62  			var height uint64
    63  			var blockID flow.Identifier
    64  			require.NoError(t,
    65  				db.View(GetHighestExecutedBlock(&height, &blockID)),
    66  			)
    67  
    68  			require.Equal(t, header3.ID(), blockID)
    69  			require.Equal(t, header3.Height, height)
    70  		})
    71  
    72  		t.Run("insert lower height later and get highest", func(t *testing.T) {
    73  			header5 := chain[5].Header
    74  			header4 := chain[4].Header
    75  			require.NoError(t,
    76  				db.Update(UpdateHighestExecutedBlockIfHigher(header5)),
    77  			)
    78  			require.NoError(t,
    79  				db.Update(UpdateHighestExecutedBlockIfHigher(header4)),
    80  			)
    81  			var height uint64
    82  			var blockID flow.Identifier
    83  			require.NoError(t,
    84  				db.View(GetHighestExecutedBlock(&height, &blockID)),
    85  			)
    86  
    87  			require.Equal(t, header5.ID(), blockID)
    88  			require.Equal(t, header5.Height, height)
    89  		})
    90  	})
    91  }