7 #include "kernel_worker.h"
11 KernelWorker::KernelWorker(unsigned char *kernel, uint32_t nBits, uint32_t nInputTxTime, int64_t nValueIn, uint32_t nIntervalBegin, uint32_t nIntervalEnd)
12 : kernel(kernel), nBits(nBits), nInputTxTime(nInputTxTime), bnValueIn(nValueIn), nIntervalBegin(nIntervalBegin), nIntervalEnd(nIntervalEnd)
14 solutions = vector<std::pair<uint256,uint32_t> >();
17 void KernelWorker::Do_generic()
19 SetThreadPriority(THREAD_PRIORITY_LOWEST);
21 // Compute maximum possible target to filter out majority of obviously insufficient hashes
22 CBigNum bnTargetPerCoinDay;
23 bnTargetPerCoinDay.SetCompact(nBits);
24 uint256 nMaxTarget = (bnTargetPerCoinDay * bnValueIn * nStakeMaxAge / COIN / nOneDay).getuint256();
26 SHA256_CTX ctx, workerCtx;
27 // Init new sha256 context and update it
28 // with first 24 bytes of kernel
30 SHA256_Update(&ctx, kernel, 8 + 16);
31 workerCtx = ctx; // save context
33 // Sha256 result buffer
34 uint32_t hashProofOfStake[8];
35 uint256 *pnHashProofOfStake = (uint256 *)&hashProofOfStake;
37 // Search forward in time from the given timestamp
38 // Stopping search in case of shutting down
39 for (uint32_t nTimeTx=nIntervalBegin, nMaxTarget32 = nMaxTarget.Get32(7); nTimeTx<nIntervalEnd && !fShutdown; nTimeTx++)
41 // Complete first hashing iteration
43 SHA256_Update(&ctx, (unsigned char*)&nTimeTx, 4);
44 SHA256_Final((unsigned char*)&hash1, &ctx);
49 // Finally, calculate kernel hash
50 SHA256((unsigned char*)&hash1, sizeof(hashProofOfStake), (unsigned char*)&hashProofOfStake);
52 // Skip if hash doesn't satisfy the maximum target
53 if (hashProofOfStake[7] > nMaxTarget32)
56 CBigNum bnCoinDayWeight = bnValueIn * GetWeight((int64_t)nInputTxTime, (int64_t)nTimeTx) / COIN / nOneDay;
57 CBigNum bnTargetProofOfStake = bnCoinDayWeight * bnTargetPerCoinDay;
59 if (bnTargetProofOfStake >= CBigNum(*pnHashProofOfStake))
60 solutions.push_back(std::pair<uint256,uint32_t>(*pnHashProofOfStake, nTimeTx));
64 void KernelWorker::Do()
69 vector<pair<uint256,uint32_t> >& KernelWorker::GetSolutions()
74 // Scan given kernel for solutions
76 bool ScanKernelBackward(unsigned char *kernel, uint32_t nBits, uint32_t nInputTxTime, int64_t nValueIn, std::pair<uint32_t, uint32_t> &SearchInterval, std::pair<uint256, uint32_t> &solution)
78 CBigNum bnTargetPerCoinDay;
79 bnTargetPerCoinDay.SetCompact(nBits);
81 CBigNum bnValueIn(nValueIn);
83 // Get maximum possible target to filter out the majority of obviously insufficient hashes
84 uint256 nMaxTarget = (bnTargetPerCoinDay * bnValueIn * nStakeMaxAge / COIN / nOneDay).getuint256();
86 SHA256_CTX ctx, workerCtx;
87 // Init new sha256 context and update it
88 // with first 24 bytes of kernel
90 SHA256_Update(&ctx, kernel, 8 + 16);
91 workerCtx = ctx; // save context
93 // Search backward in time from the given timestamp
94 // Stopping search in case of shutting down
95 for (uint32_t nTimeTx=SearchInterval.first; nTimeTx>SearchInterval.second && !fShutdown; nTimeTx--)
97 // Complete first hashing iteration
99 SHA256_Update(&ctx, (unsigned char*)&nTimeTx, 4);
100 SHA256_Final((unsigned char*)&hash1, &ctx);
105 // Finally, calculate kernel hash
106 uint256 hashProofOfStake;
107 SHA256((unsigned char*)&hash1, sizeof(hashProofOfStake), (unsigned char*)&hashProofOfStake);
109 // Skip if hash doesn't satisfy the maximum target
110 if (hashProofOfStake > nMaxTarget)
113 CBigNum bnCoinDayWeight = bnValueIn * GetWeight((int64_t)nInputTxTime, (int64_t)nTimeTx) / COIN / nOneDay;
114 CBigNum bnTargetProofOfStake = bnCoinDayWeight * bnTargetPerCoinDay;
116 if (bnTargetProofOfStake >= CBigNum(hashProofOfStake))
118 solution.first = hashProofOfStake;
119 solution.second = nTimeTx;