Skip to content

Conversation

jnewbery
Copy link
Contributor

@jnewbery jnewbery commented Oct 4, 2021

Similarly to how #18698 added ProcessNewBlock() and ProcessNewBlockHeaders() methods to the ChainstateManager class, this PR adds a new ProcessTransaction() method. Code outside validation no longer calls AcceptToMemoryPool() directly, but calls through the higher-level ProcessTransaction() method. Advantages:

  • The interface is simplified. Calling code no longer needs to know about the active chainstate or mempool object, since AcceptToMemoryPool() can only ever be called for the active chainstate, and that chainstate knows which mempool it's using. We can also remove the bypass_limits argument, since that can only be used internally in validation.
  • responsibility for calling CTxMemPool::check() is removed from the callers, and run automatically by ChainstateManager every time ProcessTransaction() is called.

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

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

Concept ACK

@DrahtBot
Copy link
Contributor

DrahtBot commented Oct 4, 2021

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #21527 (net_processing: lock clean up by ajtowns)
  • #21515 (Erlay: bandwidth-efficient transaction relay protocol by naumenkogs)

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.

@jamesob
Copy link
Contributor

jamesob commented Oct 4, 2021

Concept ACK, looks like a good change. Will review soon.

@jnewbery jnewbery force-pushed the 2021-09-process-transaction branch from 87a286d to 58538ff Compare October 4, 2021 16:08
@jnewbery
Copy link
Contributor Author

jnewbery commented Oct 4, 2021

Resolved @MarcoFalke's review comment and added a missing lock annotation.

Copy link
Member

@glozow glozow left a comment

Choose a reason for hiding this comment

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

Concept ACK

@practicalswift
Copy link
Contributor

Concept ACK

@jnewbery jnewbery force-pushed the 2021-09-process-transaction branch from 58538ff to 6b2c8c8 Compare October 7, 2021 09:50
@jnewbery
Copy link
Contributor Author

jnewbery commented Oct 7, 2021

Fixed up an error string and rebased on master to (hopefully) pick up fixes for unrelated CI failures. The fuzz test will still fail, so I'll try to fix that next.

@jnewbery jnewbery force-pushed the 2021-09-process-transaction branch from 6b2c8c8 to 1d20d43 Compare October 7, 2021 13:58
@jnewbery
Copy link
Contributor Author

jnewbery commented Oct 7, 2021

I've reverted the change that made AcceptToMemoryPool() static and left it as dynamically linkable for use by the mempool fuzz test (which was operating on a separate CChainState and CTxMemPool).

Copy link
Member

@glozow glozow left a comment

Choose a reason for hiding this comment

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

Did a first pass-through. I like that this re-delegates the responsibility of calling mempool.check() from net_processing to chainstate manager, and see it as the main purpose of this refactoring.

Copy link
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

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

Concept ACK.

Light code review ACK 1d20d43.

@jnewbery jnewbery force-pushed the 2021-09-process-transaction branch from 1d20d43 to 3bb34f0 Compare October 19, 2021 17:09
@jnewbery
Copy link
Contributor Author

Thanks for the reviews @glozow and @promag. I've addressed all review comments.

Copy link
Member

@glozow glozow left a comment

Choose a reason for hiding this comment

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

code review ACK 3bb34f0

A few small suggestions if you retouch. The comment touchups sprinkled throughout the commits make the diffs a little noisy.

AcceptToMemoryPool() is called for transactions with fees above
minRelayTxFee and with the mempool not full, so setting bypass_limits to
true or false has no impact on the test.

The only way that changing bypass_limits from true to false could change
the result would be to change the outcome to INVALID(TX_MEMPOOL_POLICY).
Since all the ATMP calls in this test result in VALID both before and
after this change, there is no change in behavior.
AcceptToMemoryPool() is called for an invalid coinbase transaction, so
setting bypass_limits to true or false has no impact on the test.

