Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

coin select: add coin selection strategy option to all on-chain RPCs #8515

Conversation

mohamedawnallah
Copy link
Contributor

@mohamedawnallah mohamedawnallah commented Mar 3, 2024

Change Description

This PR adds coin selection strategy option to the following on-chain RPCs FundPsbt, SendOutputs, BatchOpenChannel, SendCoins, SendMany, EstimateFee and add coin_selection_strategy optional parameter to lncli equivalent commands (if any).

Fixes #8498

Steps to Test

Steps for reviewers to follow to test the change.

Pull Request Checklist

Testing

  • Your PR passes all CI checks.
  • Tests covering the positive and negative (error paths) are included.
  • Bug fixes contain tests triggering the bug to prevent regressions.

Code Style and Documentation

📝 Please see our Contribution Guidelines for further guidance.

Copy link
Contributor

coderabbitai bot commented Mar 3, 2024

Important

Auto Review Skipped

Auto reviews are limited to the following labels: llm-review. Please add one of these labels to enable auto reviews.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository.

To trigger a single review, invoke the @coderabbitai review command.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@saubyk saubyk added coin selection psbt rpc Related to the RPC interface lncli chain handling cli Related to the command line interface labels Mar 3, 2024
Copy link
Contributor

@Chinwendu20 Chinwendu20 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice PR, needs release doc. I think your changes to the protobuf go files can be in the same commit where you made corresponding change to the proto file. Might help fix lint.

@mohamedawnallah mohamedawnallah force-pushed the add-coin-selection-strategy-on-chain-rpc branch from c27de4b to c819f7e Compare March 4, 2024 08:26
Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! There's a lot of duplicated code in the CLI, see my inline comments on how that can be optimized.

cmd/lncli/cmd_open_channel.go Outdated Show resolved Hide resolved
cmd/lncli/commands.go Outdated Show resolved Hide resolved
lnrpc/lightning.proto Outdated Show resolved Hide resolved
lnrpc/walletrpc/walletkit_server.go Outdated Show resolved Hide resolved
@mohamedawnallah mohamedawnallah force-pushed the add-coin-selection-strategy-on-chain-rpc branch from c819f7e to e0c07c5 Compare March 6, 2024 07:10
lnrpc/marshall_utils.go Outdated Show resolved Hide resolved
)

case CoinSelectionStrategy_STRATEGY_LARGEST:
coinSelectionStrategy = wallet.CoinSelectionLargest
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can return directly in the case statements, then no final return at the end of the function is needed (since there's a default case).

funding/batch_test.go Outdated Show resolved Hide resolved
lnrpc/lightning.proto Outdated Show resolved Hide resolved
lnrpc/marshall_utils.go Outdated Show resolved Hide resolved
lnrpc/marshall_utils.go Outdated Show resolved Hide resolved
@mohamedawnallah mohamedawnallah force-pushed the add-coin-selection-strategy-on-chain-rpc branch 3 times, most recently from cd85404 to 5eb6b28 Compare March 6, 2024 18:46
@mohamedawnallah mohamedawnallah requested a review from guggero March 6, 2024 18:54
@mohamedawnallah mohamedawnallah force-pushed the add-coin-selection-strategy-on-chain-rpc branch from 5eb6b28 to 142aa25 Compare March 6, 2024 19:03
@guggero
Copy link
Collaborator

guggero commented Mar 7, 2024

@mohamedawnallah can you please apply the fixup commits and rebase?

@lightninglabs-deploy
Copy link

@guggero: review reminder
@ellemouton: review reminder
@mohamedawnallah, remember to re-request review from reviewers when ready

Copy link
Collaborator

@ellemouton ellemouton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good @mohamedawnallah !

Left some comments :)


