From 7e1701899534151972ddff3c08cc964a9db64bc5 Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Sat, 24 Aug 2013 00:33:46 -0400 Subject: [PATCH 1/3] CreateNewBlock() now takes scriptPubKey argument, rather than a key. CreateNewBlockWithKey() helper is added to restore existing functionality, making this an equivalent-transformation change. --- src/miner.cpp | 18 ++++++++++++------ src/miner.h | 3 ++- src/rpcmining.cpp | 4 ++-- src/test/miner_tests.cpp | 22 +++++++++++----------- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/miner.cpp b/src/miner.cpp index e50c0b576..bea69ad1b 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -141,7 +141,7 @@ public: } }; -CBlockTemplate* CreateNewBlock(CReserveKey& reservekey) +CBlockTemplate* CreateNewBlock(CScript& scriptPubKeyIn) { // Create new block auto_ptr pblocktemplate(new CBlockTemplate()); @@ -154,10 +154,7 @@ CBlockTemplate* CreateNewBlock(CReserveKey& reservekey) txNew.vin.resize(1); txNew.vin[0].prevout.SetNull(); txNew.vout.resize(1); - CPubKey pubkey; - if (!reservekey.GetReservedKey(pubkey)) - return NULL; - txNew.vout[0].scriptPubKey << pubkey << OP_CHECKSIG; + txNew.vout[0].scriptPubKey = scriptPubKeyIn; // Add our coinbase tx as first transaction pblock->vtx.push_back(txNew); @@ -383,6 +380,15 @@ CBlockTemplate* CreateNewBlock(CReserveKey& reservekey) return pblocktemplate.release(); } +CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey) +{ + CPubKey pubkey; + if (!reservekey.GetReservedKey(pubkey)) + return NULL; + + CScript scriptPubKey = CScript() << pubkey << OP_CHECKSIG; + return CreateNewBlock(scriptPubKey); +} void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce) { @@ -510,7 +516,7 @@ void static BitcoinMiner(CWallet *pwallet) unsigned int nTransactionsUpdatedLast = nTransactionsUpdated; CBlockIndex* pindexPrev = pindexBest; - auto_ptr pblocktemplate(CreateNewBlock(reservekey)); + auto_ptr pblocktemplate(CreateNewBlockWithKey(reservekey)); if (!pblocktemplate.get()) return; CBlock *pblock = &pblocktemplate->block; diff --git a/src/miner.h b/src/miner.h index 51d6a2e3e..0bc76be10 100644 --- a/src/miner.h +++ b/src/miner.h @@ -11,7 +11,8 @@ /** Run the miner threads */ void GenerateBitcoins(bool fGenerate, CWallet* pwallet); /** Generate a new block, without valid proof-of-work */ -CBlockTemplate* CreateNewBlock(CReserveKey& reservekey); +CBlockTemplate* CreateNewBlock(CScript& scriptPubKeyIn); +CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey); /** Modify the extranonce in a block */ void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce); /** Do mining precalculation */ diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp index 25111d378..8b562680a 100644 --- a/src/rpcmining.cpp +++ b/src/rpcmining.cpp @@ -149,7 +149,7 @@ Value getwork(const Array& params, bool fHelp) nStart = GetTime(); // Create new block - pblocktemplate = CreateNewBlock(*pMiningKey); + pblocktemplate = CreateNewBlockWithKey(*pMiningKey); if (!pblocktemplate) throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory"); vNewBlockTemplate.push_back(pblocktemplate); @@ -280,7 +280,7 @@ Value getblocktemplate(const Array& params, bool fHelp) delete pblocktemplate; pblocktemplate = NULL; } - pblocktemplate = CreateNewBlock(*pMiningKey); + pblocktemplate = CreateNewBlockWithKey(*pMiningKey); if (!pblocktemplate) throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory"); diff --git a/src/test/miner_tests.cpp b/src/test/miner_tests.cpp index bd1d998c4..818350414 100644 --- a/src/test/miner_tests.cpp +++ b/src/test/miner_tests.cpp @@ -56,7 +56,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) uint256 hash; // Simple block creation, nothing special yet: - BOOST_CHECK(pblocktemplate = CreateNewBlock(reservekey)); + BOOST_CHECK(pblocktemplate = CreateNewBlockWithKey(reservekey)); // We can't make transactions until we have inputs // Therefore, load 100 blocks :) @@ -82,7 +82,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) delete pblocktemplate; // Just to make sure we can still make simple blocks - BOOST_CHECK(pblocktemplate = CreateNewBlock(reservekey)); + BOOST_CHECK(pblocktemplate = CreateNewBlockWithKey(reservekey)); // block sigops > limit: 1000 CHECKMULTISIG + 1 tx.vin.resize(1); @@ -99,7 +99,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) mempool.addUnchecked(hash, tx); tx.vin[0].prevout.hash = hash; } - BOOST_CHECK(pblocktemplate = CreateNewBlock(reservekey)); + BOOST_CHECK(pblocktemplate = CreateNewBlockWithKey(reservekey)); delete pblocktemplate; mempool.clear(); @@ -119,14 +119,14 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) mempool.addUnchecked(hash, tx); tx.vin[0].prevout.hash = hash; } - BOOST_CHECK(pblocktemplate = CreateNewBlock(reservekey)); + BOOST_CHECK(pblocktemplate = CreateNewBlockWithKey(reservekey)); delete pblocktemplate; mempool.clear(); // orphan in mempool hash = tx.GetHash(); mempool.addUnchecked(hash, tx); - BOOST_CHECK(pblocktemplate = CreateNewBlock(reservekey)); + BOOST_CHECK(pblocktemplate = CreateNewBlockWithKey(reservekey)); delete pblocktemplate; mempool.clear(); @@ -144,7 +144,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) tx.vout[0].nValue = 5900000000LL; hash = tx.GetHash(); mempool.addUnchecked(hash, tx); - BOOST_CHECK(pblocktemplate = CreateNewBlock(reservekey)); + BOOST_CHECK(pblocktemplate = CreateNewBlockWithKey(reservekey)); delete pblocktemplate; mempool.clear(); @@ -155,7 +155,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) tx.vout[0].nValue = 0; hash = tx.GetHash(); mempool.addUnchecked(hash, tx); - BOOST_CHECK(pblocktemplate = CreateNewBlock(reservekey)); + BOOST_CHECK(pblocktemplate = CreateNewBlockWithKey(reservekey)); delete pblocktemplate; mempool.clear(); @@ -173,7 +173,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) tx.vout[0].nValue -= 1000000; hash = tx.GetHash(); mempool.addUnchecked(hash,tx); - BOOST_CHECK(pblocktemplate = CreateNewBlock(reservekey)); + BOOST_CHECK(pblocktemplate = CreateNewBlockWithKey(reservekey)); delete pblocktemplate; mempool.clear(); @@ -187,17 +187,17 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) tx.vout[0].scriptPubKey = CScript() << OP_2; hash = tx.GetHash(); mempool.addUnchecked(hash, tx); - BOOST_CHECK(pblocktemplate = CreateNewBlock(reservekey)); + BOOST_CHECK(pblocktemplate = CreateNewBlockWithKey(reservekey)); delete pblocktemplate; mempool.clear(); // subsidy changing int nHeight = pindexBest->nHeight; pindexBest->nHeight = 209999; - BOOST_CHECK(pblocktemplate = CreateNewBlock(reservekey)); + BOOST_CHECK(pblocktemplate = CreateNewBlockWithKey(reservekey)); delete pblocktemplate; pindexBest->nHeight = 210000; - BOOST_CHECK(pblocktemplate = CreateNewBlock(reservekey)); + BOOST_CHECK(pblocktemplate = CreateNewBlockWithKey(reservekey)); delete pblocktemplate; pindexBest->nHeight = nHeight; } From 7bb0f6c5e89a4d5c9a6ef42f5643f42646da44c9 Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Sat, 24 Aug 2013 00:45:17 -0400 Subject: [PATCH 2/3] RPC: getblocktemplate does not require a key, to create a block template getblocktemplate only uses certain portions of the coinbase transaction, notably ignoring the coinbase TX output entirely. Use CreateNewBlock() rather than CreateNewBlockWithKey(), eliminating the needless key passing. Should be zero behavior changes. --- src/rpcmining.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp index 8b562680a..c7f516caa 100644 --- a/src/rpcmining.cpp +++ b/src/rpcmining.cpp @@ -280,7 +280,8 @@ Value getblocktemplate(const Array& params, bool fHelp) delete pblocktemplate; pblocktemplate = NULL; } - pblocktemplate = CreateNewBlockWithKey(*pMiningKey); + CScript scriptDummy = CScript() << OP_TRUE; + pblocktemplate = CreateNewBlock(scriptDummy); if (!pblocktemplate) throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory"); From f1dbed9233fb138026c646db0ac34e83ae2114f1 Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Sun, 25 Aug 2013 20:16:23 -0400 Subject: [PATCH 3/3] miner: constify CreateNewBlock() arg scriptPubKeyIn --- src/miner.cpp | 2 +- src/miner.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/miner.cpp b/src/miner.cpp index bea69ad1b..3ecf1609e 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -141,7 +141,7 @@ public: } }; -CBlockTemplate* CreateNewBlock(CScript& scriptPubKeyIn) +CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn) { // Create new block auto_ptr pblocktemplate(new CBlockTemplate()); diff --git a/src/miner.h b/src/miner.h index 0bc76be10..36d58be00 100644 --- a/src/miner.h +++ b/src/miner.h @@ -11,7 +11,7 @@ /** Run the miner threads */ void GenerateBitcoins(bool fGenerate, CWallet* pwallet); /** Generate a new block, without valid proof-of-work */ -CBlockTemplate* CreateNewBlock(CScript& scriptPubKeyIn); +CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn); CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey); /** Modify the extranonce in a block */ void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce);