Skip to content

wallet: skip R-value signature grinding for external signers - #26032

Merged
achow101 merged 2 commits into
bitcoin:masterfrom
Sjors:2022/09/external-signer-feerate
Feb 27, 2023
Merged

achow101 merged 2 commits into
bitcoin:masterfrom
Sjors:2022/09/external-signer-feerate

Conversation

@Sjors

@Sjors Sjors commented Sep 7, 2022 •

Copy link
Copy Markdown
Member

When producing a dummy signature for the purpose of estimating the transaction fee, do not assume an external signer performs R-value grinding on the signature.

In particular, this avoids a scenario where the fee rate is 1 sat / vbyte and a transaction with a 72 byte signature is not accepted into our mempool.

Suggested testing:

  1. On master, launch with -signet and create an external signer wallet using e.g. a Trezor and HWI, see guide (with the GUI it should "just work" once you have the HWI path configured).

  2. Create a few addresses and fund them from the faucet: https://signet.bc-2.jp/ (wait for confirmation)

  3. Create another address, and now send the entire wallet to it, set the fee to 1 sat/byte

  4. Most likely this transaction never gets broadcast and you won't see it on the signet explorer

  5. With this PR, try again.

  6. Check the explorer and inspect the transaction. Each input witness starts with either 30440220 (R has 32 bytes) or 30440221 (R has 33 bytes). See this explainer for DER encoding.

Fixes #26030

@fanquake fanquake added the Wallet label Sep 7, 2022
@DrahtBot

DrahtBot commented Sep 8, 2022 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK furszy, ishaanam, S3RK, achow101

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #26728 (wallet: Have the wallet store the key for automatically generated descriptors by achow101)
  • #26152 (Bump unconfirmed ancestor transactions to target feerate by Xekyo)
  • #26066 (wallet: Refactor and document CoinControl by aureleoules)
  • #26008 (wallet: cache IsMine scriptPubKeys to improve performance of descriptor wallets by achow101)
  • #25907 (wallet: rpc to add automatically generated descriptors by achow101)
  • #22341 (rpc: add getxpub by Sjors)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@Sjors

Sjors commented Sep 13, 2022

Copy link
Copy Markdown
Member Author

Added a release note.

@Sjors
Sjors force-pushed the 2022/09/external-signer-feerate branch from 9558c8a to a58038a Compare September 13, 2022 07:15
@DrahtBot DrahtBot mentioned this pull request Sep 14, 2022
@luke-jr

luke-jr commented Sep 20, 2022

Copy link
Copy Markdown
Contributor

A functional test would be nice

@luke-jr luke-jr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder if it would make sense to move grind to be an attribute of (or at least method of) the SigningProvider?

Comment thread src/psbt.cpp Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: grind is a bit unclear, maybe require_optimal_signature_size or !use_max_sig (like in wallet_tests)?

Comment thread src/wallet/scriptpubkeyman.cpp Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

grind gets ignored here. What if a watch-only input is used, and the signer for it can't grind?

@ishaanam ishaanam left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Concept ACK, but I am a bit confused about the approach so I've left a question below.

Comment thread src/psbt.cpp Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It seems like the dummy signature produced here isn't used for fee estimation, but for figuring out what signatures that the psbt is missing (for use when analyzing a psbt). It looks like this behavior was introduced in cb40b3a.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@achow101 I could use some hints here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Size estimation occurs in CreateTransaction, inside of SelectCoins and CalculateMaximumSignedInputSize. FillPSBT and the functions it calls shouldn't be touched.

@Sjors

Sjors commented Sep 22, 2022

Copy link
Copy Markdown
Member Author

A functional test would be nice

Indeed, but I'm not sure how. Can our test suite produce ungrinded signatures?

@luke-jr

luke-jr commented Sep 22, 2022

Copy link
Copy Markdown
Contributor

Worst case, use a hardcoded descriptor and signatures?

@Sjors

Sjors commented Sep 23, 2022 •

Copy link
Copy Markdown
Member Author

@luke-jr in that case the entire regtest chain needs to be deterministic, otherwise you're spending non-existing coins. But IIRC it is, so I'll take another look at that. Does seem a bit brittle.

@Sjors

Sjors commented Sep 23, 2022 •

Copy link
Copy Markdown
Member Author

