Skip to content

Commit cc9e2b7

Browse files
authored
Merge pull request #8961 from yyforyongyu/fix-leaseoutput
Improve the performace of `LeaseOutput`
2 parents a028064 + 363e529 commit cc9e2b7

File tree

20 files changed

+100
-87
lines changed

20 files changed

+100
-87
lines changed

docs/release-notes/release-notes-0.18.3.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ commitment when the channel was force closed.
159159
* [Allow](https://github.com/lightningnetwork/lnd/pull/8845) multiple etcd hosts
160160
to be specified in db.etcd.host.
161161

162+
* Improved the internal [`LeaseOutput`
163+
method](https://github.com/lightningnetwork/lnd/pull/8961) to be more
164+
efficient, which improves the performance of related RPC calls such as
165+
`LeaseOutput`, `SendCoins`, and PSBT funding process.
166+
162167
## RPC Updates
163168

164169
* [`xImportMissionControl`](https://github.com/lightningnetwork/lnd/pull/8779)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/btcsuite/btcd/btcutil/psbt v1.1.8
1111
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0
1212
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f
13-
github.com/btcsuite/btcwallet v0.16.10-0.20240718224643-db3a4a2543bd
13+
github.com/btcsuite/btcwallet v0.16.10-0.20240809133323-7d3434c65ae2
1414
github.com/btcsuite/btcwallet/wallet/txauthor v1.3.4
1515
github.com/btcsuite/btcwallet/wallet/txrules v1.2.1
1616
github.com/btcsuite/btcwallet/walletdb v1.4.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0/go.mod h1:7SFka0XMvUgj3hfZtyd
9292
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f h1:bAs4lUbRJpnnkd9VhRV3jjAVU7DJVjMaK+IsvSeZvFo=
9393
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
9494
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
95-
github.com/btcsuite/btcwallet v0.16.10-0.20240718224643-db3a4a2543bd h1:QDb8foTCRoXrfoZVEzSYgSde16MJh4gCtCin8OCS0kI=
96-
github.com/btcsuite/btcwallet v0.16.10-0.20240718224643-db3a4a2543bd/go.mod h1:X2xDre+j1QphTRo54y2TikUzeSvreL1t1aMXrD8Kc5A=
95+
github.com/btcsuite/btcwallet v0.16.10-0.20240809133323-7d3434c65ae2 h1:qa4Avm7p97JroZZyMJADbEb9u853pjleJYSeitENvLc=
96+
github.com/btcsuite/btcwallet v0.16.10-0.20240809133323-7d3434c65ae2/go.mod h1:X2xDre+j1QphTRo54y2TikUzeSvreL1t1aMXrD8Kc5A=
9797
github.com/btcsuite/btcwallet/wallet/txauthor v1.3.4 h1:poyHFf7+5+RdxNp5r2T6IBRD7RyraUsYARYbp/7t4D8=
9898
github.com/btcsuite/btcwallet/wallet/txauthor v1.3.4/go.mod h1:GETGDQuyq+VFfH1S/+/7slLM/9aNa4l7P4ejX6dJfb0=
9999
github.com/btcsuite/btcwallet/wallet/txrules v1.2.1 h1:UZo7YRzdHbwhK7Rhv3PO9bXgTxiOH45edK5qdsdiatk=

itest/lnd_hold_invoice_force_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ func testHoldInvoiceForceClose(ht *lntest.HarnessTest) {
9595
// We first mine enough blocks to trigger an invoice cancelation.
9696
ht.MineBlocks(int(blocksTillCancel))
9797

98-
// Wait for the nodes to be synced.
99-
ht.WaitForBlockchainSync(alice)
100-
ht.WaitForBlockchainSync(bob)
101-
10298
// Check that the invoice is canceled by Bob.
10399
err := wait.NoError(func() error {
104100
inv := bob.RPC.LookupInvoice(payHash[:])
@@ -135,10 +131,6 @@ func testHoldInvoiceForceClose(ht *lntest.HarnessTest) {
135131
// invoice cancelation message was received by Alice.
136132
ht.MineBlocks(int(blocksTillForce - blocksTillCancel))
137133

138-
// Wait for the nodes to be synced.
139-
ht.WaitForBlockchainSync(alice)
140-
ht.WaitForBlockchainSync(bob)
141-
142134
// Check that Alice has not closed the channel because there are no
143135
// outgoing HTLCs in her channel as the only HTLC has already been
144136
// canceled.

itest/lnd_psbt_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1578,7 +1578,6 @@ func sendAllCoinsToAddrType(ht *lntest.HarnessTest,
15781578
})
15791579

15801580
ht.MineBlocksAndAssertNumTxes(1, 1)
1581-
ht.WaitForBlockchainSync(hn)
15821581
}
15831582

15841583
// testPsbtChanFundingFailFlow tests the failing of a funding flow by the

itest/lnd_route_blinding_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -852,10 +852,6 @@ func testErrorHandlingOnChainFailure(ht *lntest.HarnessTest) {
852852
ht.AssertNumPendingSweeps(ht.Bob, 0)
853853
ht.MineBlocksAndAssertNumTxes(1, 1)
854854

855-
// Assert that the HTLC has cleared.
856-
ht.WaitForBlockchainSync(ht.Bob)
857-
ht.WaitForBlockchainSync(ht.Alice)
858-
859855
ht.AssertHTLCNotActive(ht.Bob, testCase.channels[0], hash[:])
860856
ht.AssertHTLCNotActive(ht.Alice, testCase.channels[0], hash[:])
861857

lnrpc/walletrpc/psbt.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ func lockInputs(w lnwallet.WalletController,
5656
},
5757
}
5858

59-
expiration, pkScript, value, err := w.LeaseOutput(
59+
// Get the details about this outpoint.
60+
utxo, err := w.FetchOutpointInfo(&lock.Outpoint)
61+
if err != nil {
62+
return nil, fmt.Errorf("fetch outpoint info: %w", err)
63+
}
64+
65+
expiration, err := w.LeaseOutput(
6066
lock.LockID, lock.Outpoint,
6167
chanfunding.DefaultLockDuration,
6268
)
@@ -80,8 +86,8 @@ func lockInputs(w lnwallet.WalletController,
8086
}
8187

8288
lock.Expiration = expiration
83-
lock.PkScript = pkScript
84-
lock.Value = int64(value)
89+
lock.PkScript = utxo.PkScript
90+
lock.Value = int64(utxo.Value)
8591
locks[idx] = lock
8692
}
8793

lnrpc/walletrpc/walletkit_server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ func (w *WalletKit) LeaseOutput(ctx context.Context,
496496
// other concurrent processes attempting to lease the same UTXO.
497497
var expiration time.Time
498498
err = w.cfg.CoinSelectionLocker.WithCoinSelectLock(func() error {
499-
expiration, _, _, err = w.cfg.Wallet.LeaseOutput(
499+
expiration, err = w.cfg.Wallet.LeaseOutput(
500500
lockID, *op, duration,
501501
)
502502
return err
@@ -1296,7 +1296,7 @@ func (w *WalletKit) sweepNewInput(op *wire.OutPoint, currentHeight uint32,
12961296
//
12971297
// We'll gather all of the information required by the UtxoSweeper in
12981298
// order to sweep the output.
1299-
utxo, err := w.cfg.Wallet.FetchInputInfo(op)
1299+
utxo, err := w.cfg.Wallet.FetchOutpointInfo(op)
13001300
if err != nil {
13011301
return err
13021302
}

lntest/mock/walletcontroller.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ func (w *WalletController) BackEnd() string {
4343
return "mock"
4444
}
4545

46-
// FetchInputInfo will be called to get info about the inputs to the funding
46+
// FetchOutpointInfo will be called to get info about the inputs to the funding
4747
// transaction.
48-
func (w *WalletController) FetchInputInfo(
48+
func (w *WalletController) FetchOutpointInfo(
4949
prevOut *wire.OutPoint) (*lnwallet.Utxo, error) {
5050

5151
utxo := &lnwallet.Utxo{
@@ -195,9 +195,9 @@ func (w *WalletController) ListTransactionDetails(int32, int32,
195195

196196
// LeaseOutput returns the current time and a nil error.
197197
func (w *WalletController) LeaseOutput(wtxmgr.LockID, wire.OutPoint,
198-
time.Duration) (time.Time, []byte, btcutil.Amount, error) {
198+
time.Duration) (time.Time, error) {
199199

200-
return time.Now(), nil, 0, nil
200+
return time.Now(), nil
201201
}
202202

203203
// ReleaseOutput currently does nothing.
@@ -292,3 +292,11 @@ func (w *WalletController) RemoveDescendants(*wire.MsgTx) error {
292292
func (w *WalletController) CheckMempoolAcceptance(tx *wire.MsgTx) error {
293293
return nil
294294
}
295+
296+
// FetchDerivationInfo queries for the wallet's knowledge of the passed
297+
// pkScript and constructs the derivation info and returns it.
298+
func (w *WalletController) FetchDerivationInfo(
299+
pkScript []byte) (*psbt.Bip32Derivation, error) {
300+
301+
return nil, nil
302+
}

lnwallet/btcwallet/btcwallet.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,36 +1079,20 @@ func (b *BtcWallet) CreateSimpleTx(inputs fn.Set[wire.OutPoint],
10791079
//
10801080
// NOTE: This method requires the global coin selection lock to be held.
10811081
func (b *BtcWallet) LeaseOutput(id wtxmgr.LockID, op wire.OutPoint,
1082-
duration time.Duration) (time.Time, []byte, btcutil.Amount, error) {
1082+
duration time.Duration) (time.Time, error) {
10831083

10841084
// Make sure we don't attempt to double lock an output that's been
10851085
// locked by the in-memory implementation.
10861086
if b.wallet.LockedOutpoint(op) {
1087-
return time.Time{}, nil, 0, wtxmgr.ErrOutputAlreadyLocked
1087+
return time.Time{}, wtxmgr.ErrOutputAlreadyLocked
10881088
}
10891089

10901090
lockedUntil, err := b.wallet.LeaseOutput(id, op, duration)
10911091
if err != nil {
1092-
return time.Time{}, nil, 0, err
1092+
return time.Time{}, err
10931093
}
10941094

1095-
// Get the pkScript and value for this lock from the list of all leased
1096-
// outputs.
1097-
allLeases, err := b.wallet.ListLeasedOutputs()
1098-
if err != nil {
1099-
return time.Time{}, nil, 0, err
1100-
}
1101-
1102-
for _, lease := range allLeases {
1103-
if lease.Outpoint == op {
1104-
return lockedUntil, lease.PkScript,
1105-
btcutil.Amount(lease.Value), nil
1106-
}
1107-
}
1108-
1109-
// We MUST find the leased output in the loop above, otherwise something
1110-
// is seriously wrong.
1111-
return time.Time{}, nil, 0, wtxmgr.ErrUnknownOutput
1095+
return lockedUntil, nil
11121096
}
11131097

11141098
// ListLeasedOutputs returns a list of all currently locked outputs.

0 commit comments

Comments
 (0)