MCPcopy Create free account
hub / github.com/ElementsProject/elements / GenerateBlock

Function GenerateBlock

src/rpc/mining.cpp:126–168  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

124}
125
126static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t& max_tries, unsigned int& extra_nonce, uint256& block_hash)
127{
128 block_hash.SetNull();
129
130 {
131 LOCK(cs_main);
132 IncrementExtraNonce(&block, chainman.ActiveChain().Tip(), extra_nonce);
133 }
134
135 CChainParams chainparams(Params());
136
137 // Signed blocks have no PoW requirements, but merkle root computed above in
138 // IncrementExtraNonce
139 if (!g_signed_blocks) {
140 while (max_tries > 0 && block.nNonce < std::numeric_limits<uint32_t>::max() && !CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus()) && !ShutdownRequested()) {
141 ++block.nNonce;
142 --max_tries;
143 }
144 if (max_tries == 0 || ShutdownRequested()) {
145 return false;
146 }
147 if (block.nNonce == std::numeric_limits<uint32_t>::max()) {
148 return true;
149 }
150 }
151
152 // Handle OP_TRUE m_signblockscript case
153 CScript op_true(OP_TRUE);
154 if (block.m_dynafed_params.m_current.m_signblockscript ==
155 GetScriptForDestination(WitnessV0ScriptHash(op_true))) {
156 block.m_signblock_witness.stack.push_back(std::vector<unsigned char>(op_true.begin(), op_true.end()));
157 } else if (!block.m_dynafed_params.IsNull()) {
158 throw JSONRPCError(RPC_MISC_ERROR, "Unable to fill out dynamic federation signblockscript witness, are you sure it's WSH(OP_TRUE)?");
159 }
160
161 std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
162 if (!chainman.ProcessNewBlock(chainparams, shared_pblock, true, nullptr)) {
163 throw JSONRPCError(RPC_INTERNAL_ERROR, "ProcessNewBlock, block not accepted");
164 }
165
166 block_hash = block.GetHash();
167 return true;
168}
169
170static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& mempool, const CScript& coinbase_script, int nGenerate, uint64_t nMaxTries)
171{

Callers 2

generateBlocksFunction · 0.85
generateblockFunction · 0.85

Calls 15

IncrementExtraNonceFunction · 0.85
CheckProofOfWorkFunction · 0.85
ShutdownRequestedFunction · 0.85
GetScriptForDestinationFunction · 0.85
WitnessV0ScriptHashClass · 0.85
JSONRPCErrorFunction · 0.85
ProcessNewBlockMethod · 0.80
ParamsClass · 0.50
SetNullMethod · 0.45
TipMethod · 0.45
GetHashMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected