lnwallet: don't enforce new reserved value in PSBT midstep

This change avoids enforcing new reserved value when PSBT funding is not
finished yet as new inputs and outputs may still be added that could
change the outcome of the check.

This originally failed in the scenario when funding a channel from
external wallet *and depositing to on-chain wallet* was done
simultaneously in a single transaction. If such transaction confirms
then reserved UTXO is guaranteed to be available but the check didn't
take it into account.

The enforcement still occurs in the final step of PSBT funding flow, so
it is safe. It also occurs in case of non-PSBT funding.
This commit is contained in:
Martin Habovstiak 2021-07-17 20:26:29 +02:00
parent f022e557bf
commit ec24767b9b

View file

@ -706,6 +706,12 @@ func (l *LightningWallet) handleFundingReserveRequest(req *InitFundingReserveMsg
return
}
// We need to avoid enforcing reserved value in the middle of PSBT
// funding because some of the following steps may add UTXOs funding
// the on-chain wallet.
// The enforcement still happens at the last step - in PsbtFundingVerify
enforceNewReservedValue := true
// If no chanFunder was provided, then we'll assume the default
// assembler, which is backed by the wallet's internal coin selection.
if req.ChanFunder == nil {
@ -720,6 +726,9 @@ func (l *LightningWallet) handleFundingReserveRequest(req *InitFundingReserveMsg
DustLimit: DustLimitForSize(input.P2WSHSize),
}
req.ChanFunder = chanfunding.NewWalletAssembler(cfg)
} else {
_, isPsbtFunder := req.ChanFunder.(*chanfunding.PsbtAssembler)
enforceNewReservedValue = !isPsbtFunder
}
localFundingAmt := req.LocalFundingAmt
@ -819,6 +828,7 @@ func (l *LightningWallet) handleFundingReserveRequest(req *InitFundingReserveMsg
// when the PSBT has been verified.
isPublic := req.Flags&lnwire.FFAnnounceChannel != 0
hasAnchors := req.CommitType.HasAnchors()
if enforceNewReservedValue {
err = l.enforceNewReservedValue(fundingIntent, isPublic, hasAnchors)
if err != nil {
fundingIntent.Cancel()
@ -827,6 +837,7 @@ func (l *LightningWallet) handleFundingReserveRequest(req *InitFundingReserveMsg
req.resp <- nil
return
}
}
// The total channel capacity will be the size of the funding output we
// created plus the remote contribution.