Replace with c+11 feature
[novacoin.git] / src / kernel_worker.cpp
index 1a470ed..f0cac66 100644 (file)
@@ -6,12 +6,14 @@
 #include "kernel.h"
 #include "kernel_worker.h"
 
+#include <openssl/sha.h>
+
 using namespace std;
 
-KernelWorker::KernelWorker(unsigned char *kernel, uint32_t nBits, uint32_t nInputTxTime, int64_t nValueIn, uint32_t nIntervalBegin, uint32_t nIntervalEnd) 
+KernelWorker::KernelWorker(uint8_t *kernel, uint32_t nBits, uint32_t nInputTxTime, int64_t nValueIn, uint32_t nIntervalBegin, uint32_t nIntervalEnd)
         : kernel(kernel), nBits(nBits), nInputTxTime(nInputTxTime), bnValueIn(nValueIn), nIntervalBegin(nIntervalBegin), nIntervalEnd(nIntervalEnd)
     {
-        solutions = vector<std::pair<uint256,uint32_t> >();
+        solutions = vector<pair<uint256,uint32_t> >();
     }
 
 void KernelWorker::Do_generic()
@@ -21,7 +23,7 @@ void KernelWorker::Do_generic()
     // Compute maximum possible target to filter out majority of obviously insufficient hashes
     CBigNum bnTargetPerCoinDay;
     bnTargetPerCoinDay.SetCompact(nBits);
-    uint256 nMaxTarget = (bnTargetPerCoinDay * bnValueIn * nStakeMaxAge / COIN / nOneDay).getuint256();
+    auto nMaxTarget = (bnTargetPerCoinDay * bnValueIn * nStakeMaxAge / COIN / nOneDay).getuint256();
 
     SHA256_CTX ctx, workerCtx;
     // Init new sha256 context and update it
@@ -36,18 +38,18 @@ void KernelWorker::Do_generic()
 
     // Search forward in time from the given timestamp
     // Stopping search in case of shutting down
-    for (uint32_t nTimeTx=nIntervalBegin, nMaxTarget32 = nMaxTarget.Get32(7); nTimeTx<nIntervalEnd && !fShutdown; nTimeTx++)
+    for (auto nTimeTx=nIntervalBegin, nMaxTarget32 = nMaxTarget.Get32(7); nTimeTx<nIntervalEnd && !fShutdown; nTimeTx++)
     {
         // Complete first hashing iteration
         uint256 hash1;
         SHA256_Update(&ctx, (unsigned char*)&nTimeTx, 4);
-        SHA256_Final((unsigned char*)&hash1, &ctx);
+        SHA256_Final(hash1.begin(), &ctx);
 
         // Restore context
         ctx = workerCtx;
 
         // Finally, calculate kernel hash
-        SHA256((unsigned char*)&hash1, sizeof(hashProofOfStake), (unsigned char*)&hashProofOfStake);
+        SHA256(hash1.begin(), sizeof(hashProofOfStake), (unsigned char*)&hashProofOfStake);
 
         // Skip if hash doesn't satisfy the maximum target
         if (hashProofOfStake[7] > nMaxTarget32)
@@ -73,7 +75,7 @@ vector<pair<uint256,uint32_t> >& KernelWorker::GetSolutions()
 
 // Scan given kernel for solutions
 
-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)
+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)
 {
     CBigNum bnTargetPerCoinDay;
     bnTargetPerCoinDay.SetCompact(nBits);
@@ -81,7 +83,7 @@ bool ScanKernelBackward(unsigned char *kernel, uint32_t nBits, uint32_t nInputTx
     CBigNum bnValueIn(nValueIn);
 
     // Get maximum possible target to filter out the majority of obviously insufficient hashes
-    uint256 nMaxTarget = (bnTargetPerCoinDay * bnValueIn * nStakeMaxAge / COIN / nOneDay).getuint256();
+    auto nMaxTarget = (bnTargetPerCoinDay * bnValueIn * nStakeMaxAge / COIN / nOneDay).getuint256();
 
     SHA256_CTX ctx, workerCtx;
     // Init new sha256 context and update it
@@ -97,14 +99,14 @@ bool ScanKernelBackward(unsigned char *kernel, uint32_t nBits, uint32_t nInputTx
         // Complete first hashing iteration
         uint256 hash1;
         SHA256_Update(&ctx, (unsigned char*)&nTimeTx, 4);
-        SHA256_Final((unsigned char*)&hash1, &ctx);
+        SHA256_Final(hash1.begin(), &ctx);
 
         // Restore context
         ctx = workerCtx;
 
         // Finally, calculate kernel hash
         uint256 hashProofOfStake;
-        SHA256((unsigned char*)&hash1, sizeof(hashProofOfStake), (unsigned char*)&hashProofOfStake);
+        SHA256(hash1.begin(), hashProofOfStake.size(), hashProofOfStake.begin());
 
         // Skip if hash doesn't satisfy the maximum target
         if (hashProofOfStake > nMaxTarget)
@@ -115,9 +117,7 @@ bool ScanKernelBackward(unsigned char *kernel, uint32_t nBits, uint32_t nInputTx
 
         if (bnTargetProofOfStake >= CBigNum(hashProofOfStake))
         {
-            solution.first = hashProofOfStake;
-            solution.second = nTimeTx;
-
+            solution = { hashProofOfStake, nTimeTx };
             return true;
         }
     }