The Stratum v2 spec defines a Job Declarator Server (JDS), whose job it is to approve custom miner templates. It currently maintains its own mempool mirror, which it populates by repeatedly calling getBlock() and waitNext() and then requesting any missing transactions from the miner (since #34020 it can use getTransactionsByWitnessID() to first ask the node).
The new TxCollection interface introduced by this PR removes the need for such a mirror mempool. We build the block template ourselves now, based on the requested transaction list and our own mempool.
The following flow is demonstrated in https://github.com/stratum-mining/sv2-apps/pull/599:
- Miner generates a template, e.g. using our
createNewBlock()IPC method - They send the list of
Wtxids to the pool's JDS - JDS calls
TxCollection::collectTxs()(this PR) with thatWtxidlist - We check the list against our mempool and hold a
CTransactionReffor each match - The JDS queries
unknownTxPos()to learn what's missing in our mempool - The JDS requests the fully serialized missing transactions from the miner (using native Stratum v2 messages)
- JDS calls
addMissingTxs()which completes the collection - JDS calls
makeTemplate()which reconstructs the block and validates it. The JDS needs the check to succeed, may relay the BIP-22 failure reason to the miner. - It returns a
BlockTemplatewhich works as if we generated the template ourselves, but with methods likewaitNext()disabled. The JDS can callsubmitSolution()(the sv2 spec allows for redundant block reconstruction by both the miner node and the JDS).
If this sounds very similar to compact block relay: it is! In fact, it would be easy to expand the above methods to take transaction short ids instead of Wtxid. That may be useful for the p2pool revival project, which needs a way to validate and relay weak blocks as compact blocks. The p2pool client software would need to (trivially) verify the PoW, but could use the TxCollection interface to reconstruct and verify the block content. That said, the SRI JDS implementation is much further along and likely to be the first consumer of this.
Commits:
- ipc: add TxCollection scaffold - step 3 and 4
- mining: add TxCollection unknownTxPos - step 5
- mining: add TxCollection addMissingTxs - step 7
- mining: add coinbase transaction helper - we need a dummy coinbase to verify the block, so extract a helper from
BlockAssembler::CreateNewBlock() - mining: make TxCollection create a BlockTemplate - step 8 and 9
- mining: restrict externally generated templates - step 9
Potential followups:
- have
addMissingTxs()(optionally) insert favorable transactions into our mempool - add a data structure to store transactions that don't meet our mempool threshold (so
TxCollectioncan grab them, instead of needing another round trip)
Conceptual review is welcome. I need to polish the commits a bit further, so it's better to hold off on that. I opened the PR now because the concept was brought up in #34020#pullrequestreview-4635352194.
Keeping this in draft until #34672 lands, to match its handling of the reason/debug fields.