Skip to content

Commit

Permalink
Merge #759: [0.18] getnewblockhex: Take data push for arbitrary data …
Browse files Browse the repository at this point in the history
…in block coinbase c…

c79ad5f getnewblockhex: Take data push for arbitrary data in block coinbase commitment (Gregory Sanders)

Pull request description:

  …ommitment

  backport of #753

Tree-SHA512: b118f33643a5f62978c4d839c7d38917b65fdf7b8ef86fe8091485ea4f80024b5e8dbaecb00b778c8e9898611bc742b4169d5ce69b59dae8faff749d040c5120
  • Loading branch information
instagibbs committed Oct 29, 2019
2 parents eace989 + c79ad5f commit 3e85c3e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void BlockAssembler::resetBlock()
Optional<int64_t> BlockAssembler::m_last_block_num_txs{nullopt};
Optional<int64_t> BlockAssembler::m_last_block_weight{nullopt};

std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn, int min_tx_age, DynaFedParamEntry* proposed_entry)
std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn, int min_tx_age, DynaFedParamEntry* proposed_entry, CScript const* commit_script)
{
assert(min_tx_age >= 0);
int64_t nTimeStart = GetTimeMicros();
Expand Down Expand Up @@ -195,6 +195,10 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
}
}
coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
// Non-consensus commitment output before finishing coinbase transaction
if (commit_script) {
coinbaseTx.vout.insert(coinbaseTx.vout.begin(), CTxOut(policyAsset, 0, *commit_script));
}
pblock->vtx[0] = MakeTransactionRef(std::move(coinbaseTx));
pblocktemplate->vchCoinbaseCommitment = GenerateCoinbaseCommitment(*pblock, pindexPrev, chainparams.GetConsensus());
pblocktemplate->vTxFees[0] = -nFees;
Expand Down
2 changes: 1 addition & 1 deletion src/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class BlockAssembler
BlockAssembler(const CChainParams& params, const Options& options);

/** Construct a new block template with coinbase to scriptPubKeyIn. min_tx_age is in seconds */
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn, int min_tx_age=0, DynaFedParamEntry* = nullptr);
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn, int min_tx_age=0, DynaFedParamEntry* = nullptr, CScript const* commit_script = nullptr);

static Optional<int64_t> m_last_block_num_txs;
static Optional<int64_t> m_last_block_weight;
Expand Down
15 changes: 12 additions & 3 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ static UniValue estimaterawfee(const JSONRPCRequest& request)

UniValue getnewblockhex(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() > 2)
if (request.fHelp || request.params.size() > 3)
throw std::runtime_error(
RPCHelpMan{"getnewblockhex",
"\nGets hex representation of a proposed, unmined new block\n",
Expand All @@ -1015,6 +1015,7 @@ UniValue getnewblockhex(const JSONRPCRequest& request)
},
},
"proposed_parameters"},
{"commit_data", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "Data in hex to be committed to in an additional coinbase output."},
},
RPCResult{
"blockhex (hex) The block hex\n"
Expand Down Expand Up @@ -1069,9 +1070,17 @@ UniValue getnewblockhex(const JSONRPCRequest& request)
proposed.m_serialize_type = 2;
}

// Any commitment required for non-consensus reasons.
// This will be placed in the first coinbase output.
CScript data_commitment;
if (!request.params[2].isNull()) {
std::vector<unsigned char> data_bytes = ParseHex(request.params[2].get_str());
data_commitment = CScript() << OP_RETURN << data_bytes;
}

CScript feeDestinationScript = Params().GetConsensus().mandatory_coinbase_destination;
if (feeDestinationScript == CScript()) feeDestinationScript = CScript() << OP_TRUE;
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler(Params()).CreateNewBlock(feeDestinationScript, required_wait, &proposed));
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler(Params()).CreateNewBlock(feeDestinationScript, required_wait, &proposed, data_commitment.empty() ? nullptr : &data_commitment));
if (!pblocktemplate.get()) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet keypool empty");
}
Expand Down Expand Up @@ -1472,7 +1481,7 @@ static const CRPCCommand commands[] =
{ "mining", "getblocktemplate", &getblocktemplate, {"template_request"} },
{ "generating", "combineblocksigs", &combineblocksigs, {"blockhex","signatures"} },
{ "mining", "submitheader", &submitheader, {"hexdata"} },
{ "generating", "getnewblockhex", &getnewblockhex, {"min_tx_age", "proposed_parameters"} },
{ "generating", "getnewblockhex", &getnewblockhex, {"min_tx_age", "proposed_parameters", "commit_data"} },
{ "generating", "getcompactsketch", &getcompactsketch, {"block_hex"} },
{ "generating", "consumecompactsketch", &consumecompactsketch, {"sketch"} },
{ "generating", "consumegetblocktxn", &consumegetblocktxn, {"full_block", "block_tx_req"} },
Expand Down
15 changes: 15 additions & 0 deletions test/functional/feature_blocksign.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
address,
key,
)
from test_framework.messages import (
FromHex,
CBlock,
)
from test_framework.script import (
CScript
)

# Generate wallet import format from private key.
def wif(pk):
Expand Down Expand Up @@ -113,6 +120,14 @@ def mine_block(self, make_transactions):
miner.sendtoaddress(miner_next.getnewaddress(), int(miner.getbalance()['bitcoin']/10), "", "", True)
# miner makes a block
block = miner.getnewblockhex()
block_struct = FromHex(CBlock(), block)

# make another block with the commitment field filled out
dummy_block = miner.getnewblockhex(commit_data="deadbeef")
dummy_struct = FromHex(CBlock(), dummy_block)
assert_equal(len(dummy_struct.vtx[0].vout), len(block_struct.vtx[0].vout) + 1)
# OP_RETURN deadbeef
assert_equal(CScript(dummy_struct.vtx[0].vout[0].scriptPubKey).hex(), '6a04deadbeef')

# All nodes get compact blocks, first node may get complete
# block in 0.5 RTT even with transactions thanks to p2p connection
Expand Down

0 comments on commit 3e85c3e

Please sign in to comment.