-
Notifications
You must be signed in to change notification settings - Fork 718
Expand file tree
/
Copy pathblock.cpp
More file actions
188 lines (159 loc) · 5.81 KB
/
block.cpp
File metadata and controls
188 lines (159 loc) · 5.81 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2016 The Bitcoin Core developers
// Copyright (c) 2017-2020 The Raven Core 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"
static const uint32_t MAINNET_X16RV2ACTIVATIONTIME = 1569945600;
static const uint32_t TESTNET_X16RV2ACTIVATIONTIME = 1567533600;
static const uint32_t REGTEST_X16RV2ACTIVATIONTIME = 1569931200;
uint32_t nKAWPOWActivationTime;
uint32_t nSHA256KawpowSwitchActivationTime;
bool fKawpowAsMiningAlgo;
BlockNetwork bNetwork = BlockNetwork();
BlockNetwork::BlockNetwork()
{
fOnTestnet = false;
fOnRegtest = false;
}
void BlockNetwork::SetNetwork(const std::string& net)
{
if (net == "test") {
fOnTestnet = true;
} else if (net == "regtest") {
fOnRegtest = true;
}
}
uint256 CBlockHeader::GetHash() const
{
// Check if we are before the Kawpow activation time
if (nTime < nKAWPOWActivationTime) {
uint32_t nTimeToUse = MAINNET_X16RV2ACTIVATIONTIME;
if (bNetwork.fOnTestnet) {
nTimeToUse = TESTNET_X16RV2ACTIVATIONTIME;
} else if (bNetwork.fOnRegtest) {
nTimeToUse = REGTEST_X16RV2ACTIVATIONTIME;
}
// Use X16RV2 or X16R before Kawpow activation
if (nTime >= nTimeToUse) {
return HashX16RV2(BEGIN(nVersion), END(nNonce), hashPrevBlock);
}
return HashX16R(BEGIN(nVersion), END(nNonce), hashPrevBlock);
} else {
// Kawpow activation time has passed, now check if we are on the testnet
if (bNetwork.fOnTestnet && !fKawpowAsMiningAlgo && nTime >= nSHA256KawpowSwitchActivationTime) {
// If on testnet and Kawpow is disabled, use SerializeHash (CPU-friendly)
return SerializeHash(*this);
} else {
// Otherwise, use Kawpow (mainnet or regtest, or Kawpow on testnet)
return KAWPOWHash_OnlyMix(*this);
}
}
}
uint256 CBlockHeader::GetHashFull(uint256& mix_hash) const
{
// Check if we are before the Kawpow activation time
if (nTime < nKAWPOWActivationTime) {
uint32_t nTimeToUse = MAINNET_X16RV2ACTIVATIONTIME;
if (bNetwork.fOnTestnet) {
nTimeToUse = TESTNET_X16RV2ACTIVATIONTIME;
} else if (bNetwork.fOnRegtest) {
nTimeToUse = REGTEST_X16RV2ACTIVATIONTIME;
}
// Use X16RV2 or X16R before Kawpow activation
if (nTime >= nTimeToUse) {
return HashX16RV2(BEGIN(nVersion), END(nNonce), hashPrevBlock);
}
return HashX16R(BEGIN(nVersion), END(nNonce), hashPrevBlock);
} else {
// Kawpow activation time has passed, now check if we are on the testnet
if (bNetwork.fOnTestnet && !fKawpowAsMiningAlgo && nTime >= nSHA256KawpowSwitchActivationTime) {
// If on testnet and Kawpow is disabled, use SerializeHash (CPU-friendly)
return SerializeHash(*this);
} else {
// Otherwise, use Kawpow (mainnet or regtest, or Kawpow on testnet)
return KAWPOWHash(*this, mix_hash);
}
}
}
uint256 CBlockHeader::GetX16RHash() const
{
return HashX16R(BEGIN(nVersion), END(nNonce), hashPrevBlock);
}
uint256 CBlockHeader::GetX16RV2Hash() const
{
return HashX16RV2(BEGIN(nVersion), END(nNonce), hashPrevBlock);
}
uint256 CBlockHeader::GetSerializeHash() const
{
return SerializeHash(*this); // Use standard SHA256 mining
}
/**
* @brief This takes a block header, removes the nNonce64 and the mixHash. Then performs a serialized hash of it SHA256D.
* This will be used as the input to the KAAAWWWPOW hashing function
* @note Only to be called and used on KAAAWWWPOW block headers
*/
uint256 CBlockHeader::GetKAWPOWHeaderHash() const
{
CKAWPOWInput input{*this};
return SerializeHash(input);
}
std::string CBlockHeader::ToString() const
{
std::stringstream s;
s << strprintf("CBlock(ver=0x%08x, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, nNonce64=%u, nHeight=%u)\n",
nVersion,
hashPrevBlock.ToString(),
hashMerkleRoot.ToString(),
nTime, nBits, nNonce, nNonce64, nHeight);
return s.str();
}
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, nNonce64=%u, vtx=%u)\n",
GetHash().ToString(),
nVersion,
hashPrevBlock.ToString(),
hashMerkleRoot.ToString(),
nTime, nBits, nNonce, nNonce64,
vtx.size());
for (const auto& tx : vtx) {
s << " " << tx->ToString() << "\n";
}
return s.str();
}
/// Used to test algo switching between X16R and X16RV2
//uint256 CBlockHeader::TestTiger() const
//{
// return HashTestTiger(BEGIN(nVersion), END(nNonce), hashPrevBlock);
//}
//
//uint256 CBlockHeader::TestSha512() const
//{
// return HashTestSha512(BEGIN(nVersion), END(nNonce), hashPrevBlock);
//}
//
//uint256 CBlockHeader::TestGost512() const
//{
// return HashTestGost512(BEGIN(nVersion), END(nNonce), hashPrevBlock);
//}
//CBlock block = GetParams().GenesisBlock();
//int64_t nStart = GetTimeMillis();
//LogPrintf("Starting Tiger %dms\n", nStart);
//block.TestTiger();
//LogPrintf("Tiger Finished %dms\n", GetTimeMillis() - nStart);
//
//nStart = GetTimeMillis();
//LogPrintf("Starting Sha512 %dms\n", nStart);
//block.TestSha512();
//LogPrintf("Sha512 Finished %dms\n", GetTimeMillis() - nStart);
//
//nStart = GetTimeMillis();
//LogPrintf("Starting Gost512 %dms\n", nStart);
//block.TestGost512();
//LogPrintf("Gost512 Finished %dms\n", GetTimeMillis() - nStart);