diff --git a/qa/rpc-tests/confidential_transactions.py b/qa/rpc-tests/confidential_transactions.py index d2c6e13839..6e47559d3a 100755 --- a/qa/rpc-tests/confidential_transactions.py +++ b/qa/rpc-tests/confidential_transactions.py @@ -36,16 +36,16 @@ class CTTest (BitcoinTestFramework): self.nodes[0].generate(101) self.sync_all() #Running balances - node0 = self.nodes[0].getbalance() + node0 = self.nodes[0].getbalance()["bitcoin"] node1 = 0 node2 = 0 self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), node0, "", "", True) self.nodes[0].generate(101) self.sync_all() - assert_equal(self.nodes[0].getbalance(), node0) - assert_equal(self.nodes[1].getbalance(), node1) - assert_equal(self.nodes[2].getbalance(), node2) + assert_equal(self.nodes[0].getbalance()["bitcoin"], node0) + assert_equal(self.nodes[1].getbalance("", 1, False, "bitcoin"), node1) + assert_equal(self.nodes[2].getbalance("", 1, False, "bitcoin"), node2) # Send 3 BTC from 0 to a new unconfidential address of 2 with # the sendtoaddress call @@ -59,9 +59,9 @@ class CTTest (BitcoinTestFramework): node0 = node0 - value0 node2 = node2 + value0 - assert_equal(self.nodes[0].getbalance(), node0) - assert_equal(self.nodes[1].getbalance(), node1) - assert_equal(self.nodes[2].getbalance(), node2) + assert_equal(self.nodes[0].getbalance()["bitcoin"], node0) + assert_equal(self.nodes[1].getbalance("", 1, False, "bitcoin"), node1) + assert_equal(self.nodes[2].getbalance()["bitcoin"], node2) # Send 5 BTC from 0 to a new address of 2 with the sendtoaddress call address = self.nodes[2].getnewaddress() @@ -74,9 +74,9 @@ class CTTest (BitcoinTestFramework): node0 = node0 - value1 node2 = node2 + value1 - assert_equal(self.nodes[0].getbalance(), node0) - assert_equal(self.nodes[1].getbalance(), node1) - assert_equal(self.nodes[2].getbalance(), node2) + assert_equal(self.nodes[0].getbalance()["bitcoin"], node0) + assert_equal(self.nodes[1].getbalance("", 1, False, "bitcoin"), node1) + assert_equal(self.nodes[2].getbalance()["bitcoin"], node2) # Send 7 BTC from 0 to the unconfidential address of 2 and 11 BTC to the # confidential address using the raw transaction interface @@ -84,7 +84,7 @@ class CTTest (BitcoinTestFramework): value2 = 7 value3 = 11 value23 = value2 + value3 - unspent = self.nodes[0].listunspent() + unspent = self.nodes[0].listunspent(1, 9999999, [], "bitcoin") unspent = [i for i in unspent if i['amount'] > value23] assert_equal(len(unspent), 1) fee = Decimal(0.0001) @@ -102,9 +102,9 @@ class CTTest (BitcoinTestFramework): node0 -= (value2 + value3) node2 += value2 + value3 - assert_equal(self.nodes[0].getbalance(), node0) - assert_equal(self.nodes[1].getbalance(), node1) - assert_equal(self.nodes[2].getbalance(), node2) + assert_equal(self.nodes[0].getbalance()["bitcoin"], node0) + assert_equal(self.nodes[1].getbalance("", 1, False, "bitcoin"), node1) + assert_equal(self.nodes[2].getbalance()["bitcoin"], node2) # Check 2's listreceivedbyaddress received_by_address = self.nodes[2].listreceivedbyaddress() @@ -118,16 +118,16 @@ class CTTest (BitcoinTestFramework): received_by_address = self.nodes[1].listreceivedbyaddress(1, False, True) #Node sees nothing unless it understands the values assert_equal(len(received_by_address), 0) - assert_equal(len(self.nodes[1].listunspent()), 0) + assert_equal(len(self.nodes[1].listunspent(1, 9999999, [], "bitcoin")), 0) # Import the blinding key blindingkey = self.nodes[2].dumpblindingkey(address) self.nodes[1].importblindingkey(address, blindingkey) # Check the auditor's gettransaction and listreceivedbyaddress # Needs rescan to update wallet txns - assert_equal(self.nodes[1].gettransaction(confidential_tx_id, True)['amount'], value1) - assert_equal(self.nodes[1].gettransaction(raw_tx_id, True)['amount'], value3) - list_unspent = self.nodes[1].listunspent() + assert_equal(self.nodes[1].gettransaction(confidential_tx_id, True)['amount']["bitcoin"], value1) + assert_equal(self.nodes[1].gettransaction(raw_tx_id, True)['amount']["bitcoin"], value3) + list_unspent = self.nodes[1].listunspent(1, 9999999, [], "bitcoin") assert_equal(list_unspent[0]['amount']+list_unspent[1]['amount'], value1+value3) received_by_address = self.nodes[1].listreceivedbyaddress(1, False, True) assert_equal(len(received_by_address), 1) @@ -137,7 +137,7 @@ class CTTest (BitcoinTestFramework): # Spending a single confidential output and sending it to a # unconfidential output is not possible with CT. Test the # correct behavior of blindrawtransaction. - unspent = self.nodes[0].listunspent() + unspent = self.nodes[0].listunspent(1, 9999999, [], "bitcoin") unspent = [i for i in unspent if i['amount'] > value23] assert_equal(len(unspent), 1) tx = self.nodes[0].createrawtransaction([{"txid": unspent[0]["txid"], @@ -170,9 +170,9 @@ class CTTest (BitcoinTestFramework): node0 -= value4 node2 += value4 - assert_equal(self.nodes[0].getbalance(), node0) - assert_equal(self.nodes[1].getbalance(), node1) - assert_equal(self.nodes[2].getbalance(), node2) + assert_equal(self.nodes[0].getbalance()["bitcoin"], node0) + assert_equal(self.nodes[1].getbalance("", 1, False, "bitcoin"), node1) + assert_equal(self.nodes[2].getbalance()["bitcoin"], node2) # Testing wallet's ability to deblind its own outputs addr = self.nodes[0].getnewaddress() @@ -223,10 +223,10 @@ class CTTest (BitcoinTestFramework): # Assets balance checking, note that accounts are completely ignored because # balance queries with accounts are horrifically broken upstream assert_equal(self.nodes[0].getbalance("*", 0, False, "bitcoin"), self.nodes[0].getbalance("accountsareignored", 0, False, "bitcoin")) - assert_equal(self.nodes[0].getwalletinfo("*")['balance']['bitcoin'], self.nodes[0].getbalance("accountsareignored", 0, False, "*")['bitcoin']) + assert_equal(self.nodes[0].getwalletinfo()['balance']['bitcoin'], self.nodes[0].getbalance("accountsareignored", 0, False, "bitcoin")) # Now test wallet interaction with unlabeled funds - wallet_list = self.nodes[0].getinfo("*")['balance'] # returns list of known non-zero assets in wallet, labels if they exist, hex otherwise + wallet_list = self.nodes[0].getinfo()['balance'] # returns list of known non-zero assets in wallet, labels if they exist, hex otherwise otherasset = "" for label in wallet_list: if label != "bitcoin" and label != "testasset": @@ -243,7 +243,7 @@ class CTTest (BitcoinTestFramework): self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), wallet_list[otherasset], "", "", False, "OTHER") self.nodes[0].generate(1) - assert_equal(self.nodes[2].getinfo("*")['balance'][otherasset], wallet_list[otherasset]) + assert_equal(self.nodes[2].getinfo()['balance'][otherasset], wallet_list[otherasset]) # Send some bitcoin and other assets over as well to fund wallet addr = self.nodes[2].getnewaddress() @@ -258,10 +258,9 @@ class CTTest (BitcoinTestFramework): assert_equal(self.nodes[2].getunconfirmedbalance("testasset"), Decimal(1)) b_utxos = self.nodes[2].listunspent(0, 0, [], "bitcoin") - assert_equal(b_utxos, self.nodes[2].listunspent(0, 0)) t_utxos = self.nodes[2].listunspent(0, 0, [], "testasset") - assert_equal(len(self.nodes[2].listunspent(0, 0, [], "*")), len(b_utxos)+len(t_utxos)) + assert_equal(len(self.nodes[2].listunspent(0, 0, [])), len(b_utxos)+len(t_utxos)) # Now craft a blinded transaction via raw api rawaddrs = [] diff --git a/qa/rpc-tests/rawtransactions.py b/qa/rpc-tests/rawtransactions.py index 6d4d2157e9..4fe4f77561 100755 --- a/qa/rpc-tests/rawtransactions.py +++ b/qa/rpc-tests/rawtransactions.py @@ -81,18 +81,18 @@ class RawTransactionsTest(BitcoinTestFramework): mSigObjValid = self.nodes[2].validateaddress(mSigObj) #use balance deltas instead of absolute values - bal = self.nodes[2].getbalance() + bal = self.nodes[2].getbalance()["bitcoin"] # send 1.2 BTC to msig adr txId = self.nodes[0].sendtoaddress(mSigObj, 1.2) self.sync_all() self.nodes[0].generate(1) self.sync_all() - assert_equal(self.nodes[2].getbalance(), bal+Decimal('1.20000000')) #node2 has both keys of the 2of2 ms addr., tx should affect the balance + assert_equal(self.nodes[2].getbalance()["bitcoin"], bal+Decimal('1.20000000')) #node2 has both keys of the 2of2 ms addr., tx should affect the balance # 2of3 test from different nodes - bal = self.nodes[2].getbalance() + bal = self.nodes[2].getbalance()["bitcoin"] addr1 = self.nodes[1].getnewaddress() addr2 = self.nodes[2].getnewaddress() addr3 = self.nodes[2].getnewaddress() @@ -114,7 +114,7 @@ class RawTransactionsTest(BitcoinTestFramework): #THIS IS A INCOMPLETE FEATURE #NODE2 HAS TWO OF THREE KEY AND THE FUNDS SHOULD BE SPENDABLE AND COUNT AT BALANCE CALCULATION - assert_equal(self.nodes[2].getbalance(), bal) #for now, assume the funds of a 2of3 multisig tx are not marked as spendable + assert_equal(self.nodes[2].getbalance()["bitcoin"], bal) #for now, assume the funds of a 2of3 multisig tx are not marked as spendable txDetails = self.nodes[0].gettransaction(txId, True) rawTx = self.nodes[0].decoderawtransaction(txDetails['hex']) diff --git a/qa/rpc-tests/receivedby.py b/qa/rpc-tests/receivedby.py index 6a46153dca..8ea1cebf06 100755 --- a/qa/rpc-tests/receivedby.py +++ b/qa/rpc-tests/receivedby.py @@ -82,19 +82,19 @@ class ReceivedByTest(BitcoinTestFramework): self.sync_all() #Check balance is 0 because of 0 confirmations - balance = self.nodes[1].getreceivedbyaddress(unblinded) + balance = self.nodes[1].getreceivedbyaddress(unblinded, 1, "bitcoin") if balance != Decimal("0.0"): raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance)) #Check balance is 0.1 - balance = self.nodes[1].getreceivedbyaddress(unblinded,0) + balance = self.nodes[1].getreceivedbyaddress(unblinded,0, "bitcoin") if balance != Decimal("0.1"): raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance)) #Bury Tx under 10 block so it will be returned by the default getreceivedbyaddress self.nodes[1].generate(10) self.sync_all() - balance = self.nodes[1].getreceivedbyaddress(unblinded) + balance = self.nodes[1].getreceivedbyaddress(unblinded, 1, "bitcoin") if balance != Decimal("0.1"): raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance)) diff --git a/qa/rpc-tests/wallet.py b/qa/rpc-tests/wallet.py index 37ef194702..4947108a5c 100755 --- a/qa/rpc-tests/wallet.py +++ b/qa/rpc-tests/wallet.py @@ -38,27 +38,26 @@ class WalletTest (BitcoinTestFramework): print("Mining blocks...") self.nodes[0].generate(1) - walletinfo = self.nodes[0].getwalletinfo() - assert_equal(walletinfo['immature_balance'], 21000000) - assert_equal(walletinfo['balance'], 0) + assert_equal(walletinfo['immature_balance']["bitcoin"], 21000000) + assert("bitcoin" not in walletinfo['balance']) self.sync_all() self.nodes[1].generate(101) self.sync_all() - assert_equal(self.nodes[0].getbalance(), 21000000) - assert_equal(self.nodes[1].getbalance(), 21000000) - assert_equal(self.nodes[2].getbalance(), 21000000) + assert_equal(self.nodes[0].getbalance("", 0, False, "bitcoin"), 21000000) + assert_equal(self.nodes[1].getbalance("", 0, False, "bitcoin"), 21000000) + assert_equal(self.nodes[2].getbalance("", 0, False, "bitcoin"), 21000000) #Set all OP_TRUE genesis outputs to single node self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 21000000, "", "", True) self.nodes[0].generate(101) self.sync_all() - assert_equal(self.nodes[0].getbalance(), 21000000) - assert_equal(self.nodes[1].getbalance(), 0) - assert_equal(self.nodes[2].getbalance(), 0) + assert_equal(self.nodes[0].getbalance("", 0, False, "bitcoin"), 21000000) + assert_equal(self.nodes[1].getbalance("", 0, False, "bitcoin"), 0) + assert_equal(self.nodes[2].getbalance("", 0, False, "bitcoin"), 0) #self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 1000000) #self.nodes[0].generate(1) @@ -77,7 +76,7 @@ class WalletTest (BitcoinTestFramework): self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11) self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 10) - walletinfo = self.nodes[0].getwalletinfo() + walletinfo = self.nodes[0].getwalletinfo("bitcoin") assert_equal(walletinfo['immature_balance'], 0) # Have node0 mine a block, thus it will collect its own fee. @@ -85,7 +84,7 @@ class WalletTest (BitcoinTestFramework): self.sync_all() # Exercise locking of unspent outputs - unspent_0 = self.nodes[2].listunspent()[0] + unspent_0 = self.nodes[2].listunspent(1, 9999999, [], "bitcoin")[0] unspent_0 = {"txid": unspent_0["txid"], "vout": unspent_0["vout"]} self.nodes[2].lockunspent(False, [unspent_0]) assert_raises(JSONRPCException, self.nodes[2].sendtoaddress, self.nodes[2].getnewaddress(), 20) @@ -99,13 +98,13 @@ class WalletTest (BitcoinTestFramework): # node0 should end up with 100 btc in block rewards plus fees, but # minus the 21 plus fees sent to node2 - assert_equal(self.nodes[0].getbalance(), 21000000-21) - assert_equal(self.nodes[2].getbalance(), 21) + assert_equal(self.nodes[0].getbalance("", 0, False, "bitcoin"), 21000000-21) + assert_equal(self.nodes[2].getbalance("", 0, False, "bitcoin"), 21) # Node0 should have three non-zero unspent outputs and 101 from generate. # Create a couple of transactions to send them to node2, submit them through # node1, and make sure both node0 and node2 pick them up properly: - node0utxos = self.nodes[0].listunspent(1) + node0utxos = self.nodes[0].listunspent(1, 9999999, [], "bitcoin") assert_equal(len(node0utxos), 104) # create both transactions diff --git a/qa/rpc-tests/zapwallettxes.py b/qa/rpc-tests/zapwallettxes.py index 7a28db30b2..31e28887dd 100755 --- a/qa/rpc-tests/zapwallettxes.py +++ b/qa/rpc-tests/zapwallettxes.py @@ -29,7 +29,7 @@ class ZapWalletTXesTest (BitcoinTestFramework): self.nodes[1].generate(101) self.sync_all() - assert_equal(self.nodes[0].getbalance(), 21000000) + assert_equal(self.nodes[0].getbalance()["bitcoin"], 21000000) txid0 = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11) txid1 = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 10) diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index 7028e0e9cc..292b5284da 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -26,7 +26,7 @@ using namespace std; -extern UniValue PushAssetBalance(CAmountMap& balance, CWallet* wallet, std::string& strasset); +UniValue PushAssetBalance(CAmountMap& balance, CWallet* wallet, std::string strasset); /** * @note Do not add or change anything in the information returned by this @@ -91,7 +91,7 @@ UniValue getinfo(const UniValue& params, bool fHelp) if (pwalletMain) { obj.push_back(Pair("walletversion", pwalletMain->GetVersion())); CAmountMap balance = pwalletMain->GetBalance(); - std::string strasset = "bitcoin"; + std::string strasset = ""; if (params.size() > 0) { strasset = params[0].get_str(); } diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index dca8ff3390..05fcc7e2d3 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -64,21 +64,21 @@ void EnsureWalletIsUnlocked() } // Attaches labeled balance reports to UniValue obj with asset filter -// "*" displays *all* assets as VOBJ pairs, while named assets must have +// "" displays *all* assets as VOBJ pairs, while named assets must have // been entered via addassetlabel RPC command and are returns as VNUM. -UniValue PushAssetBalance(CAmountMap& balance, CWallet* wallet, std::string& strasset) +UniValue PushAssetBalance(CAmountMap& balance, CWallet* wallet, std::string strasset) { UniValue obj(UniValue::VOBJ); CAsset id = wallet->GetAssetFromLabel(strasset); std::string label = wallet->GetLabelFromAsset(CAsset(uint256S(strasset))); - if (strasset != "*" && (id.IsNull() && label == "")) { + if (strasset != "" && (id.IsNull() && label == "")) { throw JSONRPCError(RPC_WALLET_ERROR, "Input does not match a known asset tag/label pair."); } else if (!id.IsNull()) { strasset = id.GetHex(); } - if (strasset == "*") { + if (strasset == "") { for(std::map::const_iterator it = balance.begin(); it != balance.end(); ++it) { // Unknown assets if (it->first.IsNull()) @@ -630,7 +630,7 @@ UniValue getreceivedbyaddress(const UniValue& params, bool fHelp) "\nArguments:\n" "1. \"bitcoinaddress\" (string, required) The bitcoin address for transactions.\n" "2. minconf (numeric, optional, default=1) Only include transactions confirmed at least this many times.\n" - "3. \"assetlabel\" (string, optional) Hex asset id or asset label for balance. \"*\" retrieves all known asset balances.\n" + "3. \"assetlabel\" (string, optional) Hex asset id or asset label for balance.\n" "\nResult:\n" "amount (numeric) The total amount in " + CURRENCY_UNIT + " received at this address.\n" "\nExamples:\n" @@ -677,7 +677,7 @@ UniValue getreceivedbyaddress(const UniValue& params, bool fHelp) } } - std::string asset = "bitcoin"; + std::string asset = ""; if (params.size() > 2 && params[2].isStr()) { asset = params[2].get_str(); } @@ -773,8 +773,10 @@ UniValue getbalance(const UniValue& params, bool fHelp) LOCK2(cs_main, pwalletMain->cs_wallet); + CAmountMap balance = pwalletMain->GetBalance(); + if (params.size() == 0) - return ValueFromAmount(pwalletMain->GetBalance()[pwalletMain->GetAssetFromLabel("bitcoin")]); + return PushAssetBalance(balance, pwalletMain, ""); int nMinDepth = 1; if (params.size() > 1) @@ -789,7 +791,6 @@ UniValue getbalance(const UniValue& params, bool fHelp) if (params.size() > 3) { if (params[3].isStr()) { std::string assettype = params[3].get_str(); - CAmountMap balance = pwalletMain->GetBalance(); UniValue obj(UniValue::VOBJ); return PushAssetBalance(balance, pwalletMain, assettype); } @@ -799,7 +800,7 @@ UniValue getbalance(const UniValue& params, bool fHelp) // Calculate total balance a different way from GetBalance() // (GetBalance() sums up all unspent TxOuts) // getbalance and "getbalance * 1 true" should return the same number - CAmount nBalance = 0; + CAmountMap mapBalance; for (map::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it) { const CWalletTx& wtx = (*it).second; @@ -814,13 +815,13 @@ UniValue getbalance(const UniValue& params, bool fHelp) if (wtx.GetDepthInMainChain() >= nMinDepth) { BOOST_FOREACH(const COutputEntry& r, listReceived) - nBalance += r.amount; + mapBalance[r.asset] += r.amount; } BOOST_FOREACH(const COutputEntry& s, listSent) - nBalance -= s.amount; - nBalance -= allFee; + mapBalance[s.asset] -= s.amount; + mapBalance[BITCOINID] -= allFee; } - return ValueFromAmount(nBalance); + return PushAssetBalance(mapBalance, pwalletMain, ""); } string strAccount = AccountFromValue(params[0]); @@ -837,22 +838,21 @@ UniValue getunconfirmedbalance(const UniValue ¶ms, bool fHelp) if (fHelp || params.size() > 1) throw runtime_error( - "getunconfirmedbalance\n" + "getunconfirmedbalance ( asset )\n" "\nArguments:\n" - "1. \"assetlabel\" (string, optional) Hex asset id or asset label for balance. \"*\" retrieves all known asset balances.\n" + "1. \"asset\" (string, optional) Hex asset id or asset label for balance.\n" "Returns the server's total unconfirmed balance\n"); LOCK2(cs_main, pwalletMain->cs_wallet); CAmountMap balance = pwalletMain->GetUnconfirmedBalance(); + std::string strasset = ""; if (params.size() > 0) { - UniValue obj(UniValue::VOBJ); - std::string strasset = params[0].get_str(); - return PushAssetBalance(balance, pwalletMain, strasset); + strasset = params[0].get_str(); } - return ValueFromAmount(balance[pwalletMain->GetAssetFromLabel("bitcoin")]); + return PushAssetBalance(balance, pwalletMain, strasset); } @@ -1910,7 +1910,7 @@ UniValue gettransaction(const UniValue& params, bool fHelp) if(params[1].get_bool()) filter = filter | ISMINE_WATCH_ONLY; - std::string strasset = "bitcoin"; + std::string strasset = ""; if (params.size() > 2) { strasset = params[2].get_str(); } @@ -2459,7 +2459,7 @@ UniValue getwalletinfo(const UniValue& params, bool fHelp) UniValue obj(UniValue::VOBJ); obj.push_back(Pair("walletversion", pwalletMain->GetVersion())); - std::string asset = "bitcoin"; + std::string asset = ""; if (params.size() > 0 && params[0].isStr()) { asset = params[0].get_str(); } @@ -2577,12 +2577,12 @@ UniValue listunspent(const UniValue& params, bool fHelp) } } - std::string assetstr = "bitcoin"; + std::string assetstr = ""; if (params.size() > 3 && params[3].isStr()) { assetstr = params[3].get_str(); } CAsset asset; - if (assetstr != "*") { + if (assetstr != "") { asset = pwalletMain->GetAssetFromString(assetstr); if (asset.IsNull()) throw JSONRPCError(RPC_WALLET_ERROR, "Unknown or invalid asset id/label"); @@ -2609,7 +2609,7 @@ UniValue listunspent(const UniValue& params, bool fHelp) if (nValue == -1 || assetid.IsNull()) continue; - if (assetstr != "*" && asset != assetid) { + if (assetstr != "" && asset != assetid) { continue; }