Add stakepower into getmininginfo output (EXPERIMENTAL)
authoralex <alex@alex-VirtualBox.(none)>
Sat, 15 Jun 2013 16:24:16 +0000 (20:24 +0400)
committeralex <alex@alex-VirtualBox.(none)>
Sat, 15 Jun 2013 16:24:16 +0000 (20:24 +0400)
src/rpcmining.cpp
src/wallet.cpp
src/wallet.h

index 3b22686..c5ccea2 100644 (file)
@@ -25,6 +25,7 @@ Value getmininginfo(const Array& params, bool fHelp)
     obj.push_back(Pair("difficulty",    (double)GetDifficulty()));
     obj.push_back(Pair("errors",        GetWarnings("statusbar")));
     obj.push_back(Pair("pooledtx",      (uint64_t)mempool.size()));
+    obj.push_back(Pair("stakepower",    (uint64_t)pwalletMain->GetStakeMintPower(*pwalletMain)));
     obj.push_back(Pair("testnet",       fTestNet));
     return obj;
 }
index 580edf5..613edc2 100644 (file)
@@ -1338,6 +1338,52 @@ bool CWallet::CreateTransaction(CScript scriptPubKey, int64 nValue, CWalletTx& w
     return CreateTransaction(vecSend, wtxNew, reservekey, nFeeRet);
 }
 
+// NovaCoin: get current stake generation power
+uint64 CWallet::GetStakeMintPower(const CKeyStore& keystore)
+{
+    LOCK2(cs_main, cs_wallet);
+
+    // Choose coins to use
+    int64 nBalance = GetBalance();
+    int64 nReserveBalance = 0;
+    uint64 nCoinAge = 0;
+
+    if (mapArgs.count("-reservebalance") && !ParseMoney(mapArgs["-reservebalance"], nReserveBalance))
+    {
+        error("CreateCoinStake : invalid reserve balance amount");
+        return 0;
+    }
+
+    if (nBalance <= nReserveBalance)
+        return 0;
+
+    set<pair<const CWalletTx*,unsigned int> > setCoins;
+    vector<const CWalletTx*> vwtxPrev;
+    int64 nValueIn = 0;
+    if (!SelectCoins(nBalance - nReserveBalance, GetTime(), setCoins, nValueIn))
+        return 0;
+    if (setCoins.empty())
+        return 0;
+
+    BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int) pcoin, setCoins)
+    {
+        CTxDB txdb("r");
+        CTxIndex txindex;
+        if (!txdb.ReadTxIndex(pcoin.first->GetHash(), txindex))
+            continue;
+
+        // Do not count input that is still too young
+        if (pcoin.first->nTime + nStakeMaxAge > GetTime())
+            continue;
+
+        uint64 unCoinAge;
+        pcoin.first->GetCoinAge(txdb, unCoinAge);
+        nCoinAge += unCoinAge;
+    }
+
+    return nCoinAge;
+}
+
 // ppcoin: create coin stake transaction
 bool CWallet::CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int64 nSearchInterval, CTransaction& txNew)
 {
index 7f848f6..ca43104 100644 (file)
@@ -177,7 +177,10 @@ public:
     bool CreateTransaction(const std::vector<std::pair<CScript, int64> >& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet);
     bool CreateTransaction(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet);
     bool CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey);
+
+    uint64 GetStakeMintPower(const CKeyStore& keystore);
     bool CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int64 nSearchInterval, CTransaction& txNew);
+
     std::string SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, bool fAskFee=false);
     std::string SendMoneyToDestination(const CTxDestination &address, int64 nValue, CWalletTx& wtxNew, bool fAskFee=false);