minor rework of SendMoney in rpcwallet

- rework the function to not log errors but use throw JSONRPCError
- remove a check for IsLocked() that is done in sendtoaddress and
  sendfrom RPC calls already
- cache GetBalance() return value, because it's possibly used twice
This commit is contained in:
Philip Kaufmann 2014-12-09 14:50:01 +01:00
parent 93b7544501
commit 25cf6f3d41

View file

@ -19,6 +19,7 @@
#include <stdint.h> #include <stdint.h>
#include <boost/assign/list_of.hpp> #include <boost/assign/list_of.hpp>
#include "json/json_spirit_utils.h" #include "json/json_spirit_utils.h"
#include "json/json_spirit_value.h" #include "json/json_spirit_value.h"
@ -316,34 +317,28 @@ Value getaddressesbyaccount(const Array& params, bool fHelp)
return ret; return ret;
} }
void SendMoney(const CTxDestination &address, CAmount nValue, CWalletTx& wtxNew) static void SendMoney(const CTxDestination &address, CAmount nValue, CWalletTx& wtxNew)
{ {
CAmount curBalance = pwalletMain->GetBalance();
// Check amount // Check amount
if (nValue <= 0) if (nValue <= 0)
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid amount"); throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid amount");
if (nValue > pwalletMain->GetBalance()) if (nValue > curBalance)
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Insufficient funds"); throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Insufficient funds");
string strError;
if (pwalletMain->IsLocked())
{
strError = "Error: Wallet locked, unable to create transaction!";
LogPrintf("SendMoney(): %s", strError);
throw JSONRPCError(RPC_WALLET_ERROR, strError);
}
// Parse Bitcoin address // Parse Bitcoin address
CScript scriptPubKey = GetScriptForDestination(address); CScript scriptPubKey = GetScriptForDestination(address);
// Create and send the transaction // Create and send the transaction
CReserveKey reservekey(pwalletMain); CReserveKey reservekey(pwalletMain);
CAmount nFeeRequired; CAmount nFeeRequired;
if (!pwalletMain->CreateTransaction(scriptPubKey, nValue, wtxNew, reservekey, nFeeRequired, strError)) std::string strError;
{ if (!pwalletMain->CreateTransaction(scriptPubKey, nValue, wtxNew, reservekey, nFeeRequired, strError)) {
if (nValue + nFeeRequired > pwalletMain->GetBalance()) if (nValue + nFeeRequired > curBalance)
strError = strprintf("Error: This transaction requires a transaction fee of at least %s because of its amount, complexity, or use of recently received funds!", FormatMoney(nFeeRequired)); throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Error: This transaction requires a transaction fee of at least %s because of its amount, complexity, or use of recently received funds!", FormatMoney(nFeeRequired)));
LogPrintf("SendMoney(): %s\n", strError); else
throw JSONRPCError(RPC_WALLET_ERROR, strError); throw JSONRPCError(RPC_WALLET_ERROR, strError);
} }
if (!pwalletMain->CommitTransaction(wtxNew, reservekey)) if (!pwalletMain->CommitTransaction(wtxNew, reservekey))