lnd.xprv/channeldb/migration12/migration.go
Olaoluwa Osuntokun f0911765af
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.
2020-03-18 19:34:49 -07:00

75 lines
1.6 KiB
Go

package migration12
import (
"bytes"
"github.com/lightningnetwork/lnd/channeldb/kvdb"
"github.com/lightningnetwork/lnd/lnwire"
)
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 kvdb.RwTx) error {
log.Infof("Migrating invoice bodies to TLV, " +
"adding payment addresses and feature vectors.")
invoiceB := tx.ReadWriteBucket(invoiceBucket)
if invoiceB == nil {
return nil
}
type keyedInvoice struct {
key []byte
invoice Invoice
}
// Read in all existing invoices using the old format.
var invoices []keyedInvoice
err := invoiceB.ForEach(func(k, v []byte) error {
if v == nil {
return nil
}
invoiceReader := bytes.NewReader(v)
invoice, err := LegacyDeserializeInvoice(invoiceReader)
if err != nil {
return err
}
// Insert an empty feature vector on all old payments.
invoice.Terms.Features = emptyFeatures
invoices = append(invoices, keyedInvoice{
key: k,
invoice: invoice,
})
return nil
})
if err != nil {
return err
}
// Write out each one under its original key using TLV.
for _, ki := range invoices {
var b bytes.Buffer
err = SerializeInvoice(&b, &ki.invoice)
if err != nil {
return err
}
err = invoiceB.Put(ki.key, b.Bytes())
if err != nil {
return err
}
}
log.Infof("Migration to TLV invoice bodies, " +
"payment address, and features complete!")
return nil
}