From 5949ad350531b1e57df9d1fdc5966132e6916080 Mon Sep 17 00:00:00 2001 From: Sunny King Date: Wed, 22 Feb 2012 02:46:54 +0000 Subject: [PATCH] PPCoin: RPC command 'reservebalance' --- src/bitcoinrpc.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ src/db.cpp | 1 + src/main.cpp | 2 +- src/main.h | 1 + src/wallet.cpp | 7 ++++++- 5 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 08c598a..fd753eb 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -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 [ [amount]]\n" + " is true or false to turn balance reserve on or off.\n" + " 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 pCallTable[] = make_pair("listsinceblock", &listsinceblock), make_pair("resetcheckpoint", &resetcheckpoint), make_pair("getbranchpoint", &getbranchpoint), + make_pair("reservebalance", &reservebalance), }; map mapCallTable(pCallTable, pCallTable + sizeof(pCallTable)/sizeof(pCallTable[0])); @@ -2525,6 +2563,8 @@ int CommandLineRPC(int argc, char *argv[]) } if (strMethod == "sendmany" && n > 2) ConvertTo(params[2]); if (strMethod == "resetcheckpoint" && n > 0) ConvertTo(params[0]); + if (strMethod == "reservebalance" && n > 0) ConvertTo(params[0]); + if (strMethod == "reservebalance" && n > 1) ConvertTo(params[1]); // Execute Object reply = CallRPC(strMethod, params); diff --git a/src/db.cpp b/src/db.cpp index c260fb3..6eece05 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -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") { diff --git a/src/main.cpp b/src/main.cpp index 7cd7414..9d9020c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -64,7 +64,7 @@ int fUseUPnP = true; #else int fUseUPnP = false; #endif - +int64 nBalanceReserve = 0; ////////////////////////////////////////////////////////////////////////////// // diff --git a/src/main.h b/src/main.h index 2203598..2e443bf 100644 --- a/src/main.h +++ b/src/main.h @@ -75,6 +75,7 @@ extern int nLimitProcessors; extern int fMinimizeToTray; extern int fMinimizeOnClose; extern int fUseUPnP; +extern int64 nBalanceReserve; diff --git a/src/wallet.cpp b/src/wallet.cpp index 52d77ac..0ef2167 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -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 > 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) { -- 1.7.1