2013-01-08 03:02:51 -08:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2019-12-30 22:39:22 +13:00
|
|
|
// Copyright (c) 2009-2019 The Bitcoin Core developers
|
2014-12-13 12:09:33 +08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-01-08 03:02:51 -08:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <primitives/block.h>
|
2013-04-13 00:13:08 -05:00
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <hash.h>
|
|
|
|
|
#include <tinyformat.h>
|
2013-01-08 04:17:15 -08:00
|
|
|
|
2019-01-03 15:49:38 -05:00
|
|
|
|
2018-10-12 14:47:35 -04:00
|
|
|
bool g_con_blockheightinheader = false;
|
2018-12-05 20:31:36 +01:00
|
|
|
bool g_signed_blocks = false;
|
2018-10-12 14:47:35 -04:00
|
|
|
|
2018-12-07 10:26:35 +01:00
|
|
|
std::string CProof::ToString() const
|
|
|
|
|
{
|
|
|
|
|
return strprintf("CProof(challenge=%s, solution=%s)",
|
|
|
|
|
HexStr(challenge), HexStr(solution));
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-04 15:55:53 +02:00
|
|
|
// ELEMENTS: GetHash manually implemented for CBlockHeader.
|
|
|
|
|
// SER_GETHASH removed in #28508 so CProof now always serializes `solution`.
|
|
|
|
|
// Only include `challenge` of CProof here, and no signblock witness
|
2013-06-25 03:03:03 +02:00
|
|
|
uint256 CBlockHeader::GetHash() const
|
|
|
|
|
{
|
2025-11-06 13:08:59 +02:00
|
|
|
HashWriter s{};
|
2025-08-04 15:55:53 +02:00
|
|
|
// Detect dynamic federation block serialization using "HF bit",
|
|
|
|
|
// or the signed bit which is invalid in Bitcoin
|
|
|
|
|
bool is_dyna = false;
|
|
|
|
|
int32_t nVersion = this->nVersion;
|
|
|
|
|
if (!m_dynafed_params.IsNull()) {
|
2025-08-19 13:18:56 +02:00
|
|
|
nVersion |= (int32_t)DYNAFED_HF_MASK;
|
2025-08-04 15:55:53 +02:00
|
|
|
is_dyna = true;
|
|
|
|
|
}
|
|
|
|
|
s << (nVersion);
|
|
|
|
|
s << hashPrevBlock;
|
|
|
|
|
s << hashMerkleRoot;
|
|
|
|
|
s << nTime;
|
|
|
|
|
|
|
|
|
|
if (is_dyna) {
|
|
|
|
|
s << block_height;
|
|
|
|
|
s << m_dynafed_params;
|
|
|
|
|
} else {
|
|
|
|
|
if (g_con_blockheightinheader) {
|
|
|
|
|
s << block_height;
|
|
|
|
|
}
|
|
|
|
|
if (g_signed_blocks) {
|
|
|
|
|
s << proof.challenge;
|
|
|
|
|
} else {
|
|
|
|
|
s << nBits;
|
|
|
|
|
s << nNonce;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return s.GetHash();
|
2013-06-25 03:03:03 +02:00
|
|
|
}
|
|
|
|
|
|
2014-08-20 10:26:27 +02:00
|
|
|
std::string CBlock::ToString() const
|
2013-06-25 03:03:03 +02:00
|
|
|
{
|
2014-08-20 10:26:27 +02:00
|
|
|
std::stringstream s;
|
2018-12-07 10:26:35 +01:00
|
|
|
s << strprintf("CBlock(hash=%s, ver=0x%08x, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, proof=%u, vtx=%u)\n",
|
2014-01-16 16:15:27 +01:00
|
|
|
GetHash().ToString(),
|
2013-06-25 03:03:03 +02:00
|
|
|
nVersion,
|
2014-01-16 16:15:27 +01:00
|
|
|
hashPrevBlock.ToString(),
|
|
|
|
|
hashMerkleRoot.ToString(),
|
2018-12-07 10:26:35 +01:00
|
|
|
nTime, nBits, nNonce, proof.ToString(),
|
2013-06-25 03:03:03 +02:00
|
|
|
vtx.size());
|
2017-07-21 12:27:23 +02:00
|
|
|
for (const auto& tx : vtx) {
|
|
|
|
|
s << " " << tx->ToString() << "\n";
|
2013-06-25 03:03:03 +02:00
|
|
|
}
|
2014-08-20 10:26:27 +02:00
|
|
|
return s.str();
|
2013-06-25 03:03:03 +02:00
|
|
|
}
|
2019-05-31 10:57:36 -04:00
|
|
|
|
2019-06-14 13:14:06 -04:00
|
|
|
uint256 DynaFedParamEntry::CalculateRoot() const
|
2019-05-31 10:57:36 -04:00
|
|
|
{
|
2019-11-07 12:07:16 -05:00
|
|
|
if (m_serialize_type == 0) {
|
2019-05-31 10:57:36 -04:00
|
|
|
return uint256();
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-07 12:07:16 -05:00
|
|
|
std::vector<uint256> compact_leaves;
|
2025-11-06 13:08:59 +02:00
|
|
|
compact_leaves.push_back((HashWriter{} << m_signblockscript).GetHash());
|
|
|
|
|
compact_leaves.push_back((HashWriter{} << m_signblock_witness_limit).GetHash());
|
2019-11-07 12:07:16 -05:00
|
|
|
uint256 compact_root(ComputeFastMerkleRoot(compact_leaves));
|
|
|
|
|
|
|
|
|
|
uint256 extra_root;
|
|
|
|
|
if (m_serialize_type ==1 ) {
|
|
|
|
|
// It's pruned, take the stored value
|
|
|
|
|
extra_root = m_elided_root;
|
|
|
|
|
} else if (m_serialize_type == 2) {
|
|
|
|
|
// It's unpruned, compute the node value
|
|
|
|
|
extra_root = CalculateExtraRoot();
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-31 10:57:36 -04:00
|
|
|
std::vector<uint256> leaves;
|
2019-11-07 12:07:16 -05:00
|
|
|
leaves.push_back(compact_root);
|
|
|
|
|
leaves.push_back(extra_root);
|
2019-05-31 10:57:36 -04:00
|
|
|
return ComputeFastMerkleRoot(leaves);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-07 12:07:16 -05:00
|
|
|
uint256 DynaFedParamEntry::CalculateExtraRoot() const
|
|
|
|
|
{
|
|
|
|
|
std::vector<uint256> extra_leaves;
|
2025-11-06 13:08:59 +02:00
|
|
|
extra_leaves.push_back((HashWriter{} << m_fedpeg_program).GetHash());
|
|
|
|
|
extra_leaves.push_back((HashWriter{} << m_fedpegscript).GetHash());
|
|
|
|
|
extra_leaves.push_back((HashWriter{} << m_extension_space).GetHash());
|
2019-11-07 12:07:16 -05:00
|
|
|
return ComputeFastMerkleRoot(extra_leaves);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-31 10:57:36 -04:00
|
|
|
uint256 DynaFedParams::CalculateRoot() const
|
|
|
|
|
{
|
|
|
|
|
if (IsNull()) {
|
|
|
|
|
return uint256();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<uint256> leaves;
|
|
|
|
|
leaves.push_back(m_current.CalculateRoot());
|
|
|
|
|
leaves.push_back(m_proposed.CalculateRoot());
|
|
|
|
|
return ComputeFastMerkleRoot(leaves);
|
|
|
|
|
}
|