I changed the approach. DummySignInput now checks (the new) coin_control->m_external_signer to determine use_max_sig. This is much simpler.

Since it piggybacks on the logic for external inputs I think a functional test is less necessary?

@Sjors
Sjors force-pushed the 2022/09/external-signer-feerate branch 3 times, most recently from bb5b170 to b133ab9 Compare September 23, 2022 14:54
Comment thread src/wallet/coincontrol.h Outdated
Comment on lines 63 to 64

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

will be*

But I don't think this is a good name for the flag. It's quite possible external signers might support grinding, and will want a way to optimise fees too?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I could call it m_can_grind_r and default to true? And then for external signers we set it to false until we have a way (e.g. via HWI) to communicate this capability. That said, I don't know if anyone will implement it, since Taproot doesn't have this problem.

Comment thread src/wallet/spend.cpp Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No need to change this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not really, but accessing both the original coin_control and tmp_cc seems like asking for future regressions.

@luke-jr

luke-jr commented Sep 24, 2022

Copy link
Copy Markdown
Contributor

Also, please don't rebase when there's no reason to. (If you could rebase back, it'd be nice.)

@Sjors

Sjors commented Sep 26, 2022

Copy link
Copy Markdown
Member Author

please don't rebase when there's no reason to

Given how much refactoring is (still) going on in the wallet, not rebasing increases the risk of a silent merge conflict. Although in this case back-porting might be very useful, so I'll see if I can move it back to something a bit older.

@furszy furszy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Instead of adding the m_external_signer flag to coin control, what about passing wallet.IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER) to the DummySignInput function? (or pass the wallet ref and call IsWalletFlagSet internally).

I think that we should keep the coin control object as a container for user modifiable parameters. And not start using it to pass wallet internal flags across functions.

@Sjors

Sjors commented Sep 28, 2022

Copy link
Copy Markdown
Member Author

@furszy there's three call sites to DummySignInput. Two are in CWallet::DummySignTx which has direct access to IsWalletFlagSet(). The third is in CalculateMaximumSignedInputSize (wallet/spend.cpp) which only has access to a SigningProvider.

