2013-01-08 03:02:51 -08:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2018-01-03 02:12:05 +09:00
|
|
|
// Copyright (c) 2009-2017 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.
|
|
|
|
|
2019-03-06 23:15:11 -08:00
|
|
|
#include <arith_uint256.h>
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <primitives/block.h>
|
2019-03-06 23:15:11 -08:00
|
|
|
#include <streams.h>
|
2013-04-13 00:13:08 -05:00
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <hash.h>
|
|
|
|
#include <tinyformat.h>
|
|
|
|
#include <utilstrencodings.h>
|
|
|
|
#include <crypto/common.h>
|
2020-02-28 17:04:37 -08:00
|
|
|
#include <validation.h>
|
2020-02-28 21:08:27 -08:00
|
|
|
#include <crypto/hash-ops.h>
|
2019-03-06 13:12:55 -08:00
|
|
|
|
2019-03-06 17:44:09 -08:00
|
|
|
extern "C" void cn_slow_hash(const void *data, size_t length, char *hash, int variant, int prehashed, uint64_t height);
|
2019-03-19 22:49:52 -07:00
|
|
|
extern "C" void cn_fast_hash(const void *data, size_t length, char *hash);
|
2019-03-06 17:44:09 -08:00
|
|
|
|
2020-02-29 20:06:56 -08:00
|
|
|
static uint256 cn_get_block_hash_by_height(uint64_t seed_height, char cnHash[32])
|
|
|
|
{
|
|
|
|
CBlockIndex* pblockindex = chainActive[seed_height];
|
2020-03-20 01:24:08 -07:00
|
|
|
if (pblockindex == NULL) {
|
|
|
|
// This will only happens during initial block download.
|
2020-03-20 10:42:20 -07:00
|
|
|
pblockindex = mapBlockSeedHeight.find(seed_height)->second;
|
2020-03-20 01:24:08 -07:00
|
|
|
}
|
2020-03-20 10:42:20 -07:00
|
|
|
assert(pblockindex != NULL);
|
2020-02-29 20:06:56 -08:00
|
|
|
uint256 blockHash = pblockindex->GetBlockHash();
|
|
|
|
const unsigned char* pHash = blockHash.begin();
|
|
|
|
for (int j = 31; j >= 0; j--) {
|
|
|
|
cnHash[31 - j] = pHash[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-21 23:10:45 -07:00
|
|
|
uint256 CBlockHeader::GetOriginalBlockHash() const
|
2013-06-25 03:03:03 +02:00
|
|
|
{
|
2019-03-08 11:19:10 -08:00
|
|
|
CHashWriter hashWriter(SER_GETHASH, PROTOCOL_VERSION);
|
|
|
|
hashWriter.write(BEGIN(nVersion), 80);
|
|
|
|
return hashWriter.GetHash();
|
2013-06-25 03:03:03 +02:00
|
|
|
}
|
|
|
|
|
2020-01-10 21:08:35 -08:00
|
|
|
// prev_id of CN header is used to store the kevacoin block hash.
|
|
|
|
// The value of prev_id and block hash must be the same to prove
|
|
|
|
// that PoW has been properly done.
|
|
|
|
bool CBlockHeader::isCNConsistent() const
|
|
|
|
{
|
|
|
|
return (GetOriginalBlockHash() == cnHeader.prev_id);
|
|
|
|
}
|
|
|
|
|
2019-03-21 23:10:45 -07:00
|
|
|
uint256 CBlockHeader::GetHash() const
|
2019-03-19 22:49:52 -07:00
|
|
|
{
|
|
|
|
uint256 thash;
|
2020-01-10 21:08:35 -08:00
|
|
|
if (!isCNConsistent()) {
|
|
|
|
memset(thash.begin(), 0xff, thash.size());
|
|
|
|
return thash;
|
|
|
|
}
|
2019-03-19 22:49:52 -07:00
|
|
|
cryptonote::blobdata blob = cryptonote::t_serializable_object_to_blob(cnHeader);
|
|
|
|
cn_fast_hash(blob.data(), blob.size(), BEGIN(thash));
|
|
|
|
return thash;
|
|
|
|
}
|
|
|
|
|
2017-02-18 11:13:07 +00:00
|
|
|
uint256 CBlockHeader::GetPoWHash() const
|
|
|
|
{
|
|
|
|
uint256 thash;
|
2020-01-10 21:08:35 -08:00
|
|
|
if (!isCNConsistent()) {
|
2019-03-08 11:55:31 -08:00
|
|
|
memset(thash.begin(), 0xff, thash.size());
|
|
|
|
return thash;
|
|
|
|
}
|
|
|
|
cryptonote::blobdata blob = cryptonote::t_serializable_object_to_blob(cnHeader);
|
2020-02-28 17:04:37 -08:00
|
|
|
uint32_t height = nNonce;
|
|
|
|
if (cnHeader.major_version >= RX_BLOCK_VERSION) {
|
|
|
|
uint64_t seed_height;
|
2020-02-29 20:06:56 -08:00
|
|
|
char cnHash[32];
|
2020-02-28 21:08:27 -08:00
|
|
|
seed_height = crypto::rx_seedheight(height);
|
2020-02-29 20:06:56 -08:00
|
|
|
cn_get_block_hash_by_height(seed_height, cnHash);
|
2020-03-20 10:42:20 -07:00
|
|
|
crypto::rx_slow_hash(height, seed_height, cnHash, blob.data(), blob.size(), BEGIN(thash), 0, 0);
|
2020-02-28 17:04:37 -08:00
|
|
|
} else {
|
|
|
|
cn_slow_hash(blob.data(), blob.size(), BEGIN(thash), cnHeader.major_version - 6, 0, height);
|
2020-02-29 20:06:56 -08:00
|
|
|
}
|
2017-02-18 11:13:07 +00:00
|
|
|
return thash;
|
|
|
|
}
|
|
|
|
|
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;
|
2016-03-29 16:46:20 +02:00
|
|
|
s << strprintf("CBlock(hash=%s, ver=0x%08x, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, vtx=%u)\n",
|
2019-03-21 23:10:45 -07:00
|
|
|
GetOriginalBlockHash().ToString(),
|
2013-06-25 03:03:03 +02:00
|
|
|
nVersion,
|
2014-01-16 16:15:27 +01:00
|
|
|
hashPrevBlock.ToString(),
|
|
|
|
hashMerkleRoot.ToString(),
|
2013-06-25 03:03:03 +02:00
|
|
|
nTime, nBits, nNonce,
|
|
|
|
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
|
|
|
}
|