PPCoin: RPC command 'reservebalance'
authorSunny King <p2pcoin@gmail.com>
Wed, 22 Feb 2012 02:46:54 +0000 (02:46 +0000)
committerSunny King <p2pcoin@gmail.com>
Wed, 22 Feb 2012 02:46:54 +0000 (02:46 +0000)
src/bitcoinrpc.cpp
src/db.cpp
src/main.cpp
src/main.h
src/wallet.cpp

index 08c598a..fd753eb 100644 (file)
@@ -1836,6 +1836,43 @@ Value getbranchpoint(const Array& params, bool fHelp)
 }
 
 
+// ppcoin: reserve balance from being staked for network protection
+Value reservebalance(const Array& params, bool fHelp)
+{
+    if (fHelp || params.size() > 2)
+        throw runtime_error(
+            "reservebalance [<reserve> [amount]]\n"
+            "<reserve> is true or false to turn balance reserve on or off.\n"
+            "<amount> is a real and rounded to cent.\n"
+            "Set reserve amount not participating in network protection.\n"
+            "If no parameters provided current setting is printed.\n");
+
+    if (params.size() > 0)
+    {
+        bool fReserve = params[0].get_bool();
+        if (fReserve)
+        {
+            if (params.size() == 1)
+                throw runtime_error("must provide amount to reserve balance.\n");
+            int64 nAmount = AmountFromValue(params[1]);
+            nAmount = (nAmount / CENT) * CENT;  // round to cent
+            if (nAmount < 0)
+                throw runtime_error("amount cannot be negative.\n");
+            WriteSetting("nBalanceReserve", nBalanceReserve = nAmount);
+        }
+        else
+        {
+            if (params.size() > 1)
+                throw runtime_error("cannot specify amount to turn off reserve.\n");
+            WriteSetting("nBalanceReserve", nBalanceReserve = 0);
+        }
+    }
+
+    Object result;
+    result.push_back(Pair("reserve", (nBalanceReserve > 0)));
+    result.push_back(Pair("amount", ValueFromAmount(nBalanceReserve)));
+    return result;
+}
 
 
 
@@ -1890,6 +1927,7 @@ pair<string, rpcfn_type> pCallTable[] =
     make_pair("listsinceblock",        &listsinceblock),
     make_pair("resetcheckpoint",        &resetcheckpoint),
     make_pair("getbranchpoint",         &getbranchpoint),
+    make_pair("reservebalance",         &reservebalance),
 };
 map<string, rpcfn_type> mapCallTable(pCallTable, pCallTable + sizeof(pCallTable)/sizeof(pCallTable[0]));
 
@@ -2525,6 +2563,8 @@ int CommandLineRPC(int argc, char *argv[])
         }
         if (strMethod == "sendmany"                && n > 2) ConvertTo<boost::int64_t>(params[2]);
         if (strMethod == "resetcheckpoint"         && n > 0) ConvertTo<boost::int64_t>(params[0]);
+        if (strMethod == "reservebalance"          && n > 0) ConvertTo<bool>(params[0]);
+        if (strMethod == "reservebalance"          && n > 1) ConvertTo<double>(params[1]);
 
         // Execute
         Object reply = CallRPC(strMethod, params);
index c260fb3..6eece05 100644 (file)
@@ -930,6 +930,7 @@ int CWalletDB::LoadWallet(CWallet* pwallet)
                 if (strKey == "fUseProxy")          ssValue >> fUseProxy;
                 if (strKey == "addrProxy")          ssValue >> addrProxy;
                 if (fHaveUPnP && strKey == "fUseUPnP")           ssValue >> fUseUPnP;
+                if (strKey == "nBalanceReserve")    ssValue >> nBalanceReserve;
             }
             else if (strType == "minversion")
             {
index 7cd7414..9d9020c 100644 (file)
@@ -64,7 +64,7 @@ int fUseUPnP = true;
 #else
 int fUseUPnP = false;
 #endif
-
+int64 nBalanceReserve = 0;
 
 //////////////////////////////////////////////////////////////////////////////
 //
index 2203598..2e443bf 100644 (file)
@@ -75,6 +75,7 @@ extern int nLimitProcessors;
 extern int fMinimizeToTray;
 extern int fMinimizeOnClose;
 extern int fUseUPnP;
+extern int64 nBalanceReserve;
 
 
 
index 52d77ac..0ef2167 100644 (file)
@@ -1069,9 +1069,12 @@ bool CWallet::CreateCoinStake(CScript scriptPubKey, CTransaction& txNew)
         scriptEmpty.clear();
         txNew.vout.push_back(CTxOut(0, scriptEmpty));
         // Choose coins to use
+        int64 nBalance = GetBalance();
+        if (nBalance <= nBalanceReserve)
+            return false;
         set<pair<const CWalletTx*,unsigned int> > setCoins;
         int64 nValueIn = 0;
-        if (!SelectCoins(GetBalance(), txNew.nTime, setCoins, nValueIn))
+        if (!SelectCoins(nBalance - nBalanceReserve, txNew.nTime, setCoins, nValueIn))
             return false;
         if (setCoins.empty())
             return false;
@@ -1082,6 +1085,8 @@ bool CWallet::CreateCoinStake(CScript scriptPubKey, CTransaction& txNew)
             // Only spend one tx for now
             break;
         }
+        if (nCredit > nBalance - nBalanceReserve)
+            return false;
         // Fill vin
         BOOST_FOREACH(const PAIRTYPE(const CWalletTx*,unsigned int)& coin, setCoins)
         {