Skip to content

Conversation

Sjors
Copy link
Member

@Sjors Sjors commented Dec 1, 2023

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. See the original branch for the evolution of the spec.

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

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'll make separate pull requests for parts that are ready for detailed review. Probably starting with ECDH and the Noise Protocol.

Implementation notes

Silent Payments also needs the ECDH module, so I cherry-picked the commit from #28122. It uses ECDH is a slightly different way, but perhaps there's more overlap to be had.

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.

Upstream issues

Spec

  • modify spec to use ProvideMissingTransactions? (followup?)
  • pick a good default for default_coinbase_tx_additional_output_size (see getblocktemplate RPC)

Noise

Networking

Testing

  • expand sv2_template_provider_tests
  • add transport fuzzer
  • add template provider fuzzer

Template generation and updating

  • hold on to templates a bit after a block is found JDC panics after receiving RequestTransactionDataError message stratum-mining/stratum#709 (in case of race to prevent downstream crashes, though we still wouldn't relay it without additional changes)
  • hold on to template transactions even if the mempool drops them (for some time)
  • group templates with the same coinbase_tx_additional_output_size
  • don't generate templates when no client is connected

Misc

Potential followups

  • implement Noise protocol and mock client in Python, add functional tests (based on test/sv2_template_provider_tests.cpp)
  • use process separation, e.g. a bitcoin-tp binary, see multiprocess.md
  • make template updates push based, on top of Cluster Mempool, see docs/stratum-v2.md (for new blocks it's already push based)
  • push empty template for the next block (downstream can ignore or use, Implement a clever way to create and manage future jobs  stratum-mining/stratum#715)
    • send prevhash for this template as soon as any new block arrives
  • push optimistic template for the next block
    • send prevhash if and only if our template won (i.e. we got a SubmitSolution message)

@DrahtBot
Copy link
Contributor

DrahtBot commented Dec 1, 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:

  • #29295 (CKey: add Serialize and Unserialize by Sjors)
  • #29256 (Improve new LogDebug/Trace/Info/Warning/Error Macros by ryanofsky)
  • #29229 (Make (Read/Write)BinaryFile work with char vector, use AutoFile by Sjors)
  • #29119 (refactor: Use std::span over Span by maflcko)
  • #29071 (refactor: Remove Span operator==, Use std::ranges::equal by maflcko)
  • #29015 (kernel: Streamline util library by ryanofsky)
  • #28955 (index: block filters sync, reduce disk read operations by caching last header by furszy)
  • #28875 (build: Pass sanitize flags to instrument libsecp256k1 code by hebasto)
  • #28417 (contrib/signet/miner updates by ajtowns)
  • #28241 (Silent payment index (for light wallets and consistency check) by Sjors)
  • #26697 (logging: use bitset for categories by LarryRuane)
  • #26593 (tracing: Only prepare tracepoint arguments when actually tracing by 0xB10C)
  • #24539 (Add a "tx output spender" index by sstone)

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
Copy link
Member Author

Sjors commented Dec 8, 2023

Rebased and fixed encoding mistake I made during rebase:

diff --git a/src/node/sv2_messages.h b/src/node/sv2_messages.h
index fd6974b998..d3b257c31c 100644
--- a/src/node/sv2_messages.h
+++ b/src/node/sv2_messages.h
@@ -533,11 +533,11 @@ struct Sv2SubmitSolutionMsg
         s >> m_template_id >> m_version >> m_header_timestamp >> m_header_nonce;
 
         // Ignore the 2 byte length as the rest of the stream is assumed to be
         // the m_coinbase_tx.
         s.ignore(2);
-        s >> TX_NO_WITNESS(m_coinbase_tx);
+        s >> TX_WITH_WITNESS(m_coinbase_tx);
     }
 };

Yes, the coinbase transaction has a witness.

@Sjors
Copy link
Member Author

Sjors commented Dec 11, 2023

I moved sv2_noise.h and sv2_messages.h to common and template_provider.h to node.

@Sjors Sjors force-pushed the 2023/11/sv2-poll branch 2 times, most recently from 2991270 to 19d37d3 Compare December 11, 2023 21:34
@Sjors
Copy link
Member Author

Sjors commented Dec 11, 2023

I de-duplicated code in Send() by having it call SendBuf(). Later I'd like to see if we can reuse some code from Transport, since there are some similarities with v2 transport.

I dropped SendTemplate and overhauled SendWork, ThreadSv2Handler, the COINBASE_OUTPUT_DATA_SIZE handler and BuildNewWorkSet to de-duplicate stuff, make it easier to understand and to avoid passing m_ variables.

Hopefully I didn't break anything, but I did test on my custom CPU mined signet.

@Sjors
Copy link
Member Author

Sjors commented Dec 12, 2023

Pleased clang-tidy. Fixed memory issue by disabling a broken test, added Assume to noise.cpp to check the message size. Added a (possibly overkill) destructor to Sv2Template.

@Sjors Sjors force-pushed the 2023/11/sv2-poll branch 2 times, most recently from 30417b2 to e1b7e8a Compare December 12, 2023 18:05
@Sjors
Copy link
Member Author

Sjors commented Dec 12, 2023

Ran clang-tidy locally to (hopefully) fix its remaining complaints. Included @Fi3's Sjors#25 (plus additional header and test change) which drops the CIPHER_CONFIRMATION state, as per stratum-mining/sv2-spec#60.

@Sjors Sjors force-pushed the 2023/11/sv2-poll branch 2 times, most recently from a546ff0 to 31f9ae2 Compare December 12, 2023 19:56
@Sjors
Copy link
Member Author

