-
Notifications
You must be signed in to change notification settings - Fork 64
/
block.cpp
86 lines (77 loc) · 2.64 KB
/
block.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2017 The Bitcoin Core developers
// Copyright (c) 2018-2020 The Sugarchain Yumekawa developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <primitives/block.h>
#include <hash.h>
#include <tinyformat.h>
#include <utilstrencodings.h>
#include <crypto/common.h>
// yespower
#include <crypto/yespower-1.0.1/yespower.h>
#include <streams.h>
#include <version.h>
// yespower exit()
#include <stdlib.h>
// yespower PoW cache
#include <sync.h>
uint256 CBlockHeaderUncached::GetHash() const
{
return SerializeHash(*this);
}
// yespowerUncached
uint256 CBlockHeaderUncached::GetPoWHash() const
{
static const yespower_params_t yespower_1_0_sugarchain = {
.version = YESPOWER_1_0,
.N = 2048,
.r = 32,
.pers = (const uint8_t *)"Satoshi Nakamoto 31/Oct/2008 Proof-of-work is essentially one-CPU-one-vote",
.perslen = 74
};
uint256 hash;
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << *this;
if (yespower_tls((const uint8_t *)&ss[0], ss.size(), &yespower_1_0_sugarchain, (yespower_binary_t *)&hash)) {
fprintf(stderr, "Error: CBlockHeaderUncached::GetPoWHash(): failed to compute PoW hash (out of memory?)\n");
exit(1);
}
return hash;
}
// yespower_cached
uint256 CBlockHeader::GetPoWHash_cached() const
{
uint256 block_hash = GetHash();
LOCK(cache_lock);
if (cache_init) {
if (block_hash != cache_block_hash) {
fprintf(stderr, "Error: CBlockHeader::GetPoWHash_cached(): block hash changed unexpectedly\n");
exit(1);
}
// yespower PoW cache: log // O (cyan) = HIT
// printf("\033[36;1mO\033[0m block = %s PoW = %s\n", cache_block_hash.ToString().c_str(), cache_PoW_hash.ToString().c_str());
} else {
cache_PoW_hash = GetPoWHash();
cache_block_hash = block_hash;
cache_init = true;
// yespower PoW cache: log // x = MISS
// printf("x block = %s PoW = %s\n", cache_block_hash.ToString().c_str(), cache_PoW_hash.ToString().c_str());
}
return cache_PoW_hash;
}
std::string CBlock::ToString() const
{
std::stringstream s;
s << strprintf("CBlock(hash=%s, ver=0x%08x, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, vtx=%u)\n",
GetHash().ToString(),
nVersion,
hashPrevBlock.ToString(),
hashMerkleRoot.ToString(),
nTime, nBits, nNonce,
vtx.size());
for (const auto& tx : vtx) {
s << " " << tx->ToString() << "\n";
}
return s.str();
}