Skip to content

Conversation

Sjors
Copy link
Member

@Sjors Sjors commented Feb 14, 2024

Based on on @Fi3's master...Fi3:bitcoin:PatchTemplates which is based on @ccdle12's #27854. I rebased it and re-wrote the commit history. Compared to #28983 it introduces EllSwift in the handshake and fixes various bugs. I used that opportunity to change the branch name, which makes testing against SRI slightly easier. There's no conceptual discussion on #28983 so it can be ignored by reviewers.

See docs/stratum-v2.md for a brief description of Stratum v2 and the role of Bitcoin Core in that system..

There's roughly four layers:

  1. Noise encryption Stratum v2 Noise Protocol #29346
  2. Messages and transport layer Stratum v2 Transport #30315
  3. Sv2Connman Stratum v2 connman Sjors/bitcoin#50
  4. The Template Provider Stratum v2 Template Provider common functionality Sjors/bitcoin#49
  5. Starting the Template Provider from init, and misc stuff (this PR)

What to test and review?

See the testing guide for various ways to test this PR. This branch is actively used by (testnet) pools, so it should be ready for high level review.

I split this PR into separate pull requests for easier review. Feedback on each of them is welcome, even if they're stacked on another one.

Even if we abandon the approach here in favor of a sidecar (see experimental below), it is useful to review the above changes. They are fully reusable as demonstrated in Sjors#48. And until we actually ship something else, the above is being used by people, including on (very limited) mainnet.

The pull requests below enable a sidecar approach:

These are needed with any approach:

Exploratory / experimental:

Related useful:

Contributing

If you want to help out with any of the issues below, please open a PR to my fork. I will then squash your commits into my own where needed.

Things left todo

Networking

  • add -sv2allowip
  • optional -sv2cert
  • limit number of connected clients

Template generation and updating

  • group templates with the same coinbase_tx_additional_output_size

Misc

Potential followups

@DrahtBot
Copy link
Contributor

DrahtBot commented Feb 14, 2024

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.

Type Reviewers
Approach NACK stickies-v, dergoegge

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:

  • #30679 (fix: handle invalid -rpcbind port earlier by tdb3)
  • #30635 (rpc: add optional blockhash to waitfornewblock by Sjors)
  • #30595 (kernel: Introduce initial C header API by TheCharlatan)
  • #30546 (util: Use consteval checked format string in FatalErrorf by maflcko)
  • #30487 (ci: skip Github CI on branch pushes for forks by Sjors)
  • #30440 (Have createNewBlock() return a BlockTemplate interface by Sjors)
  • #30409 (Introduce waitTipChanged() mining interface, replace RPCNotifyBlockChange, drop CRPCSignals & g_best_block by Sjors)
  • #30157 (Fee Estimation via Fee rate Forecasters by ismaelsadeeq)
  • #30130 (contrib/signet/miner: increase miner search space by edilmedeiros)
  • #29838 (Feature: Use different datadirs for different signets by BrandonOdiwuor)
  • #29415 (Broadcast own transactions only via short-lived Tor or I2P connections by vasild)
  • #29039 (versionbits refactoring by ajtowns)
  • #28417 (contrib/signet/miner updates by ajtowns)
  • #27433 (getblocktemplate improvements for segwit and sigops by Sjors)
  • #26812 (test: add end-to-end tests for CConnman and PeerManager by vasild)
  • #26697 (logging: use bitset for categories by LarryRuane)
  • #26619 (log: expand BCLog::LogFlags (categories) to 64 bits by LarryRuane)

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.

@DrahtBot
Copy link
Contributor

🚧 At least one of the CI tasks failed. Make sure to run all tests locally, according to the
documentation.

Possibly this is due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.

Leave a comment here, if you need help tracking down a confusing failure.

Debug: https://github.com/bitcoin/bitcoin/runs/21562393655

