<p>The miner can generate a block candidate with the <code>getblocktemplate</code> call, which uses certain heuristics to construct an approximation of an fee-optimised block given the current local mempool state. </p>

<p><strong>1) The question in regards to when to begin mining</strong> </p>

<p>Generally, the miner will want to begin mining the next block as soon as the strong chain is extended, maximising the time available to search for the solution. </p>

<p>However, consider building an optimal block template from a growing number of unconfirmed transactions being accepted into the mempool. This means a transaction selection so that total block fee reward is optimised, whilst ensuring no double-spend. A growing mempool provides a greater optimisation space, but takes time. Solving for the exact optimal block from an increasingly large pool of valid transactions is NP-Hard (knapsack/max independent sets) and expensive.</p>

<p>At some point, the gains in fee rewards of solving this optimisation problem will be offset by the opportunity cost of the miner (during optimisation, the miner hashing equipment will necessarily be idle). Simpler heuristics may optimise for a less than optimal block template (as in <code>getblocktemplate</code>), but enables the miner to begin mining immediately.</p>

<p>If you wish to apply your own block template solver, you can get a mempool dump via 
<code>getrawmempool</code> (not very efficient to acquire over RPC).</p>

<p><strong>2) Regarding regtest in a local network</strong></p>

<p><a href="https://bitcoin.stackexchange.com/questions/36013/is-it-possible-to-create-bitcoind-regtest-network-on-ubuntu-which-itself-is-on">Is it possible to create bitcoind regtest network on Ubuntu (which itself is on Virtualbox)?</a></p>

CC BY-SA 4.0
2019-01-23T19:04:34.090Z
Hey so i get that deciding what pool size is optimal is tricky, and the miner would have the highest probability of mining a block if he started as soon as the chain gets extended. What i want to confirm is, once a miner has decided a trigger condition (e.g. the chain has been extended + at least 10 transactions in the mempool), how does he configure it? Setup a blocknotify event and poll the mempool from a script when the event fires? Or is there some other way?
alokraop
CC BY-SA 4.0
2019-01-23T19:14:55.933Z
Oh and about the regtest part, i've already got a local network setup and i'm able to use the `generate` command to create blocks. My question is if there's any way to ask `bitcoind` to generate a block every x seconds or something. I want the generation process automated. The link in your answer just instructs to run the `setgenerate` command manually which is not quite what i'm after.
alokraop
CC BY-SA 4.0
2019-01-18T11:10:36.147Z
2019-01-18T13:01:43.753Z
2019-01-18T13:01:43.753Z
James C.
<p>I'm trying to setup a private network of bitcoin nodes for testing and experimentation. From what i understand, i have two choices while setting up a network:</p>

<ol>
<li>I can run <code>bitcoind -testnet</code> and connect to the public test network. The testnet has nodes that will facilitate the mining of my transactions as long as i just broadcast them.</li>
<li>I can configure the node with <code>regtest=1</code> and manually run the <code>generate</code> command whenever i want to create blocks.</li>
</ol>

<p>I've also managed to find out that the mining is no longer a part of the <a href="https://github.com/bitcoin/bitcoin/blob/master/doc/release-notes/release-notes-0.13.0.md#removal-of-internal-miner" rel="nofollow noreferrer">core codebase</a> and is usually in an <a href="https://github.com/ckolivas/cgminer" rel="nofollow noreferrer">external component</a>.</p>

<p>What i don't understand is how any of these mining softwares interact with the bitcoin node. Does it periodically poll through the JSON RPC to check if the transaction pool has reached a certain threshold (in numbers or size)? If not, what triggers the beginning of a mining operation? Can the bitcoin node send out events when the transaction pool reaches a threshold?</p>

<p>I want to setup a similar automated process for creating blocks in my <code>regtest</code> network, but using the <code>generate</code> command instead of any mining software. I don't need a robust PoW setup in my local network. My aim is to just build an app which interacts with the nodes and makes transactions. Is writing an RPC client which periodically queries the pool size and fires a <code>generate</code> command on reaching a threshold the right way to proceed? Or does the bitcoin node itself provide some way to setup periodic generation of blocks?</p>

