Skip to content

Commit 3e5b5d5

Browse files
authored
Merge pull request #6551 from yyforyongyu/fix-migration
channeldb: change balance fields to tlv records and migrate historical bucket
2 parents e319fb8 + dae8e43 commit 3e5b5d5

File tree

14 files changed

+1337
-52
lines changed

14 files changed

+1337
-52
lines changed

channeldb/channel.go

+51-11
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,14 @@ const (
182182
// A tlv type definition used to serialize and deserialize a KeyLocator
183183
// from the database.
184184
keyLocType tlv.Type = 1
185+
186+
// A tlv type used to serialize and deserialize the
187+
// `InitialLocalBalance` field.
188+
initialLocalBalanceType tlv.Type = 2
189+
190+
// A tlv type used to serialize and deserialize the
191+
// `InitialRemoteBalance` field.
192+
initialRemoteBalanceType tlv.Type = 3
185193
)
186194

187195
// indexStatus is an enum-like type that describes what state the
@@ -3280,8 +3288,7 @@ func putChanInfo(chanBucket kvdb.RwBucket, channel *OpenChannel) error {
32803288
channel.chanStatus, channel.FundingBroadcastHeight,
32813289
channel.NumConfsRequired, channel.ChannelFlags,
32823290
channel.IdentityPub, channel.Capacity, channel.TotalMSatSent,
3283-
channel.TotalMSatReceived, channel.InitialLocalBalance,
3284-
channel.InitialRemoteBalance,
3291+
channel.TotalMSatReceived,
32853292
); err != nil {
32863293
return err
32873294
}
@@ -3301,12 +3308,24 @@ func putChanInfo(chanBucket kvdb.RwBucket, channel *OpenChannel) error {
33013308
return err
33023309
}
33033310

3304-
// Write the RevocationKeyLocator as the first entry in a tlv stream.
3305-
keyLocRecord := MakeKeyLocRecord(
3306-
keyLocType, &channel.RevocationKeyLocator,
3307-
)
3311+
// Convert balance fields into uint64.
3312+
localBalance := uint64(channel.InitialLocalBalance)
3313+
remoteBalance := uint64(channel.InitialRemoteBalance)
33083314

3309-
tlvStream, err := tlv.NewStream(keyLocRecord)
3315+
// Create the tlv stream.
3316+
tlvStream, err := tlv.NewStream(
3317+
// Write the RevocationKeyLocator as the first entry in a tlv
3318+
// stream.
3319+
MakeKeyLocRecord(
3320+
keyLocType, &channel.RevocationKeyLocator,
3321+
),
3322+
tlv.MakePrimitiveRecord(
3323+
initialLocalBalanceType, &localBalance,
3324+
),
3325+
tlv.MakePrimitiveRecord(
3326+
initialRemoteBalanceType, &remoteBalance,
3327+
),
3328+
)
33103329
if err != nil {
33113330
return err
33123331
}
@@ -3468,8 +3487,7 @@ func fetchChanInfo(chanBucket kvdb.RBucket, channel *OpenChannel) error {
34683487
&channel.chanStatus, &channel.FundingBroadcastHeight,
34693488
&channel.NumConfsRequired, &channel.ChannelFlags,
34703489
&channel.IdentityPub, &channel.Capacity, &channel.TotalMSatSent,
3471-
&channel.TotalMSatReceived, &channel.InitialLocalBalance,
3472-
&channel.InitialRemoteBalance,
3490+
&channel.TotalMSatReceived,
34733491
); err != nil {
34743492
return err
34753493
}
@@ -3504,8 +3522,26 @@ func fetchChanInfo(chanBucket kvdb.RBucket, channel *OpenChannel) error {
35043522
}
35053523
}
35063524

