From 46c961aa17ba9a3e66b2cfd5bb810ff76994bd4c Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Tue, 20 Nov 2018 15:09:45 +0100 Subject: [PATCH] channeldb/migrations: migrate ChannelCloseSummary --- channeldb/db.go | 7 +++++++ channeldb/migrations.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/channeldb/db.go b/channeldb/db.go index 2c8fc69c..a635db3b 100644 --- a/channeldb/db.go +++ b/channeldb/db.go @@ -80,6 +80,13 @@ var ( number: 6, migration: migratePruneEdgeUpdateIndex, }, + { + // The DB version that migrates the ChannelCloseSummary + // to a format where optional fields are indicated with + // boolean flags. + number: 7, + migration: migrateOptionalChannelCloseSummaryFields, + }, } // Big endian is the preferred byte order, due to cursor scans over diff --git a/channeldb/migrations.go b/channeldb/migrations.go index 518271a8..cddf6278 100644 --- a/channeldb/migrations.go +++ b/channeldb/migrations.go @@ -573,3 +573,40 @@ func migratePruneEdgeUpdateIndex(tx *bolt.Tx) error { return nil } + +// migrateOptionalChannelCloseSummaryFields migrates the serialized format of +// ChannelCloseSummary to a format where optional fields' presence is indicated +// with boolean markers. +func migrateOptionalChannelCloseSummaryFields(tx *bolt.Tx) error { + closedChanBucket := tx.Bucket(closedChannelBucket) + if closedChanBucket == nil { + return nil + } + + log.Info("Migrating to new closed channel format...") + err := closedChanBucket.ForEach(func(chanID, summary []byte) error { + r := bytes.NewReader(summary) + + // Read the old (v6) format from the database. + c, err := deserializeCloseChannelSummaryV6(r) + if err != nil { + return err + } + + // Serialize using the new format, and put back into the + // bucket. + var b bytes.Buffer + if err := serializeChannelCloseSummary(&b, c); err != nil { + return err + } + + return closedChanBucket.Put(chanID, b.Bytes()) + }) + if err != nil { + return fmt.Errorf("unable to update closed channels: %v", err) + } + + log.Info("Migration to new closed channel format complete!") + + return nil +}