// MapCoinSelectionStrategy maps a coin selection strategy string to
// a wallet.CoinSelectionStrategy.
func MapCoinSelectionStrategy(strategy string) (wallet.CoinSelectionStrategy,
Copy link
Collaborator

@ellemouton ellemouton Mar 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont think we need this helper since we should only ever do this once.

lnrpc/lightning.proto Show resolved Hide resolved
// (lnd.conf).
STRATEGY_USE_GLOBAL_CONFIG = 0;

// Select the largest available coins first for spending.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest: "Select the largest available coins first during coin selection."

same below

Comment on lines 591 to 596
switch d.cfg.CoinSelectionStrategy {
case "largest":
walletConfig.CoinSelectionStrategy = wallet.CoinSelectionLargest

case "random":
walletConfig.CoinSelectionStrategy = wallet.CoinSelectionRandom

default:
return nil, nil, nil, fmt.Errorf("unknown coin selection "+
"strategy %v", d.cfg.CoinSelectionStrategy)
coinSelectionStrategy, err := lnrpc.MapCoinSelectionStrategy(
d.cfg.CoinSelectionStrategy,
)
if err != nil {
return nil, nil, nil, err
}
walletConfig.CoinSelectionStrategy = coinSelectionStrategy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can leave this as is. We only want to do this conversion from config string to wallet.CoinSelectionStrategy once. It won't ever change. Then everywhere else where we need to know the global config, we can use this already parsed wallet.CoinSelectionStrategy value instead of re-parsing the string.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sp perhaps all that is needed here is to make the wallet.CoinSelectionStrategy strategy more easily accessible from the rpcserver level

@@ -1145,6 +1162,10 @@ message SendManyRequest {

// Whether unconfirmed outputs should be used as inputs for the transaction.
bool spend_unconfirmed = 8;

// CoinSelectionStrategy is the strategy to use for selecting coins
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern of starting a comment with the name of the variable only applies to golang code btw. See the other examples here.

So this can just start with "The coin selection strategy to use ...."

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for other places here.

Usage: "(optional) the strategy to use for selecting " +
"coins to fund the PSBT from a template. " +
"If provided it will override the globally " +
"configured strategy in lnd.conf",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should mention which options are available.

Also, since this same flag is used in many places, might be worth extracting into a re-usable variable?

rpcserver.go Outdated
Comment on lines 1197 to 1202
strategy, err := lnrpc.MapCoinSelectionStrategy(
r.cfg.CoinSelectionStrategy,
)
if err != nil {
return nil, err
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not need to parse it again here

rpcserver.go Outdated
Comment on lines 1071 to 1072
feeRate chainfee.SatPerKWeight, minConfs int32,
label string) (*chainhash.Hash, error) {
label string,
strategy wallet.CoinSelectionStrategy) (*chainhash.Hash, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: things can still fit on 3 lines instead of 4:

func (r *rpcServer) sendCoinsOnChain(paymentMap map[string]int64,
	feeRate chainfee.SatPerKWeight, minConfs int32, label string,
	strategy wallet.CoinSelectionStrategy) (*chainhash.Hash, error) {

feeRate chainfee.SatPerKWeight, minConfs int32,
label string) (*wire.MsgTx, error) {
label string,
strategy base.CoinSelectionStrategy) (*wire.MsgTx, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func (b *BtcWallet) SendOutputs(outputs []*wire.TxOut,
	feeRate chainfee.SatPerKWeight, minConfs int32, label string,
	strategy base.CoinSelectionStrategy) (*wire.MsgTx, error) {

Comment on lines 247 to 248
* Add coin selection strategy option to the following on-chain RPC calls
`EstimateFee`, `SendMany`, `SendCoins`, `BatchOpenChannel`, `SendOutputs`, and `FundPsbt`. [coin select: add coin selection strategy option to all on-chain RPCs](https://github.com/lightningnetwork/lnd/pull/8515).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

notice that the other entries are wrapped at 80 chars :)

@mohamedawnallah mohamedawnallah force-pushed the add-coin-selection-strategy-on-chain-rpc branch 3 times, most recently from a923f7e to d0e74d9 Compare March 20, 2024 15:17
@mohamedawnallah
Copy link
Contributor Author

mohamedawnallah commented Mar 20, 2024

Thank you very much, @ellemouton, for your feedback! The idea of converting the CoinSelectionStrategy string to wallet.CoinSelectionStrategy only once and making it accessible across rpcserver level simplified the code significantly. Thanks for suggesting this! I've addressed all of your feedback :)

@mohamedawnallah mohamedawnallah force-pushed the add-coin-selection-strategy-on-chain-rpc branch from d0e74d9 to 3de1be9 Compare March 21, 2024 02:42
@mohamedawnallah
Copy link
Contributor Author

The latest change after asking for a review is related to the lining error solved by adding //nolint:funlen to fundPsbtCoinSelect:

lnrpc/walletrpc/walletkit_server.go:1474: Function 'fundPsbtCoinSelect' is too long (201 > 200) (funlen)

@ellemouton ellemouton removed the request for review from morehouse March 22, 2024 07:14
Copy link
Collaborator

@ellemouton ellemouton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK (tested via the FundPSBT flow) 🚀

Great work! A few more suggestions 🙏

cmd/lncli/cmd_open_channel.go Outdated Show resolved Hide resolved
Name: "coin_selection_strategy",
Usage: "(optional) the strategy to use for selecting " +
"coins. If provided, it will override the " +
"globally configured strategy in lnd.conf",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should contain a list of the available options

@mohamedawnallah mohamedawnallah force-pushed the add-coin-selection-strategy-on-chain-rpc branch from 3de1be9 to 62eb3f7 Compare March 22, 2024 09:07
Copy link
Collaborator

@ellemouton ellemouton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🚀 Great work!

Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very good now! Just a couple of small requests, then we're good to go.

config.go Outdated Show resolved Hide resolved
cmd/lncli/main.go Outdated Show resolved Hide resolved
lnrpc/walletrpc/walletkit_server.go Outdated Show resolved Hide resolved
rpcserver.go Outdated Show resolved Hide resolved
docs/release-notes/release-notes-0.18.0.md Outdated Show resolved Hide resolved
@mohamedawnallah mohamedawnallah force-pushed the add-coin-selection-strategy-on-chain-rpc branch 3 times, most recently from 8a71b33 to 8bc8f27 Compare March 26, 2024 17:13
Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last change required.

lnd.go Outdated Show resolved Hide resolved
@mohamedawnallah mohamedawnallah force-pushed the add-coin-selection-strategy-on-chain-rpc branch from 8bc8f27 to 9148022 Compare March 26, 2024 23:27
@mohamedawnallah mohamedawnallah requested a review from guggero March 27, 2024 01:23
In this commit, we add the coin selection strategy option
to all on-chain RPCs `FundPsbt`, `BatchOpenChannel`, `EstimateFee`,
`SendMany`, `SendCoins`, `SendOutputs`.
In this commit we add coin selection strategy option to the
following on-chain rpc commands `fundpsbt`, `fundtemplatepsbt`,
`batchopenchannel`, `estimatefee`, `sendcoins`, and `sendmany`.
In this commit, we add the coin selection strategy option to the following
on-chain RPCs `fundpsbt`, `batchopenchannel`, `estimatefee`, `sendcoins`,
`sendmany`, and `sendoutputs`.
@mohamedawnallah mohamedawnallah force-pushed the add-coin-selection-strategy-on-chain-rpc branch from 9148022 to 31526a0 Compare April 1, 2024 17:17
@mohamedawnallah
Copy link
Contributor Author

The latest push after asking for a review is just rebasing!

Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, LGTM 🎉

@guggero guggero merged commit b117551 into lightningnetwork:master Apr 2, 2024
25 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chain handling cli Related to the command line interface coin selection lncli psbt rpc Related to the RPC interface
Projects
None yet
Development

Successfully merging this pull request may close these issues.

coin select: add coin selection strategy option to all on-chain RPCs
6 participants