Sjors / bitcoin

Bitcoin Core integration/staging tree
https://bitcoin.org/en/download
MIT License
8 stars 11 forks source link

Introduce waitFeesChanged() mining interface #52

Closed Sjors closed 1 month ago

Sjors commented 4 months ago

This adds waitFeesChanged() to the Mining interface.

The Stratum v2 protocol allows pushing out templates as fees in the mempool increase. This interface lets us know when it's time to do so.

Without Cluster Mempool however the implementation is "fake", instead returning every time a transaction is added to the mempool. So for now I'm keeping this draft. It's here to provide a complete and stable Mining interface for https://github.com/bitcoin/bitcoin/pull/30437 to build on.

Unlike the entire Mining interface so far, this is not a refactor. It adds new functionality.

The current implementation is very similiar to how longpolling in getblocktemplate works, which checks getTransactionsUpdated every 10 seconds.

However once Cluster Mempool is added it will be cheap enough to frequently generate a block template, check if the fees have gone up enough and then return. That would cause a behaviour change if getblocktemplate were to use waitFeesChanged(), which is why this PR does not touch the longpolling code.

TODO:

Sjors commented 4 months ago

@sdaftuar @sipa repeating my comment from https://github.com/bitcoin/bitcoin/pull/30443 (with updated code).

I made a branch sv2-cluster that combines https://github.com/bitcoin/bitcoin/pull/28676 with https://github.com/bitcoin/bitcoin/pull/29432:

The main commit is https://github.com/bitcoin/bitcoin/commit/625e44445531f58c523e1c8c893a431ab5376e0b. The waitFeesChanged() interface implementation now frequently creates a new template.

The Template Provider then uses that to see if sending a new NewTemplate message is justified.

It still takes 20-30ms on mainnet to create a template, without or the cluster mempool changes. I guess BlockAssembler::CreateNewBlock doesn't actually take advance of it? How would I go about getting the total fees for the first block?

You can see it in action using src/bitcoind -sv2 -debug=sv2 -loglevel=sv2:trace

Once the node is at the tip you'll see frequent [sv2:trace] Assemble template: ...ms messages. (this even works without a connected SRI client, which is a convenient bug)

It doesn't have to be very precise. The user can configure -sv2delta as a threshold for when to send out new templates, but we can be off by a bit. The actual block is constructed later.

Sjors commented 1 month ago

Reopening this on the main repo at https://github.com/bitcoin/bitcoin/pull/31003