Skip to content

Commit

Permalink
Add comment
Browse files Browse the repository at this point in the history
  • Loading branch information
fxamacker committed Jul 27, 2022
1 parent c5455bd commit 240520d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
13 changes: 8 additions & 5 deletions ledger/complete/mtrie/forest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions ledger/complete/mtrie/forest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 240520d

Please sign in to comment.