1

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/

2 Answers 2

2

When you run 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

You can create transactions and submit them to the mempool either using the wallet RPC, e.g. sendtoaddress or using the rawtransactions RPC: e.g.

  1. createrawtransaction
  2. signrawtransaction
  3. sendrawtransaction

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

1
  • 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
    Feb 16, 2019 at 11:12
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;
    }

}
1
  • There's a prioritize transaction RPC which does this without modifying the binary.
    – Claris
    Mar 20, 2019 at 8:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.