Skip to content

Commit

Permalink
Merge pull request #5322 from oasisprotocol/kostko/stable/22.2.x/back…
Browse files Browse the repository at this point in the history
…port-5321

[BACKPORT/22.2.x] go/storage/mkvs: Fix commit of nil entries
  • Loading branch information
kostko authored Jul 14, 2023
2 parents 74a6483 + d9db4fe commit 207dcba
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions .changelog/5321.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/storage/mkvs: Fix commit of nil entries
2 changes: 1 addition & 1 deletion go/storage/mkvs/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (t *tree) commitWithHooks(
}

log = append(log, writelog.LogEntry{Key: entry.key, Value: entry.value})
if len(entry.value) == 0 {
if entry.value == nil {
logAnns = append(logAnns, writelog.LogEntryAnnotation{InsertedNode: nil})
} else {
logAnns = append(logAnns, writelog.LogEntryAnnotation{InsertedNode: entry.insertedLeaf})
Expand Down
42 changes: 42 additions & 0 deletions go/storage/mkvs/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,47 @@ func testSize(t *testing.T, ndb db.NodeDB, factory NodeDBFactory) {
require.True(t, newSize > size, "Size should be greater than before")
}

func testEmptyValueWriteLog(t *testing.T, ndb db.NodeDB, factory NodeDBFactory) {
ctx := context.Background()

// Populate the tree.
tree := New(nil, ndb, node.RootTypeState)
err := tree.Insert(ctx, []byte("foo"), []byte("bar"))
require.NoError(t, err, "Insert")
err = tree.Insert(ctx, []byte("bar"), []byte(""))
require.NoError(t, err, "Insert")
writeLog, rootHash, err := tree.Commit(ctx, testNs, 0)
require.NoError(t, err, "Commit")

// Ensure no nil entries returned.
for _, entry := range writeLog {
require.NotNil(t, entry.Value)
}

startRoot := node.Root{
Namespace: testNs,
Version: 0,
Type: node.RootTypeState,
}
startRoot.Hash.Empty()

endRoot := node.Root{
Namespace: testNs,
Hash: rootHash,
Version: 0,
Type: node.RootTypeState,
}

// Fetch write log from database.
wli, err := ndb.GetWriteLog(ctx, startRoot, endRoot)
require.NoError(t, err, "GetWriteLog")
writeLog = foldWriteLogIterator(t, wli)

for _, entry := range writeLog {
require.NotNil(t, entry.Value)
}
}

func testMergeWriteLog(t *testing.T, ndb db.NodeDB, factory NodeDBFactory) {
ctx := context.Background()

Expand Down Expand Up @@ -2224,6 +2265,7 @@ func testBackend(
{"DebugDump", testDebugDumpLocal},
{"OnCommitHooks", testOnCommitHooks},
{"CommitNoPersist", testCommitNoPersist},
{"EmptyValueWriteLog", testEmptyValueWriteLog},
{"MergeWriteLog", testMergeWriteLog},
{"HasRoot", testHasRoot},
{"GetRootsForVersion", testGetRootsForVersion},
Expand Down

0 comments on commit 207dcba

Please sign in to comment.