The only way that changing bypass_limits from true to false could change
the result would be to change the outcome to INVALID(TX_MEMPOOL_POLICY).
Since the ATMP call in this test results in INVALID(TX_CONSENSUS) both
before and after this change, there is no change in behavior.
"This logic is not necessary for memory pool transactions, as
AcceptToMemoryPool already refuses previously-known transaction ids
entirely." refers to the logic at
https://github.com/bitcoin/bitcoin/blob/a206b0ea12eb4606b93323268fc81a4f1f952531/src/main.cpp#L484-L486,
which was later removed in commit 450cbb0.
…rror string

User-facing error messages should not leak internal implementation
details like function names. Update the MEMPOOL_REJECTED error string
from "Transaction rejected by AcceptToMemoryPool" to the more generic
"Transaction rejected by mempool". Also update the MEMPOOL_ERROR error
message from "AcceptToMemoryPool failed" to the more precise "Mempool
internal error" since this error indicates and internal (e.g.
logic/hardware/etc) failure, and not a transaction rejection.
This just calls through to AcceptToMemoryPool() internally, and is currently unused.

Also add a new transaction validation failure reason TX_NO_MEMPOOL to
indicate that there is no mempool.
…action

CTxMemPool::check() will carry out internal consistency checks 1/n times,
where n is set by the `-checkmempool` configuration option. By default,
mempool consistency checks are disabled entirely on mainnet.

Therefore, this change has no effect on mainnet nodes running with
default configuration. It simply removes the responsibility to trigger
mempool consistency checks from net_processing.
@jnewbery jnewbery force-pushed the 2021-09-process-transaction branch from f87e07c to 0fdb619 Compare November 3, 2021 14:57
@jnewbery
Copy link
Contributor Author

jnewbery commented Nov 3, 2021

Thanks for the thorough review @ryanofsky! I believe I've addressed all of your review comments.

Copy link
Contributor

@stickies-v stickies-v left a comment

Choose a reason for hiding this comment

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

2c64270 seems to indicate that all AcceptToMemoryPool() calls from outside validation.cpp/h are removed, but it appears there are still two references left. Is this intentional?

@jnewbery
Copy link
Contributor Author

jnewbery commented Nov 3, 2021

2c64270 seems to indicate that all AcceptToMemoryPool() calls from outside validation.cpp/h are removed, but it appears there are still two references left. Is this intentional?

Yes, see the AcceptToMemoryPool doxygen comment:

 * Try to add a transaction to the mempool. This is an internal function and is
 * exposed only for testing. Client code should use ChainstateManager::ProcessTransaction()

Those two remaining calls to ATMP are the tests that are referenced in that comment. They can't use the ChainstateManager::ProcessTransaction() interface function since they're testing a mempool that isn't owned by a CChainState.

Copy link
Contributor

@lsilva01 lsilva01 left a comment

Choose a reason for hiding this comment

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

tACK 0fdb619 on Ubuntu 20.04

This PR successfully removes calls to AcceptToMemoryPool() from the net_processing layer and allocates them to the validation layer (in the new ProcessTransaction method and in ActivateBestChainStep).

It also adds a mempool.check() call after processing a new transaction. It has no effect on the default setting as m_check_ratio will be 0 and CTxMemPool ::check() will return immediately.

txvalidation_tests and txvalidationcache_tests ran successfully after changing bypass_limits from true to false.

Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

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

Code-review ACK 0fdb619