I could expand SigningProvider with m_can_grind_r and CanGrindR(). That seems the right place for it, but that might touch a lot of code (haven't tried).

The easier way is to add an can_grind_r argument to CalculateMaximumSignedInputSize. All it's call sites (outside of tests & bench at least) are in coin_selection.cpp and spend.cpp and have access to a CWallet.

@furszy

furszy commented Sep 29, 2022

Copy link
Copy Markdown
Member

I could expand SigningProvider with m_can_grind_r and CanGrindR(). That seems the right place for it, but that might touch a lot of code (haven't tried).

yeah, SigningProvider seems to be the right place for it.

The easier way is to add an can_grind_r argument to CalculateMaximumSignedInputSize. All it's call sites (outside of tests & bench at least) are in coin_selection.cpp and spend.cpp and have access to a CWallet.

Would look like this furszy@dccb120. Not that many changes.

Plus, could also include the unused CWallet::DummySignTx removal too: ce81b9a

@achow101

Copy link
Copy Markdown
Member

ACK b133ab9b1ee3c824cfc2ca29e3cfc7054377a481

I could expand SigningProvider with m_can_grind_r and CanGrindR(). That seems the right place for it, but that might touch a lot of code (haven't tried).

I think that would be a layer violation. The SigningProvider does not actually do any signing at all, it just provides information that is useful for signing. A SigningProvider could be providing information that pertains to both internal and external outputs, in which case the method of signing differs for each input.

@Sjors

Sjors commented Oct 27, 2022

Copy link
Copy Markdown
Member Author

I think that would be a layer violation.

I'll leave this PR as-is then. We can always refactor things.

@S3RK

S3RK commented Oct 31, 2022

Copy link
Copy Markdown
Contributor

Concept ACK. This PR is a clearly an improvement from users' perspective.

Approach wise, I don't like that CCoinControl is becoming bloated collection of loosely related parameters. We already have 15 public members there. And from my point of view, ability to grind signatures has nothing to do with coin control.

I reviewed both this PR and the alternative approach and I like the second option better. You can make it even simpler like this S3RK/bitcoin@8f11705

@Sjors

Sjors commented Feb 22, 2023 •

Copy link
Copy Markdown
Member Author

I considered switching to @S3RK's approach of adding extra argument to DummySignInput rather than expanding CCoinControl. The result is here: https://github.com/Sjors/bitcoin/tree/2023/02/external-signer-feerate-leave-ccoincontrol-alone

The downside of this is approach is that I had to sprinkle const bool can_grind_r = !wallet->IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER); in several different places. @furszy's commit does it in three places. This is fine right now, but at some point we might have some external signers that do support grinding. We'd then have to update the check everywhere. So although CCoinControl is not really meant to keep track of this, it's the most convenient place.

So I'm keeping the original commit for now.

@S3RK

S3RK commented Feb 23, 2023

Copy link
Copy Markdown
Contributor

The downside of this is approach is that I had to sprinkle const bool can_grind_r = !wallet->IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER); in several different places. @furszy's commit does it in three places. This is fine right now, but at some point we might have some external signers that do support grinding. We'd then have to update the check everywhere.

What if we encapsulate the logic in Wallet::CanGrindR()?

The ability to grind signatures is a characteristic of a wallet IIUC. So that should be pretty future proof.

@Sjors
Sjors force-pushed the 2022/09/external-signer-feerate branch from b133ab9 to 302f07e Compare February 23, 2023 11:05
@Sjors

Sjors commented Feb 23, 2023

Copy link
Copy Markdown
Member Author

@S3RK done!

@furszy furszy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ACK 302f07ee
Looking quite straightforward now.

Comment thread src/wallet/spend.cpp Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

tiny nit:
could cache this result into a variable outside of the loop (after the only_safe variable)

@Sjors Sjors Feb 23, 2023 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good point, I introduced a const bool in both all three places where it's used in a loop.

@Sjors Sjors Feb 23, 2023 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Though perhaps it's overkill, because IsWalletFlagSet is just m_wallet_flags & flag and not a database lookup.

@furszy furszy Feb 23, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

yeah, I think that the only place that worth is inside AvailableCoins. And not only because of the overhead that introduces (it's one "AND" operation on an atomic variable per wallet UTXO), I was more thinking about the code structure and what we should do if we want to decrease the cs_wallet lock contention there.

still, this is a tiny nit from a person that is thinking about future stuff.

When producing a dummy signature for the purpose of estimating the transaction fee, do not assume an external signer performs R-value grinding on the signature.

In particular, this avoids a scenario where the fee rate is 1 sat / vbyte and a transaction with a 72 byte signature is not accepted into our mempool.

This commit also  drops the nullptr default for CCoinControl arguments for functions that it touches. This is because having a boolean argument right next to an optional pointer is error prone.

Co-Authored-By: S3RK <[email protected]>

@furszy furszy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ACK 807de2c

@ishaanam ishaanam left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

utACK 807de2c

Comment thread src/wallet/wallet.cpp Outdated
Comment on lines 1560 to 1652

@ishaanam ishaanam Oct 5, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: this comment can be updated

Suggested change
// Use max sig if watch only inputs were used or if this particular input is an external input
// to ensure a sufficient fee is attained for the requested feerate.
// Use max sig if watch only inputs were used, if this particular input is an external input,
// or if this wallet uses an external signer, to ensure a sufficient fee is attained for the requested feerate.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added to a followup PR #27180.

@S3RK

S3RK commented Feb 27, 2023

Copy link
Copy Markdown
Contributor

ACK 807de2c

Thanks your patience and addressing the feedback.

@achow101

Copy link
Copy Markdown
Member

ACK 807de2c

@achow101
achow101 merged commit 710cab1 into bitcoin:master Feb 27, 2023
@Sjors
Sjors deleted the 2022/09/external-signer-feerate branch March 1, 2023 09:03
fanquake added a commit that referenced this pull request Mar 8, 2023
6fc5f4f doc: DummySignInput mention external signer (Sjors Provoost)

Pull request description:

  Followups for #26032. So far nothing major.

ACKs for top commit:
  ishaanam:
    ACK 6fc5f4f
  S3RK:
    ACK 6fc5f4f

Tree-SHA512: e27edde9853487fe3eef8213f991aae3724f318bbbe0b11da23759879adaf9a31771e6ea0c30baaebca149032780b89b32aa540ff456ca3d5ec6adb0371749c6
@bitcoin bitcoin locked and limited conversation to collaborators Feb 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Don't assume signature grinding for external signers

8 participants