lnwallet/channel: extract local balance from spend instead of stored commit

This commit is contained in:
Johan T. Halseth 2018-07-12 11:02:52 +02:00
parent 2626bba105
commit eed052eba5
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

@ -4853,6 +4853,12 @@ type UnilateralCloseSummary struct {
// that the remote party broadcasts their commitment. The commitPoint argument // that the remote party broadcasts their commitment. The commitPoint argument
// should be set to the per_commitment_point corresponding to the spending // should be set to the per_commitment_point corresponding to the spending
// commitment. // commitment.
//
// NOTE: The remoteCommit argument should be set to the stored commitment for
// this particular state. If we don't have the commitment stored (should only
// happen in case we have lost state) it should be set to an empty struct, in
// which case we will attempt to sweep the non-HTLC output using the passed
// commitPoint.
func NewUnilateralCloseSummary(chanState *channeldb.OpenChannel, signer Signer, func NewUnilateralCloseSummary(chanState *channeldb.OpenChannel, signer Signer,
pCache PreimageCache, commitSpend *chainntnfs.SpendDetail, pCache PreimageCache, commitSpend *chainntnfs.SpendDetail,
remoteCommit channeldb.ChannelCommitment, remoteCommit channeldb.ChannelCommitment,
@ -4885,13 +4891,19 @@ func NewUnilateralCloseSummary(chanState *channeldb.OpenChannel, signer Signer,
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to create self commit script: %v", err) return nil, fmt.Errorf("unable to create self commit script: %v", err)
} }
var selfPoint *wire.OutPoint
var (
selfPoint *wire.OutPoint
localBalance int64
)
for outputIndex, txOut := range commitTxBroadcast.TxOut { for outputIndex, txOut := range commitTxBroadcast.TxOut {
if bytes.Equal(txOut.PkScript, selfP2WKH) { if bytes.Equal(txOut.PkScript, selfP2WKH) {
selfPoint = &wire.OutPoint{ selfPoint = &wire.OutPoint{
Hash: *commitSpend.SpenderTxHash, Hash: *commitSpend.SpenderTxHash,
Index: uint32(outputIndex), Index: uint32(outputIndex),
} }
localBalance = txOut.Value
break break
} }
} }
@ -4902,7 +4914,6 @@ func NewUnilateralCloseSummary(chanState *channeldb.OpenChannel, signer Signer,
var commitResolution *CommitOutputResolution var commitResolution *CommitOutputResolution
if selfPoint != nil { if selfPoint != nil {
localPayBase := chanState.LocalChanCfg.PaymentBasePoint localPayBase := chanState.LocalChanCfg.PaymentBasePoint
localBalance := remoteCommit.LocalBalance.ToSatoshis()
commitResolution = &CommitOutputResolution{ commitResolution = &CommitOutputResolution{
SelfOutPoint: *selfPoint, SelfOutPoint: *selfPoint,
SelfOutputSignDesc: SignDescriptor{ SelfOutputSignDesc: SignDescriptor{
@ -4910,7 +4921,7 @@ func NewUnilateralCloseSummary(chanState *channeldb.OpenChannel, signer Signer,
SingleTweak: keyRing.LocalCommitKeyTweak, SingleTweak: keyRing.LocalCommitKeyTweak,
WitnessScript: selfP2WKH, WitnessScript: selfP2WKH,
Output: &wire.TxOut{ Output: &wire.TxOut{
Value: int64(localBalance), Value: localBalance,
PkScript: selfP2WKH, PkScript: selfP2WKH,
}, },
HashType: txscript.SigHashAll, HashType: txscript.SigHashAll,
@ -4919,7 +4930,6 @@ func NewUnilateralCloseSummary(chanState *channeldb.OpenChannel, signer Signer,
} }
} }
localBalance := remoteCommit.LocalBalance.ToSatoshis()
closeSummary := channeldb.ChannelCloseSummary{ closeSummary := channeldb.ChannelCloseSummary{
ChanPoint: chanState.FundingOutpoint, ChanPoint: chanState.FundingOutpoint,
ChainHash: chanState.ChainHash, ChainHash: chanState.ChainHash,
@ -4927,7 +4937,7 @@ func NewUnilateralCloseSummary(chanState *channeldb.OpenChannel, signer Signer,
CloseHeight: uint32(commitSpend.SpendingHeight), CloseHeight: uint32(commitSpend.SpendingHeight),
RemotePub: chanState.IdentityPub, RemotePub: chanState.IdentityPub,
Capacity: chanState.Capacity, Capacity: chanState.Capacity,
SettledBalance: localBalance, SettledBalance: btcutil.Amount(localBalance),
CloseType: channeldb.RemoteForceClose, CloseType: channeldb.RemoteForceClose,
IsPending: true, IsPending: true,
} }