9#include <chainparams.h>
45 std::vector<std::shared_ptr<CBlock>>
ret{total_height};
48 for (
size_t height{0}; height < total_height; ++height) {
49 CBlock& block{*(
ret.at(height) = std::make_shared<CBlock>())};
52 coinbase_tx.
nLockTime =
static_cast<uint32_t
>(height);
53 coinbase_tx.
vin.resize(1);
54 coinbase_tx.
vin[0].prevout.SetNull();
56 coinbase_tx.
vout.resize(1);
64 block.hashPrevBlock = (height >= 1 ? *
ret.at(height - 1) : params.
GenesisBlock()).GetHash();
79 const CScript& coinbase_script_pub_key,
81 std::vector<std::shared_ptr<CBlock>>& chain)
87 for (
auto& chain_block : chain) {
88 auto block_template{mining->createNewBlock({
90 .coinbase_output_script = coinbase_script_pub_key,
97 block.nTime = pindex->
nTime + 1;
101 tx_coinbase.vin.at(0).scriptSig =
CScript{} << pindex->
nHeight + 1;
106 while (!
CheckProofOfWork(block.GetHash(), block.nBits, consensus)) ++block.nNonce;
108 chain_block = std::make_shared<CBlock>(std::move(block));
111 if (!
Assert(
node.chainman)->ProcessNewBlockHeaders({{*chain_block}},
true, state, &pindex)) {
138 if (block->GetHash() !=
m_hash)
return;
156 const auto old_height =
WITH_LOCK(chainman.GetMutex(),
return chainman.ActiveHeight());
159 node.validation_signals->RegisterValidationInterface(&bvsc);
160 const bool processed{chainman.ProcessNewBlock(block,
true,
true, &new_block)};
161 const bool duplicate{!new_block && processed};
163 node.validation_signals->UnregisterValidationInterface(&bvsc);
164 node.validation_signals->SyncWithValidationInterfaceQueue();
165 const bool was_valid{bvsc.m_state && bvsc.m_state->IsValid()};
166 assert(old_height + was_valid ==
WITH_LOCK(chainman.GetMutex(),
return chainman.ActiveHeight()));
168 if (was_valid)
return {block->vtx[0]->GetHash(), 0};
176 auto block_template = mining->createNewBlock(assembler_options,
false);
177 auto block = std::make_shared<CBlock>(
Assert(block_template)->getBlock());
180 block->nTime =
Assert(
node.chainman)->ActiveChain().Tip()->GetMedianTimePast() + 1;
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination corresponds to one with an address.
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
const CChainParams & Params()
Return the currently selected parameters.
#define Assert(val)
Identity function.
The block chain is a tree shaped structure starting with the genesis block at the root,...
uint256 GetBlockHash() const
int nHeight
height of the entry in the chain. The genesis block has height 0
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
const CBlock & GenesisBlock() const
const Consensus::Params & GetConsensus() const
An outpoint - a combination of a transaction hash and an index n into its vout.
Serialized script, used inside transaction inputs and outputs.
static constexpr uint32_t MAX_SEQUENCE_NONFINAL
This is the maximum sequence number that enables both nLockTime and OP_CHECKLOCKTIMEVERIFY (BIP 65).
Implement this to subscribe to events generated in validation and mempool.
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg, std::vector< int > *error_locations)
std::unique_ptr< Mining > MakeMining(const node::NodeContext &node, bool wait_loaded=true)
Return implementation of Mining interface.
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params ¶ms)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
static CTransactionRef MakeTransactionRef(Tx &&txIn)
void BlockChecked(const std::shared_ptr< const CBlock > &block, const BlockValidationState &state) override
Notifies listeners of a block validation result.
BlockValidationStateCatcher(const uint256 &hash)
std::optional< BlockValidationState > m_state
A mutable version of CTransaction.
std::vector< CTxOut > vout
Parameters that influence chain consensus.
Block template creation options.
NodeContext struct containing references to chain state and connection state.
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
std::shared_ptr< CBlock > PrepareBlock(const NodeContext &node, const node::BlockCreateOptions &assembler_options)
Prepare a block to be mined.
COutPoint generatetoaddress(const NodeContext &node, const std::string &address)
RPC-like helper function, returns the generated coin.
COutPoint ProcessBlock(const NodeContext &node, const std::shared_ptr< CBlock > &block)
Returns the generated coin (or Null if the block was invalid).
COutPoint MineBlock(const NodeContext &node, const node::BlockCreateOptions &assembler_options)
Returns the generated coin.
bool BuildChain(const NodeContext &node, const CBlockIndex *pindex, const CScript &coinbase_script_pub_key, size_t length, std::vector< std::shared_ptr< CBlock > > &chain)
Build a chain of length coinbase-only blocks on top of pindex (which need not be the active tip,...
std::vector< std::shared_ptr< CBlock > > CreateBlockChain(size_t total_height, const CChainParams ¶ms)
Create a blockchain, starting from genesis.
const CScript P2WSH_OP_TRUE
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
constexpr int32_t VERSIONBITS_LAST_OLD_BLOCK_VERSION
What block version to use for new blocks (pre versionbits)