Nice improvement! Happy to see a more consistent higher-level interface for adding transactions to the mempool via ChainstateManager, with an automatic CTxMemPool::check() included (which has no effect on mainnet by default, i.e. possible performance losses don't affect users).

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

Code review ACK 0fdb619. Only changes since last review: splitting & joining commits, adding more explanations to commit messages, tweaking MEMPOOL_ERROR string, fixing up argument name comments.

This might be ready for merge. It's also a pretty straightforward review if anyone else wants to take a look.

@maflcko maflcko merged commit 38b2a0a into bitcoin:master Nov 10, 2021
@jnewbery jnewbery deleted the 2021-09-process-transaction branch November 10, 2021 13:55
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Nov 10, 2021
maflcko pushed a commit that referenced this pull request Dec 1, 2021
123f5de Remove calls to global Params() in tx_pool test (lsilva01)
9360778 Remove AcceptToMemoryPoolWithTime (lsilva01)

Pull request description:

  This PR refactors AcceptToMemoryPool.

  . Remove `AcceptToMemoryPoolWithTime` (after #23173, this function is no longer needed).
  . Remove the `CChainParams chainparams` parameter from ATMP as they can be inferred from the current chain state.
  . Update the `tx_pool` test with new function signature.

ACKs for top commit:
  jnewbery:
    reACK 123f5de
  glozow:
    light code review ACK 123f5de
  theStack:
    re-ACK 123f5de

Tree-SHA512: b672d9f091f5fcb4d163c0a443aa469b2ce6f37201136803201c620c3fa329ffb41abd50c5b528787fd1c47e8e09af5ec20ddaa0a4235352c9c619e5691dddb6
maflcko pushed a commit to bitcoin-core/gui that referenced this pull request Dec 8, 2021
f1f10c0 Remove CTxMemPool params from ATMP (lsilva01)

Pull request description:

  Remove `CTxMemPool` parameter from `AcceptToMemoryPool` function, as suggested in bitcoin/bitcoin#23437 (comment) .

  This requires that `CChainState` has access to `MockedTxPool` in  `tx_pool.cpp` as mentioned bitcoin/bitcoin#23173 (comment). So the `MockedTxPool` is attributed to `CChainState::m_mempool` before calling `AcceptToMemoryPool`.

  Requires #23437.

ACKs for top commit:
  jnewbery:
    utACK f1f10c0
  MarcoFalke:
    review ACK f1f10c0 🔙

Tree-SHA512: 2a4885f4645014fc1fa98bb1090f13721c1a0796bc0021b9cb43bc8cc13920b6eaf057d1f5ed796e0a110e7813e41fe0196334ce7c80d1231fc057a9a3bdf349
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Dec 8, 2021
f1f10c0 Remove CTxMemPool params from ATMP (lsilva01)

Pull request description:

  Remove `CTxMemPool` parameter from `AcceptToMemoryPool` function, as suggested in bitcoin#23437 (comment) .

  This requires that `CChainState` has access to `MockedTxPool` in  `tx_pool.cpp` as mentioned bitcoin#23173 (comment). So the `MockedTxPool` is attributed to `CChainState::m_mempool` before calling `AcceptToMemoryPool`.

  Requires bitcoin#23437.

ACKs for top commit:
  jnewbery:
    utACK f1f10c0
  MarcoFalke:
    review ACK f1f10c0 🔙

Tree-SHA512: 2a4885f4645014fc1fa98bb1090f13721c1a0796bc0021b9cb43bc8cc13920b6eaf057d1f5ed796e0a110e7813e41fe0196334ce7c80d1231fc057a9a3bdf349
RandyMcMillan pushed a commit to RandyMcMillan/mempool-tab that referenced this pull request Dec 23, 2021
efbb49e Remove CTxMemPool params from ATMP (lsilva01)

Pull request description:

  Remove `CTxMemPool` parameter from `AcceptToMemoryPool` function, as suggested in bitcoin/bitcoin#23437 (comment) .

  This requires that `CChainState` has access to `MockedTxPool` in  `tx_pool.cpp` as mentioned bitcoin/bitcoin#23173 (comment). So the `MockedTxPool` is attributed to `CChainState::m_mempool` before calling `AcceptToMemoryPool`.

  Requires #23437.

ACKs for top commit:
  jnewbery:
    utACK efbb49e
  MarcoFalke:
    review ACK efbb49e 🔙

Tree-SHA512: 2a4885f4645014fc1fa98bb1090f13721c1a0796bc0021b9cb43bc8cc13920b6eaf057d1f5ed796e0a110e7813e41fe0196334ce7c80d1231fc057a9a3bdf349
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 13, 2022
Summary:
> [test] Don't set bypass_limits to true in txvalidatiocache_tests.cpp
>
> AcceptToMemoryPool() is called for transactions with fees above
> minRelayTxFee and with the mempool not full, so setting bypass_limits to
> true or false has no impact on the test.
>
> The only way that changing bypass_limits from true to false could change
> the result would be to change the outcome to INVALID(TX_MEMPOOL_POLICY).
> Since all the ATMP calls in this test result in VALID both before and
> after this change, there is no change in behavior.

bitcoin/bitcoin@497c9e2

> [test] Don't set bypass_limits to true in txvalidation_tests.cpp
>
> AcceptToMemoryPool() is called for an invalid coinbase transaction, so
> setting bypass_limits to true or false has no impact on the test.
>
> The only way that changing bypass_limits from true to false could change
> the result would be to change the outcome to INVALID(TX_MEMPOOL_POLICY).
> Since the ATMP call in this test results in INVALID(TX_CONSENSUS) both
> before and after this change, there is no change in behavior.

This is a backport of [[bitcoin/bitcoin#23173 | core#23173]]
bitcoin/bitcoin@5759fd1

Test Plan: `ninja check`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D12232
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 13, 2022
Summary:
> "This logic is not necessary for memory pool transactions, as
> AcceptToMemoryPool already refuses previously-known transaction ids
> entirely." refers to the logic at
> https://github.com/bitcoin/bitcoin/blob/a206b0ea12eb4606b93323268fc81a4f1f952531/src/main.cpp#L484-L486,
> which was later removed in commit rABC450cbb0944cd20a06ce806e6679a1f4c83c50db2.

This is a backport of [[bitcoin/bitcoin#23173 | core#23173]]
bitcoin/bitcoin@4c24142

Test Plan: NA

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D12233
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 13, 2022
…rror string

Summary:
User-facing error messages should not leak internal implementation
details like function names. Update the MEMPOOL_REJECTED error string
from "Transaction rejected by AcceptToMemoryPool" to the more generic
"Transaction rejected by mempool". Also update the MEMPOOL_ERROR error
message from "AcceptToMemoryPool failed" to the more precise "Mempool
internal error" since this error indicates and internal (e.g.
logic/hardware/etc) failure, and not a transaction rejection.

These error messages are not covered by tests because they are overwritten with more precise error messages from the ATMP validation state in `sendrawtransaction`.

This is a backport of [[bitcoin/bitcoin#23173 | core#23173]]
bitcoin/bitcoin@36167fa

Test Plan: `ninja all check check-functional-extended`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D12234
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 13, 2022
…outside validation

Summary:
Also add a new transaction validation failure reason TX_NO_MEMPOOL to indicate that there is no mempool.

This is a backport of [[bitcoin/bitcoin#23173 | core#23173]]
bitcoin/bitcoin@92a3aee
bitcoin/bitcoin@2c64270

Note: due to D1667, there are calls to AcceptToMemoryPool in `DisconnectedBlockTransactions::updateMempoolForReorg` that cannot access the chain manager.
However `updateMempoolForReorg` is only called from within validation.cpp, so it should not be a problem.

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D12235
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 13, 2022
…action

Summary:
CTxMemPool::check() will carry out internal consistency checks 1/n times,
where n is set by the `-checkmempool` configuration option. By default,
mempool consistency checks are disabled entirely on mainnet.

Therefore, this change has no effect on mainnet nodes running with
default configuration. It simply removes the responsibility to trigger
mempool consistency checks from net_processing.

This concludes a backport of [[bitcoin/bitcoin#23173 | core#23173]]
bitcoin/bitcoin@0fdb619

Depends on D12235

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D12236
@bitcoin bitcoin locked and limited conversation to collaborators Nov 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.