diff --git a/ledger/complete/mtrie/forest.go b/ledger/complete/mtrie/forest.go index c8d4e52ef5b..43a5809f199 100644 --- a/ledger/complete/mtrie/forest.go +++ b/ledger/complete/mtrie/forest.go @@ -190,10 +190,11 @@ func (f *Forest) Read(r *ledger.TrieRead) ([]ledger.Value, error) { return orderedValues, nil } -// Update updates the Values for the registers, adds updated tries to forest, -// and returns rootHash and error (if any). +// Update creates a new trie by updating Values for registers in the parent trie, +// adds new trie to forest, and returns rootHash and error (if any). // In case there are multiple updates to the same register, Update will persist // the latest written value. +// Note: Update adds new trie to forest, unlike NewTrie(). func (f *Forest) Update(u *ledger.TrieUpdate) (ledger.RootHash, error) { t, err := f.NewTrie(u) if err != nil { @@ -208,9 +209,11 @@ func (f *Forest) Update(u *ledger.TrieUpdate) (ledger.RootHash, error) { return t.RootHash(), nil } -// NewTrie updates the Values for the registers and returns updated trie and error (if any). -// In case there are multiple updates to the same register, Update will persist the latest -// written value. +// NewTrie creates a new trie by updating Values for registers in the parent trie, +// and returns new trie and error (if any). +// In case there are multiple updates to the same register, NewTrie will persist +// the latest written value. +// Note: NewTrie doesn't add new trie to forest, unlike Update(). func (f *Forest) NewTrie(u *ledger.TrieUpdate) (*trie.MTrie, error) { parentTrie, err := f.GetTrie(u.RootHash) diff --git a/ledger/complete/mtrie/forest_test.go b/ledger/complete/mtrie/forest_test.go index bf7c9b6eda4..78d36d3d48c 100644 --- a/ledger/complete/mtrie/forest_test.go +++ b/ledger/complete/mtrie/forest_test.go @@ -1041,3 +1041,34 @@ func TestValueSizesWithDuplicatedKeys(t *testing.T) { require.Equal(t, expectedValueSizes[i], retValueSizes[i]) } } + +func TestNow(t *testing.T) { + + metricsCollector := &metrics.NoopCollector{} + forest, err := NewForest(5, metricsCollector, nil) + require.NoError(t, err) + rootHash := forest.GetEmptyRootHash() + + p1 := pathByUint8s([]uint8{uint8(53), uint8(74)}) + v1 := payloadBySlices([]byte{'A'}, []byte{'A'}) + + paths := []ledger.Path{p1} + payloads := []*ledger.Payload{v1} + update := &ledger.TrieUpdate{RootHash: rootHash, Paths: paths, Payloads: payloads} + updatedRoot, err := forest.Update(update) + size := forest.Size() + require.NoError(t, err) + require.Equal(t, 2, size) + + read := &ledger.TrieRead{RootHash: updatedRoot, Paths: paths} + retValues, err := forest.Read(read) + require.NoError(t, err) + require.Equal(t, retValues[0], payloads[0].Value) + + update = &ledger.TrieUpdate{RootHash: updatedRoot, Paths: nil, Payloads: nil} + updatedRoot2, err := forest.Update(update) + size = forest.Size() + require.NoError(t, err) + require.Equal(t, updatedRoot, updatedRoot2) + require.Equal(t, 2, size) +}