implement z_getnotescount api call

This commit is contained in:
Alfredo Garcia 2020-04-19 20:22:06 -03:00
parent c2a8beadec
commit 4c40f9f514
3 changed files with 53 additions and 0 deletions

View File

@ -98,6 +98,8 @@ class ListReceivedTest (BitcoinTestFramework):
r = self.nodes[1].z_listreceivedbyaddress(zaddr1)
assert_equal(0, len(r), "Should have received no confirmed note")
c = self.nodes[1].z_getnotescount()
assert_equal(0, c[release], "Count of confirmed notes should be 0")
# No confirmation required, one note should be present
r = self.nodes[1].z_listreceivedbyaddress(zaddr1, 0)
@ -110,6 +112,9 @@ class ListReceivedTest (BitcoinTestFramework):
assert_equal(-1, r[0]['blockindex'])
assert_equal(0, r[0]['blockheight'])
c = self.nodes[1].z_getnotescount(0)
assert_equal(1, c[release], "Count of unconfirmed notes should be 1")
# Confirm transaction (1 ZEC from taddr to zaddr1)
self.generate_and_sync(height+3)
@ -215,6 +220,9 @@ class ListReceivedTest (BitcoinTestFramework):
assert_false(r[0]['change'], "Note valued at 0.6 should not be change")
assert_equal(no_memo, r[0]['memo'])
c = self.nodes[1].z_getnotescount(0)
assert_equal(3, c[release], "Count of unconfirmed notes should be 3(2 in zaddr1 + 1 in zaddr2)")
def run_test(self):
self.run_test_release('sprout', 200)
self.run_test_release('sapling', 214)

View File

@ -144,6 +144,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "z_getpaymentdisclosure", 1},
{ "z_getpaymentdisclosure", 2},
{ "z_setmigration", 0},
{ "z_getnotescount", 0},
};
class CRPCConvertTable

View File

@ -4967,6 +4967,49 @@ UniValue z_listoperationids(const UniValue& params, bool fHelp)
return ret;
}
UniValue z_getnotescount(const UniValue& params, bool fHelp)
{
if (!EnsureWalletIsAvailable(fHelp))
return NullUniValue;
if (fHelp || params.size() > 1)
throw runtime_error(
"z_getnotescount\n"
"\nArguments:\n"
"1. minconf (numeric, optional, default=1) Only include notes in transactions confirmed at least this many times.\n"
"\nReturns the number of sprout and sapling notes available in the wallet.\n"
"\nResult:\n"
"{\n"
" \"sprout\" (numeric) the number of sprout notes in the wallet\n"
" \"sapling\" (numeric) the number of sapling notes in the wallet\n"
"}\n"
"\nExamples:\n"
+ HelpExampleCli("z_getnotescount", "0")
+ HelpExampleRpc("z_getnotescount", "0")
);
LOCK2(cs_main, pwalletMain->cs_wallet);
int nMinDepth = 1;
if (params.size() > 0)
nMinDepth = params[0].get_int();
int sprout = 0;
int sapling = 0;
for (auto& wtx : pwalletMain->mapWallet) {
if (wtx.second.GetDepthInMainChain() >= nMinDepth) {
sprout += wtx.second.mapSproutNoteData.size();
sapling += wtx.second.mapSaplingNoteData.size();
}
}
UniValue ret(UniValue::VOBJ);
ret.push_back(Pair("sprout", sprout));
ret.push_back(Pair("sapling", sapling));
return ret;
}
extern UniValue dumpprivkey(const UniValue& params, bool fHelp); // in rpcdump.cpp
extern UniValue importprivkey(const UniValue& params, bool fHelp);
extern UniValue importaddress(const UniValue& params, bool fHelp);
@ -5054,6 +5097,7 @@ static const CRPCCommand commands[] =
{ "wallet", "z_exportwallet", &z_exportwallet, true },
{ "wallet", "z_importwallet", &z_importwallet, true },
{ "wallet", "z_viewtransaction", &z_viewtransaction, false },
{ "wallet", "z_getnotescount", &z_getnotescount, false },
// TODO: rearrange into another category
{ "disclosure", "z_getpaymentdisclosure", &z_getpaymentdisclosure, true },
{ "disclosure", "z_validatepaymentdisclosure", &z_validatepaymentdisclosure, true }