Files
thehub/libs/utils/primitives/BlockHeader.cpp
tomFlowee 0a2e552168 Move more logic over to byte-array based Block
This introduces a new BlockHeader helper class which Block and
MutableBlock can both produce, which helps a lot of methods to
be ported to no longer be dependent on us using a MutableBlock
object, which is too costly to use when we have no intention
to alter the block.
2026-04-20 22:21:18 +02:00

48 lines
1.5 KiB
C++

/*
* This file is part of the Flowee project
* Copyright (C) 2026 Tom Zander <tom@flowee.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "BlockHeader.h"
#include "Block.h"
#include <hash.h>
BlockHeader::BlockHeader(const Block &block)
: nVersion(block.blockVersion()),
hashPrevBlock(block.previousBlockId()),
hashMerkleRoot(block.merkleRoot()),
nTime(block.timestamp()),
nBits(block.bits()),
nNonce(block.nonce())
{
}
uint256 BlockHeader::createHash() const
{
uint8_t header[80];
*reinterpret_cast<uint32_t*>(header) = nVersion;
memcpy(header + 4, hashPrevBlock.begin(), 32);
memcpy(header + 36, hashMerkleRoot.begin(), 32);
*reinterpret_cast<uint32_t*>(header + 68) = nTime;
*reinterpret_cast<uint32_t*>(header + 72) = nBits;
*reinterpret_cast<uint32_t*>(header + 76) = nNonce;
CHash256 ctx;
ctx.write(header, 80);
uint256 result;
ctx.finalize((unsigned char*)&result);
return result;
}