-
Notifications
You must be signed in to change notification settings - Fork 2.2k
kvdb: refactor as preparation for DB migration command in lndinit #5561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
guggero
merged 8 commits into
lightningnetwork:master
from
guggero:database-migration-lndinit
Oct 13, 2022
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
98f359c
etcd: fix typos
guggero d576184
etcd: extract NewEtcdClient into exported function
guggero ee9d6f2
etcd: add SequenceKey function
guggero 28c8e8b
channeldb: remove unused tx argument
guggero 7e76326
channeldb: export DB migration related functions
guggero fc2d652
channeldb: check for tombstone before opening DB
guggero 696758a
lncfg: export database namespace names
guggero 4e42ef0
docs: add release notes
guggero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"bytes" | ||
"testing" | ||
|
||
"github.com/btcsuite/btcwallet/walletdb" | ||
"github.com/go-errors/errors" | ||
"github.com/lightningnetwork/lnd/kvdb" | ||
"github.com/stretchr/testify/require" | ||
|
@@ -91,7 +92,7 @@ func TestVersionFetchPut(t *testing.T) { | |
t.Fatal(err) | ||
} | ||
|
||
meta, err := db.FetchMeta(nil) | ||
meta, err := db.FetchMeta() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
@@ -107,7 +108,7 @@ func TestVersionFetchPut(t *testing.T) { | |
t.Fatalf("update of meta failed %v", err) | ||
} | ||
|
||
meta, err = db.FetchMeta(nil) | ||
meta, err = db.FetchMeta() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
@@ -228,7 +229,7 @@ func TestMigrationWithPanic(t *testing.T) { | |
|
||
// Check that version of database and data wasn't changed. | ||
afterMigrationFunc := func(d *DB) { | ||
meta, err := d.FetchMeta(nil) | ||
meta, err := d.FetchMeta() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
@@ -303,7 +304,7 @@ func TestMigrationWithFatal(t *testing.T) { | |
|
||
// Check that version of database and initial data wasn't changed. | ||
afterMigrationFunc := func(d *DB) { | ||
meta, err := d.FetchMeta(nil) | ||
meta, err := d.FetchMeta() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
@@ -377,7 +378,7 @@ func TestMigrationWithoutErrors(t *testing.T) { | |
|
||
// Check that version of database and data was properly changed. | ||
afterMigrationFunc := func(d *DB) { | ||
meta, err := d.FetchMeta(nil) | ||
meta, err := d.FetchMeta() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
@@ -469,7 +470,7 @@ func TestMigrationDryRun(t *testing.T) { | |
// Check that version of database version is not modified. | ||
afterMigrationFunc := func(d *DB) { | ||
err := kvdb.View(d, func(tx kvdb.RTx) error { | ||
meta, err := d.FetchMeta(nil) | ||
meta, err := d.FetchMeta() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
@@ -574,3 +575,115 @@ func TestApplyOptionalVersions(t *testing.T) { | |
require.NoError(t, err, "failed to apply optional migration") | ||
require.Equal(t, 1, migrateCount, "expected no migration") | ||
} | ||
|
||
// TestFetchMeta tests that the FetchMeta returns the latest DB version for a | ||
// freshly created DB instance. | ||
func TestFetchMeta(t *testing.T) { | ||
t.Parallel() | ||
|
||
db, cleanUp, err := MakeTestDB() | ||
defer cleanUp() | ||
require.NoError(t, err) | ||
|
||
meta := &Meta{} | ||
err = db.View(func(tx walletdb.ReadTx) error { | ||
return FetchMeta(meta, tx) | ||
}, func() { | ||
meta = &Meta{} | ||
}) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, LatestDBVersion(), meta.DbVersionNumber) | ||
} | ||
|
||
// TestMarkerAndTombstone tests that markers like a tombstone can be added to a | ||
// DB. | ||
func TestMarkerAndTombstone(t *testing.T) { | ||
t.Parallel() | ||
|
||
db, cleanUp, err := MakeTestDB() | ||
defer cleanUp() | ||
require.NoError(t, err) | ||
|
||
// Test that a generic marker is not present in a fresh DB. | ||
var marker []byte | ||
err = db.View(func(tx walletdb.ReadTx) error { | ||
var err error | ||
marker, err = CheckMarkerPresent(tx, []byte("foo")) | ||
return err | ||
}, func() { | ||
marker = nil | ||
}) | ||
require.ErrorIs(t, err, ErrMarkerNotPresent) | ||
require.Nil(t, marker) | ||
|
||
// Only adding the marker bucket should not be enough to be counted as | ||
// a marker, we explicitly also want the value to be set. | ||
err = db.Update(func(tx walletdb.ReadWriteTx) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
_, err := tx.CreateTopLevelBucket([]byte("foo")) | ||
return err | ||
}, func() {}) | ||
require.NoError(t, err) | ||
|
||
err = db.View(func(tx walletdb.ReadTx) error { | ||
var err error | ||
marker, err = CheckMarkerPresent(tx, []byte("foo")) | ||
return err | ||
}, func() { | ||
marker = nil | ||
}) | ||
require.ErrorIs(t, err, ErrMarkerNotPresent) | ||
require.Nil(t, marker) | ||
|
||
// Test that a tombstone marker is not present in a fresh DB. | ||
err = db.View(EnsureNoTombstone, func() {}) | ||
require.NoError(t, err) | ||
|
||
// Add a generic marker now and assert that it can be read. | ||
err = db.Update(func(tx walletdb.ReadWriteTx) error { | ||
return AddMarker(tx, []byte("foo"), []byte("bar")) | ||
}, func() {}) | ||
require.NoError(t, err) | ||
|
||
err = db.View(func(tx walletdb.ReadTx) error { | ||
var err error | ||
marker, err = CheckMarkerPresent(tx, []byte("foo")) | ||
return err | ||
}, func() { | ||
marker = nil | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, []byte("bar"), marker) | ||
|
||
// A tombstone should still not be present. | ||
err = db.View(EnsureNoTombstone, func() {}) | ||
require.NoError(t, err) | ||
|
||
// Finally, add a tombstone. | ||
tombstoneText := []byte("RIP test DB") | ||
err = db.Update(func(tx walletdb.ReadWriteTx) error { | ||
return AddMarker(tx, TombstoneKey, tombstoneText) | ||
}, func() {}) | ||
require.NoError(t, err) | ||
|
||
// We can read it as a normal marker. | ||
err = db.View(func(tx walletdb.ReadTx) error { | ||
var err error | ||
marker, err = CheckMarkerPresent(tx, TombstoneKey) | ||
return err | ||
}, func() { | ||
marker = nil | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, tombstoneText, marker) | ||
|
||
// But also as a tombstone, and now we should get an error that the DB | ||
// cannot be used anymore. | ||
err = db.View(EnsureNoTombstone, func() {}) | ||
require.ErrorContains(t, err, string(tombstoneText)) | ||
|
||
// Now that the DB has a tombstone, we should no longer be able to open | ||
// it once we close it. | ||
_, err = CreateWithBackend(db.Backend) | ||
require.ErrorContains(t, err, string(tombstoneText)) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.