Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
242 changes: 121 additions & 121 deletions qa/rpc-tests/data/rpc_getblockstats.json

Large diffs are not rendered by default.

23 changes: 11 additions & 12 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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(CScript());
genesis.nVersion = nVersion;
genesis.vtx.push_back(MakeTransactionRef(std::move(txNew)));
genesis.hashPrevBlock.SetNull();
Expand Down Expand Up @@ -134,6 +134,10 @@ class CCustomParams : public CChainParams {
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);
Expand Down Expand Up @@ -168,10 +172,6 @@ class CCustomParams : public CChainParams {
{
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);
Expand All @@ -193,12 +193,12 @@ class CCustomParams : public CChainParams {
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);
}
Expand Down Expand Up @@ -276,4 +276,3 @@ void UpdateBIP9Parameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_
{
globalChainParams->UpdateBIP9Parameters(d, nStartTime, nTimeout);
}

1 change: 1 addition & 0 deletions src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ struct Params {
uint256 defaultAssumeValid;
uint32_t pegin_min_depth;
CScript mandatory_coinbase_destination;
CScript signblockscript;
};
} // namespace Consensus

Expand Down
9 changes: 2 additions & 7 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParam
if (nOldTime < nNewTime)
pblock->nTime = nNewTime;

// Updating time can change work required on testnet:
if (consensusParams.fPowAllowMinDifficultyBlocks)
ResetChallenge(*pblock, *pindexPrev, consensusParams);

return nNewTime - nOldTime;
}

Expand Down Expand Up @@ -146,18 +142,17 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
// Reset proof here, for weight calculations since header
// is not fixed size.
ResetProof(*pblock);
ResetChallenge(*pblock, *pindexPrev, chainparams.GetConsensus());

// 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 {
Expand Down
20 changes: 5 additions & 15 deletions src/pow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +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;
}

bool CheckChallenge(const CBlockHeader& block, const CBlockIndex& indexLast, const Consensus::Params& params)
{
return block.proof.challenge == indexLast.proof.challenge;
}

void ResetChallenge(CBlockHeader& block, const CBlockIndex& indexLast, const Consensus::Params& params)
{
block.proof.challenge = indexLast.proof.challenge;
return GenericCombineSignatures(params.signblockscript, header, sig1, sig2).scriptSig;
}

bool CheckBitcoinProof(uint256 hash, unsigned int nBits)
Expand Down Expand Up @@ -82,14 +72,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
Expand Down
8 changes: 3 additions & 5 deletions src/pow.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ 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
3 changes: 1 addition & 2 deletions src/primitives/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

std::string CProof::ToString() const
{
return strprintf("CProof(challenge=%s, solution=%s)",
ScriptToAsmStr(challenge), ScriptToAsmStr(solution));
return strprintf("CProof(solution=%s)", ScriptToAsmStr(solution));
}

uint256 CBlockHeader::GetHash() const
Expand Down
9 changes: 3 additions & 6 deletions src/primitives/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,31 @@
class CProof
{
public:
CScript challenge;
CScript solution;

CProof()
{
SetNull();
}
CProof(CScript challengeIn, CScript solutionIn) : challenge(challengeIn), solution(solutionIn) {}
CProof(CScript solutionIn) : solution(solutionIn) {}

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(*(CScriptBase*)(&challenge));
if (!(s.GetType() & SER_GETHASH))
READWRITE(*(CScriptBase*)(&solution));
}

void SetNull()
{
challenge.clear();
solution.clear();
}

bool IsNull() const
{
return challenge.empty();
return solution.empty();
}

std::string ToString() const;
Expand Down Expand Up @@ -94,7 +91,7 @@ class CBlockHeader

bool IsNull() const
{
return proof.IsNull();
return hashPrevBlock.IsNull();
}

uint256 GetHash() const;
Expand Down
6 changes: 3 additions & 3 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,7 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
+ HelpExampleRpc("getblockchaininfo", "")
);

const Consensus::Params& consensusParams = Params().GetConsensus();
LOCK(cs_main);
CBlockIndex* tip = chainActive.Tip();

Expand All @@ -1076,10 +1077,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);
Expand Down
9 changes: 5 additions & 4 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/test_bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::string& fedp
newTransaction.vout[i].scriptPubKey = scriptPubKey;
const_cast<CBlock&>(Params().GenesisBlock()).vtx[gen_size-1] = MakeTransactionRef(newTransaction);
const_cast<CBlock&>(Params().GenesisBlock()).hashMerkleRoot = BlockMerkleRoot(Params().GenesisBlock());
const_cast<CBlock&>(Params().GenesisBlock()).proof = CProof(CScript()<<OP_TRUE, CScript());
const_cast<CBlock&>(Params().GenesisBlock()).proof = CProof(CScript());
const_cast<Consensus::Params&>(Params().GetConsensus()).hashGenesisBlock = Params().GenesisBlock().GetHash();

ClearDatadirCache();
Expand Down
5 changes: 0 additions & 5 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3898,11 +3898,6 @@ std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBloc

bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev, int64_t nAdjustedTime)
{
const int nHeight = pindexPrev == NULL ? 0 : pindexPrev->nHeight + 1;
// Check proof of work
if (!CheckChallenge(block, *pindexPrev, consensusParams))
return state.DoS(100, false, REJECT_INVALID, "bad-diffbits", false, "incorrect proof of work");

// Check timestamp against prev
if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
return state.Invalid(false, REJECT_INVALID, "time-too-old", "block's timestamp is too early");
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down