3507-
keyLocRecord := MakeKeyLocRecord(keyLocType, &channel.RevocationKeyLocator)
3508-
tlvStream, err := tlv.NewStream(keyLocRecord)
3525+
// Create balance fields in uint64.
3526+
var (
3527+
localBalance uint64
3528+
remoteBalance uint64
3529+
)
3530+
3531+
// Create the tlv stream.
3532+
tlvStream, err := tlv.NewStream(
3533+
// Write the RevocationKeyLocator as the first entry in a tlv
3534+
// stream.
3535+
MakeKeyLocRecord(
3536+
keyLocType, &channel.RevocationKeyLocator,
3537+
),
3538+
tlv.MakePrimitiveRecord(
3539+
initialLocalBalanceType, &localBalance,
3540+
),
3541+
tlv.MakePrimitiveRecord(
3542+
initialRemoteBalanceType, &remoteBalance,
3543+
),
3544+
)
35093545
if err != nil {
35103546
return err
35113547
}
@@ -3514,6 +3550,10 @@ func fetchChanInfo(chanBucket kvdb.RBucket, channel *OpenChannel) error {
35143550
return err
35153551
}
35163552

3553+
// Attach the balance fields.
3554+
channel.InitialLocalBalance = lnwire.MilliSatoshi(localBalance)
3555+
channel.InitialRemoteBalance = lnwire.MilliSatoshi(remoteBalance)
3556+
35173557
channel.Packager = NewChannelPackager(channel.ShortChannelID)
35183558

35193559
// Finally, read the optional shutdown scripts.

channeldb/db.go

+14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"github.com/lightningnetwork/lnd/channeldb/migration23"
2121
"github.com/lightningnetwork/lnd/channeldb/migration24"
2222
"github.com/lightningnetwork/lnd/channeldb/migration25"
23+
"github.com/lightningnetwork/lnd/channeldb/migration26"
24+
"github.com/lightningnetwork/lnd/channeldb/migration27"
2325
"github.com/lightningnetwork/lnd/channeldb/migration_01_to_11"
2426
"github.com/lightningnetwork/lnd/clock"
2527
"github.com/lightningnetwork/lnd/kvdb"
@@ -212,6 +214,18 @@ var (
212214
number: 25,
213215
migration: migration25.MigrateInitialBalances,
214216
},
217+
{
218+
// Migrate the initial local/remote balance fields into
219+
// tlv records.
220+
number: 26,
221+
migration: migration26.MigrateBalancesToTlvRecords,
222+
},
223+
{
224+
// Patch the initial local/remote balance fields with
225+
// empty values for historical channels.
226+
number: 27,
227+
migration: migration27.MigrateHistoricalBalances,
228+
},
215229
}
216230

217231
// Big endian is the preferred byte order, due to cursor scans over

channeldb/migration25/channel.go

+36
Original file line numberDiff line numberDiff line change
@@ -720,3 +720,39 @@ func fetchChannelLogEntry(log kvdb.RBucket,
720720
commitReader := bytes.NewReader(commitBytes)
721721
return mig.DeserializeChanCommit(commitReader)
722722
}
723+
724+
func CreateChanBucket(tx kvdb.RwTx, c *OpenChannel) (kvdb.RwBucket, error) {
725+
// First fetch the top level bucket which stores all data related to
726+
// current, active channels.
727+
openChanBucket, err := tx.CreateTopLevelBucket(openChannelBucket)
728+
if err != nil {
729+
return nil, err
730+
}
731+
732+
// Within this top level bucket, fetch the bucket dedicated to storing
733+
// open channel data specific to the remote node.
734+
nodePub := c.IdentityPub.SerializeCompressed()
735+
nodeChanBucket, err := openChanBucket.CreateBucketIfNotExists(nodePub)
736+
if err != nil {
737+
return nil, err
738+
}
739+
740+
// We'll then recurse down an additional layer in order to fetch the
741+
// bucket for this particular chain.
742+
chainBucket, err := nodeChanBucket.CreateBucketIfNotExists(
743+
c.ChainHash[:],
744+
)
745+
if err != nil {
746+
return nil, err
747+
}
748+
749+
var chanPointBuf bytes.Buffer
750+
err = mig.WriteOutpoint(&chanPointBuf, &c.FundingOutpoint)
751+
if err != nil {
752+
return nil, err
753+
}
754+
755+
// With the bucket for the node fetched, we can now go down another
756+
// level, creating the bucket for this channel itself.
757+
return chainBucket.CreateBucketIfNotExists(chanPointBuf.Bytes())
758+
}

