From 22f8f6ed4ae868014b2df4e1d2e6fa47743a1680 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Fri, 8 Apr 2022 02:04:50 +0800 Subject: [PATCH] channeldb+lnwallet: save initial balances during channel opening --- channeldb/channel.go | 21 ++++++++++++++++----- channeldb/channel_test.go | 2 ++ lnwallet/reservation.go | 6 ++++-- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/channeldb/channel.go b/channeldb/channel.go index b07843563..b8bb42cae 100644 --- a/channeldb/channel.go +++ b/channeldb/channel.go @@ -129,9 +129,9 @@ var ( // bucket for a given channel. frozenChanKey = []byte("frozen-chans") - // lastWasRevokeKey is a key that stores true when the last update we sent - // was a revocation and false when it was a commitment signature. This is - // nil in the case of new channels with no updates exchanged. + // lastWasRevokeKey is a key that stores true when the last update we + // sent was a revocation and false when it was a commitment signature. + // This is nil in the case of new channels with no updates exchanged. lastWasRevokeKey = []byte("last-was-revoke") ) @@ -657,6 +657,15 @@ type OpenChannel struct { // received within this channel. TotalMSatReceived lnwire.MilliSatoshi + // InitialLocalBalance is the balance we have during the channel + // opening. When we are not the initiator, this value represents the + // push amount. + InitialLocalBalance lnwire.MilliSatoshi + + // InitialRemoteBalance is the balance they have during the channel + // opening. + InitialRemoteBalance lnwire.MilliSatoshi + // LocalChanCfg is the channel configuration for the local node. LocalChanCfg ChannelConfig @@ -3322,7 +3331,8 @@ func putChanInfo(chanBucket kvdb.RwBucket, channel *OpenChannel) error { channel.chanStatus, channel.FundingBroadcastHeight, channel.NumConfsRequired, channel.ChannelFlags, channel.IdentityPub, channel.Capacity, channel.TotalMSatSent, - channel.TotalMSatReceived, + channel.TotalMSatReceived, channel.InitialLocalBalance, + channel.InitialRemoteBalance, ); err != nil { return err } @@ -3509,7 +3519,8 @@ func fetchChanInfo(chanBucket kvdb.RBucket, channel *OpenChannel) error { &channel.chanStatus, &channel.FundingBroadcastHeight, &channel.NumConfsRequired, &channel.ChannelFlags, &channel.IdentityPub, &channel.Capacity, &channel.TotalMSatSent, - &channel.TotalMSatReceived, + &channel.TotalMSatReceived, &channel.InitialLocalBalance, + &channel.InitialRemoteBalance, ); err != nil { return err } diff --git a/channeldb/channel_test.go b/channeldb/channel_test.go index 85ecd9eb6..b4b8afdeb 100644 --- a/channeldb/channel_test.go +++ b/channeldb/channel_test.go @@ -352,6 +352,8 @@ func createTestChannelState(t *testing.T, cdb *ChannelStateDB) *OpenChannel { Packager: NewChannelPackager(chanID), FundingTxn: channels.TestFundingTx, ThawHeight: uint32(defaultPendingHeight), + InitialLocalBalance: lnwire.MilliSatoshi(9000), + InitialRemoteBalance: lnwire.MilliSatoshi(3000), } } diff --git a/lnwallet/reservation.go b/lnwallet/reservation.go index ee7de6485..c36ba170e 100644 --- a/lnwallet/reservation.go +++ b/lnwallet/reservation.go @@ -403,8 +403,10 @@ func NewChannelReservation(capacity, localFundingAmt btcutil.Amount, FeePerKw: btcutil.Amount(commitFeePerKw), CommitFee: commitFee, }, - ThawHeight: thawHeight, - Db: wallet.Cfg.Database, + ThawHeight: thawHeight, + Db: wallet.Cfg.Database, + InitialLocalBalance: ourBalance, + InitialRemoteBalance: theirBalance, }, pushMSat: pushMSat, pendingChanID: pendingChanID,