// // Create a new block with just given transactions, coinbase paying to // scriptPubKey, and try to add it to the current chain. // CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey) { const CChainParams& chainparams = Params(); std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey); CBlock& block = pblocktemplate->block; // Replace mempool-selected txns with just coinbase plus passed-in txns: block.vtx.resize(1); for (const CMutableTransaction& tx : txns) block.vtx.push_back(MakeTransactionRef(tx)); // IncrementExtraNonce creates a valid coinbase and merkleRoot { LOCK(cs_main); unsigned int extraNonce = 0; IncrementExtraNonce(&block, chainActive.Tip(), extraNonce); } while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce; std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block); ProcessNewBlock(chainparams, shared_pblock, true, nullptr); CBlock result = block; return result; }
std::shared_ptr<CBlock> FinalizeBlock(std::shared_ptr<CBlock> pblock) { pblock->hashMerkleRoot = BlockMerkleRoot(*pblock); while (!CheckProofOfWork(pblock->GetPoWHash(), pblock->nBits, Params().GetConsensus())) { ++(pblock->nNonce); } return pblock; }
bool CheckBlockProofOfWork(const CBlockHeader *pblock) { if (!Params().AllowMinDifficultyBlocks() && (pblock->nVersion & BLOCK_VERSION_AUXPOW && pblock->GetChainID() != AUXPOW_CHAIN_ID)) return error("CheckBlockProofOfWork() : block does not have our chain ID"); if (pblock->auxpow.get() != NULL) { if (!pblock->auxpow->Check(pblock->GetHash(), pblock->GetChainID())) return error("CheckBlockProofOfWork() : AUX POW is not valid"); // Check proof of work matches claimed amount if (!CheckProofOfWork(pblock->auxpow->GetParentBlockHash(), pblock->nBits)) return error("CheckBlockProofOfWork() : AUX proof of work failed"); } else { // Check proof of work matches claimed amount if (!CheckProofOfWork(pblock->GetPoWHash(), pblock->nBits)) return error("CheckBlockProofOfWork() : proof of work failed"); } return true; }
static CTxIn MineBlock(const CScript& coinbase_scriptPubKey) { auto block = PrepareBlock(coinbase_scriptPubKey); while (!CheckProofOfWork(block->GetHash(), block->nBits, Params().GetConsensus())) { assert(++block->nNonce); } bool processed{ProcessNewBlock(Params(), block, true, nullptr)}; assert(processed); return CTxIn{block->vtx[0]->GetHash(), 0}; }
UniValue generateBlocks(std::shared_ptr<CReserveScript> coinbaseScript, int nGenerate, uint64_t nMaxTries, bool keepScript) { static const int nInnerLoopCount = 0x10000; int nHeightEnd = 0; int nHeight = 0; unsigned int profile = 0x3; { // Don't keep cs_main locked LOCK(cs_main); nHeight = chainActive.Height(); nHeightEnd = nHeight+nGenerate; } unsigned int nExtraNonce = 0; UniValue blockHashes(UniValue::VARR); while (nHeight < nHeightEnd && !ShutdownRequested()) { std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler(Params()).CreateNewBlock(coinbaseScript->reserveScript)); if (!pblocktemplate.get()) throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block"); CBlock *pblock = &pblocktemplate->block; { LOCK(cs_main); IncrementExtraNonce(pblock, chainActive.Tip(), nExtraNonce); } if (pblock->GetBlockTime() >= Params().GetConsensus().nNeoScryptFork) profile = 0x0; while (nMaxTries > 0 && pblock->nNonce < nInnerLoopCount && !CheckProofOfWork(pblock->GetPoWHash(profile), pblock->nBits, Params().GetConsensus())) { ++pblock->nNonce; --nMaxTries; } if (nMaxTries == 0) { break; } if (pblock->nNonce == nInnerLoopCount) { continue; } std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(*pblock); if (!ProcessNewBlock(Params(), shared_pblock, true, nullptr)) throw JSONRPCError(RPC_INTERNAL_ERROR, "ProcessNewBlock, block not accepted"); ++nHeight; blockHashes.push_back(pblock->GetHash().GetHex()); //mark script as important because it was used at least for one coinbase output if the script came from the wallet if (keepScript) { coinbaseScript->KeepScript(); } } return blockHashes; }
/** * Mine a block (assuming minimal difficulty) that either matches * or doesn't match the difficulty target specified in the block header. * @param block The block to mine (by updating nonce). * @param ok Whether the block should be ok for PoW. * @param nBits Use this as difficulty if specified. */ static void mineBlock (CBlockHeader& block, bool ok, int nBits = -1) { if (nBits == -1) nBits = block.nBits; arith_uint256 target; target.SetCompact (nBits); block.nNonce = 0; while (true) { const bool nowOk = (UintToArith256 (block.GetHash ()) <= target); if ((ok && nowOk) || (!ok && !nowOk)) break; ++block.nNonce; } if (ok) BOOST_CHECK (CheckProofOfWork (block.GetHash (), nBits, Params().GetConsensus())); else BOOST_CHECK (!CheckProofOfWork (block.GetHash (), nBits, Params().GetConsensus())); }
// // Create a new block with just given transactions, coinbase paying to // scriptPubKey, and try to add it to the current chain. // CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey) { const CChainParams& chainparams = Params(); CBlockTemplate *pblocktemplate = CreateNewBlock(chainparams, scriptPubKey); CBlock& block = pblocktemplate->block; // Replace mempool-selected txns with just coinbase plus passed-in txns: block.vtx.resize(1); BOOST_FOREACH(const CMutableTransaction& tx, txns) block.vtx.push_back(tx); // IncrementExtraNonce creates a valid coinbase and merkleRoot unsigned int extraNonce = 0; IncrementExtraNonce(&block, chainActive.Tip(), extraNonce); while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce; ProcessNewBlock(chainparams, &block, true, NULL, NULL); CBlock result = block; delete pblocktemplate; return result; }
// // Create a new block with just given transactions, coinbase paying to // scriptPubKey, and try to add it to the current chain. // CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey) { const CChainParams& chainparams = Params(); std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey); CBlock& block = pblocktemplate->block; // Replace mempool-selected txns with just coinbase plus passed-in txns: block.vtx.resize(1); for (const CMutableTransaction& tx : txns) block.vtx.push_back(tx); // IncrementExtraNonce creates a valid coinbase and merkleRoot unsigned int extraNonce = 0; IncrementExtraNonce(&block, chainActive.Tip(), extraNonce); while (!CheckProofOfWork(block.GetPoWHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce; CValidationState state; ProcessNewBlock(state, chainparams, NULL, &block, true, NULL, false); CBlock result = block; pblocktemplate.reset(); return result; }
bool CBlockIndex::CheckIndex() const { return CheckProofOfWork(GetBlockHash(), nBits); }