SHA256_Update(&ctx, (unsigned char*)&it[0], 8 + 16);
}
+
+class ScanMidstateWorker
+{
+public:
+ ScanMidstateWorker()
+ { }
+ ScanMidstateWorker(SHA256_CTX ctx, uint32_t nBits, uint32_t nInputTxTime, int64_t nValueIn, std::pair<uint32_t, uint32_t> &SearchInterval)
+ {
+ workerSolutions = vector<std::pair<uint256,uint32_t> >();
+
+ workerCtx = ctx;
+ this->nBits = nBits;
+ this->nInputTxTime = nInputTxTime;
+ this->nValueIn = nValueIn;
+ this->SearchInterval = SearchInterval;
+ }
+
+ void Do()
+ {
+ CBigNum bnTargetPerCoinDay;
+ bnTargetPerCoinDay.SetCompact(nBits);
+
+ // Get maximum possible target to filter out the majority of obviously insufficient hashes
+ CBigNum bnMaxTargetPerCoinDay = bnTargetPerCoinDay * CBigNum(nValueIn) * nStakeMaxAge / COIN / (24 * 60 * 60);
+ uint256 maxTarget = bnMaxTargetPerCoinDay.getuint256();
+
+ SHA256_CTX ctx = workerCtx;
+
+ // Search forward in time from the given timestamp
+ // Stopping search in case of shutting down
+ for (uint32_t nTimeTx=SearchInterval.first; nTimeTx<SearchInterval.second && !fShutdown; nTimeTx++)
+ {
+ // Complete first hashing iteration
+ uint256 hash1;
+ SHA256_Update(&ctx, (unsigned char*)&nTimeTx, 4);
+ SHA256_Final((unsigned char*)&hash1, &ctx);
+
+ // Restore context
+ ctx = workerCtx;
+
+ // Finally, calculate kernel hash
+ uint256 hashProofOfStake;
+ SHA256((unsigned char*)&hash1, sizeof(hashProofOfStake), (unsigned char*)&hashProofOfStake);
+
+
+ // Skip if hash doesn't satisfy the maximum target
+ if (hashProofOfStake > maxTarget)
+ continue;
+
+ CBigNum bnCoinDayWeight = CBigNum(nValueIn) * GetWeight((int64_t)nInputTxTime, (int64_t)nTimeTx) / COIN / (24 * 60 * 60);
+ CBigNum bnTargetProofOfStake = bnCoinDayWeight * bnTargetPerCoinDay;
+
+ if (bnTargetProofOfStake >= CBigNum(hashProofOfStake))
+ {
+ workerSolutions.push_back(std::pair<uint256,uint32_t>(hashProofOfStake, nTimeTx));
+ }
+ }
+ }
+
+ vector<std::pair<uint256,uint32_t> > GetSolutions()
+ {
+ return workerSolutions;
+ }
+
+private:
+ SHA256_CTX workerCtx;
+ std::vector<std::pair<uint256,uint32_t> > workerSolutions;
+
+ uint32_t nBits;
+ uint32_t nInputTxTime;
+ int64_t nValueIn;
+ std::pair<uint32_t, uint32_t> SearchInterval;
+};
+
// Scan given midstate for solution
-bool ScanMidstateForward(SHA256_CTX &ctx, uint32_t nBits, uint32_t nInputTxTime, int64_t nValueIn, std::pair<uint32_t, uint32_t> &SearchInterval, std::pair<uint256, uint32_t> &solution)
+bool ScanMidstateForward(SHA256_CTX &ctx, uint32_t nBits, uint32_t nInputTxTime, int64_t nValueIn, std::pair<uint32_t, uint32_t> &SearchInterval, std::vector<std::pair<uint256, uint32_t> > &solutions)
{
- CBigNum bnTargetPerCoinDay;
- bnTargetPerCoinDay.SetCompact(nBits);
+ // TODO: custom threads amount
- // Get maximum possible target to filter out the majority of obviously insufficient hashes
- CBigNum bnMaxTargetPerCoinDay = bnTargetPerCoinDay * CBigNum(nValueIn) * nStakeMaxAge / COIN / (24 * 60 * 60);
- uint256 maxTarget = bnMaxTargetPerCoinDay.getuint256();
+ uint32_t nBegin = SearchInterval.first;
+ uint32_t nEnd = SearchInterval.second;
+ uint32_t nPart = (nEnd - nBegin) / 4;
- SHA256_CTX ctxCopy = ctx;
+ ScanMidstateWorker workers[4];
- // Search forward in time from the given timestamp
- // Stopping search in case of shutting down
- for (uint32_t nTimeTx=SearchInterval.first; nTimeTx<SearchInterval.second && !fShutdown; nTimeTx++)
+ boost::thread_group group;
+ for(int i = 0; i<4; i++)
{
- // Complete first hashing iteration
- uint256 hash1;
- SHA256_Update(&ctxCopy, (unsigned char*)&nTimeTx, 4);
- SHA256_Final((unsigned char*)&hash1, &ctxCopy);
+ nBegin += (nPart * i);
+ uint32_t nIntervalEnd = nBegin + nPart * (i + 1);
- // Restore context
- ctxCopy = ctx;
+ std::cout << nBegin << " " << nIntervalEnd << std::endl;
- // Finally, calculate kernel hash
- uint256 hashProofOfStake;
- SHA256((unsigned char*)&hash1, sizeof(hashProofOfStake), (unsigned char*)&hashProofOfStake);
+ std::pair<uint32_t, uint32_t> interval(nBegin, nIntervalEnd);
+ workers[i] = ScanMidstateWorker(ctx, nBits, nInputTxTime, nValueIn, interval);
- // Skip if hash doesn't satisfy the maximum target
- if (hashProofOfStake > maxTarget)
- continue;
+ boost::function<void()> workerFnc = boost::bind(&ScanMidstateWorker::Do, &workers[i]);
+ group.create_thread(workerFnc);
+ }
- CBigNum bnCoinDayWeight = CBigNum(nValueIn) * GetWeight((int64_t)nInputTxTime, (int64_t)nTimeTx) / COIN / (24 * 60 * 60);
- CBigNum bnTargetProofOfStake = bnCoinDayWeight * bnTargetPerCoinDay;
+ group.join_all();
+ solutions.clear();
- if (bnTargetProofOfStake >= CBigNum(hashProofOfStake))
- {
- solution.first = hashProofOfStake;
- solution.second = nTimeTx;
+ for(int i = 0; i<4; i++)
+ {
+ std::vector<std::pair<uint256, uint32_t> > ws = workers[i].GetSolutions();
+ solutions.insert(solutions.end(), ws.begin(), ws.end());
+ }
- return true;
- }
+ if (solutions.size() == 0)
+ {
+ // no solutions
+ return false;
}
- return false;
+ return true;
}
// Scan given midstate for solution
void GetKernelMidstate(uint64_t nStakeModifier, uint32_t nBlockTime, uint32_t nTxOffset, uint32_t nInputTxTime, uint32_t nOut, SHA256_CTX &ctx);
// Scan given midstate for kernel solutions
-bool ScanMidstateForward(SHA256_CTX &ctx, uint32_t nBits, uint32_t nInputTxTime, int64_t nValueIn, std::pair<uint32_t, uint32_t> &SearchInterval, std::pair<uint256, uint32_t> &solution);
+bool ScanMidstateForward(SHA256_CTX &ctx, uint32_t nBits, uint32_t nInputTxTime, int64_t nValueIn, std::pair<uint32_t, uint32_t> &SearchInterval, std::vector<std::pair<uint256, uint32_t> > &solutions);
bool ScanMidstateBackward(SHA256_CTX &ctx, uint32_t nBits, uint32_t nInputTxTime, int64_t nValueIn, std::pair<uint32_t, uint32_t> &SearchInterval, std::pair<uint256, uint32_t> &solution);
// Check kernel hash target and coinstake signature
SHA256_CTX ctx;
GetKernelMidstate(nStakeModifier, block.nTime, txindex.pos.nTxPos - txindex.pos.nBlockPos, tx.nTime, nOut, ctx);
- std::pair<uint256, uint32_t> solution;
- if (ScanMidstateForward(ctx, nBits, tx.nTime, tx.vout[nOut].nValue, interval, solution))
+ std::vector<std::pair<uint256, uint32_t> > solutions;
+ if (ScanMidstateForward(ctx, nBits, tx.nTime, tx.vout[nOut].nValue, interval, solutions))
{
- Object r;
- r.push_back(Pair("hash", solution.first.GetHex()));
- r.push_back(Pair("time", DateTimeStrFormat(solution.second)));
+ Array sols;
- return r;
+ BOOST_FOREACH(const PAIRTYPE(uint256, uint32_t) solution, solutions)
+ {
+
+ Object r;
+ r.push_back(Pair("hash", solution.first.GetHex()));
+ r.push_back(Pair("time", DateTimeStrFormat(solution.second)));
+
+ sols.push_back(r);
+ }
+
+ return sols;
}
}
else