diff --git a/channeldb/migration25/channel.go b/channeldb/migration25/channel.go index c8b93bb39..f33e87955 100644 --- a/channeldb/migration25/channel.go +++ b/channeldb/migration25/channel.go @@ -720,3 +720,39 @@ func fetchChannelLogEntry(log kvdb.RBucket, commitReader := bytes.NewReader(commitBytes) return mig.DeserializeChanCommit(commitReader) } + +func CreateChanBucket(tx kvdb.RwTx, c *OpenChannel) (kvdb.RwBucket, error) { + // First fetch the top level bucket which stores all data related to + // current, active channels. + openChanBucket, err := tx.CreateTopLevelBucket(openChannelBucket) + if err != nil { + return nil, err + } + + // Within this top level bucket, fetch the bucket dedicated to storing + // open channel data specific to the remote node. + nodePub := c.IdentityPub.SerializeCompressed() + nodeChanBucket, err := openChanBucket.CreateBucketIfNotExists(nodePub) + if err != nil { + return nil, err + } + + // We'll then recurse down an additional layer in order to fetch the + // bucket for this particular chain. + chainBucket, err := nodeChanBucket.CreateBucketIfNotExists( + c.ChainHash[:], + ) + if err != nil { + return nil, err + } + + var chanPointBuf bytes.Buffer + err = mig.WriteOutpoint(&chanPointBuf, &c.FundingOutpoint) + if err != nil { + return nil, err + } + + // With the bucket for the node fetched, we can now go down another + // level, creating the bucket for this channel itself. + return chainBucket.CreateBucketIfNotExists(chanPointBuf.Bytes()) +} diff --git a/channeldb/migration25/migration.go b/channeldb/migration25/migration.go index db6e156e9..5e708e4bc 100644 --- a/channeldb/migration25/migration.go +++ b/channeldb/migration25/migration.go @@ -147,7 +147,7 @@ func findOpenChannels(openChanBucket kvdb.RBucket) ([]*OpenChannel, error) { // balances and save them to the channel info. func migrateBalances(tx kvdb.RwTx, c *OpenChannel) error { // Get the bucket. - chanBucket, err := fetchChanBucket(tx, c) + chanBucket, err := FetchChanBucket(tx, c) if err != nil { return err } @@ -168,10 +168,10 @@ func migrateBalances(tx kvdb.RwTx, c *OpenChannel) error { return nil } -// fetchChanBucket is a helper function that returns the bucket where a +// FetchChanBucket is a helper function that returns the bucket where a // channel's data resides in given: the public key for the node, the outpoint, // and the chainhash that the channel resides on. -func fetchChanBucket(tx kvdb.RwTx, c *OpenChannel) (kvdb.RwBucket, error) { +func FetchChanBucket(tx kvdb.RwTx, c *OpenChannel) (kvdb.RwBucket, error) { // First fetch the top level bucket which stores all data related to // current, active channels. openChanBucket := tx.ReadWriteBucket(openChannelBucket) diff --git a/channeldb/migration25/migration_test.go b/channeldb/migration25/migration_test.go index 339815cab..58459c145 100644 --- a/channeldb/migration25/migration_test.go +++ b/channeldb/migration25/migration_test.go @@ -256,7 +256,7 @@ func genBeforeMigration(c *OpenChannel, } // Create the channel bucket. - chanBucket, err := createChanBucket(tx, c) + chanBucket, err := CreateChanBucket(tx, c) if err != nil { return err } @@ -295,7 +295,7 @@ func genAfterMigration(ourAmt, theirAmt lnwire.MilliSatoshi, return nil } - chanBucket, err := fetchChanBucket(tx, c) + chanBucket, err := FetchChanBucket(tx, c) if err != nil { return err } @@ -334,42 +334,6 @@ func genAfterMigration(ourAmt, theirAmt lnwire.MilliSatoshi, } } -func createChanBucket(tx kvdb.RwTx, c *OpenChannel) (kvdb.RwBucket, error) { - // First fetch the top level bucket which stores all data related to - // current, active channels. - openChanBucket, err := tx.CreateTopLevelBucket(openChannelBucket) - if err != nil { - return nil, err - } - - // Within this top level bucket, fetch the bucket dedicated to storing - // open channel data specific to the remote node. - nodePub := c.IdentityPub.SerializeCompressed() - nodeChanBucket, err := openChanBucket.CreateBucketIfNotExists(nodePub) - if err != nil { - return nil, err - } - - // We'll then recurse down an additional layer in order to fetch the - // bucket for this particular chain. - chainBucket, err := nodeChanBucket.CreateBucketIfNotExists( - c.ChainHash[:], - ) - if err != nil { - return nil, err - } - - var chanPointBuf bytes.Buffer - err = mig.WriteOutpoint(&chanPointBuf, &c.FundingOutpoint) - if err != nil { - return nil, err - } - - // With the bucket for the node fetched, we can now go down another - // level, creating the bucket for this channel itself. - return chainBucket.CreateBucketIfNotExists(chanPointBuf.Bytes()) -} - // putChannelLogEntryLegacy saves an old format revocation log to the bucket. func putChannelLogEntryLegacy(chanBucket kvdb.RwBucket, commit *mig.ChannelCommitment) error {