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

type-safe(r) GenTxid constructors #28658

Closed

Conversation

instagibbs
Copy link
Member

@instagibbs instagibbs commented Oct 16, 2023

Builds on #28107

Tiny(<30 loc) set of changes that detects issues with fuzz targets in master, and longer term should make things safer.

Most of this GenTxid::Txid(Txid::FromUint256()) chaining can be removed by further work pushing the types "up", and maybe other helper functions that make them directly out of COutPoints or similar since that appears to be a common pattern.

@DrahtBot
Copy link
Contributor

DrahtBot commented Oct 16, 2023

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

Code Coverage

For detailed information about the code coverage, see the test coverage report.

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:

  • #28970 ([WIP] p2p: opportunistically accept 1-parent-1-child packages by glozow)

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.

@@ -430,8 +430,8 @@ class GenTxid
GenTxid(bool is_wtxid, const uint256& hash) : m_is_wtxid(is_wtxid), m_hash(hash) {}

public:
static GenTxid Txid(const uint256& hash) { return GenTxid{false, hash}; }
static GenTxid Wtxid(const uint256& hash) { return GenTxid{true, hash}; }
Copy link
Contributor

Choose a reason for hiding this comment

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

Should GenTxid just become a std::variant<Txid,Wtxid> at this point? cf #28107 (comment)

In either case, these should be explicit GenTxid(const Txid& txid) constructors, afaics.

Copy link
Contributor

Choose a reason for hiding this comment

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

Could DRY it as

template<bool has_witness>
explicit GenTxid(const transaction_identifier<has_witness>& id) m_is_wtxid{has_witness}, m_hash{id.ToUint256()} {}

Copy link
Member Author

Choose a reason for hiding this comment

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

We could go the "whole way", would just change quite a bit of code. If that's what we want to do, sure.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think explicit GenTxid(...) requires changing a bunch of code. It is simply a constructor that can be used by new code, and old code can (for now) use the old methods to create the object.

Should be fine to add and use it, maybe as part of one of the follow-ups to convert more code?

@@ -705,7 +705,7 @@ class CTxMemPool
LOCK(cs);
// Sanity check the transaction is in the mempool & insert into
// unbroadcast set.
if (exists(GenTxid::Txid(txid))) m_unbroadcast_txids.insert(txid);
if (exists(GenTxid::Txid(Txid::FromUint256(txid)))) m_unbroadcast_txids.insert(txid);
Copy link
Contributor

Choose a reason for hiding this comment

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

Most of these changes just makes the code more verbose without making it any clearer or much safer, IMO -- this one would at least be clearer if it was changing the function to AddUnbroadcastTx(const Txid& txid)

Copy link
Member Author

Choose a reason for hiding this comment

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

Most of this GenTxid::Txid(Txid::FromUint256()) chaining can be removed by further work pushing the types "up"

Can do that if that's the feedback.

@DrahtBot
Copy link
Contributor

validation.cpp: In lambda function:
validation.cpp:365:70: error: ‘transaction_identifier<has_witness>::transaction_identifier(const uint256&) [with bool has_witness = false]’ is private within this context
  365 |                 if (m_mempool->exists(GenTxid::Txid(txin.prevout.hash))) continue;
      |                                                                      ^
In file included from ./primitives/transaction.h:14,
                 from ./primitives/block.h:9,
                 from ./chain.h:13,
                 from ./validation.h:15,
                 from validation.cpp:6:
./util/transaction_identifier.h:16:5: note: declared private here
   16 |     transaction_identifier(const uint256& wrapped) : m_wrapped{wrapped} {}
      |     ^~~~~~~~~~~~~~~~~~~~~~
make[2]: *** [Makefile:11211: libbitcoin_node_a-validation.o] Error 1
make[2]: Leaving directory '/ci_container_base/ci/scratch/build/bitcoin-x86_64-pc-linux-gnu/src'
make[1]: *** [Makefile:20226: install-recursive] Error 1
make[1]: Leaving directory '/ci_container_base/ci/scratch/build/bitcoin-x86_64-pc-linux-gnu/src'
make: *** [Makefile:812: install-recursive] Error 1

Exit status: 2������������������������

@instagibbs
Copy link
Member Author

up for grabs, opening #28997

@instagibbs instagibbs closed this Dec 4, 2023
fanquake added a commit to bitcoin-core/gui that referenced this pull request Dec 5, 2023
…d::Wtxid not GenTxid::Txid

38816ff fuzz: txorphan check wtxids using GenTxid::Wtxid not GenTxid::Txid (Greg Sanders)

Pull request description:

  Fixes the bugs in the fuzz test with no more changes as an alternative to bitcoin/bitcoin#28658

ACKs for top commit:
  naumenkogs:
    ACK 38816ff
  dergoegge:
    ACK 38816ff

Tree-SHA512: 5e46a83f2b2a2ac0672a63eb6200b019e01089ab1aa80c4ab869b6fcf27ccf2e84a064e96397f1a1869ccfa43b0c9638cbae681a27c4ca3c96ac71f41262601e
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.

None yet

6 participants