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.
This commit is contained in:
Johan T. Halseth 2019-06-17 08:55:18 +02:00
parent d6d87e12fe
commit 97dda4f8c8
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

@ -746,9 +746,14 @@ func migrateGossipMessageStoreKeys(tx *bbolt.Tx) error {
// payments) we delete those statuses, so only Completed payments remain in the // payments) we delete those statuses, so only Completed payments remain in the
// new bucket structure. // new bucket structure.
func migrateOutgoingPayments(tx *bbolt.Tx) error { func migrateOutgoingPayments(tx *bbolt.Tx) error {
oldPayments, err := tx.CreateBucketIfNotExists(paymentBucket) log.Infof("Migrating outgoing payments to new bucket structure")
if err != nil {
return err 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) newPayments, err := tx.CreateBucket(paymentsRootBucket)
@ -756,20 +761,22 @@ func migrateOutgoingPayments(tx *bbolt.Tx) error {
return err return err
} }
// Get the source pubkey. // Helper method to get the source pubkey. We define it such that we
nodes := tx.Bucket(nodeBucket) // only attempt to fetch it if needed.
if nodes == nil { sourcePub := func() ([33]byte, error) {
return ErrGraphNotFound var pub [33]byte
} nodes := tx.Bucket(nodeBucket)
if nodes == nil {
return pub, ErrGraphNotFound
}
selfPub := nodes.Get(sourceKey) selfPub := nodes.Get(sourceKey)
if selfPub == nil { if selfPub == nil {
return ErrSourceNodeNotSet 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 { err = oldPayments.ForEach(func(k, v []byte) error {
// Ignores if it is sub-bucket. // Ignores if it is sub-bucket.
@ -800,6 +807,11 @@ func migrateOutgoingPayments(tx *bbolt.Tx) error {
return err return err
} }
sourcePubKey, err := sourcePub()
if err != nil {
return err
}
// Do the same for the PaymentAttemptInfo. // Do the same for the PaymentAttemptInfo.
totalAmt := payment.Terms.Value + payment.Fee totalAmt := payment.Terms.Value + payment.Fee
rt := route.Route{ rt := route.Route{