Miner stats didn't work in RPC and UI
[novacoin.git] / src / rpcmining.cpp
index 8f02836..d5bc43b 100644 (file)
@@ -15,6 +15,7 @@ using namespace json_spirit;
 using namespace std;
 
 extern uint256 nPoWBase;
+extern uint64_t nStakeInputsMapSize;
 
 Value getsubsidy(const Array& params, bool fHelp)
 {
@@ -45,9 +46,6 @@ Value getmininginfo(const Array& params, bool fHelp)
             "getmininginfo\n"
             "Returns an object containing mining-related information.");
 
-    float nKernelsRate = 0, nCoinDaysRate = 0;
-    pwalletMain->GetStakeStats(nKernelsRate, nCoinDaysRate);
-
     Object obj, diff, weight;
     obj.push_back(Pair("blocks",        (int)nBestHeight));
     obj.push_back(Pair("currentblocksize",(uint64_t)nLastBlockSize));
@@ -64,11 +62,9 @@ Value getmininginfo(const Array& params, bool fHelp)
     obj.push_back(Pair("errors",        GetWarnings("statusbar")));
     obj.push_back(Pair("pooledtx",      (uint64_t)mempool.size()));
 
-    weight.push_back(Pair("kernelsrate",   nKernelsRate));
-    weight.push_back(Pair("cdaysrate",   nCoinDaysRate));
-    obj.push_back(Pair("stakestats", weight));
+    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("stakeinterest",    (uint64_t)GetProofOfStakeReward(0, GetLastBlockIndex(pindexBest, true)->nBits, GetLastBlockIndex(pindexBest, true)->nTime, true)));
     obj.push_back(Pair("testnet",       fTestNet));
     return obj;
 }
@@ -106,8 +102,41 @@ Value scaninput(const Array& params, bool fHelp)
     uint256 hashBlock = 0;
     if (GetTransaction(hash, tx, hashBlock))
     {
+        if (nOut > tx.vout.size())
+            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Incorrect output number");
+
+        if (hashBlock == 0)
+            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unable to find transaction in the blockchain");
+
+        CTxDB txdb("r");
+
+        CBlock block;
+        CTxIndex txindex;
+
+        // Load transaction index item
+        if (!txdb.ReadTxIndex(tx.GetHash(), txindex))
+            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unable to read block index item");
+
+        // Read block header
+        if (!block.ReadFromDisk(txindex.pos.nFile, txindex.pos.nBlockPos, false))
+            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "CBlock::ReadFromDisk() failed");
+
+        uint64_t nStakeModifier = 0;
+        if (!GetKernelStakeModifier(block.GetHash(), nStakeModifier))
+            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No kernel stake modifier generated yet");
+
+        std::pair<uint32_t, uint32_t> interval;
+        interval.first = GetTime();
+        // 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;
+
+        SHA256_CTX ctx;
+        GetKernelMidstate(nStakeModifier, block.nTime, txindex.pos.nTxPos - txindex.pos.nBlockPos, tx.nTime, nOut, ctx);
+
         std::pair<uint256, uint32_t> solution;
-        if (ScanInputForStakeKernelHash(tx, nOut, nBits, nDays * 86400, solution))
+        if (ScanMidstateForward(ctx, nBits, tx.nTime, tx.vout[nOut].nValue, interval, solution))
         {
             Object r;
             r.push_back(Pair("hash", solution.first.GetHex()));