|
| 1 | +#pragma once |
| 2 | + |
| 3 | +// NMC (Namecoin) block / header primitives. |
| 4 | +// |
| 5 | +// Mirror of src/impl/btc/coin/block.hpp. Namecoin is a Bitcoin (SHA256d) fork; |
| 6 | +// its plain block header layout is byte-identical to Bitcoin's. Re-homed into |
| 7 | +// namespace nmc::coin. No btc header is modified. |
| 8 | +// |
| 9 | +// P0-DEFER: Namecoin blocks are AUX-POW capable. The merge-mined parent-chain |
| 10 | +// proof (CAuxPow: parent coinbase tx + coinbase merkle branch + parent block |
| 11 | +// header) is NOT modelled here. The plain header below is the foundation leaf; |
| 12 | +// the AuxPow attachment is a later leaf — see header_chain.hpp AuxPowStub. |
| 13 | + |
| 14 | +#include "transaction.hpp" |
| 15 | + |
| 16 | +#include <core/uint256.hpp> |
| 17 | +#include <core/pack_types.hpp> |
| 18 | +#include <core/netaddress.hpp> |
| 19 | + |
| 20 | +namespace nmc |
| 21 | +{ |
| 22 | + |
| 23 | +namespace coin |
| 24 | +{ |
| 25 | + |
| 26 | +struct SmallBlockHeaderType |
| 27 | +{ |
| 28 | + uint64_t m_version {}; |
| 29 | + uint256 m_previous_block{}; |
| 30 | + uint32_t m_timestamp{}; |
| 31 | + uint32_t m_bits{}; |
| 32 | + uint32_t m_nonce{}; |
| 33 | + |
| 34 | + C2POOL_SERIALIZE_METHODS(SmallBlockHeaderType) { READWRITE(VarInt(obj.m_version), obj.m_previous_block, obj.m_timestamp, obj.m_bits, obj.m_nonce); } |
| 35 | + |
| 36 | + SmallBlockHeaderType() {} |
| 37 | + |
| 38 | + void SetNull() |
| 39 | + { |
| 40 | + m_version = 0; |
| 41 | + m_previous_block.SetNull(); |
| 42 | + m_timestamp = 0; |
| 43 | + m_bits = 0; |
| 44 | + m_nonce = 0; |
| 45 | + } |
| 46 | + |
| 47 | + bool IsNull() const |
| 48 | + { |
| 49 | + return (m_bits == 0); |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +struct BlockHeaderType : SmallBlockHeaderType |
| 54 | +{ |
| 55 | + uint256 m_merkle_root; |
| 56 | + |
| 57 | + // Full block header uses fixed 4-byte int32 version (not VarInt like SmallBlockHeaderType) |
| 58 | + template<typename Stream> |
| 59 | + void Serialize(Stream& s) const { |
| 60 | + uint32_t version32 = static_cast<uint32_t>(m_version); |
| 61 | + ::Serialize(s, version32); |
| 62 | + ::Serialize(s, m_previous_block); |
| 63 | + ::Serialize(s, m_merkle_root); |
| 64 | + ::Serialize(s, m_timestamp); |
| 65 | + ::Serialize(s, m_bits); |
| 66 | + ::Serialize(s, m_nonce); |
| 67 | + } |
| 68 | + template<typename Stream> |
| 69 | + void Unserialize(Stream& s) { |
| 70 | + uint32_t version32; |
| 71 | + ::Unserialize(s, version32); |
| 72 | + m_version = version32; |
| 73 | + ::Unserialize(s, m_previous_block); |
| 74 | + ::Unserialize(s, m_merkle_root); |
| 75 | + ::Unserialize(s, m_timestamp); |
| 76 | + ::Unserialize(s, m_bits); |
| 77 | + ::Unserialize(s, m_nonce); |
| 78 | + } |
| 79 | + |
| 80 | + BlockHeaderType() : SmallBlockHeaderType() { } |
| 81 | + |
| 82 | + void SetNull() |
| 83 | + { |
| 84 | + SmallBlockHeaderType::SetNull(); |
| 85 | + m_merkle_root.SetNull(); |
| 86 | + } |
| 87 | + |
| 88 | + bool IsNull() const |
| 89 | + { |
| 90 | + return (m_bits == 0); |
| 91 | + } |
| 92 | + |
| 93 | +}; |
| 94 | + |
| 95 | +struct BlockType : BlockHeaderType |
| 96 | +{ |
| 97 | + std::vector<MutableTransaction> m_txs; |
| 98 | + |
| 99 | + template <typename Stream> |
| 100 | + void Serialize(Stream& s) const { |
| 101 | + BlockHeaderType::Serialize(s); |
| 102 | + ::Serialize(s, TX_WITH_WITNESS(m_txs)); |
| 103 | + } |
| 104 | + |
| 105 | + template <typename Stream> |
| 106 | + void Unserialize(Stream& s) { |
| 107 | + BlockHeaderType::Unserialize(s); |
| 108 | + ::Unserialize(s, TX_WITH_WITNESS(m_txs)); |
| 109 | + } |
| 110 | + |
| 111 | + BlockType() : BlockHeaderType() { } |
| 112 | + |
| 113 | + void SetNull() |
| 114 | + { |
| 115 | + BlockHeaderType::SetNull(); |
| 116 | + m_txs.clear(); |
| 117 | + } |
| 118 | + |
| 119 | + bool IsNull() const |
| 120 | + { |
| 121 | + return BlockHeaderType::IsNull(); |
| 122 | + } |
| 123 | +}; |
| 124 | + |
| 125 | +} // namespace coin |
| 126 | + |
| 127 | +} // namespace nmc |
0 commit comments