2013-01-08 11:02:51 +00:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2015-12-13 16:58:29 +00:00
|
|
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
2014-12-13 04:09:33 +00:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-01-08 11:02:51 +00:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2014-11-18 21:03:02 +00:00
|
|
|
#include "primitives/block.h"
|
2013-04-13 05:13:08 +00:00
|
|
|
|
2014-09-25 03:32:36 +00:00
|
|
|
#include "hash.h"
|
2014-08-20 08:51:18 +00:00
|
|
|
#include "tinyformat.h"
|
2014-09-25 03:32:36 +00:00
|
|
|
#include "utilstrencodings.h"
|
2014-12-19 10:39:38 +00:00
|
|
|
#include "crypto/common.h"
|
2013-01-08 12:17:15 +00:00
|
|
|
|
2013-06-25 01:03:03 +00:00
|
|
|
uint256 CBlockHeader::GetHash() const
|
|
|
|
{
|
2015-01-18 18:23:05 +00:00
|
|
|
return SerializeHash(*this);
|
2013-06-25 01:03:03 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 08:26:27 +00:00
|
|
|
std::string CBlock::ToString() const
|
2013-06-25 01:03:03 +00:00
|
|
|
{
|
2014-08-20 08:26:27 +00:00
|
|
|
std::stringstream s;
|
2016-03-29 14:46:20 +00: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 15:15:27 +00:00
|
|
|
GetHash().ToString(),
|
2013-06-25 01:03:03 +00:00
|
|
|
nVersion,
|
2014-01-16 15:15:27 +00:00
|
|
|
hashPrevBlock.ToString(),
|
|
|
|
hashMerkleRoot.ToString(),
|
2013-06-25 01:03:03 +00:00
|
|
|
nTime, nBits, nNonce,
|
|
|
|
vtx.size());
|
|
|
|
for (unsigned int i = 0; i < vtx.size(); i++)
|
|
|
|
{
|
2014-08-20 08:26:27 +00:00
|
|
|
s << " " << vtx[i].ToString() << "\n";
|
2013-06-25 01:03:03 +00:00
|
|
|
}
|
2014-08-20 08:26:27 +00:00
|
|
|
return s.str();
|
2013-06-25 01:03:03 +00:00
|
|
|
}
|
2016-01-03 17:54:50 +00:00
|
|
|
|
|
|
|
int64_t GetBlockCost(const CBlock& block)
|
|
|
|
{
|
|
|
|
// This implements the cost = (stripped_size * 4) + witness_size formula,
|
|
|
|
// using only serialization with and without witness data. As witness_size
|
|
|
|
// is equal to total_size - stripped_size, this formula is identical to:
|
|
|
|
// cost = (stripped_size * 3) + total_size.
|
|
|
|
return ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
}
|