Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#ifndef BITCOIN_PRIMITIVES_BLOCK_H7#define BITCOIN_PRIMITIVES_BLOCK_H9#include <primitives/transaction.h>10#include <serialize.h>11#include <uint256.h>12#include <util/time.h>14#include <cstdint>15#include <string>16#include <utility>17#include <vector>19/** Nodes collect new transactions into a block, hash them into a hash tree,20* and scan through nonce values to make the block's hash satisfy proof-of-work21* requirements. When they solve the proof-of-work, they broadcast the block22* to everyone and the block is added to the block chain. The first transaction23* in the block is a special one that creates a new coin owned by the creator24* of the block.25*/26class CBlockHeader27{28public:29// header30int32_t nVersion;31uint256 hashPrevBlock;32uint256 hashMerkleRoot;33uint32_t nTime;34uint32_t nBits;35uint32_t nNonce;37CBlockHeader()38{39SetNull();40}42SERIALIZE_METHODS(CBlockHeader, obj) { READWRITE(obj.nVersion, obj.hashPrevBlock, obj.hashMerkleRoot, obj.nTime, obj.nBits, obj.nNonce); }44void SetNull()45{46nVersion = 0;47hashPrevBlock.SetNull();48hashMerkleRoot.SetNull();49nTime = 0;50nBits = 0;51nNonce = 0;52}54bool IsNull() const55{56return (nBits == 0);57}59uint256 GetHash() const;61NodeSeconds Time() const62{63return NodeSeconds{std::chrono::seconds{nTime}};64}66int64_t GetBlockTime() const67{68return (int64_t)nTime;69}70};73class CBlock : public CBlockHeader74{75public:76// network and disk77std::vector<CTransactionRef> vtx;79// Memory-only flags for caching expensive checks80mutable bool fChecked; // CheckBlock()81mutable bool m_checked_witness_commitment{false}; // CheckWitnessCommitment()82mutable bool m_checked_merkle_root{false}; // CheckMerkleRoot()84CBlock()85{86SetNull();87}89CBlock(const CBlockHeader &header)90{91SetNull();92*(static_cast<CBlockHeader*>(this)) = header;93}95SERIALIZE_METHODS(CBlock, obj)96{97READWRITE(AsBase<CBlockHeader>(obj), obj.vtx);98}100void SetNull()101{102CBlockHeader::SetNull();103vtx.clear();104fChecked = false;105m_checked_witness_commitment = false;106m_checked_merkle_root = false;107}109std::string ToString() const;110};112/** Describes a place in the block chain to another node such that if the113* other node doesn't have the same branch, it can find a recent common trunk.114* The further back it is, the further before the fork it may be.115*/116struct CBlockLocator117{118/** Historically CBlockLocator's version field has been written to network119* streams as the negotiated protocol version and to disk streams as the120* client version, but the value has never been used.121*122* Hard-code to the highest protocol version ever written to a network stream.123* SerParams can be used if the field requires any meaning in the future,124**/125static constexpr int DUMMY_VERSION = 70016;127std::vector<uint256> vHave;129CBlockLocator() = default;131explicit CBlockLocator(std::vector<uint256>&& have) : vHave(std::move(have)) {}133SERIALIZE_METHODS(CBlockLocator, obj)134{135int nVersion = DUMMY_VERSION;136READWRITE(nVersion);137READWRITE(obj.vHave);138}140void SetNull()141{142vHave.clear();143}145bool IsNull() const146{147return vHave.empty();148}149};151#endif // BITCOIN_PRIMITIVES_BLOCK_H