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.
|
|
|
|
|
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>
|
|
|
|
#include <utilstrencodings.h>
|
|
|
|
#include <crypto/common.h>
|
2018-09-25 18:00:00 -07:00
|
|
|
//#include <crypto/scrypt.h>
|
2018-09-28 13:03:40 -07:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#include <cryptonight/hash-ops.h>
|
|
|
|
}
|
2013-01-08 04:17:15 -08:00
|
|
|
|
2013-06-25 03:03:03 +02:00
|
|
|
uint256 CBlockHeader::GetHash() const
|
|
|
|
{
|
2015-01-18 19:23:05 +01:00
|
|
|
return SerializeHash(*this);
|
2013-06-25 03:03:03 +02:00
|
|
|
}
|
|
|
|
|
2017-02-18 11:13:07 +00:00
|
|
|
uint256 CBlockHeader::GetPoWHash() const
|
|
|
|
{
|
|
|
|
uint256 thash;
|
2019-02-21 15:26:37 -08:00
|
|
|
// Convert it to Cryptonight header format.
|
|
|
|
uint8_t cnHeader[76] = {};
|
|
|
|
// Bitcoin header size is 80 bytes, while CN is 76 bytes.
|
|
|
|
// To reduce it to 76, combine nVersion(4 bytes) and hashPrevBlock(32 byte)
|
|
|
|
// into 32 byte SHA2 hash.
|
|
|
|
uint8_t combineHash[32] = {};
|
|
|
|
CSHA256().Write((uint8_t*)BEGIN(nVersion), 36).Finalize(combineHash);
|
|
|
|
memcpy(cnHeader, combineHash, sizeof(combineHash));
|
|
|
|
uint32_t offset = sizeof(combineHash);
|
|
|
|
// Copy 7 bytes of hashMerkleRoot
|
|
|
|
memcpy(cnHeader + offset, BEGIN(nVersion) + 36, 7);
|
|
|
|
offset += 7;
|
|
|
|
// Copy nonce.
|
|
|
|
assert(offset == 39);
|
|
|
|
memcpy(cnHeader + offset, BEGIN(nVersion) + 76, 4);
|
|
|
|
offset += 4;
|
|
|
|
// Copy the rest of hashMerkleRoot (25 bytes)
|
|
|
|
memcpy(cnHeader + offset, BEGIN(nVersion) + 36 + 7, 25);
|
|
|
|
offset += 25;
|
|
|
|
assert(offset == 68);
|
|
|
|
// Copy the rest of the header (timestamp and bits).
|
|
|
|
memcpy(cnHeader + offset, BEGIN(nVersion) + 68, 8);
|
|
|
|
// Compute the CN hash.
|
|
|
|
cn_slow_hash(cnHeader, sizeof(cnHeader), BEGIN(thash), 2, 0);
|
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",
|
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(),
|
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
|
|
|
}
|