@Sjors Sjors force-pushed the sv2 branch 3 times, most recently from 87f0527 to 684890f Compare February 14, 2024 14:15
{
bool started = m_tp->Start(Sv2TemplateProviderOptions { .port = 18447 });
if (! started) return false;
// Avoid "Connection refused" on CI:
Copy link
Member Author

Choose a reason for hiding this comment

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

The template provider tests are quite brittle because they use a real socket.

Copy link
Member Author

Choose a reason for hiding this comment

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

For the time being I just added handling for MSG_MORE (on e.g. macOS sequential messages are sent separately while on Linux they're combined). I also made the timeouts a bit longer.

Hopefully that does the trick. This can be revisited closer to the time when the Template Provider is ready for its own PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Working on a fix in Sjors#34

Copy link
Member Author

Choose a reason for hiding this comment

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

I should probably look into using StaticContentsSock

Copy link
Member Author

Choose a reason for hiding this comment

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

@vasild any thoughts on how to make mock Socks that can be used to play messages in two directions?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes! See the first two commits in #26812:

bee6bdf test: put the generic parts from StaticContentsSock into a separate class
f42e4f3 test: add a mocked Sock that allows inspecting what has been Send() to it

and then how to use that in the last commit of the same PR:

8b10990 test: add unit tests exercising full call chain of CConnman and PeerManager

With those it is possible to send/receive raw bytes to/from the (mocked) socket, or NetMsgs, e.g.:

pipes->recv.PushNetMsg(NetMsgType::GETBLOCKS, block_locator, hash_stop);

ss << TX_WITH_WITNESS(tx);
tx_size = ss.size();
}

Copy link
Member Author

@Sjors Sjors Feb 14, 2024

Choose a reason for hiding this comment

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

TSAN is tripping up somewhere around here. The last thing it logs is - Connect 2 transactions:. It doesn't get to - Verify ... txins:. I wonder if this is related to mock time, which I'm testing in Sjors#34

Copy link
Member Author

Choose a reason for hiding this comment

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

@maflcko shouldn't TSan on CI output something useful about why it crashed? I currently only says "error 2": https://cirrus-ci.com/task/5124733717446656?logs=ci#L3531

When running this locally on Ubuntu with clang 16.0.6 I get a WARNING: ThreadSanitizer: data race and significantly more details (still a bit cryptic, but hopefully enough to figure out what's happening).

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I guess the unit tests don't capture the tsan output?

Copy link
Member

Choose a reason for hiding this comment

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

But they should. At least back when I tested #27667

Copy link
Member

Choose a reason for hiding this comment

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

Maybe good to re-check this when/after the cmake migration is done?

@Sjors
Copy link
Member Author

Sjors commented Feb 15, 2024

Added m_tp_mutex to Sv2TemplateProvider.

@Sjors
Copy link
Member Author

Sjors commented Feb 15, 2024

Bumping macOS to 14 on the CI does not help (tried in Sjors#35). I also can't reproduce this failure on my own Intel macOS machines, not on 13.6.4 and not on 14.2.1. A Sock mock is probably the most robust solution, but it'd be nice to find another workaround.

This extra delay seems to do the trick for now: Sjors@c8d10af

Another option to consider is using the functional test framework instead, since these are not really unit tests. However that involves implementing the sv2 noise protocol in Python and a bunch of other work to export transport functions to the functional test framework. If anyone feels up to that challenge, let me know...

vasild and others added 22 commits October 16, 2024 12:12
…lass

This allows reusing them in other mocked implementations.
…o it

And also allows gradually providing the data to be returned by `Recv()`
and sending and receiving net messages (`CNetMessage`).
Co-Authored-By: Vasil Dimov <vd@FreeBSD.org>
The template provider will listen for a Job Declarator client.
It can establish a connection and detect various protocol errors.

Co-Authored-By: Christopher Coverdale <chris.coverdale24@gmail.com>
Co-Authored-By: Fi3
Co-authored-by: Christopher Coverdale <chris.coverdale24@gmail.com>
Co-authored-by: Vasil Dimov <vd@FreeBSD.org>
This doesn't solve the underlying problem.
@Sjors
Copy link
Member Author

Sjors commented Oct 16, 2024

I'm moving this pull request to Sjors#68. The sv2 branch will continue to work as before for the time being. See #31098 the plan moving forward.

@Sjors Sjors closed this Oct 16, 2024
ryanofsky added a commit that referenced this pull request Aug 20, 2025
ce7d94a doc: add release note (Sjors Provoost)
71f29d4 doc: update build and dependencies docs for IPC (Sjors Provoost)
3cbf747 cmake: set ENABLE_IPC by default (Sjors Provoost)
32a90e1 ci: use bitcoin-node for one depends job (Sjors Provoost)
b333cc1 ci: build one depends job without multiprocess (Sjors Provoost)
16bce9a build: depends makes libmultiprocess by default (Sjors Provoost)

Pull request description:

  Have depends make libmultiprocess by default. This PR causes the following behavior changes:

  1. **bitcoin-node and bitcoin-gui binaries are included in releases**, due to `ENABLE_IPC` option being switched on by default in depends builds
  2. `ENABLE_IPC` is also switched on by default in non-depends builds (instructions updated, #33190 does this as a standalone PR)
  3. Various changes to CI: switching on `ENABLE_IPC` on in most configurations and using `bitcoin-node` binary (`bitcoin -m`) for functional tests in two of them.
  4. The `bitcoin-node` and `bitcoin-gui` are added to `Maintenance.cmake` (since they're now in the release)

  This PR doesn't need to do all of these things at once. However it's is simpler, avoids code churn (especially in CI), and  probably less confusing to make all these changes in the same PR.

  Windows is not supported yet, so `ENABLE_IPC` is off by default for it. It can be enabled after #32387.

  The initial main use case for IPC is to enable experimental support for the Mining IPC interface. A working example of a Stratum v2 Template Provider client using this interface can be found here: Sjors#48.

  See #31756 for discussion of when this should happen. Supersedes #30975.

  ## Wait what, why?

  The [Stratum v2 spec](https://stratumprotocol.org/specification) has been around for a few years now, mostly stable but with [ongoing activity](https://github.com/stratum-mining/sv2-spec/commits/main/) to clarify and fix more subtle issues encountered by implementers. Most of the implementation is built in Rust in a project called the Stratum Reference Implementation ([SRI](https://github.com/stratum-mining/stratum)).

  [Braiins](https://demand.work) added Stratum v2 support to both their (custom) firmware and pool several years ago, though they have fallen behind on recent spec changes (update: it seems they've fixed that). Apparently [new hardware is underway](#31802 (comment)) that supports Stratum v2 without the need for custom firmware.

  [DMND pool](https://www.dmnd.work) is Stratum v2 native from the start and employs several of the SRI developers (they haven't fully launched though). The industry is rather secretive, but apparently [there is more underway](#31802 (comment)).

  What does Bitcoin Core have to do with this? Well, in Stratum v2 jargon we are the Template Provider.

  Or at least, the Template Provider role needs us to make block templates. Initially back in 2023 the plan was to have Bitcoin Core implement this role entirely, see #23049. It would speak the sv2 encrypted message protocol. In fact the spec was designed around this assumption, making sure to only use cryptographic primitives already in our codebase.

  I took over that effort in late 2023, but during 2024 it became quite clear there was [strong resistance](#29432 (review)) to the idea of including all this new code, opening another network ports, etc.

  At the same time there was the long running multiprocess / IPC project #10102, and the idea was born to apply that here: instead of including Stratum v2 specific stuff, we offer a general Mining interface via an IPC connection that can e.g. push out fresh block templates as fees rise above a threshold (something not possible and/or very inefficient with `getblocktemplate`). A client sidecar application then sits between the Stratum v2 world and our node.

  Currently there's only one such sidecar application, maintained by me, and reusing the same codebase from the integrated approach. An attempt has been made to connect to our interface from Rust bitcoin-core/libmultiprocess#174, which would pave the way for SRI include the Template Provider role. Plebhash below indicates he's also working on that: #31802 (comment).

  So with this new approach in mind, between mid 2024 until spring 2025, I introduced a new Mining interface (#30200 - #31785). At the same time Russ Ryanosky worked on more tight integration of [libmultiprocess](https://github.com/bitcoin-core/libmultiprocess), including making it a subtree in #31741. See [design/multiprocess.md](https://github.com/bitcoin/bitcoin/blob/master/doc/design/multiprocess.md).

  Meanwhile I've been maintaining a fork of Bitcoin Core that includes the Template Provider, in the original integrated approach (Sjors#68) as well as an IPC + sidecar variant (Sjors#48). I've been shipping [regular releases](https://github.com/Sjors/bitcoin/releases), mostly after bug fixes or major rebases. The SRI team has been testing both variants, though the "official" [instruction on their web page](https://stratumprotocol.org/developers) is to stick to integrated version. Bug reports on [my repo fork](https://github.com/Sjors/bitcoin/issues?q=is%3Aissue) as well as on the [SRI repo](https://github.com/stratum-mining/stratum/issues?q=is%3Aissue%20%20label%3A%22template%20provider%22) are evidence of actual testing happening.

  But as Pavlenex writes below:

  > one recurring feedback I kept getting regardless of the size/type of miner is that the need to run a forked version of Bitcoin Core remains a significant barrier to adoption

  This PR gets rids of that significant barrier. People can download a "pristine" version of Bitcoin Core and the only change is to start it with `bitcoin node -m -ipcconnect=unix` instead of the usual `bitcoind`.

  Once that's released, I can dramatically simplify my sidecar codebase (Sjors#48) by removing pretty much all Bitcoin Core code  that it doesn't need. My plan is to then make that a separate repository, which should be much easier to contribute to. I can then also make (deterministically built) signed releases, while making it clear that sidecar code has nothing to do with Bitcoin Core. Perhaps later on SRI implements the same and I can stop maintaining that project.

  Conceptually the situation will be a lot clearer;
  - today: download forked version of `bitcoind` (or a forked version of `bitcoin-node`, plus `bitcoin-mine`), install SRI stuff
  - tomorrow: download Bitcoin Core v30, install `bitcoin-mine` and SRI
  - future: download Bitcoin Core v30 and SRI

  <details>
  <summary>
  Guix hashes:
  </summary>

  ```
  find guix-build-$(git rev-parse --short=12 HEAD)/output/ -type f -print0 | env LC_ALL=C sort -z | xargs -r0 sha256sum
  6dbf29baecb1d1593087ef1306ae7c78aa160c8beb04dc016e02549ae2d6d90d  guix-build-ce7d94a492e6/output/aarch64-linux-gnu/SHA256SUMS.part
  4b465e5e8f9652c176aa57cfe5c289267c28c3a3c684034a9ce471b529b95275  guix-build-ce7d94a492e6/output/aarch64-linux-gnu/bitcoin-ce7d94a492e6-aarch64-linux-gnu-debug.tar.gz
  85bc6fa008b83419d96443d9dcc212b46f0992387fd58fd2dda5da76536ee22c  guix-build-ce7d94a492e6/output/aarch64-linux-gnu/bitcoin-ce7d94a492e6-aarch64-linux-gnu.tar.gz
  5ed9ea52a8bd55361d2d9c01fbd1b25ec9970530c2776e6c1959424ba1689f52  guix-build-ce7d94a492e6/output/arm-linux-gnueabihf/SHA256SUMS.part
  2e483011fac64462d3aa000b577c3c05c825506032d879e39612e096d7a6c65b  guix-build-ce7d94a492e6/output/arm-linux-gnueabihf/bitcoin-ce7d94a492e6-arm-linux-gnueabihf-debug.tar.gz
  7ff1e3ba54944a2be89dd7d68cb91dff6f8950de9d7b521e15dfb746965f81bd  guix-build-ce7d94a492e6/output/arm-linux-gnueabihf/bitcoin-ce7d94a492e6-arm-linux-gnueabihf.tar.gz
  abdf89e701b21b8c1238a8cec46aeaa55e0c3a0b88ad718636e89cde9813ca08  guix-build-ce7d94a492e6/output/arm64-apple-darwin/SHA256SUMS.part
  fb55cff0296cd5474811fe5cedcf28603628729dd085eeefa007c72582459b33  guix-build-ce7d94a492e6/output/arm64-apple-darwin/bitcoin-ce7d94a492e6-arm64-apple-darwin-codesigning.tar.gz
  e9aa566b1e79c467d7987b7c68fa608db788e6ddf89c4d90e524cd47b4faaf86  guix-build-ce7d94a492e6/output/arm64-apple-darwin/bitcoin-ce7d94a492e6-arm64-apple-darwin-unsigned.tar.gz
  bb428fc62a1230a55f49fa3b5c7ba8d588e8fed491357f890d5a6724a38b14e9  guix-build-ce7d94a492e6/output/arm64-apple-darwin/bitcoin-ce7d94a492e6-arm64-apple-darwin-unsigned.zip
  5ef4b75e94b2c8265fbc588bbb42467a84438af969fddac0ea61ced3e4113345  guix-build-ce7d94a492e6/output/dist-archive/bitcoin-ce7d94a492e6.tar.gz
  4f55d56a108c8f312a502cd5dfdf0840b091861a6d502df31caf4636a203697a  guix-build-ce7d94a492e6/output/powerpc64-linux-gnu/SHA256SUMS.part
  66c5b1242c60e37098885a00e24efe19baee4afcd2e3d6407207523d8872f055  guix-build-ce7d94a492e6/output/powerpc64-linux-gnu/bitcoin-ce7d94a492e6-powerpc64-linux-gnu-debug.tar.gz
  d9dbbee7217544eda26e77158cd82caeaef2b40fb9fc7033323e7ffe64264109  guix-build-ce7d94a492e6/output/powerpc64-linux-gnu/bitcoin-ce7d94a492e6-powerpc64-linux-gnu.tar.gz
  d9b808cc5685c819abcebb4ace65f003ebc4bfedf3fca046b34de37994358782  guix-build-ce7d94a492e6/output/riscv64-linux-gnu/SHA256SUMS.part
  eeeea470b1cf76515bfae14c779a3ea356d89f719d1fef1a81e8f0d6b04ab747  guix-build-ce7d94a492e6/output/riscv64-linux-gnu/bitcoin-ce7d94a492e6-riscv64-linux-gnu-debug.tar.gz
  9993da4eb51618b8bd25ec88cc576496720e5589315e9eba6f3ddab25f9c3e60  guix-build-ce7d94a492e6/output/riscv64-linux-gnu/bitcoin-ce7d94a492e6-riscv64-linux-gnu.tar.gz
  1b5a676580e0e79598d182f6ebbb05fb8aee2381edc3c09c042cae2600f448ab  guix-build-ce7d94a492e6/output/x86_64-apple-darwin/SHA256SUMS.part
  9152122d95a34d5df75305c6883c87707e7b09033fffd08e264d703ed177ef12  guix-build-ce7d94a492e6/output/x86_64-apple-darwin/bitcoin-ce7d94a492e6-x86_64-apple-darwin-codesigning.tar.gz
  2793f75730dbef6bdf12b5ed7135e22ed21178abff2926dee92843837d4ab544  guix-build-ce7d94a492e6/output/x86_64-apple-darwin/bitcoin-ce7d94a492e6-x86_64-apple-darwin-unsigned.tar.gz
  e89aafd7e4a330a41f470e8f0a91ea876fad7d19547b404600867413f1a8ccb7  guix-build-ce7d94a492e6/output/x86_64-apple-darwin/bitcoin-ce7d94a492e6-x86_64-apple-darwin-unsigned.zip
  955b27f881927a86da3c566357ad8ca68dbe17e9652bde8c482a57ceacba92cb  guix-build-ce7d94a492e6/output/x86_64-linux-gnu/SHA256SUMS.part
  fd012be97bdf5c75ac12ddef21526bfdb5e17ecc77cde9c34d832194b0dc3293  guix-build-ce7d94a492e6/output/x86_64-linux-gnu/bitcoin-ce7d94a492e6-x86_64-linux-gnu-debug.tar.gz
  0ecf7f80e9049369760d0e27fe6c026391ab25eae0f42336bef43e51a2621726  guix-build-ce7d94a492e6/output/x86_64-linux-gnu/bitcoin-ce7d94a492e6-x86_64-linux-gnu.tar.gz
  2e8085f5fecc246d841b0bf6f28ecd0684a6cee49252fc88c1019d7586c7b7a2  guix-build-ce7d94a492e6/output/x86_64-w64-mingw32/SHA256SUMS.part
  c60041e8137eda352557254c5f67fb83eeb97ecfec342ee528451bd44ee4523a  guix-build-ce7d94a492e6/output/x86_64-w64-mingw32/bitcoin-ce7d94a492e6-win64-codesigning.tar.gz
  b1be6b2f4de1c69c2e0e4de6dd97a4891ae9eb50d89435ef47247b5a187915a9  guix-build-ce7d94a492e6/output/x86_64-w64-mingw32/bitcoin-ce7d94a492e6-win64-debug.zip
  bfe143f41a20c537145c7044aca889b28efe19072b0150042a3bd865983b3d7e  guix-build-ce7d94a492e6/output/x86_64-w64-mingw32/bitcoin-ce7d94a492e6-win64-setup-unsigned.exe
  94a906b83d84db7b25f7e3cfdce2a2030243f2ee6cc70b1fc088459f0b2f382d  guix-build-ce7d94a492e6/output/x86_64-w64-mingw32/bitcoin-ce7d94a492e6-win64-unsigned.zip
  ```

  </details>

ACKs for top commit:
  ryanofsky:
    Code review ACK ce7d94a. This was just rebased to fix a conflict since last review.
  josibake:
    ACK ce7d94a
  achow101:
    ACK ce7d94a
  ismaelsadeeq:
    ACK ce7d94a and tested again on macOS by building via depends and source.
  janb84:
    ACK ce7d94a

Tree-SHA512: f7ab72933854e9dfce5746cdf764944bc26eec815f97cd0aa6b54fa499c3cccb1b678861ef5c1c793de28153d46bbb6e4d1b9aa0652163b74262e2d55ec8b813
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.