Skip to content

Commit fe713a6

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 77ed5c0 commit fe713a6

File tree

10 files changed

+36
-50
lines changed

10 files changed

+36
-50
lines changed

src/chainparams.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
#include <boost/assign/list_of.hpp>
1919

2020
// Safer for users if they load incorrect parameters via arguments.
21-
static std::vector<unsigned char> CommitToArguments(const Consensus::Params& params, const std::string& networkID, const CScript& signblockscript)
21+
static std::vector<unsigned char> CommitToArguments(const Consensus::Params& params, const std::string& networkID)
2222
{
2323
CSHA256 sha2;
2424
unsigned char commitment[32];
2525
sha2.Write((const unsigned char*)networkID.c_str(), networkID.length());
2626
sha2.Write((const unsigned char*)HexStr(params.fedpegScript).c_str(), HexStr(params.fedpegScript).length());
27-
sha2.Write((const unsigned char*)HexStr(signblockscript).c_str(), HexStr(signblockscript).length());
27+
sha2.Write((const unsigned char*)HexStr(params.signblockScript).c_str(), HexStr(params.signblockScript).length());
2828
sha2.Finalize(commitment);
2929
return std::vector<unsigned char>(commitment, commitment + 32);
3030
}
@@ -41,19 +41,19 @@ static CScript StrHexToScriptWithDefault(std::string strScript, const CScript de
4141
return returnScript;
4242
}
4343

