NO-HF: Move block.proof.challenge to Consensus::Params::signblockscript

Since the scriptPubKey for signing blocks never changes, there's no
point reading repeating it from every block header.
This commit is contained in:
Jorge Timón 2018-07-10 01:42:37 +02:00
parent c6f9fc0ba0
commit 710df6d4e8
No known key found for this signature in database
GPG key ID: 8866C18EA1C944A2
8 changed files with 32 additions and 29 deletions

View file

@ -17,13 +17,13 @@
#include <boost/assign/list_of.hpp>
// Safer for users if they load incorrect parameters via arguments.
static std::vector<unsigned char> CommitToArguments(const Consensus::Params& params, const std::string& networkID, const CScript& signblockscript)
static std::vector<unsigned char> CommitToArguments(const Consensus::Params& params, const std::string& networkID)
{
CSHA256 sha2;
unsigned char commitment[32];
sha2.Write((const unsigned char*)networkID.c_str(), networkID.length());
sha2.Write((const unsigned char*)HexStr(params.fedpegScript).c_str(), HexStr(params.fedpegScript).length());
sha2.Write((const unsigned char*)HexStr(signblockscript).c_str(), HexStr(signblockscript).length());
sha2.Write((const unsigned char*)HexStr(params.signblockscript).c_str(), HexStr(params.signblockscript).length());
sha2.Finalize(commitment);
return std::vector<unsigned char>(commitment, commitment + 32);
}
@ -40,19 +40,19 @@ static CScript StrHexToScriptWithDefault(std::string strScript, const CScript de
return returnScript;
}
static CBlock CreateGenesisBlock(const Consensus::Params& params, const std::string& networkID, uint32_t nTime, const CScript& scriptChallenge, int32_t nVersion)
static CBlock CreateGenesisBlock(const Consensus::Params& params, const std::string& networkID, uint32_t nTime, int32_t nVersion)
{
CMutableTransaction txNew;
txNew.nVersion = 1;
txNew.vin.resize(1);
// Any consensus-related values that are command-line set can be added here for anti-footgun
txNew.vin[0].scriptSig = CScript(CommitToArguments(params, networkID, scriptChallenge));
txNew.vin[0].scriptSig = CScript(CommitToArguments(params, networkID));
txNew.vout.clear();
txNew.vout.push_back(CTxOut(CAsset(), 0, CScript() << OP_RETURN));
CBlock genesis;
genesis.nTime = nTime;
genesis.proof = CProof(scriptChallenge, CScript());
genesis.proof = CProof(params.signblockscript, CScript());
genesis.nVersion = nVersion;
genesis.vtx.push_back(MakeTransactionRef(std::move(txNew)));
genesis.hashPrevBlock.SetNull();
@ -134,6 +134,10 @@ protected:
parentGenesisBlockHash = uint256S(GetArg("-parentgenesisblockhash", "0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"));
initialFreeCoins = GetArg("-initialfreecoins", 0);
const CScript default_script(CScript() << OP_TRUE);
consensus.signblockscript = StrHexToScriptWithDefault(GetArg("-signblockscript", ""), default_script);
consensus.fedpegScript = StrHexToScriptWithDefault(GetArg("-fedpegscript", ""), default_script);
nDefaultPort = GetArg("-ndefaultport", 7042);
nPruneAfterHeight = GetArg("-npruneafterheight", 1000);
fMiningRequiresPeers = GetBoolArg("-fminingrequirespeers", false);
@ -168,10 +172,6 @@ public:
{
this->UpdateFromArgs();
const CScript defaultRegtestScript(CScript() << OP_TRUE);
CScript genesisChallengeScript = StrHexToScriptWithDefault(GetArg("-signblockscript", ""), defaultRegtestScript);
consensus.fedpegScript = StrHexToScriptWithDefault(GetArg("-fedpegscript", ""), defaultRegtestScript);
if (!anyonecanspend_aremine) {
assert("Anyonecanspendismine was marked as false, but they are in the genesis block"
&& initialFreeCoins == 0);
@ -193,12 +193,12 @@ public:
pchMessageStart[3] = 0xda;
// Generate pegged Bitcoin asset
std::vector<unsigned char> commit = CommitToArguments(consensus, strNetworkID, genesisChallengeScript);
std::vector<unsigned char> commit = CommitToArguments(consensus, strNetworkID);
uint256 entropy;
GenerateAssetEntropy(entropy, COutPoint(uint256(commit), 0), parentGenesisBlockHash);
CalculateAsset(consensus.pegged_asset, entropy);
genesis = CreateGenesisBlock(consensus, strNetworkID, 1296688602, genesisChallengeScript, 1);
genesis = CreateGenesisBlock(consensus, strNetworkID, 1296688602, 1);
if (initialFreeCoins != 0) {
AppendInitialIssuance(genesis, COutPoint(uint256(commit), 0), parentGenesisBlockHash, 1, initialFreeCoins, 0, 0, CScript() << OP_TRUE);
}
@ -276,4 +276,3 @@ void UpdateBIP9Parameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_
{
globalChainParams->UpdateBIP9Parameters(d, nStartTime, nTimeout);
}

View file

@ -71,6 +71,7 @@ struct Params {
uint256 defaultAssumeValid;
uint32_t pegin_min_depth;
CScript mandatory_coinbase_destination;
CScript signblockscript;
};
} // namespace Consensus

View file

@ -150,14 +150,14 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
// Pad weight for challenge
// We won't bother with serialization byte(s), we have room
nBlockWeight += pblock->proof.challenge.size()*WITNESS_SCALE_FACTOR;
nBlockWeight += chainparams.GetConsensus().signblockscript.size() * WITNESS_SCALE_FACTOR;
// Pad weight for proof
// Note: Assumes "naked" script template with pubkeys
txnouttype dummy_type;
std::vector<CTxDestination> dummy_addresses;
int required_sigs = -1;
if (!ExtractDestinations(pblock->proof.challenge, dummy_type, dummy_addresses, required_sigs)) {
if (!ExtractDestinations(chainparams.GetConsensus().signblockscript, dummy_type, dummy_addresses, required_sigs)) {
// No idea how to sign this... log error but return block.
LogPrintf("CreateNewBlock: Can not extract destinations from signblockscript");
} else {

View file

@ -21,11 +21,11 @@
#include "wallet/wallet.h"
#endif
CScript CombineBlockSignatures(const CBlockHeader& header, const CScript& scriptSig1, const CScript& scriptSig2)
CScript CombineBlockSignatures(const Consensus::Params& params, const CBlockHeader& header, const CScript& scriptSig1, const CScript& scriptSig2)
{
SignatureData sig1(scriptSig1);
SignatureData sig2(scriptSig2);
return GenericCombineSignatures(header.proof.challenge, header, sig1, sig2).scriptSig;
return GenericCombineSignatures(params.signblockscript, header, sig1, sig2).scriptSig;
}
bool CheckChallenge(const CBlockHeader& block, const CBlockIndex& indexLast, const Consensus::Params& params)
@ -82,14 +82,14 @@ bool CheckProof(const CBlockHeader& block, const Consensus::Params& params)
| SCRIPT_VERIFY_LOW_S // Stop easiest signature fiddling
| SCRIPT_VERIFY_WITNESS // Required for cleanstack eval in VerifyScript
| SCRIPT_NO_SIGHASH_BYTE; // non-Check(Multi)Sig signatures will not have sighash byte
return GenericVerifyScript(block.proof.solution, block.proof.challenge, proof_flags, block);
return GenericVerifyScript(block.proof.solution, params.signblockscript, proof_flags, block);
}
bool MaybeGenerateProof(CBlockHeader *pblock, CWallet *pwallet)
bool MaybeGenerateProof(const Consensus::Params& params, CBlockHeader *pblock, CWallet *pwallet)
{
#ifdef ENABLE_WALLET
SignatureData solution(pblock->proof.solution);
bool res = GenericSignScript(*pwallet, *pblock, pblock->proof.challenge, solution);
bool res = GenericSignScript(*pwallet, *pblock, params.signblockscript, solution);
pblock->proof.solution = solution.scriptSig;
return res;
#endif

View file

@ -22,11 +22,12 @@ class uint256;
/** Check whether a block hash satisfies the proof-of-work requirement specified by nBits */
bool CheckBitcoinProof(uint256 hash, unsigned int nBits);
bool CheckProof(const CBlockHeader& block, const Consensus::Params&);
/** Scans nonces looking for a hash with at least some zero bits */
bool MaybeGenerateProof(const Consensus::Params& params, CBlockHeader* pblock, CWallet* pwallet);
void ResetProof(CBlockHeader& block);
bool CheckChallenge(const CBlockHeader& block, const CBlockIndex& indexLast, const Consensus::Params&);
void ResetChallenge(CBlockHeader& block, const CBlockIndex& indexLast, const Consensus::Params&);
bool MaybeGenerateProof(CBlockHeader* pblock, CWallet* pwallet);
CScript CombineBlockSignatures(const CBlockHeader& header, const CScript& scriptSig1, const CScript& scriptSig2);
CScript CombineBlockSignatures(const Consensus::Params& params, const CBlockHeader& header, const CScript& scriptSig1, const CScript& scriptSig2);
#endif // BITCOIN_POW_H

View file

@ -11,6 +11,7 @@
#include "consensus/validation.h"
#include "core_io.h"
#include "validation.h"
#include "core_io.h"
#include "policy/policy.h"
#include "primitives/transaction.h"
#include "rpc/server.h"
@ -1065,6 +1066,7 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
+ HelpExampleRpc("getblockchaininfo", "")
);
const Consensus::Params& consensusParams = Params().GetConsensus();
LOCK(cs_main);
CBlockIndex* tip = chainActive.Tip();
@ -1076,10 +1078,9 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
obj.push_back(Pair("mediantime", (int64_t)tip->GetMedianTimePast()));
obj.push_back(Pair("verificationprogress", GuessVerificationProgress(Params().TxData(), tip)));
obj.push_back(Pair("pruned", fPruneMode));
obj.push_back(Pair("signblock_asm", ScriptToAsmStr(tip->proof.challenge)));
obj.push_back(Pair("signblock_hex", HexStr(tip->proof.challenge.begin(), tip->proof.challenge.end())));
obj.push_back(Pair("signblock_asm", ScriptToAsmStr(consensusParams.signblockscript)));
obj.push_back(Pair("signblock_hex", HexStr(consensusParams.signblockscript)));
const Consensus::Params& consensusParams = Params().GetConsensus();
UniValue bip9_softforks(UniValue::VOBJ);
BIP9SoftForkDescPushBack(bip9_softforks, "csv", consensusParams, Consensus::DEPLOYMENT_CSV);
BIP9SoftForkDescPushBack(bip9_softforks, "segwit", consensusParams, Consensus::DEPLOYMENT_SEGWIT);

View file

@ -223,14 +223,15 @@ UniValue combineblocksigs(const JSONRPCRequest& request)
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");
UniValue result(UniValue::VOBJ);
const Consensus::Params& params = Params().GetConsensus();
const UniValue& sigs = request.params[1].get_array();
for (unsigned int i = 0; i < sigs.size(); i++) {
const std::string& sig = sigs[i].get_str();
if (!IsHex(sig))
continue;
std::vector<unsigned char> vchScript = ParseHex(sig);
block.proof.solution = CombineBlockSignatures(block, block.proof.solution, CScript(vchScript.begin(), vchScript.end()));
if (CheckProof(block, Params().GetConsensus())) {
block.proof.solution = CombineBlockSignatures(params, block, block.proof.solution, CScript(vchScript.begin(), vchScript.end()));
if (CheckProof(block, params)) {
result.push_back(Pair("hex", EncodeHexBlock(block)));
result.push_back(Pair("complete", true));
return result;
@ -774,8 +775,8 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
result.push_back(Pair("weightlimit", (int64_t)MAX_BLOCK_WEIGHT));
}
result.push_back(Pair("curtime", pblock->GetBlockTime()));
result.push_back(Pair("signblock_asm", ScriptToAsmStr(pblock->proof.challenge)));
result.push_back(Pair("signblock_hex", HexStr(pblock->proof.challenge.begin(), pblock->proof.challenge.end())));
result.push_back(Pair("signblock_asm", ScriptToAsmStr(consensusParams.signblockscript)));
result.push_back(Pair("signblock_hex", HexStr(consensusParams.signblockscript.begin(), consensusParams.signblockscript.end())));
result.push_back(Pair("height", (int64_t)(pindexPrev->nHeight+1)));
if (!pblocktemplate->vchCoinbaseCommitment.empty() && fSupportsSegwit) {

View file

@ -3374,7 +3374,7 @@ UniValue signblock(const JSONRPCRequest& request)
}
block.proof.solution = CScript();
MaybeGenerateProof(&block, pwalletMain);
MaybeGenerateProof(Params().GetConsensus(), &block, pwalletMain);
return HexStr(block.proof.solution.begin(), block.proof.solution.end());
}