/
rpcmining.cpp
760 lines (612 loc) · 31.4 KB
/
rpcmining.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
// Copyright (c) 2010 Satoshi Nakamoto
// Copyright (c) 2009-2012 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "chainparams.h"
#include "db.h"
#include "init.h"
#include "miner.h"
#include "bitcoinrpc.h"
using namespace json_spirit;
using namespace std;
// Key used by getwork/getblocktemplate miners. Ключь, используемый для getwork/getblocktemplate майнеров
// Allocated in InitRPCMining, free'd in ShutdownRPCMining Выделенный в InitRPCMining, освобожденный в ShutdownRPCMining
static CReserveKey* pMiningKey = NULL;
void InitRPCMining()
{
// getwork/getblocktemplate mining rewards paid here: getwork/getblocktemplate майнинг вознаграждение выплачивается здесь:
pMiningKey = new CReserveKey(pwalletMain);
}
void ShutdownRPCMining()
{
delete pMiningKey; pMiningKey = NULL;
}
Value getblocktarget(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 1)
throw runtime_error(
"getblocktarget <hash>\n"
"Returns block target.");
std::string strHash = params[0].get_str();
uint256 hash(strHash);
if (mapBlockIndex.count(hash) == 0)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
CBlock block;
CBlockIndex* pblockindex = mapBlockIndex[hash];
ReadBlockFromDisk(block, pblockindex);
CBigNum sumTrDif = 0;
CBigNum maxBigNum = CBigNum(~uint256(0));
BOOST_FOREACH(CTransaction& tx, block.vtx)
{
if (!tx.IsCoinBase())
{
TransM trM;
BOOST_FOREACH(const CTxIn& txin, tx.vin)
trM.vinM.push_back(CTxIn(txin.prevout.hash, txin.prevout.n));
BOOST_FOREACH (const CTxOut& out, tx.vout)
trM.voutM.push_back(CTxOut(out.nValue, CScript()));
int txBl = abs(tx.tBlock);
if (txBl >= pblockindex->nHeight - 1)
txBl = pblockindex->nHeight - 1 - TX_TBLOCK;
trM.hashBlock = vBlockIndexByHeight[txBl]->GetBlockHash();
uint256 HashTr = SerializeHash(trM);
if (txBl > HEIGHT_OTHER_ALGO)
lyra2TDC(BEGIN(HashTr), BEGIN(HashTr), 32);
else
lyra2re2_hashTX(BEGIN(HashTr), BEGIN(HashTr), 32);
CBigNum bntx = CBigNum(HashTr);
sumTrDif += (maxBigNum / bntx) / (pblockindex->nHeight - 1 - txBl);
}
}
CBigNum divideTarget = (maxBigNum / CBigNum().SetCompact(block.nBits)) - 1;
int precision = 1000;
double snowfox = 1.05;
double CDFtrdt = 1 - exp(- (snowfox * sumTrDif.getuint256().getdouble()) / divideTarget.getuint256().getdouble());
double CDFsize = 1 - exp(- (double)block.vtx.size() / (double)QUANTITY_TX);
int backlash = precision * CDFtrdt * CDFsize;
uint256 hashTarget = (maxBigNum / (1 + divideTarget - (divideTarget / precision) * backlash)).getuint256();
Object obj;
obj.push_back(Pair("block ", block.GetHash().GetHex()));
obj.push_back(Pair("target", hashTarget.GetHex()));
obj.push_back(Pair("old tar", CBigNum().SetCompact(block.nBits).getuint256().GetHex()));
obj.push_back(Pair("total tx", (boost::int64_t)block.vtx.size()));
obj.push_back(Pair("CDFtrdt", CDFtrdt));
obj.push_back(Pair("CDFsize", CDFsize));
obj.push_back(Pair("backlash", backlash));
obj.push_back(Pair("decrease ~ %", ((divideTarget / precision) * backlash).getuint256().getdouble() / divideTarget.getuint256().getdouble()));
obj.push_back(Pair("maxBigNum", maxBigNum.getuint256().getdouble()));
obj.push_back(Pair("divideTarget", divideTarget.getuint256().getdouble()));
obj.push_back(Pair("- sumTrDif -", sumTrDif.getuint256().getdouble()));
return obj;
}
Value usetxinblock(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 1)
throw runtime_error(
"usetxinblock <hash>\n"
"Returns an object containing block and transaction hashes related information.");
std::string strHash = params[0].get_str();
uint256 hash(strHash);
if (mapBlockIndex.count(hash) == 0)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
CBlock block;
CBlockIndex* pblockindex = mapBlockIndex[hash];
ReadBlockFromDisk(block, pblockindex);
int64 txFees = 0;
BOOST_FOREACH(CTransaction& tx, block.vtx)
{
if (!tx.IsCoinBase())
{
int64 nIn = 0;
BOOST_FOREACH(const CTxIn& txin, tx.vin)
{
CTransaction getTx;
const uint256 txHash = txin.prevout.hash;
uint256 hashBlock = 0;
if (GetTransaction(txHash, getTx, hashBlock, false))
nIn += getTx.vout[txin.prevout.n].nValue;
//else проверка ошибок, если нужно
}
int64 nOut = 0;
BOOST_FOREACH (CTxOut& out, tx.vout)
{
nOut += out.nValue;
}
txFees = nIn - nOut;
}
}
Object obj;
obj.push_back(Pair("block", block.GetHash().GetHex()));
obj.push_back(Pair("tx", (boost::int64_t)block.vtx.size() - 1));
obj.push_back(Pair("txFees", ValueFromAmount(txFees)));
obj.push_back(Pair("subsidy block", ValueFromAmount(GetBlockValue(pblockindex->nHeight, txFees))));
for (int i = 0; i < (int)BLOCK_TX_FEE; i++)
obj.push_back(Pair(" - block", vBlockIndexByHeight[pblockindex->nHeight - i - 1]->GetBlockHash().GetHex()));
vector<TxHashPriority> vecTxHashPriority;
std::map<uint256, uint256> mapTxHashes;
if (pblockindex->nHeight > int(BLOCK_TX_FEE + NUMBER_BLOCK_TX))
{
uint256 useHashBack;
for (unsigned int i = 0; i < NUMBER_BLOCK_TX; i++) // получение транзакций которым возможен возврат комиссий
{
CBlock rBlock;
int bHeight = pblockindex->nHeight - BLOCK_TX_FEE - i - 1;
ReadBlockFromDisk(rBlock, vBlockIndexByHeight[bHeight]); // -5, -6, -7, -8, -9 блоки
// ReadBlockFromDisk(rBlock, vBlockIndexByHeight[pblockindex->nHeight - BLOCK_TX_FEE - i - 1]);
obj.push_back(Pair("block", rBlock.GetHash().GetHex()));
obj.push_back(Pair("tx", (boost::int64_t)rBlock.vtx.size() - 1));
if (i == 0)
useHashBack = rBlock.GetHash(); // хэш(uint256) блока для определения случайных позиций
BOOST_FOREACH(CTransaction& tx, rBlock.vtx)
{
if (!tx.IsCoinBase())
{
TransM trM;
int64 nIn = 0;
BOOST_FOREACH(const CTxIn& txin, tx.vin)
{
trM.vinM.push_back(CTxIn(txin.prevout.hash, txin.prevout.n));
CTransaction getTx;
const uint256 txHash = txin.prevout.hash;
uint256 hashBlock = 0;
if (GetTransaction(txHash, getTx, hashBlock, false))
nIn += getTx.vout[txin.prevout.n].nValue;
//else проверка ошибок, если нужно
}
int64 nOut = 0;
BOOST_FOREACH (CTxOut& out, tx.vout)
{
trM.voutM.push_back(CTxOut(out.nValue, CScript()));
nOut += out.nValue;
}
int64 nTxFees = nIn - nOut;
int txBl = abs(tx.tBlock);
if (txBl >= bHeight)
txBl = bHeight - TX_TBLOCK; // TX_TBLOCK от pindexBest (bool CWallet::CreateTransaction)
trM.hashBlock = vBlockIndexByHeight[txBl]->GetBlockHash();
uint256 HashTr = SerializeHash(trM);
if (txBl > HEIGHT_OTHER_ALGO)
lyra2TDC(BEGIN(HashTr), BEGIN(HashTr), 32);
else
lyra2re2_hashTX(BEGIN(HashTr), BEGIN(HashTr), 32);
CTransaction getTx;
const uint256 txHash = tx.vin[0].prevout.hash;
uint256 hashBlock = 0;
if (GetTransaction(txHash, getTx, hashBlock, false))
vecTxHashPriority.push_back(TxHashPriority(HashTr, CTxOut(nTxFees, getTx.vout[tx.vin[0].prevout.n].scriptPubKey)));
mapTxHashes[HashTr] = tx.GetHash();
}
}
if (vecTxHashPriority.size() > QUANTITY_TX)
break;
}
obj.push_back(Pair("total tx", (boost::int64_t)vecTxHashPriority.size()));
if (vecTxHashPriority.size() > 1)
{
TxHashPriorityCompare comparerHash(true);
std::sort(vecTxHashPriority.begin(), vecTxHashPriority.end(), comparerHash);
double powsqrt = (useHashBack & CBigNum(uint256(65535)).getuint256()).getdouble() * 0.00001 + 1.2; // получаем число от 1,2 до 1,85535
unsigned int stepTr = pow((double)vecTxHashPriority.size(), 1.0 / powsqrt); // величина промежутка
unsigned int numPosition = vecTxHashPriority.size() / stepTr; // количество промежутков
unsigned int arProgression = stepTr / numPosition; // аргумент арифметической прогрессии при котором последний промежуток почти равен первым двум
obj.push_back(Pair("random one", powsqrt));
powsqrt = (useHashBack & CBigNum(uint256(262143)).getuint256()).getdouble() * 0.000001; // число от 0 до 0,262143
obj.push_back(Pair("random two", 0.4 + powsqrt));
unsigned int retFeesTr = (stepTr + 1) * (0.4 + powsqrt); // во сколько раз нужно умножить возвращаемую комиссию (+1 чтобы не было 0)
unsigned int pos = 0;
unsigned int nextInt = 0;
unsigned int ntx = 0;
unsigned int www = 0;
unsigned int iii = 0;
BOOST_FOREACH(const TxHashPriority& tx, vecTxHashPriority)
{
if (nextInt == iii)
{
useHashBack = Hash(BEGIN(useHashBack), END(useHashBack));
unsigned int interval = stepTr + www * arProgression; // разбивка vecTxHashPriority на промежутки
pos = nextInt + interval * (useHashBack & CBigNum(uint256(1048575)).getuint256()).getdouble() / 1048575.0; // получаем число от 0 до 1
nextInt = iii + interval + 1;
www++;
obj.push_back(Pair("interval", (boost::int64_t)interval));
obj.push_back(Pair("start next interval", (boost::int64_t)nextInt));
}
if (iii == pos)
{
ntx++;
obj.push_back(Pair("this", "tx"));
obj.push_back(Pair(tx.get<0>().GetHex(), mapTxHashes[tx.get<0>()].GetHex()));
obj.push_back(Pair("fee", ValueFromAmount(tx.get<1>().nValue)));
obj.push_back(Pair("fee return", ValueFromAmount(tx.get<1>().nValue * retFeesTr)));
CTxDestination address;
if (ExtractDestination(tx.get<1>().scriptPubKey, address))
obj.push_back(Pair("address", CBitcoinAddress(address).ToString()));
}
else
obj.push_back(Pair(tx.get<0>().GetHex(), mapTxHashes[tx.get<0>()].GetHex()));
iii++;
}
obj.push_back(Pair("selected transactions", (boost::int64_t)ntx));
obj.push_back(Pair("increase fee in", (boost::int64_t)retFeesTr));
}
}
return obj;
}
// Return average network hashes per second based on the last 'lookup' blocks,
// or from the last difficulty change if 'lookup' is nonpositive.
// If 'height' is nonnegative, compute the estimate at the time when a given block was found.
Value GetNetworkHashPS(int lookup, int height) {
CBlockIndex *pb = pindexBest;
if (height >= 0 && height < nBestHeight)
pb = FindBlockByHeight(height);
if (pb == NULL || !pb->nHeight)
return 0;
// If lookup is -1, then use blocks since last difficulty change.
if (lookup <= 0)
lookup = pb->nHeight % 2016 + 1;
// If lookup is larger than chain, then set it to chain length.
if (lookup > pb->nHeight)
lookup = pb->nHeight;
CBlockIndex *pb0 = pb;
int64 minTime = pb0->GetBlockTime();
int64 maxTime = minTime;
for (int i = 0; i < lookup; i++) {
pb0 = pb0->pprev;
int64 time = pb0->GetBlockTime();
minTime = std::min(time, minTime);
maxTime = std::max(time, maxTime);
}
// In case there's a situation where minTime == maxTime, we don't want a divide by zero exception.
if (minTime == maxTime)
return 0;
uint256 workDiff = pb->nChainWork - pb0->nChainWork;
int64 timeDiff = maxTime - minTime;
return (boost::int64_t)(workDiff.getdouble() / timeDiff);
}
Value getnetworkhashps(const Array& params, bool fHelp)
{
if (fHelp || params.size() > 2)
throw runtime_error(
"getnetworkhashps [blocks] [height]\n"
"Returns the estimated network hashes per second based on the last 120 blocks.\n"
"Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change.\n"
"Pass in [height] to estimate the network speed at the time when a certain block was found.");
return GetNetworkHashPS(params.size() > 0 ? params[0].get_int() : 120, params.size() > 1 ? params[1].get_int() : -1);
}
Value getgenerate(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 0)
throw runtime_error(
"getgenerate\n"
"Returns true or false.");
return GetBoolArg("-gen", false);
}
Value setgenerate(const Array& params, bool fHelp)
{
if (fHelp || params.size() < 1 || params.size() > 2)
throw runtime_error(
"setgenerate <generate> [genproclimit]\n"
"<generate> is true or false to turn generation on or off.\n"
"Generation is limited to [genproclimit] processors, -1 is unlimited.");
bool fGenerate = true;
if (params.size() > 0)
fGenerate = params[0].get_bool();
if (params.size() > 1)
{
int nGenProcLimit = params[1].get_int();
mapArgs["-genproclimit"] = itostr(nGenProcLimit);
if (nGenProcLimit == 0)
fGenerate = false;
}
mapArgs["-gen"] = (fGenerate ? "1" : "0");
GenerateCoins(fGenerate, pwalletMain);
return Value::null;
}
Value gethashespersec(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 0)
throw runtime_error(
"gethashespersec\n"
"Returns a recent hashes per second performance measurement while generating.");
if (GetTimeMillis() - nHPSTimerStart > 8000)
return (boost::int64_t)0;
return (boost::int64_t)dHashesPerSec;
}
Value getmininginfo(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 0)
throw runtime_error(
"getmininginfo\n"
"Returns an object containing mining-related information.");
Object obj;
obj.push_back(Pair("blocks", (int)nBestHeight));
obj.push_back(Pair("currentblocksize", (uint64_t)nLastBlockSize));
obj.push_back(Pair("currentblocktx", (uint64_t)nLastBlockTx));
obj.push_back(Pair("difficulty", (double)GetDifficulty()));
obj.push_back(Pair("errors", GetWarnings("statusbar")));
obj.push_back(Pair("generate", GetBoolArg("-gen", false)));
obj.push_back(Pair("genproclimit", (int)GetArg("-genproclimit", -1)));
obj.push_back(Pair("hashespersec", gethashespersec(params, false)));
obj.push_back(Pair("networkhashps", getnetworkhashps(params, false)));
obj.push_back(Pair("pooledtx", (uint64_t)mempool.size()));
obj.push_back(Pair("testnet", TestNet()));
return obj;
}
Value getwork(const Array& params, bool fHelp)
{
if (fHelp || params.size() > 1)
throw runtime_error(
"getwork [data]\n"
"If [data] is not specified, returns formatted hash data to work on:\n"
" \"midstate\" : precomputed hash state after hashing the first half of the data (DEPRECATED)\n" // deprecated
" \"data\" : block data\n"
" \"hash1\" : formatted hash buffer for second hash (DEPRECATED)\n" // deprecated
" \"target\" : little endian hash target\n"
"If [data] is specified, tries to solve the block and returns true if it was successful.");
if (vNodes.empty())
throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "TDC is not connected!");
if (IsInitialBlockDownload())
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "TDC is downloading blocks...");
typedef map<uint256, pair<CBlock*, CScript> > mapNewBlock_t;
static mapNewBlock_t mapNewBlock; // FIXME: thread safety
static vector<CBlockTemplate*> vNewBlockTemplate;
if (params.size() == 0)
{
// Update block Обновление блока
static unsigned int nTransactionsUpdatedLast;
static CBlockIndex* pindexPrev;
static int64 nStart;
static CBlockTemplate* pblocktemplate;
if (pindexPrev != pindexBest ||
(nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 60))
{
if (pindexPrev != pindexBest)
{
// Deallocate old blocks since they're obsolete now Освобождение старых блоков, так как они являются устаревшими теперь
mapNewBlock.clear();
BOOST_FOREACH(CBlockTemplate* pblocktemplate, vNewBlockTemplate)
delete pblocktemplate;
vNewBlockTemplate.clear();
}
// Clear pindexPrev so future getworks make a new block, despite any failures from here on
// Очистить pindexPrev так будущие getworks будут делать новый блок, несмотря на какие-либо ошибки
pindexPrev = NULL;
// Store the pindexBest used before CreateNewBlock, to avoid races Сохранение pindexBest использующийся до CreateNewBlock, что бы избежать гонок
nTransactionsUpdatedLast = nTransactionsUpdated;
CBlockIndex* pindexPrevNew = pindexBest;
nStart = GetTime();
// Create new block Создание нового блока
pblocktemplate = CreateNewBlock(*pMiningKey);
if (!pblocktemplate)
throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");
vNewBlockTemplate.push_back(pblocktemplate);
// Need to update only after we know CreateNewBlock succeeded Необходимое обновление только после того, как мы узнаем, что CreateNewBlock удалось
pindexPrev = pindexPrevNew;
}
CBlock* pblock = &pblocktemplate->block; // pointer for convenience
// Update nTime обновление nTime
UpdateTime(*pblock, pindexPrev);
pblock->nNonce = 0;
// Update nExtraNonce обновление nExtraNonce
static unsigned int nExtraNonce = 0;
IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
// Save
mapNewBlock[pblock->hashMerkleRoot] = make_pair(pblock, pblock->vtx[0].vin[0].scriptSig);
// Pre-build hash buffers Предварительная сборка хеш буферов
char pmidstate[32];
char pdata[128];
char phash1[64];
FormatHashBuffers(pblock, pmidstate, pdata, phash1);
uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
Object result;
result.push_back(Pair("midstate", HexStr(BEGIN(pmidstate), END(pmidstate)))); // deprecated(возражать,устарело)
result.push_back(Pair("data", HexStr(BEGIN(pdata), END(pdata))));
result.push_back(Pair("hash1", HexStr(BEGIN(phash1), END(phash1)))); // deprecated(возражать,устарело)
result.push_back(Pair("target", HexStr(BEGIN(hashTarget), END(hashTarget))));
return result;
}
else
{
// Parse parameters Парсинг параметров
vector<unsigned char> vchData = ParseHex(params[0].get_str());
if (vchData.size() != 128)
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter");
CBlock* pdata = (CBlock*)&vchData[0];
// Byte reverse байт реверс
for (int i = 0; i < 128/4; i++)
((unsigned int*)pdata)[i] = ByteReverse(((unsigned int*)pdata)[i]);
// Get saved block Получить сохраненный блок
if (!mapNewBlock.count(pdata->hashMerkleRoot))
return false;
CBlock* pblock = mapNewBlock[pdata->hashMerkleRoot].first;
pblock->nTime = pdata->nTime;
pblock->nNonce = pdata->nNonce;
pblock->vtx[0].vin[0].scriptSig = mapNewBlock[pdata->hashMerkleRoot].second;
pblock->hashMerkleRoot = pblock->BuildMerkleTree();
//*****************************************************************
CBigNum maxBigNum = CBigNum(~uint256(0));
CBigNum sumTrDif = 0;
BOOST_FOREACH(CTransaction& tx, pblock->vtx)
{
if (!tx.IsCoinBase())
{
TransM trM;
BOOST_FOREACH(const CTxIn& txin, tx.vin)
trM.vinM.push_back(CTxIn(txin.prevout.hash, txin.prevout.n));
BOOST_FOREACH (const CTxOut& out, tx.vout)
trM.voutM.push_back(CTxOut(out.nValue, CScript()));
int txBl = abs(tx.tBlock);
if (txBl >= pindexBest->nHeight)
txBl = pindexBest->nHeight - TX_TBLOCK; // TX_TBLOCK от pindexBest (bool CWallet::CreateTransaction)
trM.hashBlock = vBlockIndexByHeight[txBl]->GetBlockHash();
uint256 HashTr = SerializeHash(trM);
if (txBl > HEIGHT_OTHER_ALGO)
lyra2TDC(BEGIN(HashTr), BEGIN(HashTr), 32);
else
lyra2re2_hashTX(BEGIN(HashTr), BEGIN(HashTr), 32);
CBigNum bntx = CBigNum(HashTr);
sumTrDif += (maxBigNum / bntx) / (pindexBest->nHeight - txBl); // защита от 51% с использованием майнингхешей транзакций ссылающихся на более старые блоки
}
}
return CheckWork(pblock, *pwalletMain, *pMiningKey, sumTrDif);
//*****************************************************************
// return CheckWork(pblock, *pwalletMain, *pMiningKey);
}
}
Value getblocktemplate(const Array& params, bool fHelp)
{
if (fHelp || params.size() > 1)
throw runtime_error(
"getblocktemplate [params]\n"
"Returns data needed to construct a block to work on:\n"
" \"version\" : block version\n"
" \"previousblockhash\" : hash of current highest block\n"
" \"transactions\" : contents of non-coinbase transactions that should be included in the next block\n"
" \"coinbaseaux\" : data that should be included in coinbase\n"
" \"coinbasevalue\" : maximum allowable input to coinbase transaction, including the generation award and transaction fees\n"
" \"target\" : hash target\n"
" \"mintime\" : minimum timestamp appropriate for next block\n"
" \"curtime\" : current timestamp\n"
" \"mutable\" : list of ways the block template may be changed\n"
" \"noncerange\" : range of valid nonces\n"
" \"sigoplimit\" : limit of sigops in blocks\n"
" \"sizelimit\" : limit of block size\n"
" \"bits\" : compressed target of next block\n"
" \"height\" : height of the next block\n"
"See https://en.bitcoin.it/wiki/BIP_0022 for full specification.");
std::string strMode = "template";
if (params.size() > 0)
{
const Object& oparam = params[0].get_obj();
const Value& modeval = find_value(oparam, "mode");
if (modeval.type() == str_type)
strMode = modeval.get_str();
else if (modeval.type() == null_type)
{
/* Do nothing */
}
else
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
}
if (strMode != "template")
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
if (vNodes.empty())
throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "TDC is not connected!");
if (IsInitialBlockDownload())
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "TDC is downloading blocks...");
// Update block Обновление блока
static unsigned int nTransactionsUpdatedLast;
static CBlockIndex* pindexPrev;
static int64 nStart;
static CBlockTemplate* pblocktemplate;
if (pindexPrev != pindexBest ||
(nTransactionsUpdated != nTransactionsUpdatedLast && GetTime() - nStart > 5))
{
// Clear pindexPrev so future calls make a new block, despite any failures from here on
// Очистить pindexPrev так будущие вызовы будут делать новый блок, несмотря на какие-либо ошибки
pindexPrev = NULL;
// Store the pindexBest used before CreateNewBlock, to avoid races
nTransactionsUpdatedLast = nTransactionsUpdated;
CBlockIndex* pindexPrevNew = pindexBest;
nStart = GetTime();
// Create new block Создание нового блока
if(pblocktemplate)
{
delete pblocktemplate;
pblocktemplate = NULL;
}
pblocktemplate = CreateNewBlock(*pMiningKey);
if (!pblocktemplate)
throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");
// Need to update only after we know CreateNewBlock succeeded Необходимое обновление только после того, как мы узнаем, что CreateNewBlock удалось
pindexPrev = pindexPrevNew;
}
CBlock* pblock = &pblocktemplate->block; // pointer for convenience Указатель для удобства
// Update nTime Обновление nTime
UpdateTime(*pblock, pindexPrev);
pblock->nNonce = 0;
Array transactions;
map<uint256, int64_t> setTxIndex;
int i = 0;
BOOST_FOREACH (CTransaction& tx, pblock->vtx)
{
uint256 txHash = tx.GetHash();
setTxIndex[txHash] = i++;
if (tx.IsCoinBase())
continue;
Object entry;
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
ssTx << tx;
entry.push_back(Pair("data", HexStr(ssTx.begin(), ssTx.end())));
entry.push_back(Pair("hash", txHash.GetHex()));
Array deps;
BOOST_FOREACH (const CTxIn &in, tx.vin)
{
if (setTxIndex.count(in.prevout.hash))
deps.push_back(setTxIndex[in.prevout.hash]);
}
entry.push_back(Pair("depends", deps));
int index_in_template = i - 1;
entry.push_back(Pair("fee", pblocktemplate->vTxFees[index_in_template]));
entry.push_back(Pair("sigops", pblocktemplate->vTxSigOps[index_in_template]));
transactions.push_back(entry);
}
Object aux;
aux.push_back(Pair("flags", HexStr(COINBASE_FLAGS.begin(), COINBASE_FLAGS.end())));
uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
static Array aMutable;
if (aMutable.empty())
{
aMutable.push_back("time");
aMutable.push_back("transactions");
aMutable.push_back("prevblock");
}
Object result;
result.push_back(Pair("version", pblock->nVersion));
result.push_back(Pair("previousblockhash", pblock->hashPrevBlock.GetHex()));
result.push_back(Pair("transactions", transactions));
result.push_back(Pair("coinbaseaux", aux));
result.push_back(Pair("coinbasevalue", (int64_t)pblock->vtx[0].vout[0].nValue));
result.push_back(Pair("target", hashTarget.GetHex()));
result.push_back(Pair("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1));
result.push_back(Pair("mutable", aMutable));
result.push_back(Pair("noncerange", "00000000ffffffff"));
result.push_back(Pair("sigoplimit", (int64_t)MAX_BLOCK_SIGOPS));
result.push_back(Pair("sizelimit", (int64_t)MAX_BLOCK_SIZE));
result.push_back(Pair("curtime", (int64_t)pblock->nTime));
result.push_back(Pair("bits", HexBits(pblock->nBits)));
result.push_back(Pair("height", (int64_t)(pindexPrev->nHeight+1)));
Array FeeBack;
for (unsigned int i = 0; pblocktemplate->vBackWhither.size() > i; i++)
{
Object entry;
CDataStream TxOut(SER_NETWORK, PROTOCOL_VERSION);
TxOut << pblocktemplate->vBackWhither[i];
entry.push_back(Pair("BackWhither", HexStr(TxOut.begin(), TxOut.end())));
FeeBack.push_back(entry);
}
result.push_back(Pair("FeeBack", FeeBack));
return result;
}
Value submitblock(const Array& params, bool fHelp)
{
if (fHelp || params.size() < 1 || params.size() > 2)
throw runtime_error(
"submitblock <hex data> [optional-params-obj]\n"
"[optional-params-obj] parameter is currently ignored.\n"
"Attempts to submit new block to network.\n"
"See https://en.bitcoin.it/wiki/BIP_0022 for full specification.");
vector<unsigned char> blockData(ParseHex(params[0].get_str()));
CDataStream ssBlock(blockData, SER_NETWORK, PROTOCOL_VERSION);
CBlock pblock;
try {
ssBlock >> pblock;
}
catch (std::exception &e) {
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");
}
CValidationState state;
bool fAccepted = ProcessBlock(state, NULL, &pblock);
if (!fAccepted)
return "rejected"; // TODO: report validation state
return Value::null;
}