RPC scaninput: Reduce default time window to 90 days, fix nOut checking issue.
[novacoin.git] / src / rpcmining.cpp
index 427f63c..325ef2c 100644 (file)
@@ -57,13 +57,13 @@ Value getmininginfo(const Array& params, bool fHelp)
     obj.push_back(Pair("difficulty",    diff));
 
     obj.push_back(Pair("blockvalue",    (uint64_t)GetProofOfWorkReward(GetLastBlockIndex(pindexBest, false)->nBits)));
-    obj.push_back(Pair("netmhashps",     GetPoWMHashPS()));
-    obj.push_back(Pair("netstakeweight", GetPoSKernelPS()));
+    obj.push_back(Pair("netmhashps",    GetPoWMHashPS()));
+    obj.push_back(Pair("netstakeweight",GetPoSKernelPS()));
     obj.push_back(Pair("errors",        GetWarnings("statusbar")));
     obj.push_back(Pair("pooledtx",      (uint64_t)mempool.size()));
 
-    obj.push_back(Pair("stakeinputs", (uint64_t)nStakeInputsMapSize));
-    obj.push_back(Pair("stakeinterest",    (int64_t)GetProofOfStakeReward(0, GetLastBlockIndex(pindexBest, true)->nBits, GetLastBlockIndex(pindexBest, true)->nTime, true)));
+    obj.push_back(Pair("stakeinputs",   (uint64_t)nStakeInputsMapSize));
+    obj.push_back(Pair("stakeinterest", GetProofOfStakeReward(0, GetLastBlockIndex(pindexBest, true)->nBits, GetLastBlockIndex(pindexBest, true)->nTime, true)));
 
     obj.push_back(Pair("testnet",       fTestNet));
     return obj;
@@ -76,14 +76,14 @@ Value scaninput(const Array& params, bool fHelp)
             "scaninput <txid> <nout> [difficulty] [days]\n"
             "Scan specified input for suitable kernel solutions.\n"
             "    [difficulty] - upper limit for difficulty, current difficulty by default;\n"
-            "    [days] - time window, 365 days by default.\n"
+            "    [days] - time window, 90 days by default.\n"
         );
 
 
-    uint256 hash;
-    hash.SetHex(params[0].get_str());
-
-    uint32_t nOut = params[1].get_int(), nBits = GetNextTargetRequired(pindexBest, true), nDays = 365;
+    uint256 hash(params[0].get_str());
+    uint32_t nOut = params[1].get_int();
+    uint32_t nBits = GetNextTargetRequired(pindexBest, true);
+    uint32_t nDays = 90;
 
     if (params.size() > 2)
     {
@@ -91,18 +91,19 @@ Value scaninput(const Array& params, bool fHelp)
         bnTarget *= 1000;
         bnTarget /= (int) (params[2].get_real() * 1000);
         nBits = bnTarget.GetCompact();
-    }
 
-    if (params.size() > 3)
-    {
-        nDays = params[3].get_int();
+        if (params.size() > 3)
+        {
+            nDays = params[3].get_int();
+        }
     }
 
+
     CTransaction tx;
     uint256 hashBlock = 0;
     if (GetTransaction(hash, tx, hashBlock))
     {
-        if (nOut > tx.vout.size())
+        if (nOut > tx.vout.size() - 1)
             throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Incorrect output number");
 
         if (hashBlock == 0)
@@ -130,19 +131,30 @@ Value scaninput(const Array& params, bool fHelp)
         // Only count coins meeting min age requirement
         if (nStakeMinAge + block.nTime > interval.first)
             interval.first += (nStakeMinAge + block.nTime - interval.first);
-        interval.second = interval.first + nDays * 86400;
+        interval.second = interval.first + nDays * nOneDay;
 
-        SHA256_CTX ctx;
-        GetKernelMidstate(nStakeModifier, block.nTime, txindex.pos.nTxPos - txindex.pos.nBlockPos, tx.nTime, nOut, ctx);
+        // Build static part of kernel
+        CDataStream ssKernel(SER_GETHASH, 0);
+        ssKernel << nStakeModifier;
+        ssKernel << block.nTime << (txindex.pos.nTxPos - txindex.pos.nBlockPos) << tx.nTime << nOut;
+        CDataStream::const_iterator itK = ssKernel.begin();
 
-        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 (ScanKernelForward((unsigned char *)&itK[0], 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 results;
+
+            BOOST_FOREACH(const PAIRTYPE(uint256, uint32_t) solution, solutions)
+            {
+
+                Object item;
+                item.push_back(Pair("hash", solution.first.GetHex()));
+                item.push_back(Pair("time", DateTimeStrFormat(solution.second)));
+
+                results.push_back(item);
+            }
 
-            return r;
+            return results;
         }
     }
     else