Sjors commented Dec 12, 2023

I guess I didn't catch them all locally. Pushed a change to the last (linter commit). Also fixed the broken noise handshake test. Replaced usleep with UninterruptibleSleep to keep Windows happy.

@hebasto any idea how to handle D:\a\bitcoin\bitcoin\src\common\sv2_noise.h(92,13): error C3646: 'WriteMsg': unknown override specifier ?

class Sv2CipherState
{
public:
    Sv2CipherState() = default;
    ...
    ssize_t WriteMsg(Span<std::byte> msg);

@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/20524379427

@DrahtBot
Copy link
Contributor

🐙 This pull request conflicts with the target branch and needs rebase.

@Sjors
Copy link
Member Author

Sjors commented Jan 29, 2024

(I plan to rebase this after some other stuff is merged)

@Sjors
Copy link
Member Author

Sjors commented Jan 29, 2024

I opened a (draft) pull request for the Noise part of this: #29346

(will rebase this PR on that later, but right now that would break compatibility)

@Sjors
Copy link
Member Author

Sjors commented Feb 2, 2024

Added two commits to stay in sync with SRI: stratum-mining/stratum#742

Next week I'll try to rebase this on the latest #29346, which has diverged a bit.

@Sjors
Copy link
Member Author

Sjors commented Feb 5, 2024

I rebased on top of #29346 and pushed the result to: https://github.com/Sjors/bitcoin/tree/2024/02/sv2-poll-ellswift

Once EllSwift lands on the SRI side (or if I make a revert commit for it) I'll update this PR.

@Sjors
Copy link
Member Author

Sjors commented Feb 5, 2024

Added to 2024/02/sv2-poll-ellswift: we now hold on to templates for 10 seconds after a new block arrives, in a case a miner was still working on it. The resulting block still wouldn't get broadcast.

@Sjors
Copy link
Member Author

Sjors commented Feb 8, 2024

I added tests for RequestTransactionData to 2024/02/sv2-poll-ellswift, including an RBF test. It seems that the node does hold on to transaction data after an RBF. I don't know if that's because the block template has a p pointer, or if there's some other mechanism at play.

We'll need some way to limit the amount of memory that this can take up. Perhaps by pruning old (lower fee) templates more aggressively.

@Sjors Sjors mentioned this pull request Feb 14, 2024
24 tasks
@Sjors
Copy link
Member Author

Sjors commented Feb 14, 2024

This PR is superseeded by #29432.

@Sjors Sjors closed this Feb 14, 2024
ryanofsky added a commit that referenced this pull request Jul 10, 2024
576828e ci: test-each-commit merge base optional (Sjors Provoost)
e9bfbb5 ci: forks can opt-out of CI branch push (Cirrus only) (Sjors Provoost)

Pull request description:

  Maintainer note: `SKIP_BRANCH_PUSH=true` must be set in Cirrus for `bitcoin-core/gui` before merging this. See `https://cirrus-ci.com/github/bitcoin-core/gui` -> Settings.

  ---

  I find myself making pull requests against my fork (mostly on top of #28983, or asking others to do so. Currently only the Github actions are run on forks, because we use self-hosted runners for the Cirrus tasks.

  While setting up my own self-hosted runners for my fork, I ran into a number of issues. Some of those were addressed by #29441, but remaining issues are:

  1. When PRs are opened in the fork, cirrus CI jobs are run twice because PRs and branches reside in the same repository, rather than a main repository and a fork repository, as is the case with bitcoin/bitcoin PRs. Fix this by adding a `SKIP_BRANCH_PUSH` configuration option that allows skipping CI runs not directly associated with a PR. The fix is a generalization of [#20328](#20328), which fixed a similar problem for the bitcoin-core/gui mirror repository, and it allows removing a hardcoded reference to that repository.

      Github actions jobs will still run twice despite this change, see [#29274 (comment)](#29274 (comment)). Initially this PR tried to prevent that with b9fdd0d, but this had some potentially negative side effects, see [#29274 (comment)](#29274 (comment)), so that commit was dropped for now.

  2. When PRs are opened in the fork, the "test-each-commit" github action can fail due to not being able to find a recent merge commit. This problem doesn't happen in the bitcoin/bitcoin repository because branches in this repository used as the base for pull requests always point at merge commits.

  This PR replaces #29259 using the self hosted workers via Cirrus instead of Github.

  You can see this PR in action on this pull request to my fork: Sjors#30

  To test it yourself:

  1. spin up at least two [self hosted runners](https://github.com/cirruslabs/cirrus-cli/blob/master/PERSISTENT-WORKERS.md). Either use a seperate VM for each, or give them their own user.
  3. Install Podman and other CI dependencies (see .cirrus.yml)
  4. Give Cirrus access to your fork at https://cirrus-ci.com/settings/github/YOU
  5. Get a token from Cirrus and use it to start your worker(s)
  6. Optionally set SKIP_BRANCH_PUSH=true ~and NO_ARM=true~ env variables (see .cirrus.yml)
  make a pull request to your own fork, with this PR as the base branch

  Security wise: when dealing with code from strangers on the internet, review it first before running the CI. There's a Cirrus check-box that requires approval for people without write access to trigger CI.

ACKs for top commit:
  maflcko:
    ACK 576828e
  ryanofsky:
    Code review ACK 576828e.

Tree-SHA512: fb6be2f228aa62f45a65ce5c613c979b6f387df396f9601ce4622b27aa317a66f198e7d7a194592b0bb397b32a2f50f8be47065834d74af4ea09407c5c8d306d
@bitcoin bitcoin locked and limited conversation to collaborators Feb 13, 2025
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.

5 participants