channeldb/migration25/migration.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func findOpenChannels(openChanBucket kvdb.RBucket) ([]*OpenChannel, error) {
147147
// balances and save them to the channel info.
148148
func migrateBalances(tx kvdb.RwTx, c *OpenChannel) error {
149149
// Get the bucket.
150-
chanBucket, err := fetchChanBucket(tx, c)
150+
chanBucket, err := FetchChanBucket(tx, c)
151151
if err != nil {
152152
return err
153153
}
@@ -168,10 +168,10 @@ func migrateBalances(tx kvdb.RwTx, c *OpenChannel) error {
168168
return nil
169169
}
170170

171-
// fetchChanBucket is a helper function that returns the bucket where a
171+
// FetchChanBucket is a helper function that returns the bucket where a
172172
// channel's data resides in given: the public key for the node, the outpoint,
173173
// and the chainhash that the channel resides on.
174-
func fetchChanBucket(tx kvdb.RwTx, c *OpenChannel) (kvdb.RwBucket, error) {
174+
func FetchChanBucket(tx kvdb.RwTx, c *OpenChannel) (kvdb.RwBucket, error) {
175175
// First fetch the top level bucket which stores all data related to
176176
// current, active channels.
177177
openChanBucket := tx.ReadWriteBucket(openChannelBucket)

channeldb/migration25/migration_test.go

+2-38
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func genBeforeMigration(c *OpenChannel,
256256
}
257257

258258
// Create the channel bucket.
259-
chanBucket, err := createChanBucket(tx, c)
259+
chanBucket, err := CreateChanBucket(tx, c)
260260
if err != nil {
261261
return err
262262
}
@@ -295,7 +295,7 @@ func genAfterMigration(ourAmt, theirAmt lnwire.MilliSatoshi,
295295
return nil
296296
}
297297

298-
chanBucket, err := fetchChanBucket(tx, c)
298+
chanBucket, err := FetchChanBucket(tx, c)
299299
if err != nil {
300300
return err
301301
}
@@ -334,42 +334,6 @@ func genAfterMigration(ourAmt, theirAmt lnwire.MilliSatoshi,
334334
}
335335
}
336336

337-
func createChanBucket(tx kvdb.RwTx, c *OpenChannel) (kvdb.RwBucket, error) {
338-
// First fetch the top level bucket which stores all data related to
339-
// current, active channels.
340-
openChanBucket, err := tx.CreateTopLevelBucket(openChannelBucket)
341-
if err != nil {
342-
return nil, err
343-
}
344-
345-
// Within this top level bucket, fetch the bucket dedicated to storing
346-
// open channel data specific to the remote node.
347-
nodePub := c.IdentityPub.SerializeCompressed()
348-
nodeChanBucket, err := openChanBucket.CreateBucketIfNotExists(nodePub)
349-
if err != nil {
350-
return nil, err
351-
}
352-
353-
// We'll then recurse down an additional layer in order to fetch the
354-
// bucket for this particular chain.
355-
chainBucket, err := nodeChanBucket.CreateBucketIfNotExists(
356-
c.ChainHash[:],
357-
)
358-
if err != nil {
359-
return nil, err
360-
}
361-
362-
var chanPointBuf bytes.Buffer
363-
err = mig.WriteOutpoint(&chanPointBuf, &c.FundingOutpoint)
364-
if err != nil {
365-
return nil, err
366-
}
367-
368-
// With the bucket for the node fetched, we can now go down another
369-
// level, creating the bucket for this channel itself.
370-
return chainBucket.CreateBucketIfNotExists(chanPointBuf.Bytes())
371-
}
372-
373337
// putChannelLogEntryLegacy saves an old format revocation log to the bucket.
374338
func putChannelLogEntryLegacy(chanBucket kvdb.RwBucket,
375339
commit *mig.ChannelCommitment) error {

0 commit comments

Comments
 (0)