Skip to content

Commit 6b4c817

Browse files
Sjorsryanofsky
andcommitted
refactor: pass BlockCreateOptions to createNewBlock
Rather than pass options individually to createNewBlock and then combining them into BlockAssembler::Options, this commit introduces BlockCreateOptions and passes that instead. Currently there's only one option (use_mempool) but the next commit adds more. Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
1 parent 323cfed commit 6b4c817

File tree

6 files changed

+22
-11
lines changed

6 files changed

+22
-11
lines changed

src/interfaces/mining.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
#ifndef BITCOIN_INTERFACES_MINING_H
66
#define BITCOIN_INTERFACES_MINING_H
77

8+
#include <node/types.h>
9+
#include <uint256.h>
10+
811
#include <memory>
912
#include <optional>
10-
#include <uint256.h>
1113

1214
namespace node {
1315
struct CBlockTemplate;
@@ -41,10 +43,10 @@ class Mining
4143
* Construct a new block template
4244
*
4345
* @param[in] script_pub_key the coinbase output
44-
* @param[in] use_mempool set false to omit mempool transactions
46+
* @param[in] options options for creating the block
4547
* @returns a block template
4648
*/
47-
virtual std::unique_ptr<node::CBlockTemplate> createNewBlock(const CScript& script_pub_key, bool use_mempool = true) = 0;
49+
virtual std::unique_ptr<node::CBlockTemplate> createNewBlock(const CScript& script_pub_key, const node::BlockCreateOptions& options={}) = 0;
4850

4951
/**
5052
* Processes new block. A valid new block is automatically relayed to peers.

src/node/interfaces.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -884,12 +884,11 @@ class MinerImpl : public Mining
884884
return TestBlockValidity(state, chainman().GetParams(), chainman().ActiveChainstate(), block, tip, /*fCheckPOW=*/false, check_merkle_root);
885885
}
886886

887-
std::unique_ptr<CBlockTemplate> createNewBlock(const CScript& script_pub_key, bool use_mempool) override
887+
std::unique_ptr<CBlockTemplate> createNewBlock(const CScript& script_pub_key, const BlockCreateOptions& options) override
888888
{
889-
BlockAssembler::Options options;
890-
ApplyArgsManOptions(gArgs, options);
891-
892-
return BlockAssembler{chainman().ActiveChainstate(), use_mempool ? context()->mempool.get() : nullptr, options}.CreateNewBlock(script_pub_key);
889+
BlockAssembler::Options assemble_options{options};
890+
ApplyArgsManOptions(*Assert(m_node.args), assemble_options);
891+
return BlockAssembler{chainman().ActiveChainstate(), context()->mempool.get(), assemble_options}.CreateNewBlock(script_pub_key);
893892
}
894893

895894
NodeContext* context() override { return &m_node; }

src/node/miner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static BlockAssembler::Options ClampOptions(BlockAssembler::Options options)
6666

6767
BlockAssembler::BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool, const Options& options)
6868
: chainparams{chainstate.m_chainman.GetParams()},
69-
m_mempool{mempool},
69+
m_mempool{options.use_mempool ? mempool : nullptr},
7070
m_chainstate{chainstate},
7171
m_options{ClampOptions(options)}
7272
{

src/node/miner.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifndef BITCOIN_NODE_MINER_H
77
#define BITCOIN_NODE_MINER_H
88

9+
#include <node/types.h>
910
#include <policy/policy.h>
1011
#include <primitives/block.h>
1112
#include <txmempool.h>
@@ -153,7 +154,7 @@ class BlockAssembler
153154
Chainstate& m_chainstate;
154155

155156
public:
156-
struct Options {
157+
struct Options : BlockCreateOptions {
157158
// Configuration parameters for the block size
158159
size_t nBlockMaxWeight{DEFAULT_BLOCK_MAX_WEIGHT};
159160
CFeeRate blockMinFeeRate{DEFAULT_BLOCK_MIN_TX_FEE};

src/node/types.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#ifndef BITCOIN_NODE_TYPES_H
1414
#define BITCOIN_NODE_TYPES_H
1515

16+
#include <cstddef>
17+
1618
namespace node {
1719
enum class TransactionError {
1820
OK, //!< No error
@@ -24,6 +26,13 @@ enum class TransactionError {
2426
MAX_BURN_EXCEEDED,
2527
INVALID_PACKAGE,
2628
};
29+
30+
struct BlockCreateOptions {
31+
/**
32+
* Set false to omit mempool transactions
33+
*/
34+
bool use_mempool{true};
35+
};
2736
} // namespace node
2837

2938
#endif // BITCOIN_NODE_TYPES_H

src/rpc/mining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ static RPCHelpMan generateblock()
371371

372372
ChainstateManager& chainman = EnsureChainman(node);
373373
{
374-
std::unique_ptr<CBlockTemplate> blocktemplate{miner.createNewBlock(coinbase_script, /*use_mempool=*/false)};
374+
std::unique_ptr<CBlockTemplate> blocktemplate{miner.createNewBlock(coinbase_script, {.use_mempool = false})};
375375
if (!blocktemplate) {
376376
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
377377
}

0 commit comments

Comments
 (0)