diff --git a/src/primitives/block.cpp b/src/primitives/block.cpp index 75edfe6..714ea4e 100644 --- a/src/primitives/block.cpp +++ b/src/primitives/block.cpp @@ -17,6 +17,25 @@ uint256 CBlockHeader::GetHash() const return SerializeHash(*this); } +int CBlockHeader::GetAlgo() const +{ + switch (nVersion & BLOCK_VERSION_ALGO) { + case BLOCK_VERSION_SHA256D: + return ALGO_SHA256D; + case BLOCK_VERSION_RANDOMX: + return ALGO_RANDOMX; + default: + return ALGO_UNKNOWN; + } +} + +uint256 CBlockHeader::GetPoWAlgoHash(const Consensus::Params& params) const +{ + // RandomX hashing is not enabled yet. + // For now, preserve existing SHA256D behavior. + return GetHash(); +} + std::string CBlock::ToString(const Consensus::Params& params) const { std::stringstream s; diff --git a/src/primitives/block.h b/src/primitives/block.h index cf7836d..cb863c7 100644 --- a/src/primitives/block.h +++ b/src/primitives/block.h @@ -13,20 +13,36 @@ namespace Consensus { struct Params; } -enum { - +enum { + ALGO_UNKNOWN = -1, + ALGO_SHA256D = 0, + ALGO_RANDOMX = 1, + NUM_ALGOS_IMPL }; +static constexpr int NUM_ALGOS = 2; + enum { // primary version - BLOCK_VERSION_DEFAULT = 2, + BLOCK_VERSION_DEFAULT = 2, - // algo + // algo mask / ids + BLOCK_VERSION_ALGO = (15 << 8), BLOCK_VERSION_SHA256D = (2 << 8), - //BLOCK_VERSION_EQUIHASH = (10 << 8), - //BLOCK_VERSION_ETHASH = (12 << 8), + BLOCK_VERSION_RANDOMX = (4 << 8), }; +inline int GetVersionForAlgo(int algo) +{ + switch (algo) { + case ALGO_SHA256D: + return BLOCK_VERSION_SHA256D; + case ALGO_RANDOMX: + return BLOCK_VERSION_RANDOMX; + default: + return 0; + } +} /** Nodes collect new transactions into a block, hash them into a hash tree, * and scan through nonce values to make the block's hash satisfy proof-of-work @@ -68,8 +84,17 @@ public: return (nBits == 0); } + void SetAlgo(int algo) + { + nVersion &= ~BLOCK_VERSION_ALGO; + nVersion |= GetVersionForAlgo(algo); + } + + int GetAlgo() const; + uint256 GetHash() const; + uint256 GetPoWAlgoHash(const Consensus::Params& params) const; int64_t GetBlockTime() const {