<p>Thanks in advance.</p>

<p>P.S. I'm very new to bitcoin development, so apologies if this is an obvious question.</p>

<json-rpc><testnet><regtest>
How to perform automated mining in regtest mode?
alokraop
# How to perform automated mining in regtest mode?

(Asked by: username_0 on 2019-01-18)

I'm trying to setup a private network of bitcoin nodes for testing and experimentation. From what i understand, i have two choices while setting up a network:

1. I can run `bitcoind -testnet` and connect to the public test network. The testnet has nodes that will facilitate the mining of my transactions as long as i just broadcast them.
2. I can configure the node with `regtest=1` and manually run the `generate` command whenever i want to create blocks.

I've also managed to find out that the mining is no longer a part of the [core codebase](https://github.com/bitcoin/bitcoin/blob/master/doc/release-notes/release-notes-0.13.0.md#removal-of-internal-miner) and is usually in an [external component](https://github.com/ckolivas/cgminer).

What i don't understand is how any of these mining softwares interact with the bitcoin node. Does it periodically poll through the JSON RPC to check if the transaction pool has reached a certain threshold (in numbers or size)? If not, what triggers the beginning of a mining operation? Can the bitcoin node send out events when the transaction pool reaches a threshold?

I want to setup a similar automated process for creating blocks in my `regtest` network, but using the `generate` command instead of any mining software. I don't need a robust PoW setup in my local network. My aim is to just build an app which interacts with the nodes and makes transactions. Is writing an RPC client which periodically queries the pool size and fires a `generate` command on reaching a threshold the right way to proceed? Or does the bitcoin node itself provide some way to setup periodic generation of blocks?

Thanks in advance.

P.S. I'm very new to bitcoin development, so apologies if this is an obvious question.

**Tags:** json-rpc, testnet, regtest


---

## 1 answers

### Answer by username_1 on 2019-01-18. Score: 1

The miner can generate a block candidate with the `getblocktemplate` call, which uses certain heuristics to construct an approximation of an fee-optimised block given the current local mempool state.

**1) The question in regards to when to begin mining**

Generally, the miner will want to begin mining the next block as soon as the strong chain is extended, maximising the time available to search for the solution.

However, consider building an optimal block template from a growing number of unconfirmed transactions being accepted into the mempool. This means a transaction selection so that total block fee reward is optimised, whilst ensuring no double-spend. A growing mempool provides a greater optimisation space, but takes time. Solving for the exact optimal block from an increasingly large pool of valid transactions is NP-Hard (knapsack/max independent sets) and expensive.

At some point, the gains in fee rewards of solving this optimisation problem will be offset by the opportunity cost of the miner (during optimisation, the miner hashing equipment will necessarily be idle). Simpler heuristics may optimise for a less than optimal block template (as in `getblocktemplate`), but enables the miner to begin mining immediately.

If you wish to apply your own block template solver, you can get a mempool dump via
`getrawmempool` (not very efficient to acquire over RPC).

**2) Regarding regtest in a local network**

[Is it possible to create bitcoind regtest network on Ubuntu (which itself is on Virtualbox)?](https://bitcoin.stackexchange.com/questions/36013/is-it-possible-to-create-bitcoind-regtest-network-on-ubuntu-which-itself-is-on)

> **Comments:**
> **username_0**: Hey so i get that deciding what pool size is optimal is tricky, and the miner would have the highest probability of mining a block if he started as soon as the chain gets extended. What i want to confirm is, once a miner has decided a trigger condition (e.g. the chain has been extended + at least 10 transactions in the mempool), how does he configure it? Setup a blocknotify event and poll the mempool from a script when the event fires? Or is there some other way?  
> **username_0**: Oh and about the regtest part, i've already got a local network setup and i'm able to use the `generate` command to create blocks. My question is if there's any way to ask `bitcoind` to generate a block every x seconds or something. I want the generation process automated. The link in your answer just instructs to run the `setgenerate` command manually which is not quite what i'm after.  

---

