From 97dda4f8c84aa5362b24aeb59c831eb6ff91f052 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Mon, 17 Jun 2019 08:55:18 +0200 Subject: [PATCH] channeldb/migrations: skip migration for empty outgoing payments bucket Previously the migration would fail if the source node was not set in the database. Since we know that the source node must have been set before making any payments, we check whether there actually are any payments to migrate, and return early if not. --- channeldb/migrations.go | 42 ++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/channeldb/migrations.go b/channeldb/migrations.go index 181927ff..d875dc8d 100644 --- a/channeldb/migrations.go +++ b/channeldb/migrations.go @@ -746,9 +746,14 @@ func migrateGossipMessageStoreKeys(tx *bbolt.Tx) error { // payments) we delete those statuses, so only Completed payments remain in the // new bucket structure. func migrateOutgoingPayments(tx *bbolt.Tx) error { - oldPayments, err := tx.CreateBucketIfNotExists(paymentBucket) - if err != nil { - return err + log.Infof("Migrating outgoing payments to new bucket structure") + + oldPayments := tx.Bucket(paymentBucket) + + // Return early if there are no payments to migrate. + if oldPayments == nil { + log.Infof("No outgoing payments found, nothing to migrate.") + return nil } newPayments, err := tx.CreateBucket(paymentsRootBucket) @@ -756,20 +761,22 @@ func migrateOutgoingPayments(tx *bbolt.Tx) error { return err } - // Get the source pubkey. - nodes := tx.Bucket(nodeBucket) - if nodes == nil { - return ErrGraphNotFound - } + // Helper method to get the source pubkey. We define it such that we + // only attempt to fetch it if needed. + sourcePub := func() ([33]byte, error) { + var pub [33]byte + nodes := tx.Bucket(nodeBucket) + if nodes == nil { + return pub, ErrGraphNotFound + } - selfPub := nodes.Get(sourceKey) - if selfPub == nil { - return ErrSourceNodeNotSet + selfPub := nodes.Get(sourceKey) + if selfPub == nil { + return pub, ErrSourceNodeNotSet + } + copy(pub[:], selfPub[:]) + return pub, nil } - var sourcePubKey [33]byte - copy(sourcePubKey[:], selfPub[:]) - - log.Infof("Migrating outgoing payments to new bucket structure") err = oldPayments.ForEach(func(k, v []byte) error { // Ignores if it is sub-bucket. @@ -800,6 +807,11 @@ func migrateOutgoingPayments(tx *bbolt.Tx) error { return err } + sourcePubKey, err := sourcePub() + if err != nil { + return err + } + // Do the same for the PaymentAttemptInfo. totalAmt := payment.Terms.Value + payment.Fee rt := route.Route{