github.com/decred/dcrlnd@v0.7.6/channeldb/migration12/migration.go (about) 1 package migration12 2 3 import ( 4 "bytes" 5 6 lnwire "github.com/decred/dcrlnd/channeldb/migration/lnwire21" 7 "github.com/decred/dcrlnd/kvdb" 8 ) 9 10 var emptyFeatures = lnwire.NewFeatureVector(nil, nil) 11 12 // MigrateInvoiceTLV migrates all existing invoice bodies over to be serialized 13 // in a single TLV stream. In the process, we drop the Receipt field and add 14 // PaymentAddr and Features to the invoice Terms. 15 func MigrateInvoiceTLV(tx kvdb.RwTx) error { 16 log.Infof("Migrating invoice bodies to TLV, " + 17 "adding payment addresses and feature vectors.") 18 19 invoiceB := tx.ReadWriteBucket(invoiceBucket) 20 if invoiceB == nil { 21 return nil 22 } 23 24 type keyedInvoice struct { 25 key []byte 26 invoice Invoice 27 } 28 29 // Read in all existing invoices using the old format. 30 var invoices []keyedInvoice 31 err := invoiceB.ForEach(func(k, v []byte) error { 32 if v == nil { 33 return nil 34 } 35 36 invoiceReader := bytes.NewReader(v) 37 invoice, err := LegacyDeserializeInvoice(invoiceReader) 38 if err != nil { 39 return err 40 } 41 42 // Insert an empty feature vector on all old payments. 43 invoice.Terms.Features = emptyFeatures 44 45 invoices = append(invoices, keyedInvoice{ 46 key: k, 47 invoice: invoice, 48 }) 49 50 return nil 51 }) 52 if err != nil { 53 return err 54 } 55 56 // Write out each one under its original key using TLV. 57 for _, ki := range invoices { 58 var b bytes.Buffer 59 err = SerializeInvoice(&b, &ki.invoice) 60 if err != nil { 61 return err 62 } 63 64 err = invoiceB.Put(ki.key, b.Bytes()) 65 if err != nil { 66 return err 67 } 68 } 69 70 log.Infof("Migration to TLV invoice bodies, " + 71 "payment address, and features complete!") 72 73 return nil 74 }