github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/execution/storehouse/block_end_snapshot_test.go (about)

     1  package storehouse_test
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  	"go.uber.org/atomic"
    10  
    11  	executionMock "github.com/onflow/flow-go/engine/execution/mock"
    12  	"github.com/onflow/flow-go/engine/execution/storehouse"
    13  	"github.com/onflow/flow-go/model/flow"
    14  	"github.com/onflow/flow-go/storage"
    15  	"github.com/onflow/flow-go/utils/unittest"
    16  )
    17  
    18  func TestBlockEndSnapshot(t *testing.T) {
    19  	t.Run("Get register", func(t *testing.T) {
    20  		header := unittest.BlockHeaderFixture()
    21  
    22  		// create mock for storage
    23  		store := executionMock.NewRegisterStore(t)
    24  		reg := unittest.MakeOwnerReg("key", "value")
    25  		store.On("GetRegister", header.Height, header.ID(), reg.Key).Return(reg.Value, nil).Once()
    26  		snapshot := storehouse.NewBlockEndStateSnapshot(store, header.ID(), header.Height)
    27  
    28  		// test get from storage
    29  		value, err := snapshot.Get(reg.Key)
    30  		require.NoError(t, err)
    31  		require.Equal(t, reg.Value, value)
    32  
    33  		// test get from cache
    34  		value, err = snapshot.Get(reg.Key)
    35  		require.NoError(t, err)
    36  		require.Equal(t, reg.Value, value)
    37  
    38  		// test get non existing register
    39  		unknownReg := unittest.MakeOwnerReg("unknown", "unknown")
    40  		store.On("GetRegister", header.Height, header.ID(), unknownReg.Key).
    41  			Return(nil, fmt.Errorf("fail: %w", storage.ErrNotFound)).Once()
    42  
    43  		value, err = snapshot.Get(unknownReg.Key)
    44  		require.NoError(t, err)
    45  		require.Nil(t, value)
    46  
    47  		// test get non existing register from cache
    48  		_, err = snapshot.Get(unknownReg.Key)
    49  		require.NoError(t, err)
    50  		require.Nil(t, value)
    51  
    52  		// test getting storage.ErrHeightNotIndexed error
    53  		heightNotIndexed := unittest.MakeOwnerReg("height not index", "height not index")
    54  		store.On("GetRegister", header.Height, header.ID(), heightNotIndexed.Key).
    55  			Return(nil, fmt.Errorf("fail: %w", storage.ErrHeightNotIndexed)).
    56  			Twice() // to verify the result is not cached
    57  
    58  		// verify getting the correct error
    59  		_, err = snapshot.Get(heightNotIndexed.Key)
    60  		require.Error(t, err)
    61  		require.True(t, errors.Is(err, storage.ErrHeightNotIndexed))
    62  
    63  		// verify result is not cached
    64  		_, err = snapshot.Get(heightNotIndexed.Key)
    65  		require.Error(t, err)
    66  		require.True(t, errors.Is(err, storage.ErrHeightNotIndexed))
    67  
    68  		// test getting storage.ErrNotExecuted error
    69  		heightNotExecuted := unittest.MakeOwnerReg("height not executed", "height not executed")
    70  		counter := atomic.NewInt32(0)
    71  		store.
    72  			On("GetRegister", header.Height, header.ID(), heightNotExecuted.Key).
    73  			Return(func(uint64, flow.Identifier, flow.RegisterID) (flow.RegisterValue, error) {
    74  				counter.Inc()
    75  				// the first call should return error
    76  				if counter.Load() == 1 {
    77  					return nil, fmt.Errorf("fail: %w", storehouse.ErrNotExecuted)
    78  				}
    79  				// the second call, it returns value
    80  				return heightNotExecuted.Value, nil
    81  			}).
    82  			Times(2)
    83  
    84  		// first time should return error
    85  		_, err = snapshot.Get(heightNotExecuted.Key)
    86  		require.Error(t, err)
    87  		require.True(t, errors.Is(err, storehouse.ErrNotExecuted))
    88  
    89  		// second time should return value
    90  		value, err = snapshot.Get(heightNotExecuted.Key)
    91  		require.NoError(t, err)
    92  		require.Equal(t, heightNotExecuted.Value, value)
    93  
    94  		// third time should be cached
    95  		value, err = snapshot.Get(heightNotExecuted.Key)
    96  		require.NoError(t, err)
    97  		require.Equal(t, heightNotExecuted.Value, value)
    98  
    99  		store.AssertExpectations(t)
   100  	})
   101  
   102  }