|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "coin/block.hpp" |
| 4 | +#include "share_types.hpp" |
| 5 | + |
| 6 | +#include <sharechain/sharechain.hpp> |
| 7 | +#include <sharechain/share.hpp> |
| 8 | +#include <core/pack_types.hpp> |
| 9 | +#include <core/netaddress.hpp> |
| 10 | +#include <core/uint256.hpp> |
| 11 | +#include <core/target_utils.hpp> |
| 12 | + |
| 13 | +#include <map> |
| 14 | +#include <vector> |
| 15 | +#include <chrono> |
| 16 | + |
| 17 | +namespace dgb |
| 18 | +{ |
| 19 | + |
| 20 | +// min_header [small_block_header_type] |
| 21 | +// |
| 22 | +// share_info_type: |
| 23 | +// share_data: |
| 24 | +// prev_hash |
| 25 | +// coinbase |
| 26 | +// nonce |
| 27 | +// address[>=34] or pubkey_hash[<34] |
| 28 | +// subsidy |
| 29 | +// donation |
| 30 | +// stale_info |
| 31 | +// desired_version |
| 32 | +// |
| 33 | +// segwit_data [if segwit_activated] |
| 34 | +// |
| 35 | +// if version < 34: |
| 36 | +// new_transaction_hashes |
| 37 | +// transaction_hash_refs |
| 38 | +// |
| 39 | +// far_share_hash |
| 40 | +// max_bits |
| 41 | +// bits |
| 42 | +// timestamp |
| 43 | +// absheight |
| 44 | +// abswork |
| 45 | +// |
| 46 | +// ref_merkle_link |
| 47 | +// last_txout_nonce |
| 48 | +// hash_link |
| 49 | +// merkle_link |
| 50 | + |
| 51 | +template <int64_t Version> |
| 52 | +struct BaseShare : chain::BaseShare<uint256, Version> |
| 53 | +{ |
| 54 | + coin::SmallBlockHeaderType m_min_header; |
| 55 | + // prev_hash |
| 56 | + BaseScript m_coinbase; // coinbase |
| 57 | + uint32_t m_nonce; // nonce |
| 58 | + // [x]address[>=34] or [x]pubkey_hash[<34] |
| 59 | + uint64_t m_subsidy; // subsidy |
| 60 | + uint16_t m_donation; // donation |
| 61 | + dgb::StaleInfo m_stale_info; // stale_info |
| 62 | + uint64_t m_desired_version; // desired_version |
| 63 | + // |
| 64 | + // [x] segwit_data [if segwit_activated] |
| 65 | + // |
| 66 | + uint256 m_far_share_hash; // far_share_hash |
| 67 | + uint32_t m_max_bits; // max_bits; bitcoin_data.FloatingIntegerType() |
| 68 | + uint32_t m_bits; // bits; bitcoin_data.FloatingIntegerType() |
| 69 | + uint32_t m_timestamp; // timestamp |
| 70 | + uint32_t m_absheight; // absheight |
| 71 | + uint128 m_abswork; // abswork |
| 72 | + |
| 73 | + // ref_merkle_link |
| 74 | + MerkleLink m_ref_merkle_link; |
| 75 | + // last_txout_nonce |
| 76 | + uint64_t m_last_txout_nonce; |
| 77 | + // hash_link |
| 78 | + HashLinkType m_hash_link; |
| 79 | + // merkle_link |
| 80 | + MerkleLink m_merkle_link; |
| 81 | + |
| 82 | + NetService peer_addr; // WHERE? |
| 83 | + |
| 84 | + BaseShare() {} |
| 85 | + BaseShare(const uint256& hash, const uint256& prev_hash) : chain::BaseShare<uint256, Version>(hash, prev_hash) {} |
| 86 | + |
| 87 | +}; |
| 88 | + |
| 89 | +struct Share : BaseShare<17> |
| 90 | +{ |
| 91 | + uint160 m_pubkey_hash; |
| 92 | + std::optional<SegwitData> m_segwit_data; |
| 93 | + dgb::ShareTxInfo m_tx_info; // new_transaction_hashes; transaction_hash_refs |
| 94 | + |
| 95 | + Share() {} |
| 96 | + Share(const uint256& hash, const uint256& prev_hash) : BaseShare<17>(hash, prev_hash) {} |
| 97 | + |
| 98 | +}; |
| 99 | + |
| 100 | +struct NewShare : BaseShare<33> |
| 101 | +{ |
| 102 | + uint160 m_pubkey_hash; |
| 103 | + std::optional<SegwitData> m_segwit_data; |
| 104 | + dgb::ShareTxInfo m_tx_info; // new_transaction_hashes; transaction_hash_refs |
| 105 | + |
| 106 | + NewShare() {} |
| 107 | + NewShare(const uint256& hash, const uint256& prev_hash) : BaseShare<33>(hash, prev_hash) {} |
| 108 | +}; |
| 109 | + |
| 110 | +namespace types |
| 111 | +{ |
| 112 | + |
| 113 | +struct DataSegwitShare |
| 114 | +{ |
| 115 | + BaseScript m_address; // Todo (check): VarStrType |
| 116 | + std::optional<SegwitData> m_segwit_data; |
| 117 | +}; |
| 118 | + |
| 119 | +} // namespace types |
| 120 | + |
| 121 | +struct SegwitMiningShare : BaseShare<34>, types::DataSegwitShare |
| 122 | +{ |
| 123 | + SegwitMiningShare() {} |
| 124 | + SegwitMiningShare(const uint256& hash, const uint256& prev_hash) : BaseShare<34>(hash, prev_hash) {} |
| 125 | +}; |
| 126 | + |
| 127 | +struct PaddingBugfixShare : BaseShare<35>, types::DataSegwitShare |
| 128 | +{ |
| 129 | + PaddingBugfixShare() {} |
| 130 | + PaddingBugfixShare(const uint256& hash, const uint256& prev_hash) : BaseShare<35>(hash, prev_hash) {} |
| 131 | +}; |
| 132 | + |
| 133 | +struct MergedMiningShare : BaseShare<36> |
| 134 | +{ |
| 135 | + uint160 m_pubkey_hash; |
| 136 | + uint8_t m_pubkey_type{0}; // 0=P2PKH, 1=P2WPKH, 2=P2SH |
| 137 | + std::optional<SegwitData> m_segwit_data; |
| 138 | + std::vector<MergedAddressEntry> m_merged_addresses; // empty = none |
| 139 | + std::vector<MergedCoinbaseEntry> m_merged_coinbase_info; // empty = none |
| 140 | + uint256 m_merged_payout_hash; // zero = none |
| 141 | + V36HashLinkType m_hash_link; // shadows BaseShare::m_hash_link |
| 142 | + BaseScript m_message_data; // empty = none |
| 143 | + |
| 144 | + MergedMiningShare() {} |
| 145 | + MergedMiningShare(const uint256& hash, const uint256& prev_hash) : BaseShare<36>(hash, prev_hash) {} |
| 146 | +}; |
| 147 | + |
| 148 | +struct Formatter |
| 149 | +{ |
| 150 | + SHARE_FORMATTER() |
| 151 | + { |
| 152 | + // small_block_header_type: |
| 153 | + READWRITE(obj->m_min_header); |
| 154 | + // share_info_type: |
| 155 | + READWRITE( |
| 156 | + obj->m_prev_hash, |
| 157 | + obj->m_coinbase, |
| 158 | + obj->m_nonce |
| 159 | + ); |
| 160 | + |
| 161 | + // Address handling — version-dependent |
| 162 | + if constexpr (version >= 36) |
| 163 | + { |
| 164 | + READWRITE(obj->m_pubkey_hash); // IntType(160) |
| 165 | + READWRITE(obj->m_pubkey_type); // IntType(8) |
| 166 | + } |
| 167 | + else if constexpr (version >= 34) |
| 168 | + { |
| 169 | + READWRITE(obj->m_address); |
| 170 | + } |
| 171 | + else |
| 172 | + { |
| 173 | + READWRITE(obj->m_pubkey_hash); // pubkey_hash |
| 174 | + } |
| 175 | + |
| 176 | + // Subsidy — V36 uses VarInt, others use fixed uint64 |
| 177 | + if constexpr (version >= 36) |
| 178 | + { |
| 179 | + READWRITE(VarInt(obj->m_subsidy)); |
| 180 | + } |
| 181 | + else |
| 182 | + { |
| 183 | + READWRITE(obj->m_subsidy); |
| 184 | + } |
| 185 | + |
| 186 | + READWRITE( |
| 187 | + obj->m_donation, |
| 188 | + Using<EnumType<IntType<8>>>(obj->m_stale_info), |
| 189 | + VarInt(obj->m_desired_version) |
| 190 | + ); |
| 191 | + |
| 192 | + if constexpr (is_segwit_activated(version)) |
| 193 | + { |
| 194 | + READWRITE(Optional(obj->m_segwit_data, SegwitDataDefault)); |
| 195 | + } |
| 196 | + |
| 197 | + // V36: merged_addresses (after segwit_data, before far_share_hash) |
| 198 | + if constexpr (version >= 36) |
| 199 | + { |
| 200 | + READWRITE(obj->m_merged_addresses); |
| 201 | + } |
| 202 | + |
| 203 | + if constexpr (version < 34) |
| 204 | + { |
| 205 | + READWRITE(obj->m_tx_info); |
| 206 | + } |
| 207 | + |
| 208 | + READWRITE( |
| 209 | + obj->m_far_share_hash, |
| 210 | + obj->m_max_bits, |
| 211 | + obj->m_bits, |
| 212 | + obj->m_timestamp, |
| 213 | + obj->m_absheight |
| 214 | + ); |
| 215 | + |
| 216 | + // Abswork — V36 uses VarInt-encoded uint64, others use fixed uint128 |
| 217 | + if constexpr (version >= 36) |
| 218 | + { |
| 219 | + READWRITE(Using<AbsworkV36Format>(obj->m_abswork)); |
| 220 | + } |
| 221 | + else |
| 222 | + { |
| 223 | + READWRITE(obj->m_abswork); |
| 224 | + } |
| 225 | + |
| 226 | + // V36: merged_coinbase_info + merged_payout_hash (after abswork) |
| 227 | + if constexpr (version >= 36) |
| 228 | + { |
| 229 | + READWRITE(obj->m_merged_coinbase_info); |
| 230 | + READWRITE(obj->m_merged_payout_hash); |
| 231 | + } |
| 232 | + |
| 233 | + // ref_merkle_link |
| 234 | + READWRITE( |
| 235 | + MERKLE_LINK_SMALL(obj->m_ref_merkle_link) |
| 236 | + ); |
| 237 | + // last_txout_nonce |
| 238 | + READWRITE(obj->m_last_txout_nonce); |
| 239 | + // hash_link (V36: V36HashLinkType with extra_data; others: HashLinkType) |
| 240 | + READWRITE(obj->m_hash_link); |
| 241 | + // merkle_link |
| 242 | + READWRITE( |
| 243 | + MERKLE_LINK_SMALL(obj->m_merkle_link) |
| 244 | + ); |
| 245 | + |
| 246 | + // V36: message_data (at the end) |
| 247 | + if constexpr (version >= 36) |
| 248 | + { |
| 249 | + READWRITE(obj->m_message_data); |
| 250 | + } |
| 251 | + } |
| 252 | +}; |
| 253 | + |
| 254 | +using ShareType = chain::ShareVariants<Formatter, Share, NewShare, SegwitMiningShare, PaddingBugfixShare, MergedMiningShare>; |
| 255 | + |
| 256 | +inline ShareType load_share(chain::RawShare& rshare, NetService peer_addr) |
| 257 | +{ |
| 258 | + auto stream = rshare.contents.as_stream(); |
| 259 | + auto share = ShareType::load(rshare.type, stream); |
| 260 | + share.ACTION({ obj->peer_addr = peer_addr; }); |
| 261 | + return share; |
| 262 | +} |
| 263 | + |
| 264 | +template <typename StreamType> |
| 265 | +inline ShareType load_share(int64_t version, StreamType& is, NetService peer_addr) |
| 266 | +{ |
| 267 | + auto share = ShareType::load(version, is); |
| 268 | + share.ACTION({ obj->peer_addr = peer_addr; }); |
| 269 | + return share; |
| 270 | +} |
| 271 | + |
| 272 | +struct ShareHasher |
| 273 | +{ |
| 274 | + // this used to call `GetCheapHash()` in uint256, which was later moved; the |
| 275 | + // cheap hash function simply calls ReadLE64() however, so the end result is |
| 276 | + // identical |
| 277 | + size_t operator()(const uint256& hash) const |
| 278 | + { |
| 279 | + return hash.GetLow64(); |
| 280 | + } |
| 281 | +}; |
| 282 | + |
| 283 | +class ShareIndex : public chain::ShareIndex<uint256, ShareType, ShareHasher, ShareIndex> |
| 284 | +{ |
| 285 | + using base_index = chain::ShareIndex<uint256, ShareType, ShareHasher, ShareIndex>; |
| 286 | + |
| 287 | +public: |
| 288 | + // Per-share fields (NOT accumulated — each share stores only its own values). |
| 289 | + // Accumulated values are computed on-the-fly by walking m_shares[tail]. |
| 290 | + uint288 work; // target_to_average_attempts(bits) |
| 291 | + uint288 min_work; // target_to_average_attempts(max_bits) |
| 292 | + |
| 293 | + // Per-share metadata |
| 294 | + int64_t time_seen{0}; |
| 295 | + int32_t naughty{0}; |
| 296 | + bool is_block_solution{false}; // pow_hash <= block_target (set during init_verify) |
| 297 | + uint256 pow_hash; // scrypt hash, cached at reception (for block scan) |
| 298 | + |
| 299 | + ShareIndex() : base_index(), work(0), min_work(0) {} |
| 300 | + |
| 301 | + template <typename ShareT> ShareIndex(ShareT* share) : base_index(share) |
| 302 | + { |
| 303 | + work = chain::target_to_average_attempts(chain::bits_to_target(share->m_bits)); |
| 304 | + min_work = chain::target_to_average_attempts(chain::bits_to_target(share->m_max_bits)); |
| 305 | + time_seen = std::chrono::duration_cast<std::chrono::seconds>( |
| 306 | + std::chrono::system_clock::now().time_since_epoch()).count(); |
| 307 | + } |
| 308 | +}; |
| 309 | + |
| 310 | +struct ShareChain : chain::ShareChain<ShareIndex> |
| 311 | +{ |
| 312 | + |
| 313 | +}; |
| 314 | + |
| 315 | +} // namespace dgb |
| 316 | + |
0 commit comments