2014-03-10 08:46:53 -07:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2015-12-13 17:58:29 +01:00
|
|
|
// Copyright (c) 2009-2015 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"
|
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
|
|
|
|
|
|
|
|
|
|
CScript CombineBlockSignatures(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)
|
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-01-11 07:14:30 -08:00
|
|
|
bool CheckBitcoinProof(const CBlockHeader& block)
|
2014-03-10 08:46:53 -07:00
|
|
|
{
|
2016-01-11 07:14:30 -08:00
|
|
|
assert(block.IsBitcoinBlock());
|
|
|
|
|
|
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
|
|
|
|
2016-01-11 07:14:30 -08:00
|
|
|
bnTarget.SetCompact(block.bitcoinproof.challenge, &fNegative, &fOverflow);
|
2014-03-10 08:46:53 -07:00
|
|
|
|
|
|
|
|
// 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
|
2016-01-11 07:14:30 -08:00
|
|
|
if (UintToArith256(block.GetHash()) > 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
|
|
|
|
|
|
|
|
bool CheckProof(const CBlockHeader& block, const Consensus::Params& params)
|
|
|
|
|
{
|
|
|
|
|
if (block.GetHash() == params.hashGenesisBlock)
|
|
|
|
|
return true;
|
|
|
|
|
return GenericVerifyScript(block.proof.solution, block.proof.challenge, SCRIPT_VERIFY_P2SH, block);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MaybeGenerateProof(CBlockHeader *pblock, CWallet *pwallet)
|
|
|
|
|
{
|
|
|
|
|
#ifdef ENABLE_WALLET
|
|
|
|
|
SignatureData solution(pblock->proof.solution);
|
|
|
|
|
bool res = GenericSignScript(*pwallet, *pblock, pblock->proof.challenge, solution);
|
|
|
|
|
pblock->proof.solution = solution.scriptSig;
|
|
|
|
|
return res;
|
|
|
|
|
#endif
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ResetProof(CBlockHeader& block)
|
|
|
|
|
{
|
|
|
|
|
block.proof.solution.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double GetChallengeDifficulty(const CBlockIndex* blockindex)
|
|
|
|
|
{
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string GetChallengeStr(const CBlockIndex& block)
|
|
|
|
|
{
|
|
|
|
|
return ScriptToAsmStr(block.proof.challenge);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string GetChallengeStrHex(const CBlockIndex& block)
|
|
|
|
|
{
|
|
|
|
|
return ScriptToAsmStr(block.proof.challenge);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t GetNonce(const CBlockHeader& block)
|
|
|
|
|
{
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetNonce(CBlockHeader& block, uint32_t nNonce)
|
|
|
|
|
{
|
|
|
|
|
}
|