-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
migration.go
74 lines (59 loc) · 1.59 KB
/
migration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package migration12
import (
"bytes"
lnwire "github.com/lightningnetwork/lnd/channeldb/migration/lnwire21"
"github.com/lightningnetwork/lnd/kvdb"
)
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
}