2014-03-10 08:46:53 -07:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2016-12-31 11:01:21 -07:00
|
|
|
// Copyright (c) 2009-2016 The Bitcoin Core developers
|
2014-12-13 12:09:33 +08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-03-10 08:46:53 -07:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
|
|
#include "pow.h"
|
|
|
|
|
|
2014-12-16 15:43:03 +01:00
|
|
|
#include "arith_uint256.h"
|
2014-10-22 01:31:01 +02:00
|
|
|
#include "chain.h"
|
2016-01-11 07:14:30 -08:00
|
|
|
#include "chainparams.h"
|
|
|
|
|
#include "core_io.h"
|
|
|
|
|
#include "hash.h"
|
|
|
|
|
#include "keystore.h"
|
2014-11-18 21:03:02 +00:00
|
|
|
#include "primitives/block.h"
|
2017-02-28 16:36:56 -08:00
|
|
|
#include "primitives/bitcoin/block.h"
|
2016-01-11 07:14:30 -08:00
|
|
|
#include "script/generic.hpp"
|
|
|
|
|
#include "script/standard.h"
|
2014-03-10 08:46:53 -07:00
|
|
|
#include "uint256.h"
|
|
|
|
|
|
2016-01-11 07:14:30 -08:00
|
|
|
#ifdef ENABLE_WALLET
|
|
|
|
|
#include "wallet/wallet.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-07-10 01:42:37 +02:00
|
|
|
CScript CombineBlockSignatures(const Consensus::Params& params, const CBlockHeader& header, const CScript& scriptSig1, const CScript& scriptSig2)
|
2016-01-11 07:14:30 -08:00
|
|
|
{
|
|
|
|
|
SignatureData sig1(scriptSig1);
|
|
|
|
|
SignatureData sig2(scriptSig2);
|
2018-07-10 01:42:37 +02:00
|
|
|
return GenericCombineSignatures(params.signblockscript, header, sig1, sig2).scriptSig;
|
2016-01-11 07:14:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CheckChallenge(const CBlockHeader& block, const CBlockIndex& indexLast, const Consensus::Params& params)
|
2014-03-10 08:46:53 -07:00
|
|
|
{
|
2016-01-11 07:14:30 -08:00
|
|
|
return block.proof.challenge == indexLast.proof.challenge;
|
2015-02-21 12:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
2016-01-11 07:14:30 -08:00
|
|
|
void ResetChallenge(CBlockHeader& block, const CBlockIndex& indexLast, const Consensus::Params& params)
|
2015-02-21 12:57:44 +00:00
|
|
|
{
|
2016-01-11 07:14:30 -08:00
|
|
|
block.proof.challenge = indexLast.proof.challenge;
|
2014-03-10 08:46:53 -07:00
|
|
|
}
|
|
|
|
|
|
2016-02-07 14:02:37 -08:00
|
|
|
bool CheckBitcoinProof(uint256 hash, unsigned int nBits)
|
2014-03-10 08:46:53 -07:00
|
|
|
{
|
|
|
|
|
bool fNegative;
|
|
|
|
|
bool fOverflow;
|
2014-12-16 15:43:03 +01:00
|
|
|
arith_uint256 bnTarget;
|
2014-09-04 16:23:42 -03:00
|
|
|
|
2014-03-10 08:46:53 -07:00
|
|
|
bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
|
|
|
|
|
|
|
|
|
|
// Check range
|
2016-06-09 13:23:11 -04:00
|
|
|
if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(Params().GetConsensus().parentChainPowLimit))
|
2015-12-02 03:15:42 +01:00
|
|
|
return false;
|
2014-03-10 08:46:53 -07:00
|
|
|
|
|
|
|
|
// Check proof of work matches claimed amount
|
2014-12-16 15:43:03 +01:00
|
|
|
if (UintToArith256(hash) > bnTarget)
|
2015-12-02 03:15:42 +01:00
|
|
|
return false;
|
2014-03-10 08:46:53 -07:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2016-01-11 07:14:30 -08:00
|
|
|
|
2018-07-10 04:53:49 +02:00
|
|
|
static bool CheckProofGeneric(const CBlockHeader& block, const Consensus::Params& params, const CScript& challenge)
|
2016-01-11 07:14:30 -08:00
|
|
|
{
|
|
|
|
|
if (block.GetHash() == params.hashGenesisBlock)
|
|
|
|
|
return true;
|
2018-05-11 09:19:58 -04:00
|
|
|
|
|
|
|
|
// Some important anti-DoS flags.
|
|
|
|
|
// Note: Blockhashes do not commit to the proof.
|
|
|
|
|
// Therefore we may have a signature be mealleated
|
|
|
|
|
// to stay valid, but cause the block to fail
|
|
|
|
|
// validation, in this case, block weight.
|
|
|
|
|
// In that case, the block will be marked as permanently
|
|
|
|
|
// invalid and not processed.
|
|
|
|
|
// NOTE: These have only been deemed sufficient for OP_CMS
|
|
|
|
|
// ANY OTHER SCRIPT TYPE MAY REQUIRE DIFFERENT FLAGS/CONSIDERATIONS
|
|
|
|
|
// TODO: Better design to not have to worry about script specifics
|
|
|
|
|
// i.e. exempt block header solution from weight limit
|
|
|
|
|
unsigned int proof_flags = SCRIPT_VERIFY_P2SH // Just allows P2SH evaluation
|
|
|
|
|
| SCRIPT_VERIFY_STRICTENC // Minimally-sized DER sigs
|
|
|
|
|
| SCRIPT_VERIFY_NULLDUMMY // No extra data stuffed into OP_CMS witness
|
|
|
|
|
| SCRIPT_VERIFY_CLEANSTACK // No extra pushes leftover in witness
|
|
|
|
|
| SCRIPT_VERIFY_MINIMALDATA // Pushes are minimally-sized
|
|
|
|
|
| SCRIPT_VERIFY_SIGPUSHONLY // Witness is push-only
|
|
|
|
|
| SCRIPT_VERIFY_LOW_S // Stop easiest signature fiddling
|
2018-05-01 12:40:47 -04:00
|
|
|
| SCRIPT_VERIFY_WITNESS // Required for cleanstack eval in VerifyScript
|
|
|
|
|
| SCRIPT_NO_SIGHASH_BYTE; // non-Check(Multi)Sig signatures will not have sighash byte
|
2018-07-10 04:53:49 +02:00
|
|
|
return GenericVerifyScript(block.proof.solution, challenge, proof_flags, block);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CheckProofSignedParent(const CBlockHeader& block, const Consensus::Params& params)
|
|
|
|
|
{
|
|
|
|
|
return CheckProofGeneric(block, params, params.parent_chain_signblockscript);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CheckProof(const CBlockHeader& block, const Consensus::Params& params)
|
|
|
|
|
{
|
|
|
|
|
return CheckProofGeneric(block, params, params.signblockscript);
|
2016-01-11 07:14:30 -08:00
|
|
|
}
|
|
|
|
|
|
2018-07-10 01:42:37 +02:00
|
|
|
bool MaybeGenerateProof(const Consensus::Params& params, CBlockHeader *pblock, CWallet *pwallet)
|
2016-01-11 07:14:30 -08:00
|
|
|
{
|
|
|
|
|
#ifdef ENABLE_WALLET
|
|
|
|
|
SignatureData solution(pblock->proof.solution);
|
2018-07-10 01:42:37 +02:00
|
|
|
bool res = GenericSignScript(*pwallet, *pblock, params.signblockscript, solution);
|
2016-01-11 07:14:30 -08:00
|
|
|
pblock->proof.solution = solution.scriptSig;
|
|
|
|
|
return res;
|
|
|
|
|
#endif
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ResetProof(CBlockHeader& block)
|
|
|
|
|
{
|
|
|
|
|
block.proof.solution.clear();
|
|
|
|
|
}
|