Skip to content

Commit 38bf14a

Browse files
committed
HF: Move block.proof.challenge to Consensus::Params::signblockScript
Since the scriptPubKey for signing blocks never changes, there's no point in repeating it with every block header.
1 parent 49eed59 commit 38bf14a

File tree

10 files changed

+33
-45
lines changed

10 files changed

+33
-45
lines changed

src/chainparams.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
#include "chainparamsseeds.h"
2121

2222
// Safer for users if they load incorrect parameters via arguments.
23-
static std::vector<unsigned char> CommitToArguments(const Consensus::Params& params, const std::string& networkID, const CScript& signblockscript)
23+
static std::vector<unsigned char> CommitToArguments(const Consensus::Params& params, const std::string& networkID)
2424
{
2525
CRIPEMD160 ripemd;
2626
unsigned char commitment[20];
2727
ripemd.Write((const unsigned char*)networkID.c_str(), networkID.length());
2828
ripemd.Write((const unsigned char*)HexStr(params.fedpegScript).c_str(), HexStr(params.fedpegScript).length());
29-
ripemd.Write((const unsigned char*)HexStr(signblockscript).c_str(), HexStr(signblockscript).length());
29+
ripemd.Write((const unsigned char*)HexStr(params.signblockScript).c_str(), HexStr(params.signblockScript).length());
3030
ripemd.Finalize(commitment);
3131
return std::vector<unsigned char>(commitment, commitment + 20);
3232
}
@@ -43,15 +43,15 @@ static CScript StrHexToScriptWithDefault(std::string strScript, const CScript de
4343
return returnScript;
4444
}
4545

