Skip to content

Commit 3d83e51

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 afbd2a0 commit 3d83e51

File tree

10 files changed

+34
-47
lines changed

10 files changed

+34
-47
lines changed

src/chainparams.cpp

Lines changed: 10 additions & 10 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,25 +43,25 @@ 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, const CAsset& asset)
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, const CAsset& genesisAsset)
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;
58-
txNew.vout[i].nAsset = asset;
58+
txNew.vout[i].nAsset = genesisAsset;
5959
txNew.vout[i].scriptPubKey = genesisOutputScript;
6060
}
6161

6262
CBlock genesis;
6363
genesis.nTime = nTime;
64-
genesis.proof = CProof(scriptChallenge, CScript());
64+
genesis.proof = CProof(CScript());
6565
genesis.nVersion = nVersion;
6666
genesis.vtx.push_back(txNew);
6767
genesis.hashPrevBlock.SetNull();
@@ -86,7 +86,7 @@ class CElementsParams : public CChainParams {
8686
CScript defaultSignblockScript;
8787
// Default blocksign script for elements
8888
defaultSignblockScript = CScript() << OP_2 << ParseHex("03206b45265ae687dfdc602b8faa7dd749d7865b0e51f986e12c532229f0c998be") << ParseHex("02cc276552e180061f64dc16e2a02e7f9ecbcc744dea84eddbe991721824df825c") << ParseHex("0204c6be425356d9200a3303d95f2c39078cc9473ca49619da1e0ec233f27516ca") << OP_3 << OP_CHECKMULTISIG;
89-
CScript genesisChallengeScript = StrHexToScriptWithDefault(GetArg("-signblockscript", "", mapArgs), defaultSignblockScript);
89+
consensus.signblockScript = StrHexToScriptWithDefault(GetArg("-signblockscript", "", mapArgs), defaultSignblockScript);
9090
CScript defaultFedpegScript;
9191
defaultFedpegScript = CScript() << OP_2 << ParseHex("02d51090b27ca8f1cc04984614bd749d8bab6f2a3681318d3fd0dd43b2a39dd774") << ParseHex("03a75bd7ac458b19f98047c76a6ffa442e592148c5d23a1ec82d379d5d558f4fd8") << ParseHex("034c55bede1bce8e486080f8ebb7a0e8f106b49efb295a8314da0e1b1723738c66") << OP_3 << OP_CHECKMULTISIG;
9292
consensus.fedpegScript = StrHexToScriptWithDefault(GetArg("-fedpegscript", "", mapArgs), defaultFedpegScript);
@@ -142,7 +142,7 @@ class CElementsParams : public CChainParams {
142142

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

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

215215
strNetworkID = CHAINPARAMS_REGTEST;
@@ -247,7 +247,7 @@ class CRegTestParams : public CChainParams {
247247
// SHA256 of Bitcoin genesis mainnet hash for NUMS bitcoin asset id
248248
bitcoinID = BITCOINID;
249249

250-
genesis = CreateGenesisBlock(consensus, strNetworkID, defaultRegtestScript, 1296688602, genesisChallengeScript, 1, MAX_MONEY, 100, bitcoinID);
250+
genesis = CreateGenesisBlock(consensus, strNetworkID, defaultRegtestScript, 1296688602, 1, MAX_MONEY, 100, bitcoinID);
251251
consensus.hashGenesisBlock = genesis.GetHash();
252252

253253
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
@@ -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(const Sidechain::Bitcoin::CBlockHeader& block)
@@ -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(const Sidechain::Bitcoin::CBlockHeader& block);
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: 2 additions & 5 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, int nType, int nVersion)
3029
{
31-
READWRITE(*(CScriptBase*)(&challenge));
3230
if (!(nType & 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;

src/rpc/blockchain.cpp

Lines changed: 6 additions & 3 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,6 @@ 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())));
6666
result.push_back(Pair("difficulty", GetDifficulty(blockindex)));
6767
result.push_back(Pair("chainwork", blockindex->nChainWork.GetHex()));
6868

@@ -106,7 +106,6 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx
106106
result.push_back(Pair("time", block.GetBlockTime()));
107107
result.push_back(Pair("mediantime", (int64_t)blockindex->GetMedianTimePast()));
108108
result.push_back(Pair("nonce", (uint64_t)GetNonce(block)));
109-
result.push_back(Pair("bits", GetChallengeStr(block)));
110109
result.push_back(Pair("difficulty", GetDifficulty(blockindex)));
111110
result.push_back(Pair("chainwork", blockindex->nChainWork.GetHex()));
112111

@@ -843,6 +842,8 @@ UniValue getblockchaininfo(const UniValue& params, bool fHelp)
843842
"\nResult:\n"
844843
"{\n"
845844
" \"chain\": \"xxxx\", (string) current network name as defined in BIP70 (main, test, regtest)\n"
845+
" \"signblockhex\": \"xxxx\", (string) the scriptPubKey for signing blocks as a hex string.\n"
846+
" \"signblockasm\": \"xxxx\", (string) the scriptPubKey for signing blocks in a format more readable for humans (asm).\n"
846847
" \"blocks\": xxxxxx, (numeric) the current number of blocks processed in the server\n"
847848
" \"headers\": xxxxxx, (numeric) the current number of headers we have validated\n"
848849
" \"bestblockhash\": \"...\", (string) the hash of the currently best block\n"
@@ -879,10 +880,13 @@ UniValue getblockchaininfo(const UniValue& params, bool fHelp)
879880
+ HelpExampleRpc("getblockchaininfo", "")
880881
);
881882

883+
const Consensus::Params& consensusParams = Params().GetConsensus();
882884
LOCK(cs_main);
883885

884886
UniValue obj(UniValue::VOBJ);
885887
obj.push_back(Pair("chain", Params().NetworkIDString()));
888+
obj.push_back(Pair("signblockhex", HexStr(consensusParams.signblockScript)));
889+
obj.push_back(Pair("signblockasm", ScriptToAsmStr(consensusParams.signblockScript)));
886890
obj.push_back(Pair("blocks", (int)chainActive.Height()));
887891
obj.push_back(Pair("headers", pindexBestHeader ? pindexBestHeader->nHeight : -1));
888892
obj.push_back(Pair("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex()));
@@ -892,7 +896,6 @@ UniValue getblockchaininfo(const UniValue& params, bool fHelp)
892896
obj.push_back(Pair("chainwork", chainActive.Tip()->nChainWork.GetHex()));
893897
obj.push_back(Pair("pruned", fPruneMode));
894898

895-
const Consensus::Params& consensusParams = Params().GetConsensus();
896899
UniValue bip9_softforks(UniValue::VOBJ);
897900
BIP9SoftForkDescPushBack(bip9_softforks, "csv", consensusParams, Consensus::DEPLOYMENT_CSV);
898901
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
@@ -213,14 +213,15 @@ UniValue combineblocksigs(const UniValue& params, bool fHelp)
213213
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");
214214

215215
UniValue result(UniValue::VOBJ);
216+
const Consensus::Params& consensusParams = Params().GetConsensus();
216217
const UniValue& sigs = params[1].get_array();
217218
for (unsigned int i = 0; i < sigs.size(); i++) {
218219
const std::string& sig = sigs[i].get_str();
219220
if (!IsHex(sig))
220221
continue;
221222
std::vector<unsigned char> vchScript = ParseHex(sig);
222-
block.proof.solution = CombineBlockSignatures(block, block.proof.solution, CScript(vchScript.begin(), vchScript.end()));
223-
if (CheckProof(block, Params().GetConsensus())) {
223+
block.proof.solution = CombineBlockSignatures(consensusParams, block, block.proof.solution, CScript(vchScript.begin(), vchScript.end()));
224+
if (CheckProof(block, consensusParams)) {
224225
result.push_back(Pair("hex", EncodeHexBlock(block)));
225226
result.push_back(Pair("complete", true));
226227
return result;
@@ -728,7 +729,6 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
728729
result.push_back(Pair("coinbaseaux", aux));
729730
result.push_back(Pair("coinbasevalue", (int64_t)pblock->vtx[0].vout[0].nValue.GetAmount()));
730731
result.push_back(Pair("longpollid", chainActive.Tip()->GetBlockHash().GetHex() + i64tostr(nTransactionsUpdatedLast)));
731-
result.push_back(Pair("target", GetChallengeStrHex(*pblock)));
732732
result.push_back(Pair("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1));
733733
result.push_back(Pair("mutable", aMutable));
734734
result.push_back(Pair("noncerange", "00000000ffffffff"));
@@ -741,7 +741,6 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
741741
result.push_back(Pair("sizelimit", (int64_t)MAX_BLOCK_SERIALIZED_SIZE));
742742
result.push_back(Pair("weightlimit", (int64_t)MAX_BLOCK_WEIGHT));
743743
result.push_back(Pair("curtime", pblock->GetBlockTime()));
744-
result.push_back(Pair("bits", GetChallengeStr(*pblock)));
745744
result.push_back(Pair("height", (int64_t)(pindexPrev->nHeight+1)));
746745
if (!pblocktemplate->vchCoinbaseCommitment.empty()) {
747746
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
@@ -2944,7 +2944,7 @@ UniValue signblock(const UniValue& params, bool fHelp)
29442944
}
29452945

29462946
block.proof.solution = CScript();
2947-
MaybeGenerateProof(&block, pwalletMain);
2947+
MaybeGenerateProof(Params().GetConsensus(), &block, pwalletMain);
29482948
return HexStr(block.proof.solution.begin(), block.proof.solution.end());
29492949
}
29502950

0 commit comments

Comments
 (0)