From aba8507b2a52f4222d3bb2c6873a36c94511569c Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Thu, 8 Aug 2024 21:53:27 +0800 Subject: [PATCH] lnrpc+lnwallet: replace `FetchInputInfo` with new methods This commit replaces the usage of `FetchInputInfo` with `FetchOutpointInfo` and `FetchDerivationInfo` to remove unncessary fetching of the derivation path. --- lnrpc/walletrpc/walletkit_server.go | 2 +- lnwallet/rpcwallet/rpcwallet.go | 25 +++++++++++++++++++------ lnwallet/wallet.go | 6 +++--- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/lnrpc/walletrpc/walletkit_server.go b/lnrpc/walletrpc/walletkit_server.go index e70a1a771..f72c6986a 100644 --- a/lnrpc/walletrpc/walletkit_server.go +++ b/lnrpc/walletrpc/walletkit_server.go @@ -1142,7 +1142,7 @@ func (w *WalletKit) sweepNewInput(op *wire.OutPoint, currentHeight uint32, // // We'll gather all of the information required by the UtxoSweeper in // order to sweep the output. - utxo, err := w.cfg.Wallet.FetchInputInfo(op) + utxo, err := w.cfg.Wallet.FetchOutpointInfo(op) if err != nil { return err } diff --git a/lnwallet/rpcwallet/rpcwallet.go b/lnwallet/rpcwallet/rpcwallet.go index c70217416..bf6aa61df 100644 --- a/lnwallet/rpcwallet/rpcwallet.go +++ b/lnwallet/rpcwallet/rpcwallet.go @@ -154,7 +154,7 @@ func (r *RPCKeyRing) SendOutputs(inputs fn.Set[wire.OutPoint], // watch-only wallet if it can map this outpoint into a coin we // own. If not, then we can't continue because our wallet state // is out of sync. - info, err := r.WalletController.FetchInputInfo( + info, err := r.WalletController.FetchOutpointInfo( &txIn.PreviousOutPoint, ) if err != nil { @@ -289,7 +289,7 @@ func (r *RPCKeyRing) FinalizePsbt(packet *psbt.Packet, _ string) error { // We can only sign this input if it's ours, so we try to map it // to a coin we own. If we can't, then we'll continue as it // isn't our input. - utxo, err := r.FetchInputInfo(&txIn.PreviousOutPoint) + utxo, err := r.FetchOutpointInfo(&txIn.PreviousOutPoint) if err != nil { continue } @@ -906,7 +906,7 @@ func (r *RPCKeyRing) remoteSign(tx *wire.MsgTx, signDesc *input.SignDescriptor, } txIn := tx.TxIn[idx] - info, err := r.WalletController.FetchInputInfo( + info, err := r.WalletController.FetchOutpointInfo( &txIn.PreviousOutPoint, ) if err != nil { @@ -1015,19 +1015,32 @@ func (r *RPCKeyRing) remoteSign(tx *wire.MsgTx, signDesc *input.SignDescriptor, signDesc.KeyDesc.PubKey = fullDesc.PubKey } + var derivation *psbt.Bip32Derivation + // Make sure we actually know about the input. We either have been // watching the UTXO on-chain or we have been given all the required // info in the sign descriptor. - info, err := r.WalletController.FetchInputInfo(&txIn.PreviousOutPoint) + info, err := r.WalletController.FetchOutpointInfo( + &txIn.PreviousOutPoint, + ) + + // If the wallet is aware of this outpoint, we go ahead and fetch the + // derivation info. + if err == nil { + derivation, err = r.WalletController.FetchDerivationInfo( + info.PkScript, + ) + } + switch { - // No error, we do have the full UTXO and derivation info available. + // No error, we do have the full UTXO info available. case err == nil: in.WitnessUtxo = &wire.TxOut{ Value: int64(info.Value), PkScript: info.PkScript, } in.NonWitnessUtxo = info.PrevTx - in.Bip32Derivation = []*psbt.Bip32Derivation{info.Derivation} + in.Bip32Derivation = []*psbt.Bip32Derivation{derivation} // The wallet doesn't know about this UTXO, so it's probably a TX that // we haven't published yet (e.g. a channel funding TX). So we need to diff --git a/lnwallet/wallet.go b/lnwallet/wallet.go index 90a0bea2d..6925446a3 100644 --- a/lnwallet/wallet.go +++ b/lnwallet/wallet.go @@ -1646,7 +1646,7 @@ func (l *LightningWallet) handleContributionMsg(req *addContributionMsg) { []*input.Script, 0, len(ourContribution.Inputs), ) for _, txIn := range fundingTx.TxIn { - _, err := l.FetchInputInfo(&txIn.PreviousOutPoint) + _, err := l.FetchOutpointInfo(&txIn.PreviousOutPoint) if err != nil { continue } @@ -2592,7 +2592,7 @@ func (c *CoinSource) ListCoins(minConfs int32, // its outpoint. If the coin isn't under the control of the backing CoinSource, // then an error should be returned. func (c *CoinSource) CoinFromOutPoint(op wire.OutPoint) (*wallet.Coin, error) { - inputInfo, err := c.wallet.FetchInputInfo(&op) + inputInfo, err := c.wallet.FetchOutpointInfo(&op) if err != nil { return nil, err } @@ -2689,7 +2689,7 @@ func NewWalletPrevOutputFetcher(wc WalletController) *WalletPrevOutputFetcher { // passed outpoint. A nil value will be returned if the passed outpoint doesn't // exist. func (w *WalletPrevOutputFetcher) FetchPrevOutput(op wire.OutPoint) *wire.TxOut { - utxo, err := w.wc.FetchInputInfo(&op) + utxo, err := w.wc.FetchOutpointInfo(&op) if err != nil { return nil }