<p>When you run <a href="https://bitcoincore.org/en/doc/0.17.0/rpc/generating/generate/" rel="nofollow noreferrer"><code>generate</code></a>, it mines a block with the transactions in the mempool:</p>

<pre><code>generate nblocks ( maxtries )

Mine up to nblocks blocks immediately (before the RPC call returns) to an address in the wallet.
</code></pre>

<p>If you want those new coins to be mined to a specific address, you can use <a href="https://bitcoincore.org/en/doc/0.17.0/rpc/generating/generatetoaddress/" rel="nofollow noreferrer"><code>generatetoaddress</code></a></p>

<p>You can create transactions and submit them to the mempool either using the wallet RPC, e.g. <a href="https://bitcoincore.org/en/doc/0.17.0/rpc/wallet/sendtoaddress/" rel="nofollow noreferrer"><code>sendtoaddress</code></a> or using the rawtransactions RPC:
e.g. </p>

<ol>
<li><a href="https://bitcoincore.org/en/doc/0.17.0/rpc/rawtransactions/createrawtransaction/" rel="nofollow noreferrer"><code>createrawtransaction</code></a></li>
<li><a href="https://bitcoincore.org/en/doc/0.17.0/rpc/rawtransactions/signrawtransaction/" rel="nofollow noreferrer"><code>signrawtransaction</code></a></li>
<li><a href="https://bitcoincore.org/en/doc/0.17.0/rpc/rawtransactions/sendrawtransaction/" rel="nofollow noreferrer"><code>sendrawtransaction</code></a></li>
</ol>

<p><code>sendrawtransaction</code> will put the transaction in the mempool, which can then be included in the next block using <code>generate</code></p>

CC BY-SA 4.0
2019-02-16T11:12:07.997Z
Thank you for your answer. but I wanted to know how to include a specific transaction in the next block. e.g. There are txA and txB in mempool now. In this case, I want to include only txA in the next block. Not txB.
zono
CC BY-SA 4.0
2019-02-15T14:46:08.813Z
2019-02-15T14:46:08.813Z
JBaczuk
<p>In the addPackageTxs func <a href="https://github.com/bitcoin/bitcoin/blob/ef70f9b52b851c7997a9f1a0834714e3eebc1fd8/src/miner.cpp#L306" rel="nofollow noreferrer">https://github.com/bitcoin/bitcoin/blob/ef70f9b52b851c7997a9f1a0834714e3eebc1fd8/src/miner.cpp#L306</a></p>

