7 #include "kernel_worker.h"
9 #include <openssl/sha.h>
13 KernelWorker::KernelWorker(uint8_t *kernel, uint32_t nBits, uint32_t nInputTxTime, int64_t nValueIn, uint32_t nIntervalBegin, uint32_t nIntervalEnd)
14 : kernel(kernel), nBits(nBits), nInputTxTime(nInputTxTime), bnValueIn(nValueIn), nIntervalBegin(nIntervalBegin), nIntervalEnd(nIntervalEnd)
16 solutions = vector<pair<uint256,uint32_t> >();
19 void KernelWorker::Do_generic()
21 SetThreadPriority(THREAD_PRIORITY_LOWEST);
23 // Compute maximum possible target to filter out majority of obviously insufficient hashes
24 CBigNum bnTargetPerCoinDay;
25 bnTargetPerCoinDay.SetCompact(nBits);
26 auto nMaxTarget = (bnTargetPerCoinDay * bnValueIn * nStakeMaxAge / COIN / nOneDay).getuint256();
28 SHA256_CTX ctx, workerCtx;
29 // Init new sha256 context and update it
30 // with first 24 bytes of kernel
32 SHA256_Update(&ctx, kernel, 8 + 16);
33 workerCtx = ctx; // save context
35 // Sha256 result buffer
36 uint32_t hashProofOfStake[8];
37 auto pnHashProofOfStake = (uint256 *)&hashProofOfStake;
39 // Search forward in time from the given timestamp
40 // Stopping search in case of shutting down
41 for (auto nTimeTx=nIntervalBegin, nMaxTarget32 = nMaxTarget.Get32(7); nTimeTx<nIntervalEnd && !fShutdown; nTimeTx++)
43 // Complete first hashing iteration
45 SHA256_Update(&ctx, (unsigned char*)&nTimeTx, 4);
46 SHA256_Final(hash1.begin(), &ctx);
51 // Finally, calculate kernel hash
52 SHA256(hash1.begin(), sizeof(hashProofOfStake), (unsigned char*)&hashProofOfStake);
54 // Skip if hash doesn't satisfy the maximum target
55 if (hashProofOfStake[7] > nMaxTarget32)
58 auto bnCoinDayWeight = bnValueIn * GetWeight((int64_t)nInputTxTime, (int64_t)nTimeTx) / COIN / nOneDay;
59 auto bnTargetProofOfStake = bnCoinDayWeight * bnTargetPerCoinDay;
61 if (bnTargetProofOfStake >= CBigNum(*pnHashProofOfStake))
62 solutions.push_back(make_pair(*pnHashProofOfStake, nTimeTx));
66 void KernelWorker::Do()
71 vector<pair<uint256,uint32_t> >& KernelWorker::GetSolutions()
76 // Scan given kernel for solutions
78 bool ScanKernelBackward(uint8_t *kernel, uint32_t nBits, uint32_t nInputTxTime, int64_t nValueIn, pair<uint32_t, uint32_t> &SearchInterval, pair<uint256, uint32_t> &solution)
80 CBigNum bnTargetPerCoinDay;
81 bnTargetPerCoinDay.SetCompact(nBits);
83 CBigNum bnValueIn(nValueIn);
85 // Get maximum possible target to filter out the majority of obviously insufficient hashes
86 auto nMaxTarget = (bnTargetPerCoinDay * bnValueIn * nStakeMaxAge / COIN / nOneDay).getuint256();
88 SHA256_CTX ctx, workerCtx;
89 // Init new sha256 context and update it
90 // with first 24 bytes of kernel
92 SHA256_Update(&ctx, kernel, 8 + 16);
93 workerCtx = ctx; // save context
95 // Search backward in time from the given timestamp
96 // Stopping search in case of shutting down
97 for (auto nTimeTx=SearchInterval.first; nTimeTx>SearchInterval.second && !fShutdown; nTimeTx--)
99 // Complete first hashing iteration
101 SHA256_Update(&ctx, (unsigned char*)&nTimeTx, 4);
102 SHA256_Final(hash1.begin(), &ctx);
107 // Finally, calculate kernel hash
108 uint256 hashProofOfStake;
109 SHA256(hash1.begin(), hashProofOfStake.size(), hashProofOfStake.begin());
111 // Skip if hash doesn't satisfy the maximum target
112 if (hashProofOfStake > nMaxTarget)
115 auto bnCoinDayWeight = bnValueIn * GetWeight((int64_t)nInputTxTime, (int64_t)nTimeTx) / COIN / nOneDay;
116 auto bnTargetProofOfStake = bnCoinDayWeight * bnTargetPerCoinDay;
118 if (bnTargetProofOfStake >= CBigNum(hashProofOfStake))
120 solution = { hashProofOfStake, nTimeTx };