-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgetwork-latest.patch
645 lines (615 loc) · 23.3 KB
/
getwork-latest.patch
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
Index: main.cpp
===================================================================
--- main.cpp (revision 186)
+++ main.cpp (working copy)
@@ -3054,8 +3054,233 @@
}
};
+CBlock* pWorkBlock = new CBlock();
+CReserveKey workKey;
+unsigned int nExtraNonce = 0;
+int64 nPrevTime = 0;
+int64 nTransactionsUpdatedTime = 0;
+unsigned int nsTransactionsUpdatedLast;
+CCriticalSection work;
+void CheckWork(Workspace& tmp, unsigned int& extraNonce)
+{
+ CRITICAL_BLOCK(work)
+ CRITICAL_BLOCK(cs_main)
+ {
+ if (pWorkBlock->hashPrevBlock != pindexBest->GetBlockHash())
+ return;
+ for (int i = 0; i < sizeof(tmp)/4; i++)
+ ((unsigned int*)&tmp)[i] = ByteReverse(((unsigned int*)&tmp)[i]);
+ if (tmp.block.hashPrevBlock == 0 || tmp.block.hashPrevBlock != pWorkBlock->hashPrevBlock)
+ return;
+ CBlock* pBlock = new CBlock(); // released in ProcessBlock
+ *pBlock = *pWorkBlock;
+ pBlock->nTime = tmp.block.nTime;
+ pBlock->nNonce = tmp.block.nNonce;
+ pBlock->vtx[0].vin[0].scriptSig.clear();
+ pBlock->vtx[0].vin[0].scriptSig << GetNextWorkRequired(pindexBest) << CBigNum(extraNonce);
+ pBlock->hashMerkleRoot = pBlock->BuildMerkleTree();
+ uint256 hash = pBlock->GetHash();
+ uint256 hashTarget = CBigNum().SetCompact(pWorkBlock->nBits).getuint256();
+ if (hash > hashTarget)
+ return;
+ //// debug print
+ printf("proof-of-work found \n hash: %s \ntarget: %s\n", hash.GetHex().c_str(), hashTarget.GetHex().c_str());
+ pBlock->print();
+ printf("%s ", DateTimeStrFormat("%x %H:%M", GetTime()).c_str());
+ printf("generated %s\n", FormatMoney(pBlock->vtx[0].vout[0].nValue).c_str());
+ workKey.KeepKey();
+ CRITICAL_BLOCK(cs_mapRequestCount)
+ mapRequestCount[pWorkBlock->GetHash()] = 0;
+ if (!ProcessBlock(NULL, pBlock))
+ {
+ printf("ERROR in CheckWork, ProcessBlock, block not accepted\n");
+ }
+ }
+}
+void PrepareWork(Workspace& workspace, uint256& state, uint256& target, unsigned int& extraNonce)
+{
+ CRITICAL_BLOCK(work)
+ {
+ CBlockIndex* pIndexPrev = pindexBest;
+ if ((nTransactionsUpdated != nsTransactionsUpdatedLast && GetTime() - nTransactionsUpdatedTime > 60)
+ || pWorkBlock->hashPrevBlock != pIndexPrev->GetBlockHash())
+ {
+ nTransactionsUpdatedTime = GetTime();
+ nsTransactionsUpdatedLast = nTransactionsUpdated;
+
+ pWorkBlock->vtx.clear();
+ CTransaction txNew;
+ txNew.vin.resize(1);
+ txNew.vin[0].prevout.SetNull();
+ txNew.vout.resize(1);
+ txNew.vout[0].scriptPubKey << workKey.GetReservedKey() << OP_CHECKSIG;
+ pWorkBlock->vtx.push_back(txNew);
+
+ // Collect memory pool transactions into the block
+ int64 nFees = 0;
+ CRITICAL_BLOCK(cs_main)
+ CRITICAL_BLOCK(cs_mapTransactions)
+ {
+ CTxDB txdb("r");
+
+ // Priority order to process transactions
+ list<COrphan> vOrphan; // list memory doesn't move
+ map<uint256, vector<COrphan*> > mapDependers;
+ multimap<double, CTransaction*> mapPriority;
+ for (map<uint256, CTransaction>::iterator mi = mapTransactions.begin(); mi != mapTransactions.end(); ++mi)
+ {
+ CTransaction& tx = (*mi).second;
+ if (tx.IsCoinBase() || !tx.IsFinal())
+ continue;
+
+ COrphan* porphan = NULL;
+ double dPriority = 0;
+ foreach(const CTxIn& txin, tx.vin)
+ {
+ // Read prev transaction
+ CTransaction txPrev;
+ CTxIndex txindex;
+ if (!txPrev.ReadFromDisk(txdb, txin.prevout, txindex))
+ {
+ // Has to wait for dependencies
+ if (!porphan)
+ {
+ // Use list for automatic deletion
+ vOrphan.push_back(COrphan(&tx));
+ porphan = &vOrphan.back();
+ }
+ mapDependers[txin.prevout.hash].push_back(porphan);
+ porphan->setDependsOn.insert(txin.prevout.hash);
+ continue;
+ }
+ int64 nValueIn = txPrev.vout[txin.prevout.n].nValue;
+
+ // Read block header
+ int nConf = 0;
+ CBlock block;
+ if (block.ReadFromDisk(txindex.pos.nFile, txindex.pos.nBlockPos, false))
+ {
+ map<uint256, CBlockIndex*>::iterator it = mapBlockIndex.find(block.GetHash());
+ if (it != mapBlockIndex.end())
+ {
+ CBlockIndex* pindex = (*it).second;
+ if (pindex->IsInMainChain())
+ nConf = 1 + nBestHeight - pindex->nHeight;
+ }
+ }
+
+ dPriority += (double)nValueIn * nConf;
+
+ if (fDebug && mapArgs.count("-printpriority"))
+ printf("priority nValueIn=%-12I64d nConf=%-5d dPriority=%-20.1f\n", nValueIn, nConf, dPriority);
+ }
+
+ // Priority is sum(valuein * age) / txsize
+ dPriority /= ::GetSerializeSize(tx, SER_NETWORK);
+
+ if (porphan)
+ porphan->dPriority = dPriority;
+ else
+ mapPriority.insert(make_pair(-dPriority, &(*mi).second));
+
+ if (fDebug && mapArgs.count("-printpriority"))
+ {
+ printf("priority %-20.1f %s\n%s", dPriority, tx.GetHash().ToString().substr(0,10).c_str(), tx.ToString().c_str());
+ if (porphan)
+ porphan->print();
+ printf("\n");
+ }
+ }
+
+ // Collect transactions into block
+ map<uint256, CTxIndex> mapTestPool;
+ uint64 nBlockSize = 1000;
+ int nBlockSigOps = 100;
+ while (!mapPriority.empty())
+ {
+ // Take highest priority transaction off priority queue
+ double dPriority = -(*mapPriority.begin()).first;
+ CTransaction& tx = *(*mapPriority.begin()).second;
+ mapPriority.erase(mapPriority.begin());
+
+ // Size limits
+ unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK);
+ if (nBlockSize + nTxSize >= MAX_BLOCK_SIZE_GEN)
+ continue;
+ int nTxSigOps = tx.GetSigOpCount();
+ if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
+ continue;
+
+ // Transaction fee required depends on block size
+ bool fAllowFree = (nBlockSize + nTxSize < 4000 || dPriority > COIN * 144 / 250);
+ int64 nMinFee = tx.GetMinFee(nBlockSize, fAllowFree);
+
+ // Connecting shouldn't fail due to dependency on other memory pool transactions
+ // because we're already processing them in order of dependency
+ map<uint256, CTxIndex> mapTestPoolTmp(mapTestPool);
+ if (!tx.ConnectInputs(txdb, mapTestPoolTmp, CDiskTxPos(1,1,1), pIndexPrev, nFees, false, true, nMinFee))
+ continue;
+ swap(mapTestPool, mapTestPoolTmp);
+
+ // Added
+ pWorkBlock->vtx.push_back(tx);
+ nBlockSize += nTxSize;
+ nBlockSigOps += nTxSigOps;
+
+ // Add transactions that depend on this one to the priority queue
+ uint256 hash = tx.GetHash();
+ if (mapDependers.count(hash))
+ {
+ foreach(COrphan* porphan, mapDependers[hash])
+ {
+ if (!porphan->setDependsOn.empty())
+ {
+ porphan->setDependsOn.erase(hash);
+ if (porphan->setDependsOn.empty())
+ mapPriority.insert(make_pair(-porphan->dPriority, porphan->ptx));
+ }
+ }
+ }
+ }
+ }
+ pWorkBlock->vtx[0].vout[0].nValue = GetBlockValue(pIndexPrev->nHeight+1, nFees);
+
+ pWorkBlock->hashPrevBlock = pIndexPrev->GetBlockHash();
+ pWorkBlock->nBits = GetNextWorkRequired(pIndexPrev);
+ }
+
+ int64 nNow = max(pIndexPrev->GetMedianTimePast()+1, GetAdjustedTime());
+ if (++nExtraNonce >= 0x7f && nNow > nPrevTime+1)
+ {
+ nExtraNonce = 1;
+ nPrevTime = nNow;
+ }
+
+ pWorkBlock->vtx[0].vin[0].scriptSig.clear();
+ pWorkBlock->vtx[0].vin[0].scriptSig << pWorkBlock->nBits << CBigNum(nExtraNonce);
+ extraNonce = nExtraNonce;
+
+ workspace.block.nVersion = pWorkBlock->nVersion;
+ workspace.block.hashPrevBlock = pWorkBlock->hashPrevBlock;
+ workspace.block.hashMerkleRoot = pWorkBlock->hashMerkleRoot = pWorkBlock->BuildMerkleTree();
+ workspace.block.nBits = pWorkBlock->nBits;
+ workspace.block.nNonce = 0;
+
+ workspace.block.nTime = pWorkBlock->nTime = nNow;
+ unsigned int nBlocks0 = FormatHashBlocks(&workspace.block, sizeof(workspace.block));
+ unsigned int nBlocks1 = FormatHashBlocks(&workspace.hash1, sizeof(workspace.hash1));
+
+ target = CBigNum().SetCompact(pWorkBlock->nBits).getuint256();
+
+ for (int i = 0; i < sizeof(workspace)/4; i++)
+ ((unsigned int*)&workspace)[i] = ByteReverse(((unsigned int*)&workspace)[i]);
+
+ SHA256Transform(&state, &workspace.block, pSHA256InitState);
+ }
+}
+
void BitcoinMiner()
{
printf("BitcoinMiner started\n");
@@ -3064,9 +3289,12 @@
if (mapArgs.count("-4way"))
f4WaySSE2 = (mapArgs["-4way"] != "0");
- CReserveKey reservekey;
unsigned int nExtraNonce = 0;
- int64 nPrevTime = 0;
+ uint256 midstatebuf2[2];
+ uint256& midstate = *alignup<16>(midstatebuf2);
+ char tmpbuf2[sizeof(Workspace)+16];
+ Workspace& tmp = *(Workspace*)alignup<16>(tmpbuf2);
+ uint256 hashTarget;
while (fGenerateBitcoins)
{
if (AffinityBugWorkaround(ThreadBitcoinMiner))
@@ -3084,215 +3312,15 @@
unsigned int nTransactionsUpdatedLast = nTransactionsUpdated;
CBlockIndex* pindexPrev = pindexBest;
- unsigned int nBits = GetNextWorkRequired(pindexPrev);
+ PrepareWork(tmp, midstate, hashTarget, nExtraNonce);
- //
- // Create coinbase tx
- //
- CTransaction txNew;
- txNew.vin.resize(1);
- txNew.vin[0].prevout.SetNull();
- int64 nNow = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
- if (++nExtraNonce >= 0x7f && nNow > nPrevTime+1)
- {
- nExtraNonce = 1;
- nPrevTime = nNow;
- }
- txNew.vin[0].scriptSig << nBits << CBigNum(nExtraNonce);
- txNew.vout.resize(1);
- txNew.vout[0].scriptPubKey << reservekey.GetReservedKey() << OP_CHECKSIG;
+ printf("Running BitcoinMiner with %d transactions in block\n", pWorkBlock->vtx.size());
-
//
- // Create new block
- //
- auto_ptr<CBlock> pblock(new CBlock());
- if (!pblock.get())
- return;
-
- // Add our coinbase tx as first transaction
- pblock->vtx.push_back(txNew);
-
- // Collect memory pool transactions into the block
- int64 nFees = 0;
- CRITICAL_BLOCK(cs_main)
- CRITICAL_BLOCK(cs_mapTransactions)
- {
- CTxDB txdb("r");
-
- // Priority order to process transactions
- list<COrphan> vOrphan; // list memory doesn't move
- map<uint256, vector<COrphan*> > mapDependers;
- multimap<double, CTransaction*> mapPriority;
- for (map<uint256, CTransaction>::iterator mi = mapTransactions.begin(); mi != mapTransactions.end(); ++mi)
- {
- CTransaction& tx = (*mi).second;
- if (tx.IsCoinBase() || !tx.IsFinal())
- continue;
-
- COrphan* porphan = NULL;
- double dPriority = 0;
- foreach(const CTxIn& txin, tx.vin)
- {
- // Read prev transaction
- CTransaction txPrev;
- CTxIndex txindex;
- if (!txPrev.ReadFromDisk(txdb, txin.prevout, txindex))
- {
- // Has to wait for dependencies
- if (!porphan)
- {
- // Use list for automatic deletion
- vOrphan.push_back(COrphan(&tx));
- porphan = &vOrphan.back();
- }
- mapDependers[txin.prevout.hash].push_back(porphan);
- porphan->setDependsOn.insert(txin.prevout.hash);
- continue;
- }
- int64 nValueIn = txPrev.vout[txin.prevout.n].nValue;
-
- // Read block header
- int nConf = 0;
- CBlock block;
- if (block.ReadFromDisk(txindex.pos.nFile, txindex.pos.nBlockPos, false))
- {
- map<uint256, CBlockIndex*>::iterator it = mapBlockIndex.find(block.GetHash());
- if (it != mapBlockIndex.end())
- {
- CBlockIndex* pindex = (*it).second;
- if (pindex->IsInMainChain())
- nConf = 1 + nBestHeight - pindex->nHeight;
- }
- }
-
- dPriority += (double)nValueIn * nConf;
-
- if (fDebug && mapArgs.count("-printpriority"))
- printf("priority nValueIn=%-12I64d nConf=%-5d dPriority=%-20.1f\n", nValueIn, nConf, dPriority);
- }
-
- // Priority is sum(valuein * age) / txsize
- dPriority /= ::GetSerializeSize(tx, SER_NETWORK);
-
- if (porphan)
- porphan->dPriority = dPriority;
- else
- mapPriority.insert(make_pair(-dPriority, &(*mi).second));
-
- if (fDebug && mapArgs.count("-printpriority"))
- {
- printf("priority %-20.1f %s\n%s", dPriority, tx.GetHash().ToString().substr(0,10).c_str(), tx.ToString().c_str());
- if (porphan)
- porphan->print();
- printf("\n");
- }
- }
-
- // Collect transactions into block
- map<uint256, CTxIndex> mapTestPool;
- uint64 nBlockSize = 1000;
- int nBlockSigOps = 100;
- while (!mapPriority.empty())
- {
- // Take highest priority transaction off priority queue
- double dPriority = -(*mapPriority.begin()).first;
- CTransaction& tx = *(*mapPriority.begin()).second;
- mapPriority.erase(mapPriority.begin());
-
- // Size limits
- unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK);
- if (nBlockSize + nTxSize >= MAX_BLOCK_SIZE_GEN)
- continue;
- int nTxSigOps = tx.GetSigOpCount();
- if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
- continue;
-
- // Transaction fee required depends on block size
- bool fAllowFree = (nBlockSize + nTxSize < 4000 || dPriority > COIN * 144 / 250);
- int64 nMinFee = tx.GetMinFee(nBlockSize, fAllowFree);
-
- // Connecting shouldn't fail due to dependency on other memory pool transactions
- // because we're already processing them in order of dependency
- map<uint256, CTxIndex> mapTestPoolTmp(mapTestPool);
- if (!tx.ConnectInputs(txdb, mapTestPoolTmp, CDiskTxPos(1,1,1), pindexPrev, nFees, false, true, nMinFee))
- continue;
- swap(mapTestPool, mapTestPoolTmp);
-
- // Added
- pblock->vtx.push_back(tx);
- nBlockSize += nTxSize;
- nBlockSigOps += nTxSigOps;
-
- // Add transactions that depend on this one to the priority queue
- uint256 hash = tx.GetHash();
- if (mapDependers.count(hash))
- {
- foreach(COrphan* porphan, mapDependers[hash])
- {
- if (!porphan->setDependsOn.empty())
- {
- porphan->setDependsOn.erase(hash);
- if (porphan->setDependsOn.empty())
- mapPriority.insert(make_pair(-porphan->dPriority, porphan->ptx));
- }
- }
- }
- }
- }
- pblock->nBits = nBits;
- pblock->vtx[0].vout[0].nValue = GetBlockValue(pindexPrev->nHeight+1, nFees);
- printf("Running BitcoinMiner with %d transactions in block\n", pblock->vtx.size());
-
-
- //
- // Prebuild hash buffer
- //
- struct tmpworkspace
- {
- struct unnamed2
- {
- int nVersion;
- uint256 hashPrevBlock;
- uint256 hashMerkleRoot;
- unsigned int nTime;
- unsigned int nBits;
- unsigned int nNonce;
- }
- block;
- unsigned char pchPadding0[64];
- uint256 hash1;
- unsigned char pchPadding1[64];
- };
- char tmpbuf[sizeof(tmpworkspace)+16];
- tmpworkspace& tmp = *(tmpworkspace*)alignup<16>(tmpbuf);
-
- tmp.block.nVersion = pblock->nVersion;
- tmp.block.hashPrevBlock = pblock->hashPrevBlock = pindexPrev->GetBlockHash();
- tmp.block.hashMerkleRoot = pblock->hashMerkleRoot = pblock->BuildMerkleTree();
- tmp.block.nTime = pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
- tmp.block.nBits = pblock->nBits = nBits;
- tmp.block.nNonce = pblock->nNonce = 0;
-
- unsigned int nBlocks0 = FormatHashBlocks(&tmp.block, sizeof(tmp.block));
- unsigned int nBlocks1 = FormatHashBlocks(&tmp.hash1, sizeof(tmp.hash1));
-
- // Byte swap all the input buffer
- for (int i = 0; i < sizeof(tmp)/4; i++)
- ((unsigned int*)&tmp)[i] = ByteReverse(((unsigned int*)&tmp)[i]);
-
- // Precalc the first half of the first hash, which stays constant
- uint256 midstatebuf[2];
- uint256& midstate = *alignup<16>(midstatebuf);
- SHA256Transform(&midstate, &tmp.block, pSHA256InitState);
-
-
- //
// Search
//
int64 nStart = GetTime();
- uint256 hashTarget = CBigNum().SetCompact(pblock->nBits).getuint256();
uint256 hashbuf[2];
uint256& hash = *alignup<16>(hashbuf);
loop
@@ -3318,35 +3346,13 @@
if (hash <= hashTarget)
{
// Found a solution
- pblock->nNonce = ByteReverse(nNonceFound);
- assert(hash == pblock->GetHash());
+ //// debug print
+ printf("BitcoinMiner: \n");
+ tmp.block.nNonce = nNonceFound;
+ SetThreadPriority(THREAD_PRIORITY_NORMAL);
+ CheckWork(tmp, nExtraNonce);
+ SetThreadPriority(THREAD_PRIORITY_LOWEST);
- //// debug print
- printf("BitcoinMiner:\n");
- printf("proof-of-work found \n hash: %s \ntarget: %s\n", hash.GetHex().c_str(), hashTarget.GetHex().c_str());
- pblock->print();
- printf("%s ", DateTimeStrFormat("%x %H:%M", GetTime()).c_str());
- printf("generated %s\n", FormatMoney(pblock->vtx[0].vout[0].nValue).c_str());
-
- SetThreadPriority(THREAD_PRIORITY_NORMAL);
- CRITICAL_BLOCK(cs_main)
- {
- if (pindexPrev == pindexBest)
- {
- // Remove key from key pool
- reservekey.KeepKey();
-
- // Track how many getdata requests this block gets
- CRITICAL_BLOCK(cs_mapRequestCount)
- mapRequestCount[pblock->GetHash()] = 0;
-
- // Process this block the same as if we had received it from another node
- if (!ProcessBlock(NULL, pblock.release()))
- printf("ERROR in BitcoinMiner, ProcessBlock, block not accepted\n");
- }
- }
- SetThreadPriority(THREAD_PRIORITY_LOWEST);
-
Sleep(500);
break;
}
@@ -3401,8 +3407,7 @@
break;
// Update nTime every few seconds
- pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
- tmp.block.nTime = ByteReverse(pblock->nTime);
+ tmp.block.nTime = ByteReverse((unsigned int)max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()));
}
}
}
Index: main.h
===================================================================
--- main.h (revision 186)
+++ main.h (working copy)
@@ -60,6 +60,22 @@
+struct Workspace
+{
+ struct unnamed2
+ {
+ int nVersion;
+ uint256 hashPrevBlock;
+ uint256 hashMerkleRoot;
+ unsigned int nTime;
+ unsigned int nBits;
+ unsigned int nNonce;
+ }
+ block;
+ unsigned char pchPadding0[64];
+ uint256 hash1;
+ unsigned char pchPadding1[64];
+};
bool CheckDiskSpace(uint64 nAdditionalBytes=0);
@@ -87,6 +103,8 @@
bool CheckProofOfWork(uint256 hash, unsigned int nBits);
bool IsInitialBlockDownload();
string GetWarnings(string strFor);
+void CheckWork(Workspace& workspace, unsigned int& extraNonce);
+void PrepareWork(Workspace& workspace, uint256& state, uint256& target, unsigned int& extraNonce);
Index: rpc.cpp
===================================================================
--- rpc.cpp (revision 186)
+++ rpc.cpp (working copy)
@@ -681,14 +681,73 @@
return ret;
}
+Value getwork(const Array& params, bool fHelp)
+{
+ static const short BLOCK_SIZE = 128;
+ static const short HEX_BLOCK_SIZE = BLOCK_SIZE * 2;
+ Workspace workspace;
+ uint256 state;
+ uint256 target;
+ unsigned int extraNonce = 0;
+ if (fHelp)
+ throw runtime_error(
+ "getwork\n"
+ "Input:\n"
+ "int extraNonce, String (Hex) block (256 characters)\n"
+ "If parameters are present will try to solve latest block with them\n"
+ "\n"
+ "Output:\n"
+ "String (Hex) block - block data;\n"
+ "String (Hex) state - already computed hash of block's first half;\n"
+ "String (Hex) target - little endian hash target;\n"
+ "int extraNonce - changed every time to guarantee different hash space;\n"
+ "\n"
+ "Call frequently (at ~10 seconds) to avoid searching for solution of a stale block");
+ if (vNodes.empty())
+ throw runtime_error("Bitcoin is not connected!");
+ if (IsInitialBlockDownload())
+ throw runtime_error("Bitcoin is downloading blocks...");
+ if (params.size() >= 2)
+ {
+ extraNonce = params[0].get_int();
+ string hexBlock = params[1].get_str();
+ if (hexBlock.length() == HEX_BLOCK_SIZE)
+ {
+ vector<unsigned char> blockData = ParseHex(hexBlock.c_str());
+ if (blockData.size() == BLOCK_SIZE)
+ {
+ for (int i = 0; i < BLOCK_SIZE; i++)
+ ((unsigned char*)&workspace)[i] = blockData.at(i);
+ CheckWork(workspace, extraNonce);
+ }
+ }
+ }
+ PrepareWork(workspace, state, target, extraNonce);
+
+ char workspaceHex[HEX_BLOCK_SIZE + 1];
+ for (int i = 0; i < BLOCK_SIZE; i++)
+ sprintf(workspaceHex + (i << 1), "%02x", ((unsigned char*)&workspace)[i]);
+ char stateHex[64 + 1];
+ for (int i = 0; i < 32; i++)
+ sprintf(stateHex + (i << 1), "%02x", ((unsigned char*)&(state))[i]);
+ char targetHex[64 + 1];
+ for (int i = 0; i < 32; i++)
+ sprintf(targetHex + (i << 1), "%02x", ((unsigned char*)&(target))[i]);
+ Object result;
+ result.push_back(Pair("block", string(workspaceHex, workspaceHex + HEX_BLOCK_SIZE)));
+ result.push_back(Pair("state", string(stateHex, stateHex + 64)));
+ result.push_back(Pair("target", string(targetHex, targetHex + 64)));
+ result.push_back(Pair("extraNonce", (int)extraNonce));
+ return result;
+}
//
@@ -721,6 +780,7 @@
make_pair("listreceivedbylabel", &listreceivedbylabel),
make_pair("backupwallet", &backupwallet),
make_pair("validateaddress", &validateaddress),
+ make_pair("getwork", &getwork),
};
map<string, rpcfn_type> mapCallTable(pCallTable, pCallTable + sizeof(pCallTable)/sizeof(pCallTable[0]));