channeldb: convert to uniformly use new kvdb abstractions

In this commit, we migrate all the code in `channeldb` to only reference
the new `kvdb` package rather than `bbolt` directly.

In many instances, we need to add two version to fetch a bucket as both
read and write when needed. As an example, we add a new
`fetchChanBucketRw` function. This function is identical to
`fetchChanBucket`, but it will be used to fetch the main channel bucket
for all _write_ transactions. We need a new method as you can pass a
write transaction where a read is accepted, but not the other way around
due to the stronger typing of the new `kvdb` package.
This commit is contained in:
Olaoluwa Osuntokun committed 2020-03-18 19:34:49 -07:00
1 parent fc808ac538
commit f0911765af
36 files changed
+804 -752

No files matched your search

+3 -3
View File
@@ -3,7 +3,7 @@ package migration12
import (
"bytes"
"github.com/coreos/bbolt"
"github.com/lightningnetwork/lnd/channeldb/kvdb"
"github.com/lightningnetwork/lnd/lnwire"
)
@@ -12,11 +12,11 @@ var emptyFeatures = lnwire.NewFeatureVector(nil, nil)
// MigrateInvoiceTLV migrates all existing invoice bodies over to be serialized
// in a single TLV stream. In the process, we drop the Receipt field and add
// PaymentAddr and Features to the invoice Terms.
func MigrateInvoiceTLV(tx *bbolt.Tx) error {
func MigrateInvoiceTLV(tx kvdb.RwTx) error {
log.Infof("Migrating invoice bodies to TLV, " +
"adding payment addresses and feature vectors.")
invoiceB := tx.Bucket(invoiceBucket)
invoiceB := tx.ReadWriteBucket(invoiceBucket)
if invoiceB == nil {
return nil
}
+11 -11
View File
@@ -5,7 +5,7 @@ import (
"fmt"
"testing"
"github.com/coreos/bbolt"
"github.com/lightningnetwork/lnd/channeldb/kvdb"
"github.com/lightningnetwork/lnd/channeldb/migration12"
"github.com/lightningnetwork/lnd/channeldb/migtest"
"github.com/lightningnetwork/lnd/lntypes"
@@ -121,15 +121,15 @@ var (
type migrationTest struct {
name string
beforeMigration func(*bbolt.Tx) error
afterMigration func(*bbolt.Tx) error
beforeMigration func(kvdb.RwTx) error
afterMigration func(kvdb.RwTx) error
}
var migrationTests = []migrationTest{
{
name: "no invoices",
beforeMigration: func(*bbolt.Tx) error { return nil },
afterMigration: func(*bbolt.Tx) error { return nil },
beforeMigration: func(kvdb.RwTx) error { return nil },
afterMigration: func(kvdb.RwTx) error { return nil },
},
{
name: "zero htlcs",
@@ -145,9 +145,9 @@ var migrationTests = []migrationTest{
// genBeforeMigration creates a closure that inserts an invoice serialized under
// the old format under the test payment hash.
func genBeforeMigration(beforeBytes []byte) func(*bbolt.Tx) error {
return func(tx *bbolt.Tx) error {
invoices, err := tx.CreateBucketIfNotExists(
func genBeforeMigration(beforeBytes []byte) func(kvdb.RwTx) error {
return func(tx kvdb.RwTx) error {
invoices, err := tx.CreateTopLevelBucket(
invoiceBucket,
)
if err != nil {
@@ -162,9 +162,9 @@ func genBeforeMigration(beforeBytes []byte) func(*bbolt.Tx) error {
// succeeded, but comparing the resulting encoding of the invoice to the
// expected serialization. In addition, the decoded invoice is compared against
// the expected invoice for equality.
func genAfterMigration(afterBytes []byte) func(*bbolt.Tx) error {
return func(tx *bbolt.Tx) error {
invoices := tx.Bucket(invoiceBucket)
func genAfterMigration(afterBytes []byte) func(kvdb.RwTx) error {
return func(tx kvdb.RwTx) error {
invoices := tx.ReadWriteBucket(invoiceBucket)
if invoices == nil {
return fmt.Errorf("invoice bucket not found")
}