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