Inside Bitcoin
Opening the selected file…
Inside Bitcoin
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.
751}753template <typename Derived>754class BlockHashApi755{756private:757auto impl() const758{759return static_cast<const Derived*>(this)->get();760}762public:763bool operator==(const Derived& other) const764{765return btck_block_hash_equals(impl(), other.get()) != 0;766}768bool operator!=(const Derived& other) const769{770return btck_block_hash_equals(impl(), other.get()) == 0;771}773std::array<std::byte, 32> ToBytes() const774{775std::array<std::byte, 32> hash;776btck_block_hash_to_bytes(impl(), reinterpret_cast<unsigned char*>(hash.data()));777return hash;778}779};781class BlockHashView : public View<btck_BlockHash>, public BlockHashApi<BlockHashView>782{783public:784explicit BlockHashView(const btck_BlockHash* ptr) : View{ptr} {}785};787class BlockHash : public Handle<btck_BlockHash, btck_block_hash_copy, btck_block_hash_destroy>, public BlockHashApi<BlockHash>788{789public:790explicit BlockHash(const std::array<std::byte, 32>& hash)791: Handle{btck_block_hash_create(reinterpret_cast<const unsigned char*>(hash.data()))} {}793explicit BlockHash(btck_BlockHash* hash)794: Handle{hash} {}796BlockHash(const BlockHashView& view)797: Handle{view} {}798};800template <typename Derived>801class BlockHeaderApi802{803private:804auto impl() const805{806return static_cast<const Derived*>(this)->get();807}809friend Derived;810BlockHeaderApi() = default;812public:813BlockHash Hash() const814{815return BlockHash{btck_block_header_get_hash(impl())};816}818BlockHashView PrevHash() const819{820return BlockHashView{btck_block_header_get_prev_hash(impl())};821}823uint32_t Timestamp() const824{825return btck_block_header_get_timestamp(impl());826}828uint32_t Bits() const829{830return btck_block_header_get_bits(impl());831}833int32_t Version() const834{835return btck_block_header_get_version(impl());836}838uint32_t Nonce() const839{840return btck_block_header_get_nonce(impl());841}843std::array<std::byte, 80> ToBytes() const844{845std::array<std::byte, 80> header;846int res{btck_block_header_to_bytes(impl(), reinterpret_cast<unsigned char*>(header.data()))};847if (res != 0) {848throw std::runtime_error("Failed to serialize block header");849}850return header;851}852};854class BlockHeaderView : public View<btck_BlockHeader>, public BlockHeaderApi<BlockHeaderView>855{856public:857explicit BlockHeaderView(const btck_BlockHeader* ptr) : View{ptr} {}858};860class BlockHeader : public Handle<btck_BlockHeader, btck_block_header_copy, btck_block_header_destroy>, public BlockHeaderApi<BlockHeader>861{862public:863explicit BlockHeader(std::span<const std::byte> raw_header)864: Handle{btck_block_header_create(reinterpret_cast<const unsigned char*>(raw_header.data()), raw_header.size())} {}866BlockHeader(const BlockHeaderView& view)867: Handle{view} {}869BlockHeader(btck_BlockHeader* header)870: Handle{header} {}871};873class ConsensusParamsView : public View<btck_ConsensusParams>874{875public:876explicit ConsensusParamsView(const btck_ConsensusParams* ptr) : View{ptr} {}877};879class Block : public Handle<btck_Block, btck_block_copy, btck_block_destroy>880{881public:882Block(const std::span<const std::byte> raw_block)883: Handle{btck_block_create(raw_block.data(), raw_block.size())}884{885}887Block(btck_Block* block) : Handle{block} {}889size_t CountTransactions() const890{891return btck_block_count_transactions(get());892}894TransactionView GetTransaction(size_t index) const895{896return TransactionView{btck_block_get_transaction_at(get(), index)};897}899bool Check(const ConsensusParamsView& consensus_params,900BlockCheckFlags flags,901BlockValidationState& state) const;903MAKE_RANGE_METHOD(Transactions, Block, &Block::CountTransactions, &Block::GetTransaction, *this)905BlockHash GetHash() const906{907return BlockHash{btck_block_get_hash(get())};908}910BlockHeader GetHeader() const911{912return BlockHeader{btck_block_get_header(get())};913}915std::vector<std::byte> ToBytes() const916{917return write_bytes(get(), btck_block_to_bytes);918}919};921inline void logging_disable()922{923btck_logging_disable();924}926inline void logging_set_options(const btck_LoggingOptions& logging_options)927{928btck_logging_set_options(logging_options);929}931inline void logging_set_level_category(LogCategory category, LogLevel level)932{933btck_logging_set_level_category(static_cast<btck_LogCategory>(category), static_cast<btck_LogLevel>(level));934}936inline void logging_enable_category(LogCategory category)937{938btck_logging_enable_category(static_cast<btck_LogCategory>(category));939}941inline void logging_disable_category(LogCategory category)942{943btck_logging_disable_category(static_cast<btck_LogCategory>(category));944}946template <typename T>947concept Log = requires(T a, std::string_view message) {948{ a.LogMessage(message) } -> std::same_as<void>;949};951template <Log T>952class Logger : UniqueHandle<btck_LoggingConnection, btck_logging_connection_destroy>953{954public:955Logger(std::unique_ptr<T> log)956: UniqueHandle{btck_logging_connection_create(957+[](void* user_data, const char* message, size_t message_len) { static_cast<T*>(user_data)->LogMessage({message, message_len}); },958log.release(),959+[](void* user_data) { delete static_cast<T*>(user_data); })}960{961}962};964class BlockTreeEntry : public View<btck_BlockTreeEntry>965{966public:967BlockTreeEntry(const btck_BlockTreeEntry* entry)968: View{entry}969{970}972bool operator==(const BlockTreeEntry& other) const973{974return btck_block_tree_entry_equals(get(), other.get()) != 0;975}977std::optional<BlockTreeEntry> GetPrevious() const978{979auto entry{btck_block_tree_entry_get_previous(get())};980if (!entry) return std::nullopt;981return entry;982}984int32_t GetHeight() const985{986return btck_block_tree_entry_get_height(get());987}989BlockHashView GetHash() const990{991return BlockHashView{btck_block_tree_entry_get_block_hash(get())};992}994BlockHeader GetHeader() const995{996return BlockHeader{btck_block_tree_entry_get_block_header(get())};997}999BlockTreeEntry GetAncestor(int32_t height) const1000{