44-
static CBlock CreateGenesisBlock(const Consensus::Params& params, const std::string& networkID, uint32_t nTime, const CScript& scriptChallenge, int32_t nVersion)
44+
static CBlock CreateGenesisBlock(const Consensus::Params& params, const std::string& networkID, uint32_t nTime, int32_t nVersion)
4545
{
4646
CMutableTransaction txNew;
4747
txNew.nVersion = 1;
4848
txNew.vin.resize(1);
4949
// Any consensus-related values that are command-line set can be added here for anti-footgun
50-
txNew.vin[0].scriptSig = CScript(CommitToArguments(params, networkID, scriptChallenge));
50+
txNew.vin[0].scriptSig = CScript(CommitToArguments(params, networkID));
5151
txNew.vout.clear();
5252
txNew.vout.push_back(CTxOut(CAsset(), 0, CScript() << OP_RETURN));
5353

5454
CBlock genesis;
5555
genesis.nTime = nTime;
56-
genesis.proof = CProof(scriptChallenge, CScript());
56+
genesis.proof = CProof(CScript());
5757
genesis.nVersion = nVersion;
5858
genesis.vtx.push_back(MakeTransactionRef(std::move(txNew)));
5959
genesis.hashPrevBlock.SetNull();
@@ -132,6 +132,10 @@ class CCustomParams : public CChainParams {
132132
// bitcoin regtest is the parent chain by default
133133
parentGenesisBlockHash = uint256S(GetArg("-parentgenesisblockhash", "0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"));
134134

135+
const CScript default_script(CScript() << OP_TRUE);
136+
consensus.signblockScript = StrHexToScriptWithDefault(GetArg("-signblockscript", ""), default_script);
137+
consensus.fedpegScript = StrHexToScriptWithDefault(GetArg("-fedpegscript", ""), default_script);
138+
135139
nDefaultPort = GetArg("-ndefaultport", 7042);
136140
nPruneAfterHeight = GetArg("-npruneafterheight", 1000);
137141
fMiningRequiresPeers = GetBoolArg("-fminingrequirespeers", false);
@@ -146,10 +150,6 @@ class CCustomParams : public CChainParams {
146150
{
147151
this->UpdateFromArgs();
148152

149-
const CScript defaultRegtestScript(CScript() << OP_TRUE);
150-
CScript genesisChallengeScript = StrHexToScriptWithDefault(GetArg("-signblockscript", ""), defaultRegtestScript);
151-
consensus.fedpegScript = StrHexToScriptWithDefault(GetArg("-fedpegscript", ""), defaultRegtestScript);
152-
153153
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28;
154154
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime = 0;
155155
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout = 999999999999ULL;
@@ -166,12 +166,12 @@ class CCustomParams : public CChainParams {
166166
pchMessageStart[3] = 0xda;
167167

168168
// Generate pegged Bitcoin asset
169-
std::vector<unsigned char> commit = CommitToArguments(consensus, strNetworkID, genesisChallengeScript);
169+
std::vector<unsigned char> commit = CommitToArguments(consensus, strNetworkID);
170170
uint256 entropy;
171171
GenerateAssetEntropy(entropy, COutPoint(uint256(commit), 0), parentGenesisBlockHash);
172172
CalculateAsset(consensus.pegged_asset, entropy);
173173

174-
genesis = CreateGenesisBlock(consensus, strNetworkID, 1296688602, genesisChallengeScript, 1);
174+
genesis = CreateGenesisBlock(consensus, strNetworkID, 1296688602, 1);
175175
AppendInitialIssuance(genesis, COutPoint(uint256(commit), 0), parentGenesisBlockHash, 100, 21000000000000, 0, 0, CScript() << OP_TRUE);
176176
consensus.hashGenesisBlock = genesis.GetHash();
177177

@@ -257,4 +257,3 @@ void UpdateBIP9Parameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_
257257
{
258258
globalChainParams->UpdateBIP9Parameters(d, nStartTime, nTimeout);
259259
}
260-

src/consensus/params.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ struct Params {
6969
CScript fedpegScript;
7070
CAsset pegged_asset;
7171
uint256 defaultAssumeValid;
72+
CScript signblockScript;
7273
};
7374
} // namespace Consensus
7475

src/pow.cpp

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

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

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

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

4140
bool CheckBitcoinProof(uint256 hash, unsigned int nBits)
@@ -61,14 +60,14 @@ bool CheckProof(const CBlockHeader& block, const Consensus::Params& params)
6160
{
6261
if (block.GetHash() == params.hashGenesisBlock)
6362
return true;
64-
return GenericVerifyScript(block.proof.solution, block.proof.challenge, SCRIPT_VERIFY_P2SH, block);
63+
return GenericVerifyScript(block.proof.solution, params.signblockScript, SCRIPT_VERIFY_P2SH, block);
6564
}
6665

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

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

src/pow.h

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

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

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

src/primitives/block.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
std::string CProof::ToString() const
1515
{
16-
return strprintf("CProof(challenge=%s, solution=%s)",
17-
ScriptToAsmStr(challenge), ScriptToAsmStr(solution));
16+
return strprintf("CProof(solution=%s)",
17+
ScriptToAsmStr(solution));
1818
}
1919

2020
uint256 CBlockHeader::GetHash() const

src/primitives/block.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,31 @@
1414
class CProof
1515
{
1616
public:
17-
CScript challenge;
1817
CScript solution;
1918

2019
CProof()
2120
{
2221
SetNull();
2322
}
24-
CProof(CScript challengeIn, CScript solutionIn) : challenge(challengeIn), solution(solutionIn) {}
23+
CProof(CScript solutionIn) : solution(solutionIn) {}
2524

2625
ADD_SERIALIZE_METHODS;
2726

2827
template <typename Stream, typename Operation>
2928
inline void SerializationOp(Stream& s, Operation ser_action)
3029
{
31-
READWRITE(*(CScriptBase*)(&challenge));
3230
if (!(s.GetType() & SER_GETHASH))
3331
READWRITE(*(CScriptBase*)(&solution));
3432
}
3533

3634
void SetNull()
3735
{
38-
challenge.clear();
3936
solution.clear();
4037
}
4138

4239
bool IsNull() const
4340
{
44-
return challenge.empty();
41+
return solution.empty();
4542
}
4643

4744
std::string ToString() const;
@@ -94,7 +91,7 @@ class CBlockHeader
9491

9592
bool IsNull() const
9693
{
97-
return proof.IsNull();
94+
return hashPrevBlock.IsNull();
9895
}
9996

10097
uint256 GetHash() const;

src/rpc/blockchain.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "coins.h"
1111
#include "consensus/validation.h"
1212
#include "validation.h"
13+
#include "core_io.h"
1314
#include "policy/policy.h"
1415
#include "primitives/transaction.h"
1516
#include "rpc/server.h"
@@ -74,7 +75,6 @@ UniValue blockheaderToJSON(const CBlockIndex* blockindex)
7475
result.push_back(Pair("time", (int64_t)blockindex->nTime));
7576
result.push_back(Pair("mediantime", (int64_t)blockindex->GetMedianTimePast()));
7677
result.push_back(Pair("nonce", (uint64_t)GetNonce(blockindex->GetBlockHeader())));
77-
result.push_back(Pair("bits", GetChallengeStr(blockindex->GetBlockHeader())));
7878
result.push_back(Pair("difficulty", GetDifficulty(blockindex)));
7979
result.push_back(Pair("chainwork", blockindex->nChainWork.GetHex()));
8080

@@ -118,7 +118,6 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx
118118
result.push_back(Pair("time", block.GetBlockTime()));
119119
result.push_back(Pair("mediantime", (int64_t)blockindex->GetMedianTimePast()));
120120
result.push_back(Pair("nonce", (uint64_t)GetNonce(block)));
121-
result.push_back(Pair("bits", GetChallengeStr(block)));
122121
result.push_back(Pair("difficulty", GetDifficulty(blockindex)));
123122
result.push_back(Pair("chainwork", blockindex->nChainWork.GetHex()));
124123

@@ -1046,6 +1045,8 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
10461045
"\nResult:\n"
10471046
"{\n"
10481047
" \"chain\": \"xxxx\", (string) current network name as defined in BIP70 (main, test, regtest)\n"
1048+
" \"signblockhex\": \"xxxx\", (string) the scriptPubKey for signing blocks as a hex string.\n"
1049+
" \"signblockasm\": \"xxxx\", (string) the scriptPubKey for signing blocks in a format more readable for humans (asm).\n"
10491050
" \"blocks\": xxxxxx, (numeric) the current number of blocks processed in the server\n"
10501051
" \"headers\": xxxxxx, (numeric) the current number of headers we have validated\n"
10511052
" \"bestblockhash\": \"...\", (string) the hash of the currently best block\n"
@@ -1079,10 +1080,13 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
10791080
+ HelpExampleRpc("getblockchaininfo", "")
10801081
);
10811082

1083+
const Consensus::Params& consensusParams = Params().GetConsensus();
10821084
LOCK(cs_main);
10831085

10841086
UniValue obj(UniValue::VOBJ);
10851087
obj.push_back(Pair("chain", Params().NetworkIDString()));
1088+
obj.push_back(Pair("signblockhex", HexStr(consensusParams.signblockScript)));
1089+
obj.push_back(Pair("signblockasm", ScriptToAsmStr(consensusParams.signblockScript)));
10861090
obj.push_back(Pair("blocks", (int)chainActive.Height()));
10871091
obj.push_back(Pair("headers", pindexBestHeader ? pindexBestHeader->nHeight : -1));
10881092
obj.push_back(Pair("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex()));
@@ -1092,7 +1096,6 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
10921096
obj.push_back(Pair("chainwork", chainActive.Tip()->nChainWork.GetHex()));
10931097
obj.push_back(Pair("pruned", fPruneMode));
10941098

1095-
const Consensus::Params& consensusParams = Params().GetConsensus();
10961099
UniValue bip9_softforks(UniValue::VOBJ);
10971100
BIP9SoftForkDescPushBack(bip9_softforks, "csv", consensusParams, Consensus::DEPLOYMENT_CSV);
10981101
BIP9SoftForkDescPushBack(bip9_softforks, "segwit", consensusParams, Consensus::DEPLOYMENT_SEGWIT);

src/rpc/mining.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,15 @@ UniValue combineblocksigs(const JSONRPCRequest& request)
216216
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");
217217

218218
UniValue result(UniValue::VOBJ);
219+
const Consensus::Params& consensusParams = Params().GetConsensus();
219220
const UniValue& sigs = request.params[1].get_array();
220221
for (unsigned int i = 0; i < sigs.size(); i++) {
221222
const std::string& sig = sigs[i].get_str();
222223
if (!IsHex(sig))
223224
continue;
224225
std::vector<unsigned char> vchScript = ParseHex(sig);
225-
block.proof.solution = CombineBlockSignatures(block, block.proof.solution, CScript(vchScript.begin(), vchScript.end()));
226-
if (CheckProof(block, Params().GetConsensus())) {
226+
block.proof.solution = CombineBlockSignatures(consensusParams, block, block.proof.solution, CScript(vchScript.begin(), vchScript.end()));
227+
if (CheckProof(block, consensusParams)) {
227228
result.push_back(Pair("hex", EncodeHexBlock(block)));
228229
result.push_back(Pair("complete", true));
229230
return result;
@@ -751,7 +752,6 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
751752
result.push_back(Pair("coinbaseaux", aux));
752753
result.push_back(Pair("coinbasevalue", (int64_t)pblock->vtx[0]->vout[0].nValue.GetAmount()));
753754
result.push_back(Pair("longpollid", chainActive.Tip()->GetBlockHash().GetHex() + i64tostr(nTransactionsUpdatedLast)));
754-
result.push_back(Pair("target", GetChallengeStrHex(*pblock)));
755755
result.push_back(Pair("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1));
756756
result.push_back(Pair("mutable", aMutable));
757757
result.push_back(Pair("noncerange", "00000000ffffffff"));
@@ -768,7 +768,6 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
768768
result.push_back(Pair("weightlimit", (int64_t)MAX_BLOCK_WEIGHT));
769769
}
770770
result.push_back(Pair("curtime", pblock->GetBlockTime()));
771-
result.push_back(Pair("bits", GetChallengeStr(*pblock)));
772771
result.push_back(Pair("height", (int64_t)(pindexPrev->nHeight+1)));
773772

774773
if (!pblocktemplate->vchCoinbaseCommitment.empty() && fSupportsSegwit) {

src/test/test_bitcoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::string& fedp
7979
newTransaction.vout[i].scriptPubKey = scriptPubKey;
8080
const_cast<CBlock&>(Params().GenesisBlock()).vtx[gen_size-1] = MakeTransactionRef(newTransaction);
8181
const_cast<CBlock&>(Params().GenesisBlock()).hashMerkleRoot = BlockMerkleRoot(Params().GenesisBlock());
82-
const_cast<CBlock&>(Params().GenesisBlock()).proof = CProof(CScript()<<OP_TRUE, CScript());
82+
const_cast<CBlock&>(Params().GenesisBlock()).proof = CProof(CScript());
8383
const_cast<Consensus::Params&>(Params().GetConsensus()).hashGenesisBlock = Params().GenesisBlock().GetHash();
8484

8585
ClearDatadirCache();

src/wallet/rpcwallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3374,7 +3374,7 @@ UniValue signblock(const JSONRPCRequest& request)
33743374
}
33753375

33763376
block.proof.solution = CScript();
3377-
MaybeGenerateProof(&block, pwalletMain);
3377+
MaybeGenerateProof(Params().GetConsensus(), &block, pwalletMain);
33783378
return HexStr(block.proof.solution.begin(), block.proof.solution.end());
33793379
}
33803380

0 commit comments

Comments
 (0)