Skip to content

Commit

Permalink
channeldb: fix migration bug due to interplay between migration #9 and
Browse files Browse the repository at this point in the history
…#10

In this commit, we fix an issue that was recently introduced as a result
of migration #10. The new TLV format ended up modifying the
serialization functions called in `serializePaymentAttemptInfo`.
Migration #9, also used this `serializePaymentAttemptInfo` method to
serialize the _new_ (pre TLV, but new payment attempt structure) routes
into the database during its migration. However, migration #10 failed to
copy over the existing unmodified `serializePaymentAttemptInfo` method
into the legacy serialization for migration #9. As a result, once
migration #9 was run, the routes/payments were serialized using the
_new_ format, rather than the format used for v0.7.1. This then lead to
de-serialization either failing, or causing partial payment corruption
as migration #10 was expecting the "legacy" format (no TLV info).

We fix this issue by adding a new fully enclosed
`serializePaymentAttemptInfoMigration9`method that will be used for
migration #9. Note that our tests didn't catch this, as they test the
migration in isolation, rather than in series which is how users will
encounter the migrations.

Fixes lightningnetwork#3463.
  • Loading branch information
Roasbeef authored and matheusd committed Oct 16, 2019
1 parent 5cd1735 commit 5bf1f82
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
48 changes: 48 additions & 0 deletions channeldb/migration_09_legacy_serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"

"github.com/decred/dcrlnd/lnwire"
"github.com/decred/dcrlnd/routing/route"
bolt "go.etcd.io/bbolt"
)

Expand Down Expand Up @@ -253,3 +254,50 @@ func deserializeOutgoingPayment(r io.Reader) (*outgoingPayment, error) {

return p, nil
}

// serializePaymentAttemptInfoMigration9 is the serializePaymentAttemptInfo
// version as existed when migration #9 was created. We keep this around, along
// with the methods below to ensure that clients that upgrade will use the
// correct version of this method.
func serializePaymentAttemptInfoMigration9(w io.Writer, a *PaymentAttemptInfo) error {
if err := WriteElements(w, a.PaymentID, a.SessionKey); err != nil {
return err
}

if err := serializeRouteMigration9(w, a.Route); err != nil {
return err
}

return nil
}

func serializeHopMigration9(w io.Writer, h *route.Hop) error {
if err := WriteElements(w,
h.PubKeyBytes[:], h.ChannelID, h.OutgoingTimeLock,
h.AmtToForward,
); err != nil {
return err
}

return nil
}

func serializeRouteMigration9(w io.Writer, r route.Route) error {
if err := WriteElements(w,
r.TotalTimeLock, r.TotalAmount, r.SourcePubKey[:],
); err != nil {
return err
}

if err := WriteElements(w, uint32(len(r.Hops))); err != nil {
return err
}

for _, h := range r.Hops {
if err := serializeHopMigration9(w, h); err != nil {
return err
}
}

return nil
}
2 changes: 1 addition & 1 deletion channeldb/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ func migrateOutgoingPayments(tx *bolt.Tx) error {
}

var attemptBuf bytes.Buffer
if err := serializePaymentAttemptInfo(&attemptBuf, s); err != nil {
if err := serializePaymentAttemptInfoMigration9(&attemptBuf, s); err != nil {
return err
}

Expand Down

0 comments on commit 5bf1f82

Please sign in to comment.