]> Git Repo - VerusCoin.git/blob - src/test/rpc_wallet_tests.cpp
Implementation of Overwinter transaction format ZIP 202.
[VerusCoin.git] / src / test / rpc_wallet_tests.cpp
1 // Copyright (c) 2013-2014 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5 #include "rpcserver.h"
6 #include "rpcclient.h"
7
8 #include "base58.h"
9 #include "main.h"
10 #include "wallet/wallet.h"
11
12 #include "test/test_bitcoin.h"
13
14 #include "zcash/Address.hpp"
15
16 #include "rpcserver.h"
17 #include "asyncrpcqueue.h"
18 #include "asyncrpcoperation.h"
19 #include "wallet/asyncrpcoperation_sendmany.h"
20 #include "wallet/asyncrpcoperation_shieldcoinbase.h"
21
22 #include "rpcprotocol.h"
23 #include "init.h"
24
25 #include <chrono>
26 #include <thread>
27
28 #include <fstream>
29 #include <unordered_set>
30
31 #include <boost/algorithm/string.hpp>
32 #include <boost/test/unit_test.hpp>
33 #include <boost/format.hpp>
34 #include <boost/filesystem.hpp>
35
36 #include <univalue.h>
37
38 using namespace std;
39
40 extern UniValue createArgs(int nRequired, const char* address1 = NULL, const char* address2 = NULL);
41 extern UniValue CallRPC(string args);
42
43 extern CWallet* pwalletMain;
44
45 bool find_error(const UniValue& objError, const std::string& expected) {
46     return find_value(objError, "message").get_str().find(expected) != string::npos;
47 }
48
49 BOOST_FIXTURE_TEST_SUITE(rpc_wallet_tests, TestingSetup)
50
51 BOOST_AUTO_TEST_CASE(rpc_addmultisig)
52 {
53     LOCK(pwalletMain->cs_wallet);
54
55     rpcfn_type addmultisig = tableRPC["addmultisigaddress"]->actor;
56
57     // old, 65-byte-long:
58     const char address1Hex[] = "0434e3e09f49ea168c5bbf53f877ff4206923858aab7c7e1df25bc263978107c95e35065a27ef6f1b27222db0ec97e0e895eaca603d3ee0d4c060ce3d8a00286c8";
59     // new, compressed:
60     const char address2Hex[] = "0388c2037017c62240b6b72ac1a2a5f94da790596ebd06177c8572752922165cb4";
61
62     UniValue v;
63     CBitcoinAddress address;
64     BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(1, address1Hex), false));
65     address.SetString(v.get_str());
66     BOOST_CHECK(address.IsValid() && address.IsScript());
67
68     BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(1, address1Hex, address2Hex), false));
69     address.SetString(v.get_str());
70     BOOST_CHECK(address.IsValid() && address.IsScript());
71
72     BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(2, address1Hex, address2Hex), false));
73     address.SetString(v.get_str());
74     BOOST_CHECK(address.IsValid() && address.IsScript());
75
76     BOOST_CHECK_THROW(addmultisig(createArgs(0), false), runtime_error);
77     BOOST_CHECK_THROW(addmultisig(createArgs(1), false), runtime_error);
78     BOOST_CHECK_THROW(addmultisig(createArgs(2, address1Hex), false), runtime_error);
79
80     BOOST_CHECK_THROW(addmultisig(createArgs(1, ""), false), runtime_error);
81     BOOST_CHECK_THROW(addmultisig(createArgs(1, "NotAValidPubkey"), false), runtime_error);
82
83     string short1(address1Hex, address1Hex + sizeof(address1Hex) - 2); // last byte missing
84     BOOST_CHECK_THROW(addmultisig(createArgs(2, short1.c_str()), false), runtime_error);
85
86     string short2(address1Hex + 1, address1Hex + sizeof(address1Hex)); // first byte missing
87     BOOST_CHECK_THROW(addmultisig(createArgs(2, short2.c_str()), false), runtime_error);
88 }
89
90 BOOST_AUTO_TEST_CASE(rpc_wallet)
91 {
92     // Test RPC calls for various wallet statistics
93     UniValue r;
94
95     LOCK2(cs_main, pwalletMain->cs_wallet);
96
97     CPubKey demoPubkey = pwalletMain->GenerateNewKey();
98     CBitcoinAddress demoAddress = CBitcoinAddress(CTxDestination(demoPubkey.GetID()));
99     UniValue retValue;
100     string strAccount = "";
101     string strPurpose = "receive";
102     BOOST_CHECK_NO_THROW({ /*Initialize Wallet with an account */
103         CWalletDB walletdb(pwalletMain->strWalletFile);
104         CAccount account;
105         account.vchPubKey = demoPubkey;
106         pwalletMain->SetAddressBook(account.vchPubKey.GetID(), strAccount, strPurpose);
107         walletdb.WriteAccount(strAccount, account);
108     });
109
110     CPubKey setaccountDemoPubkey = pwalletMain->GenerateNewKey();
111     CBitcoinAddress setaccountDemoAddress = CBitcoinAddress(CTxDestination(setaccountDemoPubkey.GetID()));
112
113     /*********************************
114      *                  setaccount
115      *********************************/
116     BOOST_CHECK_NO_THROW(CallRPC("setaccount " + setaccountDemoAddress.ToString() + " \"\""));
117     /* Accounts are disabled */
118     BOOST_CHECK_THROW(CallRPC("setaccount " + setaccountDemoAddress.ToString() + " nullaccount"), runtime_error);
119     /* t1VtArtnn1dGPiD2WFfMXYXW5mHM3q1GpgV is not owned by the test wallet. */
120     BOOST_CHECK_THROW(CallRPC("setaccount t1VtArtnn1dGPiD2WFfMXYXW5mHM3q1GpgV nullaccount"), runtime_error);
121     BOOST_CHECK_THROW(CallRPC("setaccount"), runtime_error);
122     /* t1VtArtnn1dGPiD2WFfMXYXW5mHM3q1Gpg (34 chars) is an illegal address (should be 35 chars) */
123     BOOST_CHECK_THROW(CallRPC("setaccount t1VtArtnn1dGPiD2WFfMXYXW5mHM3q1Gpg nullaccount"), runtime_error);
124
125
126     /*********************************
127      *                  getbalance
128      *********************************/
129     BOOST_CHECK_NO_THROW(CallRPC("getbalance"));
130     BOOST_CHECK_THROW(CallRPC("getbalance " + demoAddress.ToString()), runtime_error);
131
132     /*********************************
133      *                  listunspent
134      *********************************/
135     BOOST_CHECK_NO_THROW(CallRPC("listunspent"));
136     BOOST_CHECK_THROW(CallRPC("listunspent string"), runtime_error);
137     BOOST_CHECK_THROW(CallRPC("listunspent 0 string"), runtime_error);
138     BOOST_CHECK_THROW(CallRPC("listunspent 0 1 not_array"), runtime_error);
139     BOOST_CHECK_THROW(CallRPC("listunspent 0 1 [] extra"), runtime_error);
140     BOOST_CHECK_NO_THROW(r = CallRPC("listunspent 0 1 []"));
141     BOOST_CHECK(r.get_array().empty());
142
143     /*********************************
144      *          listreceivedbyaddress
145      *********************************/
146     BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaddress"));
147     BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaddress 0"));
148     BOOST_CHECK_THROW(CallRPC("listreceivedbyaddress not_int"), runtime_error);
149     BOOST_CHECK_THROW(CallRPC("listreceivedbyaddress 0 not_bool"), runtime_error);
150     BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaddress 0 true"));
151     BOOST_CHECK_THROW(CallRPC("listreceivedbyaddress 0 true extra"), runtime_error);
152
153     /*********************************
154      *          listreceivedbyaccount
155      *********************************/
156     BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaccount"));
157     BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaccount 0"));
158     BOOST_CHECK_THROW(CallRPC("listreceivedbyaccount not_int"), runtime_error);
159     BOOST_CHECK_THROW(CallRPC("listreceivedbyaccount 0 not_bool"), runtime_error);
160     BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaccount 0 true"));
161     BOOST_CHECK_THROW(CallRPC("listreceivedbyaccount 0 true extra"), runtime_error);
162
163     /*********************************
164      *          listsinceblock
165      *********************************/
166     BOOST_CHECK_NO_THROW(CallRPC("listsinceblock"));
167
168     /*********************************
169      *          listtransactions
170      *********************************/
171     BOOST_CHECK_NO_THROW(CallRPC("listtransactions"));
172     BOOST_CHECK_NO_THROW(CallRPC("listtransactions " + demoAddress.ToString()));
173     BOOST_CHECK_NO_THROW(CallRPC("listtransactions " + demoAddress.ToString() + " 20"));
174     BOOST_CHECK_NO_THROW(CallRPC("listtransactions " + demoAddress.ToString() + " 20 0"));
175     BOOST_CHECK_THROW(CallRPC("listtransactions " + demoAddress.ToString() + " not_int"), runtime_error);
176
177     /*********************************
178      *          listlockunspent
179      *********************************/
180     BOOST_CHECK_NO_THROW(CallRPC("listlockunspent"));
181
182     /*********************************
183      *          listaccounts
184      *********************************/
185     BOOST_CHECK_NO_THROW(CallRPC("listaccounts"));
186
187     /*********************************
188      *          listaddressgroupings
189      *********************************/
190     BOOST_CHECK_NO_THROW(CallRPC("listaddressgroupings"));
191
192     /*********************************
193      *          getrawchangeaddress
194      *********************************/
195     BOOST_CHECK_NO_THROW(CallRPC("getrawchangeaddress"));
196
197     /*********************************
198      *          getnewaddress
199      *********************************/
200     BOOST_CHECK_NO_THROW(CallRPC("getnewaddress"));
201     BOOST_CHECK_NO_THROW(CallRPC("getnewaddress \"\""));
202     /* Accounts are deprecated */
203     BOOST_CHECK_THROW(CallRPC("getnewaddress getnewaddress_demoaccount"), runtime_error);
204
205     /*********************************
206      *          getaccountaddress
207      *********************************/
208     BOOST_CHECK_NO_THROW(CallRPC("getaccountaddress \"\""));
209     /* Accounts are deprecated */
210     BOOST_CHECK_THROW(CallRPC("getaccountaddress accountThatDoesntExists"), runtime_error);
211     BOOST_CHECK_NO_THROW(retValue = CallRPC("getaccountaddress " + strAccount));
212     BOOST_CHECK(CBitcoinAddress(retValue.get_str()).Get() == demoAddress.Get());
213
214     /*********************************
215      *                  getaccount
216      *********************************/
217     BOOST_CHECK_THROW(CallRPC("getaccount"), runtime_error);
218     BOOST_CHECK_NO_THROW(CallRPC("getaccount " + demoAddress.ToString()));
219
220     /*********************************
221      *  signmessage + verifymessage
222      *********************************/
223     BOOST_CHECK_NO_THROW(retValue = CallRPC("signmessage " + demoAddress.ToString() + " mymessage"));
224     BOOST_CHECK_THROW(CallRPC("signmessage"), runtime_error);
225     /* Should throw error because this address is not loaded in the wallet */
226     BOOST_CHECK_THROW(CallRPC("signmessage t1h8SqgtM3QM5e2M8EzhhT1yL2PXXtA6oqe mymessage"), runtime_error);
227
228     /* missing arguments */
229     BOOST_CHECK_THROW(CallRPC("verifymessage " + demoAddress.ToString()), runtime_error);
230     BOOST_CHECK_THROW(CallRPC("verifymessage " + demoAddress.ToString() + " " + retValue.get_str()), runtime_error);
231     /* Illegal address */
232     BOOST_CHECK_THROW(CallRPC("verifymessage t1VtArtnn1dGPiD2WFfMXYXW5mHM3q1Gpg " + retValue.get_str() + " mymessage"), runtime_error);
233     /* wrong address */
234     BOOST_CHECK(CallRPC("verifymessage t1VtArtnn1dGPiD2WFfMXYXW5mHM3q1GpgV " + retValue.get_str() + " mymessage").get_bool() == false);
235     /* Correct address and signature but wrong message */
236     BOOST_CHECK(CallRPC("verifymessage " + demoAddress.ToString() + " " + retValue.get_str() + " wrongmessage").get_bool() == false);
237     /* Correct address, message and signature*/
238     BOOST_CHECK(CallRPC("verifymessage " + demoAddress.ToString() + " " + retValue.get_str() + " mymessage").get_bool() == true);
239
240     /*********************************
241      *          getaddressesbyaccount
242      *********************************/
243     BOOST_CHECK_THROW(CallRPC("getaddressesbyaccount"), runtime_error);
244     BOOST_CHECK_NO_THROW(retValue = CallRPC("getaddressesbyaccount " + strAccount));
245     UniValue arr = retValue.get_array();
246     BOOST_CHECK_EQUAL(4, arr.size());
247     bool notFound = true;
248     for (auto a : arr.getValues()) {
249         notFound &= CBitcoinAddress(a.get_str()).Get() != demoAddress.Get();
250     }
251     BOOST_CHECK(!notFound);
252
253     /*********************************
254      *       fundrawtransaction
255      *********************************/
256     BOOST_CHECK_THROW(CallRPC("fundrawtransaction 28z"), runtime_error);
257     BOOST_CHECK_THROW(CallRPC("fundrawtransaction 01000000000180969800000000001976a91450ce0a4b0ee0ddeb633da85199728b940ac3fe9488ac00000000"), runtime_error);
258
259     /*
260      * getblocksubsidy
261      */
262     BOOST_CHECK_THROW(CallRPC("getblocksubsidy too many args"), runtime_error);
263     BOOST_CHECK_THROW(CallRPC("getblocksubsidy -1"), runtime_error);
264     BOOST_CHECK_NO_THROW(retValue = CallRPC("getblocksubsidy 50000"));
265     UniValue obj = retValue.get_obj();
266     BOOST_CHECK_EQUAL(find_value(obj, "miner").get_real(), 10.0);
267     BOOST_CHECK_EQUAL(find_value(obj, "founders").get_real(), 2.5);
268     BOOST_CHECK_NO_THROW(retValue = CallRPC("getblocksubsidy 1000000"));
269     obj = retValue.get_obj();
270     BOOST_CHECK_EQUAL(find_value(obj, "miner").get_real(), 6.25);
271     BOOST_CHECK_EQUAL(find_value(obj, "founders").get_real(), 0.0);
272     BOOST_CHECK_NO_THROW(retValue = CallRPC("getblocksubsidy 2000000"));
273     obj = retValue.get_obj();
274     BOOST_CHECK_EQUAL(find_value(obj, "miner").get_real(), 3.125);
275     BOOST_CHECK_EQUAL(find_value(obj, "founders").get_real(), 0.0);
276
277     /*
278      * getblock
279      */
280     BOOST_CHECK_THROW(CallRPC("getblock too many args"), runtime_error);
281     BOOST_CHECK_THROW(CallRPC("getblock -1"), runtime_error);
282     BOOST_CHECK_THROW(CallRPC("getblock 2147483647"), runtime_error); // allowed, but > height of active chain tip
283     BOOST_CHECK_THROW(CallRPC("getblock 2147483648"), runtime_error); // not allowed, > int32 used for nHeight
284     BOOST_CHECK_THROW(CallRPC("getblock 100badchars"), runtime_error);
285     BOOST_CHECK_NO_THROW(CallRPC("getblock 0"));
286 }
287
288 BOOST_AUTO_TEST_CASE(rpc_wallet_getbalance)
289 {
290     SelectParams(CBaseChainParams::TESTNET);
291
292     LOCK(pwalletMain->cs_wallet);
293
294
295     BOOST_CHECK_THROW(CallRPC("z_getbalance too many args"), runtime_error);
296     BOOST_CHECK_THROW(CallRPC("z_getbalance invalidaddress"), runtime_error);
297     BOOST_CHECK_NO_THROW(CallRPC("z_getbalance tmC6YZnCUhm19dEXxh3Jb7srdBJxDawaCab"));
298     BOOST_CHECK_THROW(CallRPC("z_getbalance tmC6YZnCUhm19dEXxh3Jb7srdBJxDawaCab -1"), runtime_error);
299     BOOST_CHECK_NO_THROW(CallRPC("z_getbalance tmC6YZnCUhm19dEXxh3Jb7srdBJxDawaCab 0"));
300     BOOST_CHECK_THROW(CallRPC("z_getbalance tnRZ8bPq2pff3xBWhTJhNkVUkm2uhzksDeW5PvEa7aFKGT9Qi3YgTALZfjaY4jU3HLVKBtHdSXxoPoLA3naMPcHBcY88FcF 1"), runtime_error);
301
302
303     BOOST_CHECK_THROW(CallRPC("z_gettotalbalance too manyargs"), runtime_error);
304     BOOST_CHECK_THROW(CallRPC("z_gettotalbalance -1"), runtime_error);
305     BOOST_CHECK_NO_THROW(CallRPC("z_gettotalbalance 0"));
306
307
308     BOOST_CHECK_THROW(CallRPC("z_listreceivedbyaddress too many args"), runtime_error);
309     // negative minconf not allowed
310     BOOST_CHECK_THROW(CallRPC("z_listreceivedbyaddress tmC6YZnCUhm19dEXxh3Jb7srdBJxDawaCab -1"), runtime_error);
311     // invalid zaddr, taddr not allowed
312     BOOST_CHECK_THROW(CallRPC("z_listreceivedbyaddress tmC6YZnCUhm19dEXxh3Jb7srdBJxDawaCab 0"), runtime_error);
313     // don't have the spending key
314     BOOST_CHECK_THROW(CallRPC("z_listreceivedbyaddress tnRZ8bPq2pff3xBWhTJhNkVUkm2uhzksDeW5PvEa7aFKGT9Qi3YgTALZfjaY4jU3HLVKBtHdSXxoPoLA3naMPcHBcY88FcF 1"), runtime_error);
315 }
316
317 /**
318  * This test covers RPC command z_validateaddress
319  */
320 BOOST_AUTO_TEST_CASE(rpc_wallet_z_validateaddress)
321 {
322     SelectParams(CBaseChainParams::MAIN);
323
324     LOCK2(cs_main, pwalletMain->cs_wallet);
325
326     UniValue retValue;
327
328     // Check number of args
329     BOOST_CHECK_THROW(CallRPC("z_validateaddress"), runtime_error);
330     BOOST_CHECK_THROW(CallRPC("z_validateaddress toomany args"), runtime_error);
331
332     // Wallet should be empty
333     std::set<libzcash::PaymentAddress> addrs;
334     pwalletMain->GetPaymentAddresses(addrs);
335     BOOST_CHECK(addrs.size()==0);
336
337     // This address is not valid, it belongs to another network
338     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_validateaddress ztaaga95QAPyp1kSQ1hD2kguCpzyMHjxWZqaYDEkzbvo7uYQYAw2S8X4Kx98AvhhofMtQL8PAXKHuZsmhRcanavKRKmdCzk"));
339     UniValue resultObj = retValue.get_obj();
340     bool b = find_value(resultObj, "isvalid").get_bool();
341     BOOST_CHECK_EQUAL(b, false);
342
343     // This address is valid, but the spending key is not in this wallet
344     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_validateaddress zcfA19SDAKRYHLoRDoShcoz4nPohqWxuHcqg8WAxsiB2jFrrs6k7oSvst3UZvMYqpMNSRBkxBsnyjjngX5L55FxMzLKach8"));
345     resultObj = retValue.get_obj();
346     b = find_value(resultObj, "isvalid").get_bool();
347     BOOST_CHECK_EQUAL(b, true);
348     b = find_value(resultObj, "ismine").get_bool();
349     BOOST_CHECK_EQUAL(b, false);
350
351     // Let's import a spending key to the wallet and validate its payment address
352     BOOST_CHECK_NO_THROW(CallRPC("z_importkey SKxoWv77WGwFnUJitQKNEcD636bL4X5Gd6wWmgaA4Q9x8jZBPJXT"));
353     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_validateaddress zcWsmqT4X2V4jgxbgiCzyrAfRT1vi1F4sn7M5Pkh66izzw8Uk7LBGAH3DtcSMJeUb2pi3W4SQF8LMKkU2cUuVP68yAGcomL"));
354     resultObj = retValue.get_obj();
355     b = find_value(resultObj, "isvalid").get_bool();
356     BOOST_CHECK_EQUAL(b, true);
357     b = find_value(resultObj, "ismine").get_bool();
358     BOOST_CHECK_EQUAL(b, true);
359     BOOST_CHECK_EQUAL(find_value(resultObj, "payingkey").get_str(), "f5bb3c888ccc9831e3f6ba06e7528e26a312eec3acc1823be8918b6a3a5e20ad");
360     BOOST_CHECK_EQUAL(find_value(resultObj, "transmissionkey").get_str(), "7a58c7132446564e6b810cf895c20537b3528357dc00150a8e201f491efa9c1a");
361 }
362
363 /*
364  * This test covers RPC command z_exportwallet
365  */
366 BOOST_AUTO_TEST_CASE(rpc_wallet_z_exportwallet)
367 {
368     LOCK2(cs_main, pwalletMain->cs_wallet);
369
370     // wallet should be empty
371     std::set<libzcash::PaymentAddress> addrs;
372     pwalletMain->GetPaymentAddresses(addrs);
373     BOOST_CHECK(addrs.size()==0);
374
375     // wallet should have one key
376     CZCPaymentAddress paymentAddress = pwalletMain->GenerateNewZKey();
377     pwalletMain->GetPaymentAddresses(addrs);
378     BOOST_CHECK(addrs.size()==1);
379
380     // Set up paths
381     boost::filesystem::path tmppath = boost::filesystem::temp_directory_path();
382     boost::filesystem::path tmpfilename = boost::filesystem::unique_path("%%%%%%%%");
383     boost::filesystem::path exportfilepath = tmppath / tmpfilename;
384
385     // export will fail since exportdir is not set
386     BOOST_CHECK_THROW(CallRPC(string("z_exportwallet ") + tmpfilename.string()), runtime_error);
387
388     // set exportdir
389     mapArgs["-exportdir"] = tmppath.native();
390
391     // run some tests
392     BOOST_CHECK_THROW(CallRPC("z_exportwallet"), runtime_error);
393
394     BOOST_CHECK_THROW(CallRPC("z_exportwallet toomany args"), runtime_error);
395
396     BOOST_CHECK_THROW(CallRPC(string("z_exportwallet invalid!*/_chars.txt")), runtime_error);
397
398     BOOST_CHECK_NO_THROW(CallRPC(string("z_exportwallet ") + tmpfilename.string()));
399
400
401     auto addr = paymentAddress.Get();
402     libzcash::SpendingKey key;
403     BOOST_CHECK(pwalletMain->GetSpendingKey(addr, key));
404
405     std::string s1 = paymentAddress.ToString();
406     std::string s2 = CZCSpendingKey(key).ToString();
407
408     // There's no way to really delete a private key so we will read in the
409     // exported wallet file and search for the spending key and payment address.
410
411     EnsureWalletIsUnlocked();
412
413     ifstream file;
414     file.open(exportfilepath.string().c_str(), std::ios::in | std::ios::ate);
415     BOOST_CHECK(file.is_open());
416     bool fVerified = false;
417     int64_t nFilesize = std::max((int64_t)1, (int64_t)file.tellg());
418     file.seekg(0, file.beg);
419     while (file.good()) {
420         std::string line;
421         std::getline(file, line);
422         if (line.empty() || line[0] == '#')
423             continue;
424         if (line.find(s1) != std::string::npos && line.find(s2) != std::string::npos) {
425             fVerified = true;
426             break;
427         }
428     }
429     BOOST_CHECK(fVerified);
430 }
431
432
433 /*
434  * This test covers RPC command z_importwallet
435  */
436 BOOST_AUTO_TEST_CASE(rpc_wallet_z_importwallet)
437 {
438     LOCK2(cs_main, pwalletMain->cs_wallet);
439
440     // error if no args
441     BOOST_CHECK_THROW(CallRPC("z_importwallet"), runtime_error);
442
443     // error if too many args
444     BOOST_CHECK_THROW(CallRPC("z_importwallet toomany args"), runtime_error);
445
446     // create a random key locally
447     auto testSpendingKey = libzcash::SpendingKey::random();
448     auto testPaymentAddress = testSpendingKey.address();
449     std::string testAddr = CZCPaymentAddress(testPaymentAddress).ToString();
450     std::string testKey = CZCSpendingKey(testSpendingKey).ToString();
451
452     // create test data using the random key
453     std::string format_str = "# Wallet dump created by Zcash v0.11.2.0.z8-9155cc6-dirty (2016-08-11 11:37:00 -0700)\n"
454             "# * Created on 2016-08-12T21:55:36Z\n"
455             "# * Best block at time of backup was 0 (0de0a3851fef2d433b9b4f51d4342bdd24c5ddd793eb8fba57189f07e9235d52),\n"
456             "#   mined on 2009-01-03T18:15:05Z\n"
457             "\n"
458             "# Zkeys\n"
459             "\n"
460             "%s 2016-08-12T21:55:36Z # zaddr=%s\n"
461             "\n"
462             "\n# End of dump";
463
464     boost::format formatobject(format_str);
465     std::string testWalletDump = (formatobject % testKey % testAddr).str();
466
467     // write test data to file
468     boost::filesystem::path temp = boost::filesystem::temp_directory_path() /
469             boost::filesystem::unique_path();
470     const std::string path = temp.native();
471     std::ofstream file(path);
472     file << testWalletDump;
473     file << std::flush;
474
475     // wallet should currently be empty
476     std::set<libzcash::PaymentAddress> addrs;
477     pwalletMain->GetPaymentAddresses(addrs);
478     BOOST_CHECK(addrs.size()==0);
479
480     // import test data from file into wallet
481     BOOST_CHECK_NO_THROW(CallRPC(string("z_importwallet ") + path));
482
483     // wallet should now have one zkey
484     pwalletMain->GetPaymentAddresses(addrs);
485     BOOST_CHECK(addrs.size()==1);
486
487     // check that we have the spending key for the address
488     CZCPaymentAddress address(testAddr);
489     auto addr = address.Get();
490     BOOST_CHECK(pwalletMain->HaveSpendingKey(addr));
491
492     // Verify the spending key is the same as the test data
493     libzcash::SpendingKey k;
494     BOOST_CHECK(pwalletMain->GetSpendingKey(addr, k));
495     CZCSpendingKey spendingkey(k);
496     BOOST_CHECK_EQUAL(testKey, spendingkey.ToString());
497 }
498
499
500 /*
501  * This test covers RPC commands z_listaddresses, z_importkey, z_exportkey
502  */
503 BOOST_AUTO_TEST_CASE(rpc_wallet_z_importexport)
504 {
505     LOCK2(cs_main, pwalletMain->cs_wallet);
506     UniValue retValue;
507     int n1 = 1000; // number of times to import/export
508     int n2 = 1000; // number of addresses to create and list
509
510     // error if no args
511     BOOST_CHECK_THROW(CallRPC("z_importkey"), runtime_error);
512     BOOST_CHECK_THROW(CallRPC("z_exportkey"), runtime_error);
513
514     // error if too many args
515     BOOST_CHECK_THROW(CallRPC("z_importkey way too many args"), runtime_error);
516     BOOST_CHECK_THROW(CallRPC("z_exportkey toomany args"), runtime_error);
517
518     // error if invalid args
519     auto sk = libzcash::SpendingKey::random();
520     std::string prefix = std::string("z_importkey ") + CZCSpendingKey(sk).ToString() + " yes ";
521     BOOST_CHECK_THROW(CallRPC(prefix + "-1"), runtime_error);
522     BOOST_CHECK_THROW(CallRPC(prefix + "2147483647"), runtime_error); // allowed, but > height of active chain tip
523     BOOST_CHECK_THROW(CallRPC(prefix + "2147483648"), runtime_error); // not allowed, > int32 used for nHeight
524     BOOST_CHECK_THROW(CallRPC(prefix + "100badchars"), runtime_error);
525
526     // wallet should currently be empty
527     std::set<libzcash::PaymentAddress> addrs;
528     pwalletMain->GetPaymentAddresses(addrs);
529     BOOST_CHECK(addrs.size()==0);
530
531     // verify import and export key
532     for (int i = 0; i < n1; i++) {
533         // create a random key locally
534         auto testSpendingKey = libzcash::SpendingKey::random();
535         auto testPaymentAddress = testSpendingKey.address();
536         std::string testAddr = CZCPaymentAddress(testPaymentAddress).ToString();
537         std::string testKey = CZCSpendingKey(testSpendingKey).ToString();
538         BOOST_CHECK_NO_THROW(CallRPC(string("z_importkey ") + testKey));
539         BOOST_CHECK_NO_THROW(retValue = CallRPC(string("z_exportkey ") + testAddr));
540         BOOST_CHECK_EQUAL(retValue.get_str(), testKey);
541     }
542
543     // Verify we can list the keys imported
544     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_listaddresses"));
545     UniValue arr = retValue.get_array();
546     BOOST_CHECK(arr.size() == n1);
547
548     // Put addresses into a set
549     std::unordered_set<std::string> myaddrs;
550     for (UniValue element : arr.getValues()) {
551         myaddrs.insert(element.get_str());
552     }
553
554     // Make new addresses for the set
555     for (int i=0; i<n2; i++) {
556         myaddrs.insert((pwalletMain->GenerateNewZKey()).ToString());
557     }
558
559     // Verify number of addresses stored in wallet is n1+n2
560     int numAddrs = myaddrs.size();
561     BOOST_CHECK(numAddrs == n1+n2);
562     pwalletMain->GetPaymentAddresses(addrs);
563     BOOST_CHECK(addrs.size()==numAddrs);
564
565     // Ask wallet to list addresses
566     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_listaddresses"));
567     arr = retValue.get_array();
568     BOOST_CHECK(arr.size() == numAddrs);
569
570     // Create a set from them
571     std::unordered_set<std::string> listaddrs;
572     for (UniValue element : arr.getValues()) {
573         listaddrs.insert(element.get_str());
574     }
575
576     // Verify the two sets of addresses are the same
577     BOOST_CHECK(listaddrs.size() == numAddrs);
578     BOOST_CHECK(myaddrs == listaddrs);
579
580     // Add one more address
581     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_getnewaddress"));
582     std::string newaddress = retValue.get_str();
583     CZCPaymentAddress pa(newaddress);
584     auto newAddr = pa.Get();
585     BOOST_CHECK(pwalletMain->HaveSpendingKey(newAddr));
586
587     // Check if too many args
588     BOOST_CHECK_THROW(CallRPC("z_getnewaddress toomanyargs"), runtime_error);
589 }
590
591
592
593 /**
594  * Test Async RPC operations.
595  * Tip: Create mock operations by subclassing AsyncRPCOperation.
596  */
597
598 class MockSleepOperation : public AsyncRPCOperation {
599 public:
600     std::chrono::milliseconds naptime;
601     MockSleepOperation(int t=1000) {
602         this->naptime = std::chrono::milliseconds(t);
603     }
604     virtual ~MockSleepOperation() {
605     }
606     virtual void main() {
607         set_state(OperationStatus::EXECUTING);
608         start_execution_clock();
609         std::this_thread::sleep_for(std::chrono::milliseconds(naptime));
610         stop_execution_clock();
611         set_result(UniValue(UniValue::VSTR, "done"));
612         set_state(OperationStatus::SUCCESS);
613     }
614 };
615
616
617 /*
618  * Test Aysnc RPC queue and operations.
619  */
620 BOOST_AUTO_TEST_CASE(rpc_wallet_async_operations)
621 {
622     std::shared_ptr<AsyncRPCQueue> q = std::make_shared<AsyncRPCQueue>();
623     BOOST_CHECK(q->getNumberOfWorkers() == 0);
624     std::vector<AsyncRPCOperationId> ids = q->getAllOperationIds();
625     BOOST_CHECK(ids.size()==0);
626
627     std::shared_ptr<AsyncRPCOperation> op1 = std::make_shared<AsyncRPCOperation>();
628     q->addOperation(op1);
629     BOOST_CHECK(q->getOperationCount() == 1);
630
631     OperationStatus status = op1->getState();
632     BOOST_CHECK(status == OperationStatus::READY);
633
634     AsyncRPCOperationId id1 = op1->getId();
635     int64_t creationTime1 = op1->getCreationTime();
636
637     q->addWorker();
638     BOOST_CHECK(q->getNumberOfWorkers() == 1);
639
640     // an AsyncRPCOperation doesn't do anything so will finish immediately
641     std::this_thread::sleep_for(std::chrono::seconds(1));
642     BOOST_CHECK(q->getOperationCount() == 0);
643
644     // operation should be a success
645     BOOST_CHECK_EQUAL(op1->isCancelled(), false);
646     BOOST_CHECK_EQUAL(op1->isExecuting(), false);
647     BOOST_CHECK_EQUAL(op1->isReady(), false);
648     BOOST_CHECK_EQUAL(op1->isFailed(), false);
649     BOOST_CHECK_EQUAL(op1->isSuccess(), true);
650     BOOST_CHECK_EQUAL(op1->getError().isNull(), true);
651     BOOST_CHECK_EQUAL(op1->getResult().isNull(), false);
652     BOOST_CHECK_EQUAL(op1->getStateAsString(), "success");
653     BOOST_CHECK_NE(op1->getStateAsString(), "executing");
654
655     // Create a second operation which just sleeps
656     std::shared_ptr<AsyncRPCOperation> op2(new MockSleepOperation(2500));
657     AsyncRPCOperationId id2 = op2->getId();
658     int64_t creationTime2 = op2->getCreationTime();
659
660     // it's different from the previous operation
661     BOOST_CHECK_NE(op1.get(), op2.get());
662     BOOST_CHECK_NE(id1, id2);
663     BOOST_CHECK_NE(creationTime1, creationTime2);
664
665     // Only the first operation has been added to the queue
666     std::vector<AsyncRPCOperationId> v = q->getAllOperationIds();
667     std::set<AsyncRPCOperationId> opids(v.begin(), v.end());
668     BOOST_CHECK(opids.size() == 1);
669     BOOST_CHECK(opids.count(id1)==1);
670     BOOST_CHECK(opids.count(id2)==0);
671     std::shared_ptr<AsyncRPCOperation> p1 = q->getOperationForId(id1);
672     BOOST_CHECK_EQUAL(p1.get(), op1.get());
673     std::shared_ptr<AsyncRPCOperation> p2 = q->getOperationForId(id2);
674     BOOST_CHECK(!p2); // null ptr as not added to queue yet
675
676     // Add operation 2 and 3 to the queue
677     q->addOperation(op2);
678     std::shared_ptr<AsyncRPCOperation> op3(new MockSleepOperation(1000));
679     q->addOperation(op3);
680     std::this_thread::sleep_for(std::chrono::milliseconds(500));
681     BOOST_CHECK_EQUAL(op2->isExecuting(), true);
682     op2->cancel();  // too late, already executing
683     op3->cancel();
684     std::this_thread::sleep_for(std::chrono::milliseconds(3000));
685     BOOST_CHECK_EQUAL(op2->isSuccess(), true);
686     BOOST_CHECK_EQUAL(op2->isCancelled(), false);
687     BOOST_CHECK_EQUAL(op3->isCancelled(), true);
688
689
690     v = q->getAllOperationIds();
691     std::copy( v.begin(), v.end(), std::inserter( opids, opids.end() ) );
692     BOOST_CHECK(opids.size() == 3);
693     BOOST_CHECK(opids.count(id1)==1);
694     BOOST_CHECK(opids.count(id2)==1);
695     BOOST_CHECK(opids.count(op3->getId())==1);
696     q->finishAndWait();
697 }
698
699
700 // The CountOperation will increment this global
701 std::atomic<int64_t> gCounter(0);
702
703 class CountOperation : public AsyncRPCOperation {
704 public:
705     CountOperation() {}
706     virtual ~CountOperation() {}
707     virtual void main() {
708         set_state(OperationStatus::EXECUTING);
709         gCounter++;
710         std::this_thread::sleep_for(std::chrono::milliseconds(1000));
711         set_state(OperationStatus::SUCCESS);
712     }
713 };
714
715 // This tests the queue waiting for multiple workers to finish
716 BOOST_AUTO_TEST_CASE(rpc_wallet_async_operations_parallel_wait)
717 {
718     gCounter = 0;
719
720     std::shared_ptr<AsyncRPCQueue> q = std::make_shared<AsyncRPCQueue>();
721     q->addWorker();
722     q->addWorker();
723     q->addWorker();
724     q->addWorker();
725     BOOST_CHECK(q->getNumberOfWorkers() == 4);
726
727     int64_t numOperations = 10;     // 10 * 1000ms / 4 = 2.5 secs to finish
728     for (int i=0; i<numOperations; i++) {
729         std::shared_ptr<AsyncRPCOperation> op(new CountOperation());
730         q->addOperation(op);
731     }
732
733     std::vector<AsyncRPCOperationId> ids = q->getAllOperationIds();
734     BOOST_CHECK(ids.size()==numOperations);
735     q->finishAndWait();
736     BOOST_CHECK_EQUAL(q->isFinishing(), true);
737     BOOST_CHECK_EQUAL(numOperations, gCounter.load());
738 }
739
740 // This tests the queue shutting down immediately
741 BOOST_AUTO_TEST_CASE(rpc_wallet_async_operations_parallel_cancel)
742 {
743     gCounter = 0;
744
745     std::shared_ptr<AsyncRPCQueue> q = std::make_shared<AsyncRPCQueue>();
746     q->addWorker();
747     q->addWorker();
748     BOOST_CHECK(q->getNumberOfWorkers() == 2);
749
750     int numOperations = 10000;  // 10000 seconds to complete
751     for (int i=0; i<numOperations; i++) {
752         std::shared_ptr<AsyncRPCOperation> op(new CountOperation());
753         q->addOperation(op);
754     }
755     std::vector<AsyncRPCOperationId> ids = q->getAllOperationIds();
756     BOOST_CHECK(ids.size()==numOperations);
757     q->closeAndWait();
758
759     int numSuccess = 0;
760     int numCancelled = 0;
761     for (auto & id : ids) {
762         std::shared_ptr<AsyncRPCOperation> ptr = q->popOperationForId(id);
763         if (ptr->isCancelled()) {
764             numCancelled++;
765         } else if (ptr->isSuccess()) {
766             numSuccess++;
767         }
768     }
769
770     BOOST_CHECK_EQUAL(numOperations, numSuccess+numCancelled);
771     BOOST_CHECK_EQUAL(gCounter.load(), numSuccess);
772     BOOST_CHECK(q->getOperationCount() == 0);
773     ids = q->getAllOperationIds();
774     BOOST_CHECK(ids.size()==0);
775 }
776
777 // This tests z_getoperationstatus, z_getoperationresult, z_listoperationids
778 BOOST_AUTO_TEST_CASE(rpc_z_getoperations)
779 {
780     std::shared_ptr<AsyncRPCQueue> q = getAsyncRPCQueue();
781     std::shared_ptr<AsyncRPCQueue> sharedInstance = AsyncRPCQueue::sharedInstance();
782     BOOST_CHECK(q == sharedInstance);
783
784     BOOST_CHECK_NO_THROW(CallRPC("z_getoperationstatus"));
785     BOOST_CHECK_NO_THROW(CallRPC("z_getoperationstatus []"));
786     BOOST_CHECK_NO_THROW(CallRPC("z_getoperationstatus [\"opid-1234\"]"));
787     BOOST_CHECK_THROW(CallRPC("z_getoperationstatus [] toomanyargs"), runtime_error);
788     BOOST_CHECK_THROW(CallRPC("z_getoperationstatus not_an_array"), runtime_error);
789
790     BOOST_CHECK_NO_THROW(CallRPC("z_getoperationresult"));
791     BOOST_CHECK_NO_THROW(CallRPC("z_getoperationresult []"));
792     BOOST_CHECK_NO_THROW(CallRPC("z_getoperationresult [\"opid-1234\"]"));
793     BOOST_CHECK_THROW(CallRPC("z_getoperationresult [] toomanyargs"), runtime_error);
794     BOOST_CHECK_THROW(CallRPC("z_getoperationresult not_an_array"), runtime_error);
795
796     std::shared_ptr<AsyncRPCOperation> op1 = std::make_shared<AsyncRPCOperation>();
797     q->addOperation(op1);
798     std::shared_ptr<AsyncRPCOperation> op2 = std::make_shared<AsyncRPCOperation>();
799     q->addOperation(op2);
800
801     BOOST_CHECK(q->getOperationCount() == 2);
802     BOOST_CHECK(q->getNumberOfWorkers() == 0);
803     q->addWorker();
804     BOOST_CHECK(q->getNumberOfWorkers() == 1);
805     std::this_thread::sleep_for(std::chrono::milliseconds(1000));
806     BOOST_CHECK(q->getOperationCount() == 0);
807
808     // Check if too many args
809     BOOST_CHECK_THROW(CallRPC("z_listoperationids toomany args"), runtime_error);
810
811     UniValue retValue;
812     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_listoperationids"));
813     BOOST_CHECK(retValue.get_array().size() == 2);
814
815     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_getoperationstatus"));
816     UniValue array = retValue.get_array();
817     BOOST_CHECK(array.size() == 2);
818
819     // idempotent
820     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_getoperationstatus"));
821     array = retValue.get_array();
822     BOOST_CHECK(array.size() == 2);
823
824     for (UniValue v : array.getValues()) {
825         UniValue obj = v.get_obj();
826         UniValue id = find_value(obj, "id");
827
828         UniValue result;
829         // removes result from internal storage
830         BOOST_CHECK_NO_THROW(result = CallRPC("z_getoperationresult [\"" + id.get_str() + "\"]"));
831         UniValue resultArray = result.get_array();
832         BOOST_CHECK(resultArray.size() == 1);
833
834         UniValue resultObj = resultArray[0].get_obj();
835         UniValue resultId = find_value(resultObj, "id");
836         BOOST_CHECK_EQUAL(id.get_str(), resultId.get_str());
837
838         // verify the operation has been removed
839         BOOST_CHECK_NO_THROW(result = CallRPC("z_getoperationresult [\"" + id.get_str() + "\"]"));
840         resultArray = result.get_array();
841         BOOST_CHECK(resultArray.size() == 0);
842     }
843
844     // operations removed
845     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_getoperationstatus"));
846     array = retValue.get_array();
847     BOOST_CHECK(array.size() == 0);
848
849     q->close();
850 }
851
852 BOOST_AUTO_TEST_CASE(rpc_z_sendmany_parameters)
853 {
854     SelectParams(CBaseChainParams::TESTNET);
855
856     LOCK(pwalletMain->cs_wallet);
857
858     BOOST_CHECK_THROW(CallRPC("z_sendmany"), runtime_error);
859     BOOST_CHECK_THROW(CallRPC("z_sendmany toofewargs"), runtime_error);
860     BOOST_CHECK_THROW(CallRPC("z_sendmany just too many args here"), runtime_error);
861
862     // bad from address
863     BOOST_CHECK_THROW(CallRPC("z_sendmany "
864             "INVALIDtmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ []"), runtime_error);
865     // empty amounts
866     BOOST_CHECK_THROW(CallRPC("z_sendmany "
867             "tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ []"), runtime_error);
868
869     // don't have the spending key for this address
870     BOOST_CHECK_THROW(CallRPC("z_sendmany "
871             "tnpoQJVnYBZZqkFadj2bJJLThNCxbADGB5gSGeYTAGGrT5tejsxY9Zc1BtY8nnHmZkB"
872             "UkJ1oSfbhTJhm72WiZizvkZz5aH1 []"), runtime_error);
873
874     // duplicate address
875     BOOST_CHECK_THROW(CallRPC("z_sendmany "
876             "tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ "
877             "[{\"address\":\"tmQP9L3s31cLsghVYf2Jb5MhKj1jRBPoeQn\", \"amount\":50.0},"
878             " {\"address\":\"tmQP9L3s31cLsghVYf2Jb5MhKj1jRBPoeQn\", \"amount\":12.0} ]"
879             ), runtime_error);
880
881     // invalid fee amount, cannot be negative
882     BOOST_CHECK_THROW(CallRPC("z_sendmany "
883             "tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ "
884             "[{\"address\":\"tmQP9L3s31cLsghVYf2Jb5MhKj1jRBPoeQn\", \"amount\":50.0}] "
885             "1 -0.0001"
886             ), runtime_error);
887
888     // invalid fee amount, bigger than MAX_MONEY
889     BOOST_CHECK_THROW(CallRPC("z_sendmany "
890             "tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ "
891             "[{\"address\":\"tmQP9L3s31cLsghVYf2Jb5MhKj1jRBPoeQn\", \"amount\":50.0}] "
892             "1 21000001"
893             ), runtime_error);
894
895     // fee amount is bigger than sum of outputs
896     BOOST_CHECK_THROW(CallRPC("z_sendmany "
897             "tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ "
898             "[{\"address\":\"tmQP9L3s31cLsghVYf2Jb5MhKj1jRBPoeQn\", \"amount\":50.0}] "
899             "1 50.00000001"
900             ), runtime_error);
901
902     // memo bigger than allowed length of ZC_MEMO_SIZE
903     std::vector<char> v (2 * (ZC_MEMO_SIZE+1));     // x2 for hexadecimal string format
904     std::fill(v.begin(),v.end(), 'A');
905     std::string badmemo(v.begin(), v.end());
906     CZCPaymentAddress pa = pwalletMain->GenerateNewZKey();
907     std::string zaddr1 = pa.ToString();
908     BOOST_CHECK_THROW(CallRPC(string("z_sendmany tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ ")
909             + "[{\"address\":\"" + zaddr1 + "\", \"amount\":123.456}]"), runtime_error);
910
911     // Mutable tx containing contextual information we need to build tx
912     UniValue retValue = CallRPC("getblockcount");
913     int nHeight = retValue.get_int();
914     CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), nHeight + 1);
915     if (mtx.nVersion == 1) {
916         mtx.nVersion = 2;
917     }
918
919     // Test constructor of AsyncRPCOperation_sendmany
920     try {
921         std::shared_ptr<AsyncRPCOperation> operation(new AsyncRPCOperation_sendmany(mtx, "",{}, {}, -1));
922     } catch (const UniValue& objError) {
923         BOOST_CHECK( find_error(objError, "Minconf cannot be negative"));
924     }
925
926     try {
927         std::shared_ptr<AsyncRPCOperation> operation(new AsyncRPCOperation_sendmany(mtx, "",{}, {}, 1));
928     } catch (const UniValue& objError) {
929         BOOST_CHECK( find_error(objError, "From address parameter missing"));
930     }
931
932     try {
933         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(mtx, "tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ", {}, {}, 1) );
934     } catch (const UniValue& objError) {
935         BOOST_CHECK( find_error(objError, "No recipients"));
936     }
937
938     try {
939         std::vector<SendManyRecipient> recipients = { SendManyRecipient("dummy",1.0, "") };
940         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(mtx, "INVALID", recipients, {}, 1) );
941     } catch (const UniValue& objError) {
942         BOOST_CHECK( find_error(objError, "payment address is invalid"));
943     }
944
945     // Testnet payment addresses begin with 'zt'.  This test detects an incorrect prefix.
946     try {
947         std::vector<SendManyRecipient> recipients = { SendManyRecipient("dummy",1.0, "") };
948         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(mtx, "zcMuhvq8sEkHALuSU2i4NbNQxshSAYrpCExec45ZjtivYPbuiFPwk6WHy4SvsbeZ4siy1WheuRGjtaJmoD1J8bFqNXhsG6U", recipients, {}, 1) );
949     } catch (const UniValue& objError) {
950         BOOST_CHECK( find_error(objError, "payment address is for wrong network type"));
951     }
952
953     // Note: The following will crash as a google test because AsyncRPCOperation_sendmany
954     // invokes a method on pwalletMain, which is undefined in the google test environment.
955     try {
956         std::vector<SendManyRecipient> recipients = { SendManyRecipient("dummy",1.0, "") };
957         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(mtx, "ztjiDe569DPNbyTE6TSdJTaSDhoXEHLGvYoUnBU1wfVNU52TEyT6berYtySkd21njAeEoh8fFJUT42kua9r8EnhBaEKqCpP", recipients, {}, 1) );
958     } catch (const UniValue& objError) {
959         BOOST_CHECK( find_error(objError, "no spending key found for zaddr"));
960     }
961 }
962
963
964 // TODO: test private methods
965 BOOST_AUTO_TEST_CASE(rpc_z_sendmany_internals)
966 {
967     SelectParams(CBaseChainParams::TESTNET);
968
969     LOCK(pwalletMain->cs_wallet);
970
971     UniValue retValue;
972
973     // Mutable tx containing contextual information we need to build tx
974     retValue = CallRPC("getblockcount");
975     int nHeight = retValue.get_int();
976     CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), nHeight + 1);
977     if (mtx.nVersion == 1) {
978         mtx.nVersion = 2;
979     }
980
981     // add keys manually
982     BOOST_CHECK_NO_THROW(retValue = CallRPC("getnewaddress"));
983     std::string taddr1 = retValue.get_str();
984     CZCPaymentAddress pa = pwalletMain->GenerateNewZKey();
985     std::string zaddr1 = pa.ToString();
986
987     // there are no utxos to spend
988     {
989         std::vector<SendManyRecipient> recipients = { SendManyRecipient(zaddr1,100.0, "DEADBEEF") };
990         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(mtx, taddr1, {}, recipients, 1) );
991         operation->main();
992         BOOST_CHECK(operation->isFailed());
993         std::string msg = operation->getErrorMessage();
994         BOOST_CHECK( msg.find("Insufficient funds, no UTXOs found") != string::npos);
995     }
996
997     // minconf cannot be zero when sending from zaddr
998     {
999         try {
1000             std::vector<SendManyRecipient> recipients = {SendManyRecipient(taddr1, 100.0, "DEADBEEF")};
1001             std::shared_ptr<AsyncRPCOperation> operation(new AsyncRPCOperation_sendmany(mtx, zaddr1, recipients, {}, 0));
1002             BOOST_CHECK(false); // Fail test if an exception is not thrown
1003         } catch (const UniValue& objError) {
1004             BOOST_CHECK(find_error(objError, "Minconf cannot be zero when sending from zaddr"));
1005         }
1006     }
1007
1008
1009     // there are no unspent notes to spend
1010     {
1011         std::vector<SendManyRecipient> recipients = { SendManyRecipient(taddr1,100.0, "DEADBEEF") };
1012         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(mtx, zaddr1, recipients, {}, 1) );
1013         operation->main();
1014         BOOST_CHECK(operation->isFailed());
1015         std::string msg = operation->getErrorMessage();
1016         BOOST_CHECK( msg.find("Insufficient funds, no unspent notes") != string::npos);
1017     }
1018
1019     // get_memo_from_hex_string())
1020     {
1021         std::vector<SendManyRecipient> recipients = { SendManyRecipient(zaddr1,100.0, "DEADBEEF") };
1022         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(mtx, zaddr1, recipients, {}, 1) );
1023         std::shared_ptr<AsyncRPCOperation_sendmany> ptr = std::dynamic_pointer_cast<AsyncRPCOperation_sendmany> (operation);
1024         TEST_FRIEND_AsyncRPCOperation_sendmany proxy(ptr);
1025
1026         std::string memo = "DEADBEEF";
1027         boost::array<unsigned char, ZC_MEMO_SIZE> array = proxy.get_memo_from_hex_string(memo);
1028         BOOST_CHECK_EQUAL(array[0], 0xDE);
1029         BOOST_CHECK_EQUAL(array[1], 0xAD);
1030         BOOST_CHECK_EQUAL(array[2], 0xBE);
1031         BOOST_CHECK_EQUAL(array[3], 0xEF);
1032         for (int i=4; i<ZC_MEMO_SIZE; i++) {
1033             BOOST_CHECK_EQUAL(array[i], 0x00);  // zero padding
1034         }
1035
1036         // memo is longer than allowed
1037         std::vector<char> v (2 * (ZC_MEMO_SIZE+1));
1038         std::fill(v.begin(),v.end(), 'A');
1039         std::string bigmemo(v.begin(), v.end());
1040
1041         try {
1042             proxy.get_memo_from_hex_string(bigmemo);
1043         } catch (const UniValue& objError) {
1044             BOOST_CHECK( find_error(objError, "too big"));
1045         }
1046
1047         // invalid hexadecimal string
1048         std::fill(v.begin(),v.end(), '@'); // not a hex character
1049         std::string badmemo(v.begin(), v.end());
1050
1051         try {
1052             proxy.get_memo_from_hex_string(badmemo);
1053         } catch (const UniValue& objError) {
1054             BOOST_CHECK( find_error(objError, "hexadecimal format"));
1055         }
1056
1057         // odd length hexadecimal string
1058         std::fill(v.begin(),v.end(), 'A');
1059         v.resize(v.size() - 1);
1060         assert(v.size() %2 == 1); // odd length
1061         std::string oddmemo(v.begin(), v.end());
1062         try {
1063             proxy.get_memo_from_hex_string(oddmemo);
1064         } catch (const UniValue& objError) {
1065             BOOST_CHECK( find_error(objError, "hexadecimal format"));
1066         }
1067     }
1068
1069
1070     // add_taddr_change_output_to_tx() will append a vout to a raw transaction
1071     {
1072         std::vector<SendManyRecipient> recipients = { SendManyRecipient(zaddr1,100.0, "DEADBEEF") };
1073         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(mtx, zaddr1, recipients, {}, 1) );
1074         std::shared_ptr<AsyncRPCOperation_sendmany> ptr = std::dynamic_pointer_cast<AsyncRPCOperation_sendmany> (operation);
1075         TEST_FRIEND_AsyncRPCOperation_sendmany proxy(ptr);
1076
1077         CTransaction tx = proxy.getTx();
1078         BOOST_CHECK(tx.vout.size() == 0);
1079
1080         CAmount amount = 123.456;
1081         proxy.add_taddr_change_output_to_tx(amount);
1082         tx = proxy.getTx();
1083         BOOST_CHECK(tx.vout.size() == 1);
1084         CTxOut out = tx.vout[0];
1085         BOOST_CHECK_EQUAL(out.nValue, amount);
1086
1087         amount = 1.111;
1088         proxy.add_taddr_change_output_to_tx(amount);
1089         tx = proxy.getTx();
1090         BOOST_CHECK(tx.vout.size() == 2);
1091         out = tx.vout[1];
1092         BOOST_CHECK_EQUAL(out.nValue, amount);
1093     }
1094
1095     // add_taddr_outputs_to_tx() will append many vouts to a raw transaction
1096     {
1097         std::vector<SendManyRecipient> recipients = {
1098             SendManyRecipient("tmTGScYwiLMzHe4uGZtBYmuqoW4iEoYNMXt",CAmount(1.23), ""),
1099             SendManyRecipient("tmUSbHz3vxnwLvRyNDXbwkZxjVyDodMJEhh",CAmount(4.56), ""),
1100             SendManyRecipient("tmYZAXYPCP56Xa5JQWWPZuK7o7bfUQW6kkd",CAmount(7.89), ""),
1101         };
1102         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(mtx, zaddr1, recipients, {}, 1) );
1103         std::shared_ptr<AsyncRPCOperation_sendmany> ptr = std::dynamic_pointer_cast<AsyncRPCOperation_sendmany> (operation);
1104         TEST_FRIEND_AsyncRPCOperation_sendmany proxy(ptr);
1105
1106         proxy.add_taddr_outputs_to_tx();
1107
1108         CTransaction tx = proxy.getTx();
1109         BOOST_CHECK(tx.vout.size() == 3);
1110         BOOST_CHECK_EQUAL(tx.vout[0].nValue, CAmount(1.23));
1111         BOOST_CHECK_EQUAL(tx.vout[1].nValue, CAmount(4.56));
1112         BOOST_CHECK_EQUAL(tx.vout[2].nValue, CAmount(7.89));
1113     }
1114
1115
1116     // Raw joinsplit is a zaddr->zaddr
1117     {
1118         std::string raw = "020000000000000000000100000000000000001027000000000000183a0d4c46c369078705e39bcfebee59a978dbd210ce8de3efc9555a03fbabfd3cea16693d730c63850d7e48ccde79854c19adcb7e9dcd7b7d18805ee09083f6b16e1860729d2d4a90e2f2acd009cf78b5eb0f4a6ee4bdb64b1262d7ce9eb910c460b02022991e968d0c50ee44908e4ccccbc591d0053bcca154dd6d6fc400a29fa686af4682339832ccea362a62aeb9df0d5aa74f86a1e75ac0f48a8ccc41e0a940643c6c33e1d09223b0a46eaf47a1bb4407cfc12b1dcf83a29c0cef51e45c7876ca5b9e5bae86d92976eb3ef68f29cd29386a8be8451b50f82bf9da10c04651868655194da8f6ed3d241bb5b5ff93a3e2bbe44644544d88bcde5cc35978032ee92699c7a61fcbb395e7583f47e698c4d53ede54f956629400bf510fb5e22d03158cc10bdcaaf29e418ef18eb6480dd9c8b9e2a377809f9f32a556ef872febd0021d4ad013aa9f0b7255e98e408d302abefd33a71180b720271835b487ab309e160b06dfe51932120fb84a7ede16b20c53599a11071592109e10260f265ee60d48c62bfe24074020e9b586ce9e9356e68f2ad1a9538258234afe4b83a209f178f45202270eaeaeecaf2ce3100b2c5a714f75f35777a9ebff5ebf47059d2bbf6f3726190216468f2b152673b766225b093f3a2f827c86d6b48b42117fec1d0ac38dd7af700308dcfb02eba821612b16a2c164c47715b9b0c93900893b1aba2ea03765c94d87022db5be06ab338d1912e0936dfe87586d0a8ee49144a6cd2e306abdcb652faa3e0222739deb23154d778b50de75069a4a2cce1208cd1ced3cb4744c9888ce1c2fcd2e66dc31e62d3aa9e423d7275882525e9981f92e84ac85975b8660739407efbe1e34c2249420fde7e17db3096d5b22e83d051d01f0e6e7690dca7d168db338aadf0897fedac10de310db2b1bff762d322935dddbb60c2efb8b15d231fa17b84630371cb275c209f0c4c7d0c68b150ea5cd514122215e3f7fcfb351d69514788d67c2f3c8922581946e3a04bdf1f07f15696ca76eb95b10698bf1188fd882945c57657515889d042a6fc45d38cbc943540c4f0f6d1c45a1574c81f3e42d1eb8702328b729909adee8a5cfed7c79d54627d1fd389af941d878376f7927b9830ca659bf9ab18c5ca5192d52d02723008728d03701b8ab3e1c4a3109409ec0b13df334c7deec3523eeef4c97b5603e643de3a647b873f4c1b47fbfc6586ba66724f112e51fc93839648005043620aa3ce458e246d77977b19c53d98e3e812de006afc1a79744df236582943631d04cc02941ac4be500e4ed9fb9e3e7cc187b1c4050fad1d9d09d5fd70d5d01d615b439d8c0015d2eb10398bcdbf8c4b2bd559dbe4c288a186aed3f86f608da4d582e120c4a896e015e2241900d1daeccd05db968852677c71d752bec46de9962174b46f980e8cc603654daf8b98a3ee92dac066033954164a89568b70b1780c2ce2410b2f816dbeddb2cd463e0c8f21a52cf6427d9647a6fd4bafa8fb4cd4d47ac057b0160bee86c6b2fb8adce214c2bcdda277512200adf0eaa5d2114a2c077b009836a68ec254bfe56f51d147b9afe2ddd9cb917c0c2de19d81b7b8fd9f4574f51fa1207630dc13976f4d7587c962f761af267de71f3909a576e6bedaf6311633910d291ac292c467cc8331ef577aef7646a5d949322fa0367a49f20597a13def53136ee31610395e3e48d291fd8f58504374031fe9dcfba5e06086ebcf01a9106f6a4d6e16e19e4c5bb893f7da79419c94eca31a384be6fa1747284dee0fc3bbc8b1b860172c10b29c1594bb8c747d7fe05827358ff2160f49050001625ffe2e880bd7fc26cd0ffd89750745379a8e862816e08a5a2008043921ab6a4976064ac18f7ee37b6628bc0127d8d5ebd3548e41d8881a082d86f20b32e33094f15a0e6ea6074b08c6cd28142de94713451640a55985051f5577eb54572699d838cb34a79c8939e981c0c277d06a6e2ce69ccb74f8a691ff08f81d8b99e6a86223d29a2b7c8e7b041aba44ea678ae654277f7e91cbfa79158b989164a3d549d9f4feb0cc43169699c13e321fe3f4b94258c75d198ff9184269cd6986c55409e07528c93f64942c6c283ce3917b4bf4c3be2fe3173c8c38cccb35f1fbda0ca88b35a599c0678cb22aa8eabea8249dbd2e4f849fffe69803d299e435ebcd7df95854003d8eda17a74d98b4be0e62d45d7fe48c06a6f464a14f8e0570077cc631279092802a89823f031eef5e1028a6d6fdbd502869a731ee7d28b4d6c71b419462a30d31442d3ee444ffbcbd16d558c9000c97e949c2b1f9d6f6d8db7b9131ebd963620d3fc8595278d6f8fdf49084325373196d53e64142fa5a23eccd6ef908c4d80b8b3e6cc334b7f7012103a3682e4678e9b518163d262a39a2c1a69bf88514c52b7ccd7cc8dc80e71f7c2ec0701cff982573eb0c2c4daeb47fa0b586f4451c10d1da2e5d182b03dd067a5e971b3a6138ca6667aaf853d2ac03b80a1d5870905f2cfb6c78ec3c3719c02f973d638a0f973424a2b0f2b0023f136d60092fe15fba4bc180b9176bd0ff576e053f1af6939fe9ca256203ffaeb3e569f09774d2a6cbf91873e56651f4d6ff77e0b5374b0a1a201d7e523604e0247644544cc571d48c458a4f96f45580b";
1119         UniValue obj(UniValue::VOBJ);
1120         obj.push_back(Pair("rawtxn", raw));
1121
1122         // we have the spending key for the dummy recipient zaddr1
1123         std::vector<SendManyRecipient> recipients = { SendManyRecipient(zaddr1, 0.0005, "ABCD") };
1124
1125         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(mtx, zaddr1, {}, recipients, 1) );
1126         std::shared_ptr<AsyncRPCOperation_sendmany> ptr = std::dynamic_pointer_cast<AsyncRPCOperation_sendmany> (operation);
1127         TEST_FRIEND_AsyncRPCOperation_sendmany proxy(ptr);
1128
1129         // Enable test mode so tx is not sent
1130         static_cast<AsyncRPCOperation_sendmany *>(operation.get())->testmode = true;
1131
1132         // Pretend that the operation completed successfully
1133         proxy.set_state(OperationStatus::SUCCESS);
1134
1135         // Verify test mode is returning output (since no input taddrs, signed and unsigned are the same).
1136         BOOST_CHECK_NO_THROW( proxy.sign_send_raw_transaction(obj) );
1137         UniValue result = operation->getResult();
1138         BOOST_CHECK(!result.isNull());
1139         UniValue resultObj = result.get_obj();
1140         std::string hex = find_value(resultObj, "hex").get_str();
1141         BOOST_CHECK_EQUAL(hex, raw);
1142     }
1143
1144
1145     // Test the perform_joinsplit methods.
1146     {
1147         // Dummy input so the operation object can be instantiated.
1148         std::vector<SendManyRecipient> recipients = { SendManyRecipient(zaddr1, 0.0005, "ABCD") };
1149
1150         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(mtx, zaddr1, {}, recipients, 1) );
1151         std::shared_ptr<AsyncRPCOperation_sendmany> ptr = std::dynamic_pointer_cast<AsyncRPCOperation_sendmany> (operation);
1152         TEST_FRIEND_AsyncRPCOperation_sendmany proxy(ptr);
1153
1154         // Enable test mode so tx is not sent and proofs are not generated
1155         static_cast<AsyncRPCOperation_sendmany *>(operation.get())->testmode = true;
1156
1157         AsyncJoinSplitInfo info;
1158         std::vector<boost::optional < ZCIncrementalWitness>> witnesses;
1159         uint256 anchor;
1160         try {
1161             proxy.perform_joinsplit(info, witnesses, anchor);
1162         } catch (const std::runtime_error & e) {
1163             BOOST_CHECK( string(e.what()).find("anchor is null")!= string::npos);
1164         }
1165
1166         try {
1167             std::vector<JSOutPoint> v;
1168             proxy.perform_joinsplit(info, v);
1169         } catch (const std::runtime_error & e) {
1170             BOOST_CHECK( string(e.what()).find("anchor is null")!= string::npos);
1171         }
1172
1173         info.notes.push_back(Note());
1174         try {
1175             proxy.perform_joinsplit(info);
1176         } catch (const std::runtime_error & e) {
1177             BOOST_CHECK( string(e.what()).find("number of notes")!= string::npos);
1178         }
1179
1180         info.notes.clear();
1181         info.vjsin.push_back(JSInput());
1182         info.vjsin.push_back(JSInput());
1183         info.vjsin.push_back(JSInput());
1184         try {
1185             proxy.perform_joinsplit(info);
1186         } catch (const std::runtime_error & e) {
1187             BOOST_CHECK( string(e.what()).find("unsupported joinsplit input")!= string::npos);
1188         }
1189
1190         info.vjsin.clear();
1191         try {
1192             proxy.perform_joinsplit(info);
1193         } catch (const std::runtime_error & e) {
1194             BOOST_CHECK( string(e.what()).find("error verifying joinsplit")!= string::npos);
1195         }
1196     }
1197
1198 }
1199
1200
1201 /*
1202  * This test covers storing encrypted zkeys in the wallet.
1203  */
1204 BOOST_AUTO_TEST_CASE(rpc_wallet_encrypted_wallet_zkeys)
1205 {
1206     LOCK2(cs_main, pwalletMain->cs_wallet);
1207     UniValue retValue;
1208     int n = 100;
1209
1210     // wallet should currently be empty
1211     std::set<libzcash::PaymentAddress> addrs;
1212     pwalletMain->GetPaymentAddresses(addrs);
1213     BOOST_CHECK(addrs.size()==0);
1214
1215     // create keys
1216     for (int i = 0; i < n; i++) {
1217         CallRPC("z_getnewaddress");
1218     }
1219
1220     // Verify we can list the keys imported
1221     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_listaddresses"));
1222     UniValue arr = retValue.get_array();
1223     BOOST_CHECK(arr.size() == n);
1224
1225     // Verify that the wallet encryption RPC is disabled
1226     BOOST_CHECK_THROW(CallRPC("encryptwallet passphrase"), runtime_error);
1227
1228     // Encrypt the wallet (we can't call RPC encryptwallet as that shuts down node)
1229     SecureString strWalletPass;
1230     strWalletPass.reserve(100);
1231     strWalletPass = "hello";
1232
1233     boost::filesystem::current_path(GetArg("-datadir","/tmp/thisshouldnothappen"));
1234     BOOST_CHECK(pwalletMain->EncryptWallet(strWalletPass));
1235
1236     // Verify we can still list the keys imported
1237     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_listaddresses"));
1238     arr = retValue.get_array();
1239     BOOST_CHECK(arr.size() == n);
1240
1241     // Try to add a new key, but we can't as the wallet is locked
1242     BOOST_CHECK_THROW(CallRPC("z_getnewaddress"), runtime_error);
1243
1244     // We can't call RPC walletpassphrase as that invokes RPCRunLater which breaks tests.
1245     // So we manually unlock.
1246     BOOST_CHECK(pwalletMain->Unlock(strWalletPass));
1247
1248     // Now add a key
1249     BOOST_CHECK_NO_THROW(CallRPC("z_getnewaddress"));
1250
1251     // Verify the key has been added
1252     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_listaddresses"));
1253     arr = retValue.get_array();
1254     BOOST_CHECK(arr.size() == n+1);
1255
1256     // We can't simulate over RPC the wallet closing and being reloaded
1257     // but there are tests for this in gtest.
1258 }
1259
1260
1261
1262 BOOST_AUTO_TEST_CASE(rpc_z_shieldcoinbase_parameters)
1263 {
1264     SelectParams(CBaseChainParams::TESTNET);
1265
1266     LOCK(pwalletMain->cs_wallet);
1267
1268     BOOST_CHECK_THROW(CallRPC("z_shieldcoinbase"), runtime_error);
1269     BOOST_CHECK_THROW(CallRPC("z_shieldcoinbase toofewargs"), runtime_error);
1270     BOOST_CHECK_THROW(CallRPC("z_shieldcoinbase too many args shown here"), runtime_error);
1271
1272     // bad from address
1273     BOOST_CHECK_THROW(CallRPC("z_shieldcoinbase "
1274             "INVALIDtmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ tnpoQJVnYBZZqkFadj2bJJLThNCxbADGB5gSGeYTAGGrT5tejsxY9Zc1BtY8nnHmZkB"), runtime_error);
1275
1276     // bad from address
1277     BOOST_CHECK_THROW(CallRPC("z_shieldcoinbase "
1278     "** tnpoQJVnYBZZqkFadj2bJJLThNCxbADGB5gSGeYTAGGrT5tejsxY9Zc1BtY8nnHmZkB"), runtime_error);
1279
1280     // bad to address
1281     BOOST_CHECK_THROW(CallRPC("z_shieldcoinbase "
1282     "tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ INVALIDtnpoQJVnYBZZqkFadj2bJJLThNCxbADGB5gSGeYTAGGrT5tejsxY9Zc1BtY8nnHmZkB"), runtime_error);
1283
1284     // invalid fee amount, cannot be negative
1285     BOOST_CHECK_THROW(CallRPC("z_shieldcoinbase "
1286             "tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ "
1287             "tnpoQJVnYBZZqkFadj2bJJLThNCxbADGB5gSGeYTAGGrT5tejsxY9Zc1BtY8nnHmZkB "
1288             "-0.0001"
1289             ), runtime_error);
1290
1291     // invalid fee amount, bigger than MAX_MONEY
1292     BOOST_CHECK_THROW(CallRPC("z_shieldcoinbase "
1293             "tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ "
1294             "tnpoQJVnYBZZqkFadj2bJJLThNCxbADGB5gSGeYTAGGrT5tejsxY9Zc1BtY8nnHmZkB "
1295             "21000001"
1296             ), runtime_error);
1297
1298     // invalid limit, must be at least 0
1299     BOOST_CHECK_THROW(CallRPC("z_shieldcoinbase "
1300     "tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ "
1301     "tnpoQJVnYBZZqkFadj2bJJLThNCxbADGB5gSGeYTAGGrT5tejsxY9Zc1BtY8nnHmZkB "
1302     "100 -1"
1303     ), runtime_error);
1304
1305     // Mutable tx containing contextual information we need to build tx
1306     UniValue retValue = CallRPC("getblockcount");
1307     int nHeight = retValue.get_int();
1308     CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), nHeight + 1);
1309     if (mtx.nVersion == 1) {
1310         mtx.nVersion = 2;
1311     }
1312
1313     // Test constructor of AsyncRPCOperation_sendmany
1314     std::string testnetzaddr = "ztjiDe569DPNbyTE6TSdJTaSDhoXEHLGvYoUnBU1wfVNU52TEyT6berYtySkd21njAeEoh8fFJUT42kua9r8EnhBaEKqCpP";
1315     std::string mainnetzaddr = "zcMuhvq8sEkHALuSU2i4NbNQxshSAYrpCExec45ZjtivYPbuiFPwk6WHy4SvsbeZ4siy1WheuRGjtaJmoD1J8bFqNXhsG6U";
1316
1317     try {
1318         std::shared_ptr<AsyncRPCOperation> operation(new AsyncRPCOperation_shieldcoinbase(mtx, {}, testnetzaddr, -1 ));
1319     } catch (const UniValue& objError) {
1320         BOOST_CHECK( find_error(objError, "Fee is out of range"));
1321     }
1322
1323     try {
1324         std::shared_ptr<AsyncRPCOperation> operation(new AsyncRPCOperation_shieldcoinbase(mtx, {}, testnetzaddr, 1));
1325     } catch (const UniValue& objError) {
1326         BOOST_CHECK( find_error(objError, "Empty inputs"));
1327     }
1328
1329     // Testnet payment addresses begin with 'zt'.  This test detects an incorrect prefix.
1330     try {
1331         std::vector<ShieldCoinbaseUTXO> inputs = { ShieldCoinbaseUTXO{uint256(),0,0} };
1332         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_shieldcoinbase(mtx, inputs, mainnetzaddr, 1) );
1333     } catch (const UniValue& objError) {
1334         BOOST_CHECK( find_error(objError, "payment address is for wrong network type"));
1335     }
1336
1337 }
1338
1339
1340
1341 BOOST_AUTO_TEST_CASE(rpc_z_shieldcoinbase_internals)
1342 {
1343     SelectParams(CBaseChainParams::TESTNET);
1344
1345     LOCK(pwalletMain->cs_wallet);
1346
1347     // Mutable tx containing contextual information we need to build tx
1348     UniValue retValue = CallRPC("getblockcount");
1349     int nHeight = retValue.get_int();
1350     CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), nHeight + 1);
1351     if (mtx.nVersion == 1) {
1352         mtx.nVersion = 2;
1353     }
1354     
1355     // Test that option -mempooltxinputlimit is respected.
1356     mapArgs["-mempooltxinputlimit"] = "1";
1357
1358     // Add keys manually
1359     CZCPaymentAddress pa = pwalletMain->GenerateNewZKey();
1360     std::string zaddr = pa.ToString();
1361
1362     // Supply 2 inputs when mempool limit is 1
1363     {
1364         std::vector<ShieldCoinbaseUTXO> inputs = { ShieldCoinbaseUTXO{uint256(),0,0}, ShieldCoinbaseUTXO{uint256(),0,0} };
1365         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_shieldcoinbase(mtx, inputs, zaddr) );
1366         operation->main();
1367         BOOST_CHECK(operation->isFailed());
1368         std::string msg = operation->getErrorMessage();
1369         BOOST_CHECK( msg.find("Number of inputs 2 is greater than mempooltxinputlimit of 1") != string::npos);
1370     }
1371
1372     // Insufficient funds
1373     {
1374         std::vector<ShieldCoinbaseUTXO> inputs = { ShieldCoinbaseUTXO{uint256(),0,0} };
1375         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_shieldcoinbase(mtx, inputs, zaddr) );
1376         operation->main();
1377         BOOST_CHECK(operation->isFailed());
1378         std::string msg = operation->getErrorMessage();
1379         BOOST_CHECK( msg.find("Insufficient coinbase funds") != string::npos);
1380     }
1381
1382     // Test the perform_joinsplit methods.
1383     {
1384         // Dummy input so the operation object can be instantiated.
1385         std::vector<ShieldCoinbaseUTXO> inputs = { ShieldCoinbaseUTXO{uint256(),0,100000} };
1386         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_shieldcoinbase(mtx, inputs, zaddr) );
1387         std::shared_ptr<AsyncRPCOperation_shieldcoinbase> ptr = std::dynamic_pointer_cast<AsyncRPCOperation_shieldcoinbase> (operation);
1388         TEST_FRIEND_AsyncRPCOperation_shieldcoinbase proxy(ptr);
1389         static_cast<AsyncRPCOperation_shieldcoinbase *>(operation.get())->testmode = true;
1390
1391         ShieldCoinbaseJSInfo info;
1392         info.vjsin.push_back(JSInput());
1393         info.vjsin.push_back(JSInput());
1394         info.vjsin.push_back(JSInput());
1395         try {
1396             proxy.perform_joinsplit(info);
1397         } catch (const std::runtime_error & e) {
1398             BOOST_CHECK( string(e.what()).find("unsupported joinsplit input")!= string::npos);
1399         }
1400
1401         info.vjsin.clear();
1402         try {
1403             proxy.perform_joinsplit(info);
1404         } catch (const std::runtime_error & e) {
1405             BOOST_CHECK( string(e.what()).find("error verifying joinsplit")!= string::npos);
1406         }
1407     }
1408
1409 }
1410
1411
1412 BOOST_AUTO_TEST_SUITE_END()
This page took 0.106197 seconds and 4 git commands to generate.