kevacoin/src/primitives/block.cpp

98 lines
3.1 KiB
C++
Raw Normal View History

// Copyright (c) 2009-2010 Satoshi Nakamoto
// 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
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2019-03-06 23:15:11 -08:00
#include <arith_uint256.h>
#include <primitives/block.h>
2019-03-06 23:15:11 -08:00
#include <streams.h>
#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];
if (pblockindex == NULL) {
// This will only happens during initial block download.
pblockindex = mapBlockSeedHeight.find(seed_height)->second;
}
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];
}
}
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
}
// 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);
}
uint256 CBlockHeader::GetHash() const
2019-03-19 22:49:52 -07:00
{
uint256 thash;
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;
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);
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;
}
std::string CBlock::ToString() const
2013-06-25 03:03:03 +02:00
{
std::stringstream s;
s << strprintf("CBlock(hash=%s, ver=0x%08x, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, vtx=%u)\n",
GetOriginalBlockHash().ToString(),
2013-06-25 03:03:03 +02:00
nVersion,
hashPrevBlock.ToString(),
hashMerkleRoot.ToString(),
2013-06-25 03:03:03 +02:00
nTime, nBits, nNonce,
vtx.size());
for (const auto& tx : vtx) {
s << " " << tx->ToString() << "\n";
2013-06-25 03:03:03 +02:00
}
return s.str();
2013-06-25 03:03:03 +02:00
}