diff --git a/src/chain.h b/src/chain.h index d834790f06..99ca8ea53b 100644 --- a/src/chain.h +++ b/src/chain.h @@ -135,8 +135,7 @@ public: int nVersion; uint256 hashMerkleRoot; unsigned int nTime; - unsigned int nBits; - unsigned int nNonce; + CProof proof; //! (memory only) Sequential id assigned to distinguish order in which blocks are received. uint32_t nSequenceId; @@ -159,8 +158,7 @@ public: nVersion = 0; hashMerkleRoot = 0; nTime = 0; - nBits = 0; - nNonce = 0; + proof.SetNull(); } CBlockIndex() @@ -175,8 +173,7 @@ public: nVersion = block.nVersion; hashMerkleRoot = block.hashMerkleRoot; nTime = block.nTime; - nBits = block.nBits; - nNonce = block.nNonce; + proof = block.proof; } CDiskBlockPos GetBlockPos() const { @@ -205,8 +202,7 @@ public: block.hashPrevBlock = pprev->GetBlockHash(); block.hashMerkleRoot = hashMerkleRoot; block.nTime = nTime; - block.nBits = nBits; - block.nNonce = nNonce; + block.proof = proof; return block; } @@ -319,8 +315,7 @@ public: READWRITE(hashPrev); READWRITE(hashMerkleRoot); READWRITE(nTime); - READWRITE(nBits); - READWRITE(nNonce); + READWRITE(proof); } uint256 GetBlockHash() const @@ -330,8 +325,7 @@ public: block.hashPrevBlock = hashPrev; block.hashMerkleRoot = hashMerkleRoot; block.nTime = nTime; - block.nBits = nBits; - block.nNonce = nNonce; + block.proof = proof; return block.GetHash(); } diff --git a/src/chainparams.cpp b/src/chainparams.cpp index 1cca535b16..d0e8e5dcd4 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -127,8 +127,7 @@ public: genesis.hashMerkleRoot = genesis.BuildMerkleTree(); genesis.nVersion = 1; genesis.nTime = 1231006505; - genesis.nBits = 0x1d00ffff; - genesis.nNonce = 2083236893; + genesis.proof = CProof(0x1d00ffff, 2083236893); hashGenesisBlock = genesis.GetHash(); mapCheckpoints[0] = hashGenesisBlock; @@ -187,7 +186,7 @@ public: //! Modify the testnet genesis block so the timestamp is valid for a later start. genesis.nTime = 1296688602; - genesis.nNonce = 414098458; + genesis.proof = CProof(0x1d00ffff, 414098458); hashGenesisBlock = genesis.GetHash(); mapCheckpointsTestnet[0] = hashGenesisBlock; @@ -243,8 +242,7 @@ public: nTargetSpacing = 10 * 60; bnProofOfWorkLimit = ~uint256(0) >> 1; genesis.nTime = 1296688602; - genesis.nBits = 0x207fffff; - genesis.nNonce = 2; + genesis.proof = CProof(0x207fffff, 2); nDefaultPort = 18444; hashGenesisBlock = genesis.GetHash(); diff --git a/src/main.cpp b/src/main.cpp index 786cb40165..27f59ec500 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1317,7 +1317,7 @@ bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos) } // Check the header - if (!CheckProofOfWork(block.GetHash(), block.nBits)) + if (!CheckProof(block)) return error("ReadBlockFromDisk : Errors in block header"); return true; @@ -2696,7 +2696,7 @@ bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigne bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, bool fCheckPOW) { // Check proof of work matches claimed amount - if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits)) + if (fCheckPOW && !CheckProof(block)) return state.DoS(50, error("CheckBlockHeader() : proof of work failed"), REJECT_INVALID, "high-hash"); @@ -2780,7 +2780,7 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta // Check proof of work if ((!Params().SkipProofOfWorkCheck()) && - (block.nBits != GetNextWorkRequired(pindexPrev, &block))) + (!CheckChallenge(block, *pindexPrev))) return state.DoS(100, error("%s : incorrect proof of work", __func__), REJECT_INVALID, "bad-diffbits"); diff --git a/src/main.h b/src/main.h index ea7420ca96..c71d1fccc4 100644 --- a/src/main.h +++ b/src/main.h @@ -17,7 +17,6 @@ #include "primitives/block.h" #include "primitives/transaction.h" #include "net.h" -#include "pow.h" #include "script/script.h" #include "script/sigcache.h" #include "script/standard.h" diff --git a/src/miner.cpp b/src/miner.cpp index d10c35cdbe..dd77a203da 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -8,7 +8,6 @@ #include "amount.h" #include "primitives/block.h" #include "primitives/transaction.h" -#include "hash.h" #include "main.h" #include "net.h" #include "pow.h" @@ -89,7 +88,7 @@ int64_t UpdateTime(CBlockHeader* pblock, const CBlockIndex* pindexPrev) // Updating time can change work required on testnet: if (Params().AllowMinDifficultyBlocks()) - pblock->nBits = GetNextWorkRequired(pindexPrev, pblock); + ResetChallenge(*pblock, *pindexPrev); return nNewTime - nOldTime; } @@ -342,8 +341,8 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn) // Fill in header pblock->hashPrevBlock = pindexPrev->GetBlockHash(); UpdateTime(pblock, pindexPrev); - pblock->nBits = GetNextWorkRequired(pindexPrev, pblock); - pblock->nNonce = 0; + ResetChallenge(*pblock, *pindexPrev); + ResetProof(*pblock); pblocktemplate->vTxSigOps[0] = GetLegacySigOpCount(pblock->vtx[0]); CValidationState state; @@ -379,40 +378,6 @@ void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& // Internal miner // -bool GenerateProof(CBlockHeader *pblock) -{ - // Write the first 76 bytes of the block header to a double-SHA256 state. - uint256 hash; - CHash256 hasher; - CDataStream ss(SER_NETWORK, PROTOCOL_VERSION); - ss << *pblock; - assert(ss.size() == 80); - hasher.Write((unsigned char*)&ss[0], 76); - - while (true) { - pblock->nNonce++; - - // Write the last 4 bytes of the block header (the nonce) to a copy of - // the double-SHA256 state, and compute the result. - CHash256(hasher).Write((unsigned char*)&pblock->nNonce, 4).Finalize((unsigned char*)&hash); - - // Check if the hash has at least some zero bits, - if (((uint16_t*)&hash)[15] == 0) { - // then check if it has enough to reach the target - uint256 hashTarget = uint256().SetCompact(pblock->nBits); - if (hash <= hashTarget) { - assert(hash == pblock->GetHash()); - LogPrintf("hash: %s \ntarget: %s\n", hash.GetHex(), hashTarget.GetHex()); - return true; - } - } - - // If nothing found after trying for a while, return -1 - if ((pblock->nNonce & 0xfff) == 0) - return false; - } -} - CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey) { CPubKey pubkey; @@ -501,7 +466,7 @@ void static BitcoinMiner(CWallet *pwallet) // Search // int64_t nStart = GetTime(); - pblock->nNonce = 0; + ResetProof(*pblock); for (int i=0; i < 1000; i++) { // Check if something found if (GenerateProof(pblock)) { diff --git a/src/miner.h b/src/miner.h index 4175c08534..1696256e27 100644 --- a/src/miner.h +++ b/src/miner.h @@ -17,8 +17,6 @@ class CWallet; struct CBlockTemplate; -/** Scans nonces looking for a hash with at least some zero bits */ -bool GenerateProof(CBlockHeader *pblock); /** Run the miner threads */ void GenerateBitcoins(bool fGenerate, CWallet* pwallet, int nThreads); /** Generate a new block, without valid proof-of-work */ diff --git a/src/pow.cpp b/src/pow.cpp index c3dddbd614..92282ea36f 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -7,11 +7,14 @@ #include "chain.h" #include "chainparams.h" +#include "hash.h" #include "primitives/block.h" +#include "serialize.h" +#include "streams.h" #include "uint256.h" #include "util.h" -unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock) +unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, int64_t nTime) { unsigned int nProofOfWorkLimit = Params().ProofOfWorkLimit().GetCompact(); @@ -27,18 +30,18 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead // Special difficulty rule for testnet: // If the new block's timestamp is more than 2* 10 minutes // then allow mining of a min-difficulty block. - if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + Params().TargetSpacing()*2) + if (nTime > pindexLast->GetBlockTime() + Params().TargetSpacing()*2) return nProofOfWorkLimit; else { // Return the last non-special-min-difficulty-rules-block const CBlockIndex* pindex = pindexLast; - while (pindex->pprev && pindex->nHeight % Params().Interval() != 0 && pindex->nBits == nProofOfWorkLimit) + while (pindex->pprev && pindex->nHeight % Params().Interval() != 0 && pindex->proof.nBits == nProofOfWorkLimit) pindex = pindex->pprev; - return pindex->nBits; + return pindex->proof.nBits; } } - return pindexLast->nBits; + return pindexLast->proof.nBits; } // Go back by what we want to be 14 days worth of blocks @@ -58,7 +61,7 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead // Retarget uint256 bnNew; uint256 bnOld; - bnNew.SetCompact(pindexLast->nBits); + bnNew.SetCompact(pindexLast->proof.nBits); bnOld = bnNew; bnNew *= nActualTimespan; bnNew /= Params().TargetTimespan(); @@ -69,40 +72,89 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead /// debug print LogPrintf("GetNextWorkRequired RETARGET\n"); LogPrintf("Params().TargetTimespan() = %d nActualTimespan = %d\n", Params().TargetTimespan(), nActualTimespan); - LogPrintf("Before: %08x %s\n", pindexLast->nBits, bnOld.ToString()); + LogPrintf("Before: %08x %s\n", pindexLast->proof.nBits, bnOld.ToString()); LogPrintf("After: %08x %s\n", bnNew.GetCompact(), bnNew.ToString()); return bnNew.GetCompact(); } -bool CheckProofOfWork(uint256 hash, unsigned int nBits) +bool CheckChallenge(const CBlockHeader& block, const CBlockIndex& indexLast) +{ + return block.proof.nBits == GetNextWorkRequired(&indexLast, block.GetBlockTime()); +} + +void ResetChallenge(CBlockHeader& block, const CBlockIndex& indexLast) +{ + block.proof.nBits = GetNextWorkRequired(&indexLast, block.GetBlockTime()); +} + +bool CheckProof(const CBlockHeader& block) { bool fNegative; bool fOverflow; uint256 bnTarget; - if (Params().SkipProofOfWorkCheck() || hash == Params().HashGenesisBlock()) + if (Params().SkipProofOfWorkCheck() || block.GetHash() == Params().HashGenesisBlock()) return true; - bnTarget.SetCompact(nBits, &fNegative, &fOverflow); + bnTarget.SetCompact(block.proof.nBits, &fNegative, &fOverflow); // Check range if (fNegative || bnTarget == 0 || fOverflow || bnTarget > Params().ProofOfWorkLimit()) - return error("CheckProofOfWork() : nBits below minimum work"); + return error("%s : nBits below minimum work", __func__); // Check proof of work matches claimed amount - if (hash > bnTarget) - return error("CheckProofOfWork() : hash doesn't match nBits"); + if (block.GetHash() > bnTarget) + return error("%s : hash doesn't match nBits", __func__); return true; } +bool GenerateProof(CBlockHeader *pblock) +{ + // Write the first 76 bytes of the block header to a double-SHA256 state. + uint256 hash; + CHash256 hasher; + CDataStream ss(SER_NETWORK, PROTOCOL_VERSION); + ss << *pblock; + assert(ss.size() == 80); + hasher.Write((unsigned char*)&ss[0], 76); + + while (true) { + pblock->proof.nNonce++; + + // Write the last 4 bytes of the block header (the nonce) to a copy of + // the double-SHA256 state, and compute the result. + CHash256(hasher).Write((unsigned char*)&pblock->proof.nNonce, 4).Finalize((unsigned char*)&hash); + + // Check if the hash has at least some zero bits, + if (((uint16_t*)&hash)[15] == 0) { + // then check if it has enough to reach the target + uint256 hashTarget = uint256().SetCompact(pblock->proof.nBits); + if (hash <= hashTarget) { + assert(hash == pblock->GetHash()); + LogPrintf("hash: %s \ntarget: %s\n", hash.GetHex(), hashTarget.GetHex()); + return true; + } + } + + // If nothing found after trying for a while, return -1 + if ((pblock->proof.nNonce & 0xfff) == 0) + return false; + } +} + +void ResetProof(CBlockHeader& block) +{ + block.proof.nNonce = 0; +} + uint256 GetBlockProof(const CBlockIndex& block) { uint256 bnTarget; bool fNegative; bool fOverflow; - bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow); + bnTarget.SetCompact(block.proof.nBits, &fNegative, &fOverflow); if (fNegative || fOverflow || bnTarget == 0) return 0; // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256 @@ -111,3 +163,43 @@ uint256 GetBlockProof(const CBlockIndex& block) // or ~bnTarget / (nTarget+1) + 1. return (~bnTarget / (bnTarget + 1)) + 1; } + +double GetChallengeDifficulty(const CBlockIndex* blockindex) +{ + int nShift = (blockindex->proof.nBits >> 24) & 0xff; + + double dDiff = + (double)0x0000ffff / (double)(blockindex->proof.nBits & 0x00ffffff); + + while (nShift < 29) + { + dDiff *= 256.0; + nShift++; + } + while (nShift > 29) + { + dDiff /= 256.0; + nShift--; + } + return dDiff; +} + +std::string GetChallengeStr(const CBlockIndex& block) +{ + return strprintf("%08x", block.proof.nBits); +} + +std::string GetChallengeStrHex(const CBlockIndex& block) +{ + return uint256().SetCompact(block.proof.nBits).GetHex(); +} + +uint32_t GetNonce(const CBlockHeader& block) +{ + return block.proof.nNonce; +} + +void SetNonce(CBlockHeader& block, uint32_t nNonce) +{ + block.proof.nNonce = nNonce; +} diff --git a/src/pow.h b/src/pow.h index cf28656bd8..fdcae50fbe 100644 --- a/src/pow.h +++ b/src/pow.h @@ -7,15 +7,27 @@ #define BITCOIN_POW_H #include +#include class CBlockHeader; class CBlockIndex; +class CProof; class uint256; -unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock); - /** Check whether a block hash satisfies the proof-of-work requirement specified by nBits */ -bool CheckProofOfWork(uint256 hash, unsigned int nBits); +bool CheckProof(const CBlockHeader& block); +/** Scans nonces looking for a hash with at least some zero bits */ +bool GenerateProof(CBlockHeader* pblock); +void ResetProof(CBlockHeader& block); +bool CheckChallenge(const CBlockHeader& block, const CBlockIndex& indexLast); +void ResetChallenge(CBlockHeader& block, const CBlockIndex& indexLast); uint256 GetBlockProof(const CBlockIndex& block); +/** Avoid using these functions when possible */ +double GetChallengeDifficulty(const CBlockIndex* blockindex); +std::string GetChallengeStr(const CBlockIndex& block); +std::string GetChallengeStrHex(const CBlockIndex& block); +uint32_t GetNonce(const CBlockHeader& block); +void SetNonce(CBlockHeader& block, uint32_t nNonce); + #endif // BITCOIN_POW_H diff --git a/src/primitives/block.cpp b/src/primitives/block.cpp index 225bb80be8..64af76a4c7 100644 --- a/src/primitives/block.cpp +++ b/src/primitives/block.cpp @@ -9,9 +9,15 @@ #include "tinyformat.h" #include "utilstrencodings.h" +std::string CProof::ToString() const +{ + return strprintf("CProof(nBits=%08x, nNonce=%u)", + nBits, nNonce); +} + uint256 CBlockHeader::GetHash() const { - return Hash(BEGIN(nVersion), END(nNonce)); + return SerializeHash(*this); } uint256 CBlock::BuildMerkleTree(bool* fMutated) const @@ -111,12 +117,13 @@ uint256 CBlock::CheckMerkleBranch(uint256 hash, const std::vector& vMer std::string CBlock::ToString() const { std::stringstream s; - s << strprintf("CBlock(hash=%s, ver=%d, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, vtx=%u)\n", + s << strprintf("CBlock(hash=%s, ver=%d, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, proof=%u, vtx=%u)\n", GetHash().ToString(), nVersion, hashPrevBlock.ToString(), hashMerkleRoot.ToString(), - nTime, nBits, nNonce, + nTime, + proof.ToString(), vtx.size()); for (unsigned int i = 0; i < vtx.size(); i++) { diff --git a/src/primitives/block.h b/src/primitives/block.h index 9c17902e15..1f5ef8488a 100644 --- a/src/primitives/block.h +++ b/src/primitives/block.h @@ -13,6 +13,42 @@ /** The maximum allowed size for a serialized block, in bytes (network rule) */ static const unsigned int MAX_BLOCK_SIZE = 1000000; +class CProof +{ +public: + uint32_t nBits; + uint32_t nNonce; + + CProof() + { + SetNull(); + } + CProof(uint32_t nBitsIn, uint32_t nNonceIn) : + nBits(nBitsIn), nNonce(nNonceIn) {} + + ADD_SERIALIZE_METHODS; + + template + inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) + { + READWRITE(nBits); + READWRITE(nNonce); + } + + void SetNull() + { + nBits = 0; + nNonce = 0; + } + + bool IsNull() const + { + return (nBits == 0); + } + + std::string ToString() const; +}; + /** Nodes collect new transactions into a block, hash them into a hash tree, * and scan through nonce values to make the block's hash satisfy proof-of-work * requirements. When they solve the proof-of-work, they broadcast the block @@ -29,8 +65,7 @@ public: uint256 hashPrevBlock; uint256 hashMerkleRoot; uint32_t nTime; - uint32_t nBits; - uint32_t nNonce; + CProof proof; CBlockHeader() { @@ -46,8 +81,7 @@ public: READWRITE(hashPrevBlock); READWRITE(hashMerkleRoot); READWRITE(nTime); - READWRITE(nBits); - READWRITE(nNonce); + READWRITE(proof); } void SetNull() @@ -56,13 +90,12 @@ public: hashPrevBlock = 0; hashMerkleRoot = 0; nTime = 0; - nBits = 0; - nNonce = 0; + proof.SetNull(); } bool IsNull() const { - return (nBits == 0); + return proof.IsNull(); } uint256 GetHash() const; @@ -115,9 +148,8 @@ public: block.nVersion = nVersion; block.hashPrevBlock = hashPrevBlock; block.hashMerkleRoot = hashMerkleRoot; - block.nTime = nTime; - block.nBits = nBits; - block.nNonce = nNonce; + block.nTime = nTime; + block.proof = proof; return block; } diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index 66e86ebace..147db97886 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -5,6 +5,7 @@ #include "checkpoints.h" #include "main.h" +#include "pow.h" #include "rpcserver.h" #include "sync.h" #include "util.h" @@ -30,24 +31,7 @@ double GetDifficulty(const CBlockIndex* blockindex) else blockindex = chainActive.Tip(); } - - int nShift = (blockindex->nBits >> 24) & 0xff; - - double dDiff = - (double)0x0000ffff / (double)(blockindex->nBits & 0x00ffffff); - - while (nShift < 29) - { - dDiff *= 256.0; - nShift++; - } - while (nShift > 29) - { - dDiff /= 256.0; - nShift--; - } - - return dDiff; + return GetChallengeDifficulty(blockindex); } @@ -78,8 +62,8 @@ Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDe } result.push_back(Pair("tx", txs)); result.push_back(Pair("time", block.GetBlockTime())); - result.push_back(Pair("nonce", (uint64_t)block.nNonce)); - result.push_back(Pair("bits", strprintf("%08x", block.nBits))); + result.push_back(Pair("nonce", (uint64_t)GetNonce(block))); + result.push_back(Pair("bits", GetChallengeStr(block))); result.push_back(Pair("difficulty", GetDifficulty(blockindex))); result.push_back(Pair("chainwork", blockindex->nChainWork.GetHex())); diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp index a35474a1e9..e192a15f02 100644 --- a/src/rpcmining.cpp +++ b/src/rpcmining.cpp @@ -177,7 +177,7 @@ Value setgenerate(const Array& params, bool fHelp) do { LOCK(cs_main); IncrementExtraNonce(pblock, chainActive.Tip(), nExtraNonce); - } while (!GenerateProof(pblock) && !CheckProofOfWork(pblock->GetHash(), pblock->nBits)); + } while (!(GenerateProof(pblock) && CheckProof(*pblock))); CValidationState state; if (!ProcessNewBlock(state, NULL, pblock)) throw JSONRPCError(RPC_INTERNAL_ERROR, "ProcessNewBlock, block not accepted"); @@ -498,7 +498,7 @@ Value getblocktemplate(const Array& params, bool fHelp) // Update nTime UpdateTime(pblock, pindexPrev); - pblock->nNonce = 0; + ResetProof(*pblock); static const Array aCaps = boost::assign::list_of("proposal"); @@ -537,8 +537,6 @@ Value getblocktemplate(const Array& params, bool fHelp) Object aux; aux.push_back(Pair("flags", HexStr(COINBASE_FLAGS.begin(), COINBASE_FLAGS.end()))); - uint256 hashTarget = uint256().SetCompact(pblock->nBits); - static Array aMutable; if (aMutable.empty()) { @@ -555,14 +553,14 @@ Value getblocktemplate(const Array& params, bool fHelp) result.push_back(Pair("coinbaseaux", aux)); result.push_back(Pair("coinbasevalue", (int64_t)pblock->vtx[0].vout[0].nValue)); result.push_back(Pair("longpollid", chainActive.Tip()->GetBlockHash().GetHex() + i64tostr(nTransactionsUpdatedLast))); - result.push_back(Pair("target", hashTarget.GetHex())); + result.push_back(Pair("target", GetChallengeStrHex(*pblock))); result.push_back(Pair("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1)); result.push_back(Pair("mutable", aMutable)); result.push_back(Pair("noncerange", "00000000ffffffff")); result.push_back(Pair("sigoplimit", (int64_t)MAX_BLOCK_SIGOPS)); result.push_back(Pair("sizelimit", (int64_t)MAX_BLOCK_SIZE)); result.push_back(Pair("curtime", pblock->GetBlockTime())); - result.push_back(Pair("bits", strprintf("%08x", pblock->nBits))); + result.push_back(Pair("bits", GetChallengeStr(*pblock))); result.push_back(Pair("height", (int64_t)(pindexPrev->nHeight+1))); return result; diff --git a/src/test/miner_tests.cpp b/src/test/miner_tests.cpp index 100a4db0aa..5dbb8d3632 100644 --- a/src/test/miner_tests.cpp +++ b/src/test/miner_tests.cpp @@ -80,8 +80,8 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) if (txFirst.size() < 4) txFirst.push_back(new CTransaction(pblock->vtx[0])); pblock->hashMerkleRoot = pblock->BuildMerkleTree(); - pblock->nNonce = blockinfo[i].nonce; - if (!CheckProofOfWork(pblock->GetHash(), pblock->nBits)) + SetNonce(*pblock, blockinfo[i].nonce); + if (!CheckProof(*pblock)) { printf("WARNING: Skipping miner tests due to changed genesis block\n"); return; diff --git a/src/txdb.cpp b/src/txdb.cpp index 1086dd5b82..b47998b4bb 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -224,13 +224,12 @@ bool CBlockTreeDB::LoadBlockIndexGuts() pindexNew->nVersion = diskindex.nVersion; pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot; pindexNew->nTime = diskindex.nTime; - pindexNew->nBits = diskindex.nBits; - pindexNew->nNonce = diskindex.nNonce; + pindexNew->proof = diskindex.proof; pindexNew->nStatus = diskindex.nStatus; pindexNew->nTx = diskindex.nTx; - if (!CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits)) - return error("LoadBlockIndex() : CheckProofOfWork failed: %s", pindexNew->ToString()); + if (!CheckProof(pindexNew->GetBlockHeader())) + return error("LoadBlockIndex() : CheckProof failed: %s", pindexNew->ToString()); pcursor->Next(); } else {