Improve incapsulation. Yep, finally.
[novacoin.git] / src / kernel_worker.cpp
index 42e6172..dc31005 100644 (file)
@@ -8,7 +8,7 @@
 
 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<pair<uint256,uint32_t> >();
@@ -41,13 +41,13 @@ void KernelWorker::Do_generic()
         // 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,15 +73,13 @@ 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, pair<uint32_t, uint32_t> &SearchInterval, 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);
-
-    CBigNum bnValueIn(nValueIn);
+    uint256 nTargetPerCoinDay;
+    nTargetPerCoinDay.SetCompact(nBits);
 
     // Get maximum possible target to filter out the majority of obviously insufficient hashes
-    auto nMaxTarget = (bnTargetPerCoinDay * bnValueIn * nStakeMaxAge / COIN / nOneDay).getuint256();
+    auto nMaxTarget = nTargetPerCoinDay * (uint64_t)nValueIn * (uint64_t)nStakeMaxAge / (uint64_t)COIN / (uint64_t)nOneDay;
 
     SHA256_CTX ctx, workerCtx;
     // Init new sha256 context and update it
@@ -97,27 +95,25 @@ 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)
             continue;
 
-        auto bnCoinDayWeight = bnValueIn * GetWeight((int64_t)nInputTxTime, (int64_t)nTimeTx) / COIN / nOneDay;
-        auto bnTargetProofOfStake = bnCoinDayWeight * bnTargetPerCoinDay;
+        auto nCoinDayWeight = uint256(nValueIn) * (uint64_t)GetWeight((int64_t)nInputTxTime, (int64_t)nTimeTx) / (uint64_t)COIN / (uint64_t)nOneDay; // TODO: Stop using signed types for value, time, weight and so on, because all these casts are really stupid.
+        auto nTargetProofOfStake = nCoinDayWeight * nTargetPerCoinDay;
 
-        if (bnTargetProofOfStake >= CBigNum(hashProofOfStake))
+        if (nTargetProofOfStake >= hashProofOfStake)
         {
-            solution.first = hashProofOfStake;
-            solution.second = nTimeTx;
-
+            solution = { hashProofOfStake, nTimeTx };
             return true;
         }
     }