PIVX Core  5.6.99
P2P Digital Currency
block.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Copyright (c) 2015-2021 The PIVX Core developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include "primitives/block.h"
8 
9 #include "hash.h"
10 #include "script/standard.h"
11 #include "tinyformat.h"
12 #include "utilstrencodings.h"
13 #include "util/system.h"
14 
16 {
17  if (nVersion < 4) {
18 #if defined(WORDS_BIGENDIAN)
19  uint8_t data[80];
20  WriteLE32(&data[0], nVersion);
23  WriteLE32(&data[68], nTime);
24  WriteLE32(&data[72], nBits);
25  WriteLE32(&data[76], nNonce);
26  return HashQuark(data, data + 80);
27 #else // Can take shortcut for little endian
28  return HashQuark(BEGIN(nVersion), END(nNonce));
29 #endif
30  }
31  // version >= 4
32  return SerializeHash(*this);
33 }
34 
35 std::string CBlock::ToString() const
36 {
37  std::stringstream s;
38  s << strprintf("CBlock(hash=%s, ver=%d, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, hashFinalSaplingRoot=%s, vtx=%u)\n",
39  GetHash().ToString(),
40  nVersion,
43  nTime, nBits, nNonce,
45  vtx.size());
46  for (unsigned int i = 0; i < vtx.size(); i++)
47  {
48  s << " " << vtx[i]->ToString() << "\n";
49  }
50  return s.str();
51 }
52 
53 void CBlock::print() const
54 {
55  LogPrintf("%s\n", ToString());
56 }
uint32_t nNonce
Definition: block.h:32
uint256 hashFinalSaplingRoot
Definition: block.h:34
uint32_t nBits
Definition: block.h:31
uint32_t nTime
Definition: block.h:30
int32_t nVersion
Definition: block.h:27
uint256 hashPrevBlock
Definition: block.h:28
uint256 hashMerkleRoot
Definition: block.h:29
uint256 GetHash() const
Definition: block.cpp:15
void print() const
Definition: block.cpp:53
std::string ToString() const
Definition: block.cpp:35
std::vector< CTransactionRef > vtx
Definition: block.h:83
unsigned int size() const
Definition: uint256.h:83
std::string ToString() const
Definition: uint256.cpp:65
unsigned char * begin()
Definition: uint256.h:63
256-bit opaque blob.
Definition: uint256.h:138
void * memcpy(void *a, const void *b, size_t c)
uint256 HashQuark(const T1 pbegin, const T1 pend)
Definition: hash.h:345
uint256 SerializeHash(const T &obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Compute the 256-bit hash of an object's serialization.
Definition: hash.h:289
#define strprintf
Definition: tinyformat.h:1056
#define BEGIN(a)
Utilities for converting data from/to strings.
#define END(a)