Skip to content

refactor: Enforces Txid and Wtxid types in RelayTransaction#32104

Closed
sr-gi wants to merge 1 commit into
bitcoin:masterfrom
sr-gi:2025-03-refactor-process-tx
Closed

refactor: Enforces Txid and Wtxid types in RelayTransaction#32104
sr-gi wants to merge 1 commit into
bitcoin:masterfrom
sr-gi:2025-03-refactor-process-tx

Conversation

@sr-gi

@sr-gi sr-gi commented Mar 20, 2025

Copy link
Copy Markdown
Member

RelayTransaction expects to pass a TxId and Wtxid as arguments, but any uint256 type is really accepted. When calling RelayTransaction, most times we are already passing tx->GetHash() and tx->GetWitnessHash() to it, and their value are downcasted.

Updates RelayTransaction and make sure we are consistent with its arguments

@DrahtBot

DrahtBot commented Mar 20, 2025

Copy link
Copy Markdown
Contributor

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

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/32104.

Reviews

See the guideline for information on the review process.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #32189 (refactor: Txid type safety (parent PR) by marcofleon)
  • #30277 ([DO NOT MERGE] Erlay: bandwidth-efficient transaction relay protocol (Full implementation) by sr-gi)
  • #30116 (p2p: Fill reconciliation sets (Erlay) attempt 2 by sr-gi)
  • #29641 (scripted-diff: Use LogInfo over LogPrintf [WIP, NOMERGE, DRAFT] by maflcko)
  • #29415 (Broadcast own transactions only via short-lived Tor or I2P connections by vasild)

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.

Comment thread src/net_processing.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.

not sure. This seems like a pessimization that makes it harder to use the type-safe Txid and Wtxid in RelayTransaction

It would be better to make this code type-safe, to avoid having to touch it again in the future.

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.

If that is the case, shouldn't we be enforcing RelayTransaction to take Txid and Wtxid instead of uint256?

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.

Yes. Alternatively, at a minimum the txid and wtxid types should be adjusted here. Otherwise, this refactor may cause a silent merge conflict with the type-safety refactor.

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've updated the approach to enforce RelayTransaction types, and extended it to all other calls to the method

@Jassor909 Jassor909 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@sr-gi sr-gi force-pushed the 2025-03-refactor-process-tx branch 2 times, most recently from 4871540 to a5ddb59 Compare March 20, 2025 18:09
@sr-gi sr-gi changed the title refactor: Simplifies ProcessMessage for NetMsgType::TX refactor: Enforces Txid and Wtxid types in RelayTransactio Mar 20, 2025
@sr-gi sr-gi changed the title refactor: Enforces Txid and Wtxid types in RelayTransactio refactor: Enforces Txid and Wtxid types in RelayTransaction Mar 20, 2025
RelayTransaction expects to pass a TxId and Wtxid as arguments, but
any uint256 type is really accepted. When calling RelayTransaction, most times
we are already passing tx->GetHash() and  tx->GetWitnessHash() to it, and their
value are downcasted.

Updates RelayTransaction and make sure we are consistent with its arguments
@sr-gi sr-gi force-pushed the 2025-03-refactor-process-tx branch from a5ddb59 to 1b58696 Compare March 20, 2025 18:11

@Jassor909 Jassor909 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@fanquake

Copy link
Copy Markdown
Member

cc @marcofleon & #31001.

@sr-gi

sr-gi commented Mar 21, 2025

Copy link
Copy Markdown
Member Author

Oh, I didn't realize Marco was already working on this. I saw this when updating the Erlay PR and thought it may be good to standalone fix. I'm happy to close it if you're planning to get it addressed @marcofleon

@marcofleon

Copy link
Copy Markdown
Contributor

Lgtm as a standalone change. It's the same change I implement in fa8400494e3c411a55375e71abaaa2637a22e216 as part of the complete refactor. I actually missed all the tx.GetHash() and tx.GetWitnessHash() in ProcessMessage so good catch there.

My final refactor does end up altering RelayTransaction in a more involved way, but that includes changes (switching GenTxid to a variant) that haven't been discussed yet. I'll open a draft PR of that branch this week.

@sr-gi

sr-gi commented Apr 10, 2025

Copy link
Copy Markdown
Member Author

Closing in favor of #32189

@sr-gi sr-gi closed this Apr 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants