Prepare SHA256D and RandomX multi-algo framework

This commit is contained in:
Capri 2026-09-12 19:54:01 +00:00
commit b913f39508
Signed by: VextaProject
SSH key fingerprint: SHA256:739BZv8LvIDY2tR7lXLy/IQ6MaGTQtYcG9GNvCjmpVM
2 changed files with 50 additions and 6 deletions

View file

@ -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;

View file

@ -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
{