PRCYCoin  2.0.0.7rc1
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-2018 The PIVX developers
4 // Copyright (c) 2018-2020 The DAPS Project developers
5 // Distributed under the MIT/X11 software license, see the accompanying
6 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 
8 #include "primitives/block.h"
9 
10 #include "hash.h"
11 #include "script/standard.h"
12 #include "tinyformat.h"
13 #include "utilstrencodings.h"
14 #include "util.h"
15 
17  return Hash(BEGIN(hash), END(hash),
18  BEGIN(nTime), END(nTime),
19  BEGIN(height), END(height));
20 }
21 /*
22 int scanhash_scrypt(uint32_t *pdata) {
23  uint32_t data[20], hash[8];
24  unsigned char scratchbuf = malloc((size_t)N * 128 + 63);
25  uint32_t midstate[8];
26  uint32_t n = pdata[19] - 1;
27  const uint32_t Htarg = ptarget[7];
28  int throughput = scrypt_best_throughput();
29  int i;
30 
31 #ifdef HAVE_SHA256_4WAY
32  if (sha256_use_4way())
33  throughput *= 4;
34 #endif
35 
36  for (i = 0; i < throughput; i++)
37  memcpy(data + i * 20, pdata, 80);
38 
39  sha256_init(midstate);
40  sha256_transform(midstate, data, 0);
41 
42 #if defined(HAVE_SHA256_4WAY)
43  if (throughput == 4)
44  scrypt_1024_1_1_256_4way(data, hash, midstate, scratchbuf, N);
45  else
46 #endif
47 #if defined(HAVE_SCRYPT_3WAY) && defined(HAVE_SHA256_4WAY)
48  if (throughput == 12)
49  scrypt_1024_1_1_256_12way(data, hash, midstate, scratchbuf, N);
50  else
51 #endif
52 #if defined(HAVE_SCRYPT_6WAY)
53  if (throughput == 24)
54  scrypt_1024_1_1_256_24way(data, hash, midstate, scratchbuf, N);
55  else
56 #endif
57 #if defined(HAVE_SCRYPT_3WAY)
58  if (throughput == 3)
59  scrypt_1024_1_1_256_3way(data, hash, midstate, scratchbuf, N);
60  else
61 #endif
62  scrypt_1024_1_1_256(data, hash, midstate, scratchbuf, N);
63  }
64 }*/
65 
67 {
68  if (IsPoABlockByVersion()) {
69  return Hash(BEGIN(nVersion), END(nVersion),
73  BEGIN(nTime), END(nTime),
74  BEGIN(nBits), END(nBits),
75  BEGIN(nNonce), END(nNonce));
76  }
77  return UINT256_ZERO;
78 }
79 
81 {
82  if (IsPoABlockByVersion()) {
83 #if defined(WORDS_BIGENDIAN)
84  // TODO: Big Endian PoA hashing
85 #else // Can take shortcut for little endian
88 #endif
89  }
90  if (nVersion >= 5) {
91 #if defined(WORDS_BIGENDIAN)
92  uint8_t data[80];
93  WriteLE32(&data[0], nVersion);
96  WriteLE32(&data[68], nTime);
97  WriteLE32(&data[72], nBits);
98  WriteLE32(&data[76], nAccumulatorCheckpoint);
99  return Hash(data, data + 80);
100 #else // Can take shortcut for little endian
102 #endif
103  }
104  if (nVersion < 4) {
105 #if defined(WORDS_BIGENDIAN)
106  uint8_t data[80];
107  WriteLE32(&data[0], nVersion);
108  memcpy(&data[4], hashPrevBlock.begin(), hashPrevBlock.size());
109  memcpy(&data[36], hashMerkleRoot.begin(), hashMerkleRoot.size());
110  WriteLE32(&data[68], nTime);
111  WriteLE32(&data[72], nBits);
112  WriteLE32(&data[76], nNonce);
113  return HashQuark(data, data + 80);
114 #else // Can take shortcut for little endian
115  return HashQuark(BEGIN(nVersion), END(nNonce));
116 #endif
117  }
118  // version >= 6
119  return SerializeHash(*this);
120 }
121 
123 {
124  std::vector<uint256> poaMerkleTree;
125  poaMerkleTree.reserve(posBlocksAudited.size() * 2 + 16); // Safe upper bound for the number of total nodes.
126  for (std::vector<PoSBlockSummary>::const_iterator it(posBlocksAudited.begin()); it != posBlocksAudited.end(); ++it)
127  poaMerkleTree.push_back(it->GetHash());
128  int j = 0;
129  bool mutated = false;
130  for (int nSize = posBlocksAudited.size(); nSize > 1; nSize = (nSize + 1) / 2)
131  {
132  for (int i = 0; i < nSize; i += 2)
133  {
134  int i2 = std::min(i+1, nSize-1);
135  if (i2 == i + 1 && i2 + 1 == nSize && poaMerkleTree[j+i] == poaMerkleTree[j+i2]) {
136  // Two identical hashes at the end of the list at a particular level.
137  mutated = true;
138  }
139  poaMerkleTree.push_back(Hash(BEGIN(poaMerkleTree[j+i]), END(poaMerkleTree[j+i]),
140  BEGIN(poaMerkleTree[j+i2]), END(poaMerkleTree[j+i2])));
141  }
142  j += nSize;
143  }
144  if (fMutated) {
145  *fMutated = mutated;
146  }
147  return (poaMerkleTree.empty() ? UINT256_ZERO : poaMerkleTree.back());
148 }
149 
150 std::string CBlock::ToString() const
151 {
152  std::stringstream s;
153  if (IsProofOfAudit()) {
154  s << strprintf("PoABlock(hash=%s, ver=%d, hashPrevBlock=%s, hashPrevPoABlock=%s, hashMerkleRoot=%s, hashPoAMerkleRoot=%s, minedHash=%s, nTime=%u, nBits=%08x, nNonce=%u, vtx=%u, PoSBlocks=%u)\n",
155  GetHash().ToString(),
156  nVersion,
162  nTime, nBits, nNonce,
163  vtx.size(),
164  posBlocksAudited.size());
165  } else {
166  s << strprintf("CBlock(hash=%s, ver=%d, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, vtx=%u)\n",
167  GetHash().ToString(),
168  nVersion,
171  nTime, nBits, nNonce,
172  vtx.size());
173  }
174  for (unsigned int i = 0; i < vtx.size(); i++)
175  {
176  s << " " << vtx[i].ToString() << "\n";
177  }
178  return s.str();
179 }
180 
181 void CBlock::print() const
182 {
183  LogPrintf("%s", ToString());
184 }
block.h
CBlockHeader::hashPoAMerkleRoot
uint256 hashPoAMerkleRoot
Definition: block.h:75
UINT256_ZERO
const uint256 UINT256_ZERO
constant uint256 instances
Definition: uint256.h:129
SerializeHash
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:295
CBlockHeader::hashMerkleRoot
uint256 hashMerkleRoot
Definition: block.h:62
CBlockHeader::nBits
uint32_t nBits
Definition: block.h:65
BEGIN
#define BEGIN(a)
Utilities for converting data from/to strings.
Definition: utilstrencodings.h:17
PoSBlockSummary::nTime
uint32_t nTime
Definition: block.h:20
CBlock::ComputePoAMerkleTree
uint256 ComputePoAMerkleTree(bool *mutated=NULL) const
Definition: block.cpp:122
base_uint::begin
unsigned char * begin()
Definition: arith_uint256.h:240
CBlockHeader::nVersion
int32_t nVersion
Definition: block.h:59
base_uint::size
unsigned int size() const
Definition: arith_uint256.h:260
CBlockHeader::nAccumulatorCheckpoint
uint256 nAccumulatorCheckpoint
Definition: block.h:67
CBlockHeader::IsPoABlockByVersion
bool IsPoABlockByVersion() const
Definition: block.h:105
memcpy
void * memcpy(void *a, const void *b, size_t c)
Definition: glibc_compat.cpp:15
CBlockHeader::GetHash
uint256 GetHash() const
Definition: block.cpp:80
tinyformat.h
CBlockHeader::nNonce
uint32_t nNonce
Definition: block.h:66
PoSBlockSummary::GetHash
uint256 GetHash() const
Definition: block.cpp:16
CBlock::print
void print() const
Definition: block.cpp:181
LogPrintf
#define LogPrintf(...)
Definition: logging.h:147
standard.h
CBlockHeader::minedHash
uint256 minedHash
Definition: block.h:79
CBlockHeader::nTime
uint32_t nTime
Definition: block.h:64
uint256
256-bit unsigned big integer.
Definition: uint256.h:38
HashQuark
uint256 HashQuark(const T1 pbegin, const T1 pend)
Definition: hash.h:312
CBlockHeader::hashPrevBlock
uint256 hashPrevBlock
Definition: block.h:61
CBlock::vtx
std::vector< CTransaction > vtx
Definition: block.h:146
strprintf
#define strprintf
Definition: tinyformat.h:1056
PoSBlockSummary::height
uint32_t height
Definition: block.h:21
END
#define END(a)
Definition: utilstrencodings.h:18
PoSBlockSummary::hash
uint256 hash
Definition: block.h:19
CBlockHeader::hashPrevPoABlock
uint256 hashPrevPoABlock
Definition: block.h:73
CBlock::ToString
std::string ToString() const
Definition: block.cpp:150
CBlock::IsProofOfAudit
bool IsProofOfAudit() const
Definition: block.h:228
utilstrencodings.h
Hash
std::string Hash(std::string input)
Compute the 256-bit hash of a std::string.
Definition: hash.h:122
CBlockHeader::ComputeMinedHash
uint256 ComputeMinedHash() const
Definition: block.cpp:66
CBlock::poaMerkleTree
std::vector< uint256 > poaMerkleTree
Definition: block.h:157
CBlock::posBlocksAudited
std::vector< PoSBlockSummary > posBlocksAudited
Definition: block.h:149
base_uint::ToString
std::string ToString() const
Definition: arith_uint256.cpp:199