github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/ledger/partial/ledger_test.go (about)

     1  package partial_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/rs/zerolog"
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	executionState "github.com/onflow/flow-go/engine/execution/state"
    11  	"github.com/onflow/flow-go/ledger"
    12  	"github.com/onflow/flow-go/ledger/common/convert"
    13  	"github.com/onflow/flow-go/ledger/common/testutils"
    14  	"github.com/onflow/flow-go/ledger/complete"
    15  	"github.com/onflow/flow-go/ledger/complete/wal/fixtures"
    16  	"github.com/onflow/flow-go/ledger/partial"
    17  	"github.com/onflow/flow-go/model/flow"
    18  	"github.com/onflow/flow-go/module/metrics"
    19  	"github.com/onflow/flow-go/utils/unittest"
    20  )
    21  
    22  func TestFunctionalityWithCompleteTrie(t *testing.T) {
    23  
    24  	w := &fixtures.NoopWAL{}
    25  
    26  	l, err := complete.NewLedger(w, 100, &metrics.NoopCollector{}, zerolog.Logger{}, complete.DefaultPathFinderVersion)
    27  	require.NoError(t, err)
    28  
    29  	compactor := fixtures.NewNoopCompactor(l)
    30  	<-compactor.Ready()
    31  
    32  	defer func() {
    33  		<-l.Done()
    34  		<-compactor.Done()
    35  	}()
    36  
    37  	// create empty update
    38  	state := l.InitialState()
    39  	keys := testutils.RandomUniqueKeys(3, 2, 2, 4)
    40  	values := testutils.RandomValues(3, 1, 32)
    41  	update, err := ledger.NewUpdate(state, keys[0:2], values[0:2])
    42  	require.NoError(t, err)
    43  
    44  	newState, _, err := l.Set(update)
    45  	require.NoError(t, err)
    46  
    47  	query, err := ledger.NewQuery(newState, keys[0:2])
    48  	require.NoError(t, err)
    49  	proof, err := l.Prove(query)
    50  	require.NoError(t, err)
    51  
    52  	pled, err := partial.NewLedger(proof, newState, partial.DefaultPathFinderVersion)
    53  	assert.NoError(t, err)
    54  	assert.Equal(t, pled.InitialState(), newState)
    55  
    56  	// test batch querying existent keys
    57  	query, err = ledger.NewQuery(newState, keys[0:2])
    58  	require.NoError(t, err)
    59  
    60  	retValues, err := pled.Get(query)
    61  	require.NoError(t, err)
    62  	require.Equal(t, 2, len(retValues))
    63  	for i := 0; i < len(retValues); i++ {
    64  		require.Equal(t, values[i], retValues[i])
    65  	}
    66  
    67  	// test querying single existent key
    68  	querySingleValue, err := ledger.NewQuerySingleValue(newState, keys[0])
    69  	require.NoError(t, err)
    70  
    71  	retValue, err := pled.GetSingleValue(querySingleValue)
    72  	require.NoError(t, err)
    73  	require.Equal(t, values[0], retValue)
    74  
    75  	// test batch getting missing keys
    76  	query, err = ledger.NewQuery(newState, keys[1:3])
    77  	require.NoError(t, err)
    78  
    79  	retValues, err = pled.Get(query)
    80  	require.Error(t, err)
    81  	require.Nil(t, retValues)
    82  
    83  	e, ok := err.(*ledger.ErrMissingKeys)
    84  	require.True(t, ok)
    85  	assert.Equal(t, len(e.Keys), 1)
    86  	require.True(t, e.Keys[0].Equals(&keys[2]))
    87  
    88  	// test querying single non-existent key
    89  	querySingleValue, err = ledger.NewQuerySingleValue(newState, keys[2])
    90  	require.NoError(t, err)
    91  
    92  	retValue, err = pled.GetSingleValue(querySingleValue)
    93  	require.Error(t, err)
    94  	require.Nil(t, retValue)
    95  
    96  	e, ok = err.(*ledger.ErrMissingKeys)
    97  	require.True(t, ok)
    98  	assert.Equal(t, len(e.Keys), 1)
    99  	require.True(t, e.Keys[0].Equals(&keys[2]))
   100  
   101  	// test missing keys (set)
   102  	update, err = ledger.NewUpdate(state, keys[1:3], values[1:3])
   103  	require.NoError(t, err)
   104  
   105  	_, _, err = pled.Set(update)
   106  	require.Error(t, err)
   107  
   108  	e, ok = err.(*ledger.ErrMissingKeys)
   109  	require.True(t, ok)
   110  	assert.Equal(t, len(e.Keys), 1)
   111  	require.True(t, e.Keys[0].Equals(&keys[2]))
   112  
   113  }
   114  
   115  func TestProofsForEmptyRegisters(t *testing.T) {
   116  
   117  	l, err := complete.NewLedger(&fixtures.NoopWAL{}, 100, &metrics.NoopCollector{}, zerolog.Logger{}, complete.DefaultPathFinderVersion)
   118  	require.NoError(t, err)
   119  
   120  	// create empty update
   121  	emptyState := l.InitialState()
   122  
   123  	// No updates.
   124  	keys, values := executionState.RegisterEntriesToKeysValues(nil)
   125  
   126  	updated, err := ledger.NewUpdate(emptyState, keys, values)
   127  	require.NoError(t, err)
   128  
   129  	// Read one register during execution.
   130  	registerID := flow.NewRegisterID(unittest.RandomAddressFixture(), "nk")
   131  	allKeys := []ledger.Key{
   132  		convert.RegisterIDToLedgerKey(registerID),
   133  	}
   134  
   135  	newState := updated.State()
   136  
   137  	proofQuery, err := ledger.NewQuery(newState, allKeys)
   138  	require.NoError(t, err)
   139  
   140  	proof, err := l.Prove(proofQuery)
   141  	require.NoError(t, err)
   142  
   143  	pled, err := partial.NewLedger(proof, newState, partial.DefaultPathFinderVersion)
   144  	assert.NoError(t, err)
   145  	assert.Equal(t, pled.InitialState(), emptyState)
   146  
   147  	query, err := ledger.NewQuery(newState, []ledger.Key{convert.RegisterIDToLedgerKey(registerID)})
   148  	require.NoError(t, err)
   149  
   150  	results, err := pled.Get(query)
   151  	require.NoError(t, err)
   152  	require.Len(t, results, 1)
   153  
   154  	require.Empty(t, results[0])
   155  }
   156  
   157  func TestEmptyLedger(t *testing.T) {
   158  	l, err := complete.NewLedger(&fixtures.NoopWAL{}, 100, &metrics.NoopCollector{}, zerolog.Logger{}, complete.DefaultPathFinderVersion)
   159  	require.NoError(t, err)
   160  
   161  	u, err := ledger.NewUpdate(
   162  		ledger.State(unittest.StateCommitmentFixture()),
   163  		[]ledger.Key{},
   164  		[]ledger.Value{},
   165  	)
   166  	require.NoError(t, err)
   167  	newState, trieUpdate, err := l.Set(u)
   168  	require.NoError(t, err)
   169  	require.True(t, trieUpdate.IsEmpty())
   170  	require.Equal(t, u.State(), newState)
   171  }