Skip to content

Commit 7d8ffac

Browse files
committed
Changes necessary now that zero values accepted in AmountFromValue
- Add an accept test for zero amounts, and a reject test for negative amounts - Remove ugly hack in `settxfee` that is no longer necessary - Do explicit zero checks in wallet RPC functions - Don't add a check for zero amounts in `createrawtransaction` - this could be seen as a feature
1 parent a04bdef commit 7d8ffac

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/test/rpc_tests.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ static UniValue ValueFromString(const std::string &str)
131131

132132
BOOST_AUTO_TEST_CASE(rpc_parse_monetary_values)
133133
{
134+
BOOST_CHECK_THROW(AmountFromValue(ValueFromString("-0.00000001")), UniValue);
135+
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0")), 0LL);
136+
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.00000000")), 0LL);
134137
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.00000001")), 1LL);
135138
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.17622195")), 17622195LL);
136139
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.5")), 50000000LL);

src/wallet/rpcwallet.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ UniValue sendtoaddress(const UniValue& params, bool fHelp)
414414

415415
// Amount
416416
CAmount nAmount = AmountFromValue(params[1]);
417+
if (nAmount <= 0)
418+
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount for send");
417419

418420
// Wallet comments
419421
CWalletTx wtx;
@@ -809,6 +811,8 @@ UniValue movecmd(const UniValue& params, bool fHelp)
809811
string strFrom = AccountFromValue(params[0]);
810812
string strTo = AccountFromValue(params[1]);
811813
CAmount nAmount = AmountFromValue(params[2]);
814+
if (nAmount <= 0)
815+
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount for send");
812816
if (params.size() > 3)
813817
// unused parameter, used to be nMinDepth, keep type-checking it though
814818
(void)params[3].get_int();
@@ -888,6 +892,8 @@ UniValue sendfrom(const UniValue& params, bool fHelp)
888892
if (!address.IsValid())
889893
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
890894
CAmount nAmount = AmountFromValue(params[2]);
895+
if (nAmount <= 0)
896+
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount for send");
891897
int nMinDepth = 1;
892898
if (params.size() > 3)
893899
nMinDepth = params[3].get_int();
@@ -987,6 +993,8 @@ UniValue sendmany(const UniValue& params, bool fHelp)
987993

988994
CScript scriptPubKey = GetScriptForDestination(address.Get());
989995
CAmount nAmount = AmountFromValue(sendTo[name_]);
996+
if (nAmount <= 0)
997+
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount for send");
990998
totalAmount += nAmount;
991999

9921000
bool fSubtractFeeFromAmount = false;
@@ -2168,9 +2176,7 @@ UniValue settxfee(const UniValue& params, bool fHelp)
21682176
LOCK2(cs_main, pwalletMain->cs_wallet);
21692177

21702178
// Amount
2171-
CAmount nAmount = 0;
2172-
if (params[0].get_real() != 0.0)
2173-
nAmount = AmountFromValue(params[0]); // rejects 0.0 amounts
2179+
CAmount nAmount = AmountFromValue(params[0]);
21742180

21752181
payTxFee = CFeeRate(nAmount, 1000);
21762182
return true;
@@ -2352,4 +2358,4 @@ UniValue listunspent(const UniValue& params, bool fHelp)
23522358
}
23532359

23542360
return results;
2355-
}
2361+
}

0 commit comments

Comments
 (0)