From db4a09d3b5b817b8e500c16258ca8dd19c910d25 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 5 Jul 2018 22:07:40 -0500 Subject: [PATCH] channeldb: skip sub-buckets during invoice time series migration In this commit, we fix an existing bu gin the invoice time series migration code. Before this commit, the migration would fail as we would try to migrate an empty invoice. We now detect this case and skip all empty invoices. We also add a bit more logging on both the info and trace logging level. --- channeldb/migrations.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/channeldb/migrations.go b/channeldb/migrations.go index a1f402285..75ee8f6e3 100644 --- a/channeldb/migrations.go +++ b/channeldb/migrations.go @@ -136,10 +136,17 @@ func migrateInvoiceTimeSeries(tx *bolt.Tx) error { return err } + log.Infof("Migrating invoice database to new time series format") + // Now that we have all the buckets we need, we'll run through each // invoice in the database, and update it to reflect the new format // expected post migration. - return invoices.ForEach(func(invoiceNum, invoiceBytes []byte) error { + err = invoices.ForEach(func(invoiceNum, invoiceBytes []byte) error { + // If this is a sub bucket, then we'll skip it. + if invoiceBytes == nil { + return nil + } + // First, we'll make a copy of the encoded invoice bytes. invoiceBytesCopy := make([]byte, len(invoiceBytes)) copy(invoiceBytesCopy, invoiceBytes) @@ -153,7 +160,7 @@ func migrateInvoiceTimeSeries(tx *bolt.Tx) error { invoiceReader := bytes.NewReader(invoiceBytesCopy) invoice, err := deserializeInvoice(invoiceReader) if err != nil { - return err + return fmt.Errorf("unable to decode invoice: %v", err) } // Now that we have the fully decoded invoice, we can update @@ -173,6 +180,10 @@ func migrateInvoiceTimeSeries(tx *bolt.Tx) error { return err } + log.Tracef("Adding invoice (preimage=%v, add_index=%v) to add "+ + "time series", invoice.Terms.PaymentPreimage[:], + nextAddSeqNo) + // Next, we'll check if the invoice has been settled or not. If // so, then we'll also add it to the settle index. var nextSettleSeqNo uint64 @@ -190,6 +201,11 @@ func migrateInvoiceTimeSeries(tx *bolt.Tx) error { } invoice.AmtPaid = invoice.Terms.Value + + log.Tracef("Adding invoice (preimage=%v, "+ + "settle_index=%v) to add time series", + invoice.Terms.PaymentPreimage[:], + nextSettleSeqNo) } // Finally, we'll update the invoice itself with the new @@ -207,4 +223,11 @@ func migrateInvoiceTimeSeries(tx *bolt.Tx) error { return invoices.Put(invoiceNum, b.Bytes()) }) + if err != nil { + return err + } + + log.Infof("Migration to invoice time series index complete!") + + return nil }