46-
static CBlock CreateGenesisBlock(const Consensus::Params& params, const std::string& networkID, const CScript& genesisOutputScript, uint32_t nTime, const CScript& scriptChallenge, int32_t nVersion, const CAmount& genesisReward, const uint32_t rewardShards)
46+
static CBlock CreateGenesisBlock(const Consensus::Params& params, const std::string& networkID, const CScript& genesisOutputScript, uint32_t nTime, int32_t nVersion, const CAmount& genesisReward, const uint32_t rewardShards)
4747
{
4848
// Shards must be evenly divisible
4949
assert(MAX_MONEY % rewardShards == 0);
5050
CMutableTransaction txNew;
5151
txNew.nVersion = 1;
5252
txNew.vin.resize(1);
5353
// Any consensus-related values that are command-line set can be added here for anti-footgun
54-
txNew.vin[0].scriptSig = CScript(CommitToArguments(params, networkID, scriptChallenge));
54+
txNew.vin[0].scriptSig = CScript(CommitToArguments(params, networkID));
5555
txNew.vout.resize(rewardShards);
5656
for (unsigned int i = 0; i < rewardShards; i++) {
5757
txNew.vout[i].nValue = genesisReward/rewardShards;
@@ -60,7 +60,7 @@ static CBlock CreateGenesisBlock(const Consensus::Params& params, const std::str
6060

6161
CBlock genesis;
6262
genesis.nTime = nTime;
63-
genesis.proof = CProof(scriptChallenge, CScript());
63+
genesis.proof = CProof(CScript());
6464
genesis.nVersion = nVersion;
6565
genesis.vtx.push_back(txNew);
6666
genesis.hashPrevBlock.SetNull();
@@ -85,7 +85,7 @@ class CElementsParams : public CChainParams {
8585
CScript defaultSignblockScript;
8686
// Default blocksign script for elements
8787
defaultSignblockScript = CScript() << OP_2 << ParseHex("03206b45265ae687dfdc602b8faa7dd749d7865b0e51f986e12c532229f0c998be") << ParseHex("02cc276552e180061f64dc16e2a02e7f9ecbcc744dea84eddbe991721824df825c") << ParseHex("0204c6be425356d9200a3303d95f2c39078cc9473ca49619da1e0ec233f27516ca") << OP_3 << OP_CHECKMULTISIG;
88-
CScript genesisChallengeScript = StrHexToScriptWithDefault(GetArg("-signblockscript", "", mapArgs), defaultSignblockScript);
88+
consensus.signblockScript = StrHexToScriptWithDefault(GetArg("-signblockscript", "", mapArgs), defaultSignblockScript);
8989
CScript defaultFedpegScript;
9090
defaultFedpegScript = CScript() << OP_2 << ParseHex("02d51090b27ca8f1cc04984614bd749d8bab6f2a3681318d3fd0dd43b2a39dd774") << ParseHex("03a75bd7ac458b19f98047c76a6ffa442e592148c5d23a1ec82d379d5d558f4fd8") << ParseHex("034c55bede1bce8e486080f8ebb7a0e8f106b49efb295a8314da0e1b1723738c66") << OP_3 << OP_CHECKMULTISIG;
9191
consensus.fedpegScript = StrHexToScriptWithDefault(GetArg("-fedpegscript", "", mapArgs), defaultFedpegScript);
@@ -138,7 +138,7 @@ class CElementsParams : public CChainParams {
138138

139139
parentGenesisBlockHash = uint256S("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943");
140140
CScript scriptDestination(CScript() << std::vector<unsigned char>(parentGenesisBlockHash.begin(), parentGenesisBlockHash.end()) << OP_WITHDRAWPROOFVERIFY);
141-
genesis = CreateGenesisBlock(consensus, strNetworkID, scriptDestination, 1231006505, genesisChallengeScript, 1, MAX_MONEY, 100);
141+
genesis = CreateGenesisBlock(consensus, strNetworkID, scriptDestination, 1231006505, 1, MAX_MONEY, 100);
142142
consensus.hashGenesisBlock = genesis.GetHash();
143143

144144
scriptCoinbaseDestination = CScript() << ParseHex("0229536c4c83789f59c30b93eb40d4abbd99b8dcc99ba8bd748f29e33c1d279e3c") << OP_CHECKSIG;
@@ -206,7 +206,7 @@ class CRegTestParams : public CChainParams {
206206
void Reset(const std::map<std::string, std::string>& mapArgs)
207207
{
208208
const CScript defaultRegtestScript(CScript() << OP_TRUE);
209-
CScript genesisChallengeScript = StrHexToScriptWithDefault(GetArg("-signblockscript", "", mapArgs), defaultRegtestScript);
209+
consensus.signblockScript = StrHexToScriptWithDefault(GetArg("-signblockscript", "", mapArgs), defaultRegtestScript);
210210
consensus.fedpegScript = StrHexToScriptWithDefault(GetArg("-fedpegscript", "", mapArgs), defaultRegtestScript);
211211

212212
strNetworkID = CHAINPARAMS_REGTEST;
@@ -241,7 +241,7 @@ class CRegTestParams : public CChainParams {
241241
nDefaultPort = 7042;
242242
nPruneAfterHeight = 1000;
243243

244-
genesis = CreateGenesisBlock(consensus, strNetworkID, defaultRegtestScript, 1296688602, genesisChallengeScript, 1, MAX_MONEY, 100);
244+
genesis = CreateGenesisBlock(consensus, strNetworkID, defaultRegtestScript, 1296688602, 1, MAX_MONEY, 100);
245245
consensus.hashGenesisBlock = genesis.GetHash();
246246

247247
parentGenesisBlockHash = uint256S("0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206");

src/consensus/params.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct Params {
6464
int64_t nPowTargetTimespan;
6565
int64_t DifficultyAdjustmentInterval() const { return nPowTargetTimespan / nPowTargetSpacing; }
6666
CScript fedpegScript;
67+
CScript signblockScript;
6768
};
6869
} // namespace Consensus
6970

src/pow.cpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,20 @@
2020
#include "wallet/wallet.h"
2121
#endif
2222

23-
CScript CombineBlockSignatures(const CBlockHeader& header, const CScript& scriptSig1, const CScript& scriptSig2)
23+
CScript CombineBlockSignatures(const Consensus::Params& params, const CBlockHeader& header, const CScript& scriptSig1, const CScript& scriptSig2)
2424
{
2525
SignatureData sig1(scriptSig1);
2626
SignatureData sig2(scriptSig2);
27-
return GenericCombineSignatures(header.proof.challenge, header, sig1, sig2).scriptSig;
27+
return GenericCombineSignatures(params.signblockScript, header, sig1, sig2).scriptSig;
2828
}
2929

3030
bool CheckChallenge(const CBlockHeader& block, const CBlockIndex& indexLast, const Consensus::Params& params)
3131
{
32-
return block.proof.challenge == indexLast.proof.challenge;
32+
return true;
3333
}
3434

3535
void ResetChallenge(CBlockHeader& block, const CBlockIndex& indexLast, const Consensus::Params& params)
3636
{
37-
block.proof.challenge = indexLast.proof.challenge;
3837
}
3938

4039
bool CheckBitcoinProof(const CBlockHeader& block)
@@ -62,14 +61,14 @@ bool CheckProof(const CBlockHeader& block, const Consensus::Params& params)
6261
{
6362
if (block.GetHash() == params.hashGenesisBlock)
6463
return true;
65-
return GenericVerifyScript(block.proof.solution, block.proof.challenge, SCRIPT_VERIFY_P2SH, block);
64+
return GenericVerifyScript(block.proof.solution, params.signblockScript, SCRIPT_VERIFY_P2SH, block);
6665
}
6766

68-
bool MaybeGenerateProof(CBlockHeader *pblock, CWallet *pwallet)
67+
bool MaybeGenerateProof(const Consensus::Params& params, CBlockHeader *pblock, CWallet *pwallet)
6968
{
7069
#ifdef ENABLE_WALLET
7170
SignatureData solution(pblock->proof.solution);
72-
bool res = GenericSignScript(*pwallet, *pblock, pblock->proof.challenge, solution);
71+
bool res = GenericSignScript(*pwallet, *pblock, params.signblockScript, solution);
7372
pblock->proof.solution = solution.scriptSig;
7473
return res;
7574
#endif
@@ -86,16 +85,6 @@ double GetChallengeDifficulty(const CBlockIndex* blockindex)
8685
return 1;
8786
}
8887

89-
std::string GetChallengeStr(const CBlockIndex& block)
90-
{
91-
return ScriptToAsmStr(block.proof.challenge);
92-
}
93-
94-
std::string GetChallengeStrHex(const CBlockIndex& block)
95-
{
96-
return ScriptToAsmStr(block.proof.challenge);
97-
}
98-
9988
uint32_t GetNonce(const CBlockHeader& block)
10089
{
10190
return 1;

src/pow.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,15 @@ class uint256;
2323
bool CheckBitcoinProof(const CBlockHeader& block);
2424
bool CheckProof(const CBlockHeader& block, const Consensus::Params&);
2525
/** Scans nonces looking for a hash with at least some zero bits */
26-
bool MaybeGenerateProof(CBlockHeader* pblock, CWallet* pwallet);
26+
bool MaybeGenerateProof(const Consensus::Params& params, CBlockHeader* pblock, CWallet* pwallet);
2727
void ResetProof(CBlockHeader& block);
2828
bool CheckChallenge(const CBlockHeader& block, const CBlockIndex& indexLast, const Consensus::Params&);
2929
void ResetChallenge(CBlockHeader& block, const CBlockIndex& indexLast, const Consensus::Params&);
3030

31-
CScript CombineBlockSignatures(const CBlockHeader& header, const CScript& scriptSig1, const CScript& scriptSig2);
31+
CScript CombineBlockSignatures(const Consensus::Params& params, const CBlockHeader& header, const CScript& scriptSig1, const CScript& scriptSig2);
3232

3333
/** Avoid using these functions when possible */
3434
double GetChallengeDifficulty(const CBlockIndex* blockindex);
35-
std::string GetChallengeStr(const CBlockIndex& block);
36-
std::string GetChallengeStrHex(const CBlockIndex& block);
3735
uint32_t GetNonce(const CBlockHeader& block);
3836
void SetNonce(CBlockHeader& block, uint32_t nNonce);
3937

src/primitives/block.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ std::string CBitcoinProof::ToString() const
1919

2020
std::string CProof::ToString() const
2121
{
22-
return strprintf("CProof(challenge=%s, solution=%s)",
23-
ScriptToAsmStr(challenge), ScriptToAsmStr(solution));
22+
return strprintf("CProof(solution=%s)",
23+
ScriptToAsmStr(solution));
2424
}
2525

2626
uint256 CBlockHeader::GetHash() const

src/primitives/block.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,31 @@ class CBitcoinProof
5151
class CProof
5252
{
5353
public:
54-
CScript challenge;
5554
CScript solution;
5655

5756
CProof()
5857
{
5958
SetNull();
6059
}
61-
CProof(CScript challengeIn, CScript solutionIn) : challenge(challengeIn), solution(solutionIn) {}
60+
CProof(CScript solutionIn) : solution(solutionIn) {}
6261

6362
ADD_SERIALIZE_METHODS;
6463

6564
template <typename Stream, typename Operation>
6665
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
6766
{
68-
READWRITE(*(CScriptBase*)(&challenge));
6967
if (!(nType & SER_GETHASH))
7068
READWRITE(*(CScriptBase*)(&solution));
7169
}
7270

7371
void SetNull()
7472
{
75-
challenge.clear();
7673
solution.clear();
7774
}
7875

7976
bool IsNull() const
8077
{
81-
return challenge.empty();
78+
return solution.empty();
8279
}
8380

8481
std::string ToString() const;

src/rpc/blockchain.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "checkpoints.h"
1010
#include "coins.h"
1111
#include "consensus/validation.h"
12+
#include "core_io.h"
1213
#include "main.h"
1314
#include "policy/policy.h"
1415
#include "primitives/transaction.h"
@@ -62,7 +63,7 @@ UniValue blockheaderToJSON(const CBlockIndex* blockindex)
6263
result.push_back(Pair("time", (int64_t)blockindex->nTime));
6364
result.push_back(Pair("mediantime", (int64_t)blockindex->GetMedianTimePast()));
6465
result.push_back(Pair("nonce", (uint64_t)GetNonce(blockindex->GetBlockHeader())));
65-
result.push_back(Pair("bits", GetChallengeStr(blockindex->GetBlockHeader())));
66+
result.push_back(Pair("bits", ScriptToAsmStr(Params().GetConsensus().signblockScript)));
6667
result.push_back(Pair("difficulty", GetDifficulty(blockindex)));
6768
result.push_back(Pair("chainwork", blockindex->nChainWork.GetHex()));
6869

@@ -106,7 +107,7 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx
106107
result.push_back(Pair("time", block.GetBlockTime()));
107108
result.push_back(Pair("mediantime", (int64_t)blockindex->GetMedianTimePast()));
108109
result.push_back(Pair("nonce", (uint64_t)GetNonce(block)));
109-
result.push_back(Pair("bits", GetChallengeStr(block)));
110+
result.push_back(Pair("bits", ScriptToAsmStr(Params().GetConsensus().signblockScript)));
110111
result.push_back(Pair("difficulty", GetDifficulty(blockindex)));
111112
result.push_back(Pair("chainwork", blockindex->nChainWork.GetHex()));
112113

src/rpc/mining.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,15 @@ UniValue combineblocksigs(const UniValue& params, bool fHelp)
211211
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");
212212

213213
UniValue result(UniValue::VOBJ);
214+
const Consensus::Params& consensusParams = Params().GetConsensus();
214215
const UniValue& sigs = params[1].get_array();
215216
for (unsigned int i = 0; i < sigs.size(); i++) {
216217
const std::string& sig = sigs[i].get_str();
217218
if (!IsHex(sig))
218219
continue;
219220
std::vector<unsigned char> vchScript = ParseHex(sig);
220-
block.proof.solution = CombineBlockSignatures(block, block.proof.solution, CScript(vchScript.begin(), vchScript.end()));
221-
if (CheckProof(block, Params().GetConsensus())) {
221+
block.proof.solution = CombineBlockSignatures(consensusParams, block, block.proof.solution, CScript(vchScript.begin(), vchScript.end()));
222+
if (CheckProof(block, consensusParams)) {
222223
result.push_back(Pair("hex", EncodeHexBlock(block)));
223224
result.push_back(Pair("complete", true));
224225
return result;
@@ -662,6 +663,7 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
662663
aMutable.push_back("transactions");
663664
aMutable.push_back("prevblock");
664665

666+
const std::string signblockStr = ScriptToAsmStr(Params().GetConsensus().signblockScript);
665667
UniValue result(UniValue::VOBJ);
666668
result.push_back(Pair("capabilities", aCaps));
667669

@@ -725,7 +727,7 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
725727
result.push_back(Pair("coinbaseaux", aux));
726728
result.push_back(Pair("coinbasevalue", (int64_t)pblock->vtx[0].vout[0].nValue.GetAmount()));
727729
result.push_back(Pair("longpollid", chainActive.Tip()->GetBlockHash().GetHex() + i64tostr(nTransactionsUpdatedLast)));
728-
result.push_back(Pair("target", GetChallengeStrHex(*pblock)));
730+
result.push_back(Pair("target", signblockStr));
729731
result.push_back(Pair("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1));
730732
result.push_back(Pair("mutable", aMutable));
731733
result.push_back(Pair("noncerange", "00000000ffffffff"));
@@ -738,7 +740,7 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
738740
result.push_back(Pair("sizelimit", (int64_t)MAX_BLOCK_SERIALIZED_SIZE));
739741
result.push_back(Pair("weightlimit", (int64_t)MAX_BLOCK_WEIGHT));
740742
result.push_back(Pair("curtime", pblock->GetBlockTime()));
741-
result.push_back(Pair("bits", GetChallengeStr(*pblock)));
743+
result.push_back(Pair("bits", signblockStr));
742744
result.push_back(Pair("height", (int64_t)(pindexPrev->nHeight+1)));
743745
if (!pblocktemplate->vchCoinbaseCommitment.empty()) {
744746
result.push_back(Pair("default_witness_commitment", HexStr(pblocktemplate->vchCoinbaseCommitment.begin(), pblocktemplate->vchCoinbaseCommitment.end())));

src/test/test_bitcoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
6161
newCoinbase.vout[i].scriptPubKey = scriptPubKey;
6262
const_cast<CBlock&>(Params().GenesisBlock()).vtx[0] = newCoinbase;
6363
const_cast<CBlock&>(Params().GenesisBlock()).hashMerkleRoot = BlockMerkleRoot(Params().GenesisBlock());
64-
const_cast<CBlock&>(Params().GenesisBlock()).proof = CProof(CScript()<<OP_TRUE, CScript());
64+
const_cast<CBlock&>(Params().GenesisBlock()).proof = CProof(CScript());
6565
const_cast<Consensus::Params&>(Params().GetConsensus()).hashGenesisBlock = Params().GenesisBlock().GetHash();
6666

6767
ClearDatadirCache();

src/wallet/rpcwallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2694,7 +2694,7 @@ UniValue signblock(const UniValue& params, bool fHelp)
26942694
}
26952695

26962696
block.proof.solution = CScript();
2697-
MaybeGenerateProof(&block, pwalletMain);
2697+
MaybeGenerateProof(Params().GetConsensus(), &block, pwalletMain);
26982698
return HexStr(block.proof.solution.begin(), block.proof.solution.end());
26992699
}
27002700

0 commit comments

Comments
 (0)