<pre><code>while (mi != mempool.mapTx.get&lt;ancestor_score&gt;().end() || !mapModifiedTx.empty())
{
    // First try to find a new transaction in mapTx to evaluate.
    if (mi != mempool.mapTx.get&lt;ancestor_score&gt;().end() &amp;&amp;
            SkipMapTxEntry(mempool.mapTx.project&lt;0&gt;(mi), mapModifiedTx, failedTx)) {
        ++mi;
        continue;
    }
</code></pre>

<p>you can modify the while loop and select which transation index should you include in block.</p>

<p>ex: you can skip transactions which are in mempool within 2 seconds before current time </p>

<pre><code>while (mi != mempool.mapTx.get&lt;entry_time&gt;().end() || !mapModifiedTx.empty())
{
    CTxMemPool::txiter iter = mempool.mapTx.project&lt;0&gt;(mi);
    auto time_now = GetTime();

    if (time_now - iter-&gt;GetTime() &lt; 2){
       continue;
    }

}
</code></pre>

CC BY-SA 4.0
2019-03-20T08:01:17.170Z
There's a prioritize transaction RPC which does this without modifying the binary.
Claris
CC BY-SA 4.0
2019-03-17T23:56:37.543Z
2019-03-17T23:56:37.543Z
Thành Nguyễn Công
<p>I want to generate a block with specific transaction via bitcoin-cli on regtest.</p>

<p>The "generate" command does not have the option. It collects transactions in mempool by the default rule.</p>

<p>I guess I can do it with some code change like the following way but I don't want to change it if possible.</p>

<pre><code>TestChain100Setup::CreateAndProcessBlock(const std::vector&lt;CMutableTransaction&gt;&amp; txns, const CScript&amp; scriptPubKey)
{
    const CChainParams&amp; chainparams = Params();
    std::unique_ptr&lt;CBlockTemplate&gt; pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey);
    CBlock&amp; block = pblocktemplate-&gt;block;

    // Replace mempool-selected txns with just coinbase plus passed-in txns:
    block.vtx.resize(1);
    for (const CMutableTransaction&amp; tx : txns)
        block.vtx.push_back(MakeTransactionRef(tx));
</code></pre>

<p><a href="https://github.com/bitcoin/bitcoin/blob/v0.17.1/src/miner.cpp#L147" rel="nofollow noreferrer">https://github.com/bitcoin/bitcoin/blob/v0.17.1/src/miner.cpp#L147</a>
<a href="https://github.com/bitcoin/bitcoin/blob/v0.17.1/src/test/test_bitcoin.cpp#L155" rel="nofollow noreferrer">https://github.com/bitcoin/bitcoin/blob/v0.17.1/src/test/test_bitcoin.cpp#L155</a>
<a href="https://bitcoincore.org/en/doc/0.17.0/rpc/generating/generate/" rel="nofollow noreferrer">https://bitcoincore.org/en/doc/0.17.0/rpc/generating/generate/</a></p>

<bitcoin-core><mining-theory>
Is it possible to generate(mine) a block with specific transaction via bitcoin-cli on regtest?
zono
# Is it possible to generate(mine) a block with specific transaction via bitcoin-cli on regtest?

(Asked by: username_0 on 2019-02-15)

I want to generate a block with specific transaction via bitcoin-cli on regtest.

The "generate" command does not have the option. It collects transactions in mempool by the default rule.

I guess I can do it with some code change like the following way but I don't want to change it if possible.

```
TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey)
{
    const CChainParams& chainparams = Params();
    std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey);
    CBlock& block = pblocktemplate->block;

    // Replace mempool-selected txns with just coinbase plus passed-in txns:
    block.vtx.resize(1);
    for (const CMutableTransaction& tx : txns)
        block.vtx.push_back(MakeTransactionRef(tx));

```

<https://github.com/bitcoin/bitcoin/blob/v0.17.1/src/miner.cpp#L147>
<https://github.com/bitcoin/bitcoin/blob/v0.17.1/src/test/test_bitcoin.cpp#L155>
<https://bitcoincore.org/en/doc/0.17.0/rpc/generating/generate/>

**Tags:** bitcoin-core, mining-theory


---

## 2 answers

### Answer by username_1 on 2019-02-15. Score: 2

When you run [`generate`](https://bitcoincore.org/en/doc/0.17.0/rpc/generating/generate/), it mines a block with the transactions in the mempool:

```
generate nblocks ( maxtries )

Mine up to nblocks blocks immediately (before the RPC call returns) to an address in the wallet.

```

If you want those new coins to be mined to a specific address, you can use [`generatetoaddress`](https://bitcoincore.org/en/doc/0.17.0/rpc/generating/generatetoaddress/)

You can create transactions and submit them to the mempool either using the wallet RPC, e.g. [`sendtoaddress`](https://bitcoincore.org/en/doc/0.17.0/rpc/wallet/sendtoaddress/) or using the rawtransactions RPC:
e.g.

1. [`createrawtransaction`](https://bitcoincore.org/en/doc/0.17.0/rpc/rawtransactions/createrawtransaction/)
2. [`signrawtransaction`](https://bitcoincore.org/en/doc/0.17.0/rpc/rawtransactions/signrawtransaction/)
3. [`sendrawtransaction`](https://bitcoincore.org/en/doc/0.17.0/rpc/rawtransactions/sendrawtransaction/)

`sendrawtransaction` will put the transaction in the mempool, which can then be included in the next block using `generate`

> **Comments:**
> **username_0**: Thank you for your answer. but I wanted to know how to include a specific transaction in the next block. e.g. There are txA and txB in mempool now. In this case, I want to include only txA in the next block. Not txB.  

---

### Answer by username_2 on 2019-03-17. Score: 0

In the addPackageTxs func <https://github.com/bitcoin/bitcoin/blob/ef70f9b52b851c7997a9f1a0834714e3eebc1fd8/src/miner.cpp#L306>

```
while (mi != mempool.mapTx.get<ancestor_score>().end() || !mapModifiedTx.empty())
{
    // First try to find a new transaction in mapTx to evaluate.
    if (mi != mempool.mapTx.get<ancestor_score>().end() &&
            SkipMapTxEntry(mempool.mapTx.project<0>(mi), mapModifiedTx, failedTx)) {
        ++mi;
        continue;
    }

```

you can modify the while loop and select which transation index should you include in block.

ex: you can skip transactions which are in mempool within 2 seconds before current time

```
while (mi != mempool.mapTx.get<entry_time>().end() || !mapModifiedTx.empty())
{
    CTxMemPool::txiter iter = mempool.mapTx.project<0>(mi);
    auto time_now = GetTime();

    if (time_now - iter->GetTime() < 2){
       continue;
    }

}

```

> **Comments:**
> **username_3**: There's a prioritize transaction RPC which does this without modifying the binary.  

---

