@@ -1551,21 +1551,82 @@ func (w *WalletKit) fundPsbtInternalWallet(account string,
15511551 return err
15521552 }
15531553
1554+ // filterFn makes sure utxos which are unconfirmed and
1555+ // still used by the sweeper are not used.
1556+ filterFn := func (u * lnwallet.Utxo ) bool {
1557+ // Confirmed utxos are always allowed.
1558+ if u .Confirmations > 0 {
1559+ return true
1560+ }
1561+
1562+ // Unconfirmed utxos in use by the sweeper are
1563+ // not stable to use because they can be
1564+ // replaced.
1565+ if w .cfg .Sweeper .IsSweeperOutpoint (u .OutPoint ) {
1566+ log .Warnf ("Cannot use unconfirmed " +
1567+ "utxo=%v because it is " +
1568+ "unstable and could be " +
1569+ "replaced" , u .OutPoint )
1570+
1571+ return false
1572+ }
1573+
1574+ return true
1575+ }
1576+
1577+ eligible := fn .Filter (filterFn , utxos )
1578+
15541579 // Validate all inputs against our known list of UTXOs
15551580 // now.
1556- err = verifyInputsUnspent (packet .UnsignedTx .TxIn , utxos )
1581+ err = verifyInputsUnspent (
1582+ packet .UnsignedTx .TxIn , eligible ,
1583+ )
15571584 if err != nil {
15581585 return err
15591586 }
15601587 }
15611588
1589+ // currentHeight is needed to determine whether the internal
1590+ // wallet utxo is still unconfirmed.
1591+ _ , currentHeight , err := w .cfg .Chain .GetBestBlock ()
1592+ if err != nil {
1593+ return fmt .Errorf ("unable to retrieve current " +
1594+ "height: %v" , err )
1595+ }
1596+
1597+ // restrictUnstableUtxos is a filter function which disallows
1598+ // the usage of unconfirmed outputs published (still in use) by
1599+ // the sweeper.
1600+ restrictUnstableUtxos := func (utxo wtxmgr.Credit ) bool {
1601+ // Wallet utxos which are unmined have a height
1602+ // of -1.
1603+ if utxo .Height != - 1 && utxo .Height <= currentHeight {
1604+ // Confirmed utxos are always allowed.
1605+ return true
1606+ }
1607+
1608+ // Utxos used by the sweeper are not used for
1609+ // channel openings.
1610+ allowed := ! w .cfg .Sweeper .IsSweeperOutpoint (
1611+ utxo .OutPoint ,
1612+ )
1613+ if ! allowed {
1614+ log .Warnf ("Cannot use unconfirmed " +
1615+ "utxo=%v because it is " +
1616+ "unstable and could be " +
1617+ "replaced" , utxo .OutPoint )
1618+ }
1619+
1620+ return allowed
1621+ }
1622+
15621623 // We made sure the input from the user is as sane as possible.
15631624 // We can now ask the wallet to fund the TX. This will not yet
15641625 // lock any coins but might still change the wallet DB by
15651626 // generating a new change address.
15661627 changeIndex , err := w .cfg .Wallet .FundPsbt (
1567- packet , minConfs , feeSatPerKW , account ,
1568- keyScope , strategy ,
1628+ packet , minConfs , feeSatPerKW , account , keyScope ,
1629+ strategy , restrictUnstableUtxos ,
15691630 )
15701631 if err != nil {
15711632 return fmt .Errorf ("wallet couldn't fund PSBT: %w" , err )
0 commit comments