]> Git Repo - VerusCoin.git/blob - src/test/rpc_wallet_tests.cpp
Auto merge of #1512 - arcalinea:cleanup-documentation, r=daira
[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 "rpcprotocol.h"
21 #include "init.h"
22
23 #include <chrono>
24 #include <thread>
25
26 #include <fstream>
27 #include <unordered_set>
28
29 #include <boost/algorithm/string.hpp>
30 #include <boost/test/unit_test.hpp>
31 #include <boost/format.hpp>
32
33 using namespace std;
34 using namespace json_spirit;
35
36 extern Array createArgs(int nRequired, const char* address1 = NULL, const char* address2 = NULL);
37 extern Value CallRPC(string args);
38
39 extern CWallet* pwalletMain;
40
41 bool find_error(const Object& objError, const std::string& expected) {
42     return find_value(objError, "message").get_str().find(expected) != string::npos;
43 }
44
45 BOOST_FIXTURE_TEST_SUITE(rpc_wallet_tests, TestingSetup)
46
47 BOOST_AUTO_TEST_CASE(rpc_addmultisig)
48 {
49     LOCK(pwalletMain->cs_wallet);
50
51     rpcfn_type addmultisig = tableRPC["addmultisigaddress"]->actor;
52
53     // old, 65-byte-long:
54     const char address1Hex[] = "0434e3e09f49ea168c5bbf53f877ff4206923858aab7c7e1df25bc263978107c95e35065a27ef6f1b27222db0ec97e0e895eaca603d3ee0d4c060ce3d8a00286c8";
55     // new, compressed:
56     const char address2Hex[] = "0388c2037017c62240b6b72ac1a2a5f94da790596ebd06177c8572752922165cb4";
57
58     Value v;
59     CBitcoinAddress address;
60     BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(1, address1Hex), false));
61     address.SetString(v.get_str());
62     BOOST_CHECK(address.IsValid() && address.IsScript());
63
64     BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(1, address1Hex, address2Hex), false));
65     address.SetString(v.get_str());
66     BOOST_CHECK(address.IsValid() && address.IsScript());
67
68     BOOST_CHECK_NO_THROW(v = addmultisig(createArgs(2, address1Hex, address2Hex), false));
69     address.SetString(v.get_str());
70     BOOST_CHECK(address.IsValid() && address.IsScript());
71
72     BOOST_CHECK_THROW(addmultisig(createArgs(0), false), runtime_error);
73     BOOST_CHECK_THROW(addmultisig(createArgs(1), false), runtime_error);
74     BOOST_CHECK_THROW(addmultisig(createArgs(2, address1Hex), false), runtime_error);
75
76     BOOST_CHECK_THROW(addmultisig(createArgs(1, ""), false), runtime_error);
77     BOOST_CHECK_THROW(addmultisig(createArgs(1, "NotAValidPubkey"), false), runtime_error);
78
79     string short1(address1Hex, address1Hex + sizeof(address1Hex) - 2); // last byte missing
80     BOOST_CHECK_THROW(addmultisig(createArgs(2, short1.c_str()), false), runtime_error);
81
82     string short2(address1Hex + 1, address1Hex + sizeof(address1Hex)); // first byte missing
83     BOOST_CHECK_THROW(addmultisig(createArgs(2, short2.c_str()), false), runtime_error);
84 }
85
86 BOOST_AUTO_TEST_CASE(rpc_wallet)
87 {
88     // Test RPC calls for various wallet statistics
89     Value r;
90
91     LOCK2(cs_main, pwalletMain->cs_wallet);
92
93     CPubKey demoPubkey = pwalletMain->GenerateNewKey();
94     CBitcoinAddress demoAddress = CBitcoinAddress(CTxDestination(demoPubkey.GetID()));
95     Value retValue;
96     string strAccount = "";
97     string strPurpose = "receive";
98     BOOST_CHECK_NO_THROW({ /*Initialize Wallet with an account */
99         CWalletDB walletdb(pwalletMain->strWalletFile);
100         CAccount account;
101         account.vchPubKey = demoPubkey;
102         pwalletMain->SetAddressBook(account.vchPubKey.GetID(), strAccount, strPurpose);
103         walletdb.WriteAccount(strAccount, account);
104     });
105
106     CPubKey setaccountDemoPubkey = pwalletMain->GenerateNewKey();
107     CBitcoinAddress setaccountDemoAddress = CBitcoinAddress(CTxDestination(setaccountDemoPubkey.GetID()));
108
109     /*********************************
110      *                  setaccount
111      *********************************/
112     BOOST_CHECK_NO_THROW(CallRPC("setaccount " + setaccountDemoAddress.ToString() + " \"\""));
113     /* Accounts are disabled */
114     BOOST_CHECK_THROW(CallRPC("setaccount " + setaccountDemoAddress.ToString() + " nullaccount"), runtime_error);
115     /* t1VtArtnn1dGPiD2WFfMXYXW5mHM3q1GpgV is not owned by the test wallet. */
116     BOOST_CHECK_THROW(CallRPC("setaccount t1VtArtnn1dGPiD2WFfMXYXW5mHM3q1GpgV nullaccount"), runtime_error);
117     BOOST_CHECK_THROW(CallRPC("setaccount"), runtime_error);
118     /* t1VtArtnn1dGPiD2WFfMXYXW5mHM3q1Gpg (34 chars) is an illegal address (should be 35 chars) */
119     BOOST_CHECK_THROW(CallRPC("setaccount t1VtArtnn1dGPiD2WFfMXYXW5mHM3q1Gpg nullaccount"), runtime_error);
120
121
122     /*********************************
123      *                  getbalance
124      *********************************/
125     BOOST_CHECK_NO_THROW(CallRPC("getbalance"));
126     BOOST_CHECK_THROW(CallRPC("getbalance " + demoAddress.ToString()), runtime_error);
127
128     /*********************************
129      *                  listunspent
130      *********************************/
131     BOOST_CHECK_NO_THROW(CallRPC("listunspent"));
132     BOOST_CHECK_THROW(CallRPC("listunspent string"), runtime_error);
133     BOOST_CHECK_THROW(CallRPC("listunspent 0 string"), runtime_error);
134     BOOST_CHECK_THROW(CallRPC("listunspent 0 1 not_array"), runtime_error);
135     BOOST_CHECK_THROW(CallRPC("listunspent 0 1 [] extra"), runtime_error);
136     BOOST_CHECK_NO_THROW(r = CallRPC("listunspent 0 1 []"));
137     BOOST_CHECK(r.get_array().empty());
138
139     /*********************************
140      *          listreceivedbyaddress
141      *********************************/
142     BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaddress"));
143     BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaddress 0"));
144     BOOST_CHECK_THROW(CallRPC("listreceivedbyaddress not_int"), runtime_error);
145     BOOST_CHECK_THROW(CallRPC("listreceivedbyaddress 0 not_bool"), runtime_error);
146     BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaddress 0 true"));
147     BOOST_CHECK_THROW(CallRPC("listreceivedbyaddress 0 true extra"), runtime_error);
148
149     /*********************************
150      *          listreceivedbyaccount
151      *********************************/
152     BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaccount"));
153     BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaccount 0"));
154     BOOST_CHECK_THROW(CallRPC("listreceivedbyaccount not_int"), runtime_error);
155     BOOST_CHECK_THROW(CallRPC("listreceivedbyaccount 0 not_bool"), runtime_error);
156     BOOST_CHECK_NO_THROW(CallRPC("listreceivedbyaccount 0 true"));
157     BOOST_CHECK_THROW(CallRPC("listreceivedbyaccount 0 true extra"), runtime_error);
158
159     /*********************************
160      *          listsinceblock
161      *********************************/
162     BOOST_CHECK_NO_THROW(CallRPC("listsinceblock"));
163
164     /*********************************
165      *          listtransactions
166      *********************************/
167     BOOST_CHECK_NO_THROW(CallRPC("listtransactions"));
168     BOOST_CHECK_NO_THROW(CallRPC("listtransactions " + demoAddress.ToString()));
169     BOOST_CHECK_NO_THROW(CallRPC("listtransactions " + demoAddress.ToString() + " 20"));
170     BOOST_CHECK_NO_THROW(CallRPC("listtransactions " + demoAddress.ToString() + " 20 0"));
171     BOOST_CHECK_THROW(CallRPC("listtransactions " + demoAddress.ToString() + " not_int"), runtime_error);
172
173     /*********************************
174      *          listlockunspent
175      *********************************/
176     BOOST_CHECK_NO_THROW(CallRPC("listlockunspent"));
177
178     /*********************************
179      *          listaccounts
180      *********************************/
181     BOOST_CHECK_NO_THROW(CallRPC("listaccounts"));
182
183     /*********************************
184      *          listaddressgroupings
185      *********************************/
186     BOOST_CHECK_NO_THROW(CallRPC("listaddressgroupings"));
187
188     /*********************************
189      *          getrawchangeaddress
190      *********************************/
191     BOOST_CHECK_NO_THROW(CallRPC("getrawchangeaddress"));
192
193     /*********************************
194      *          getnewaddress
195      *********************************/
196     BOOST_CHECK_NO_THROW(CallRPC("getnewaddress"));
197     BOOST_CHECK_NO_THROW(CallRPC("getnewaddress \"\""));
198     /* Accounts are deprecated */
199     BOOST_CHECK_THROW(CallRPC("getnewaddress getnewaddress_demoaccount"), runtime_error);
200
201     /*********************************
202      *          getaccountaddress
203      *********************************/
204     BOOST_CHECK_NO_THROW(CallRPC("getaccountaddress \"\""));
205     /* Accounts are deprecated */
206     BOOST_CHECK_THROW(CallRPC("getaccountaddress accountThatDoesntExists"), runtime_error);
207     BOOST_CHECK_NO_THROW(retValue = CallRPC("getaccountaddress " + strAccount));
208     BOOST_CHECK(CBitcoinAddress(retValue.get_str()).Get() == demoAddress.Get());
209
210     /*********************************
211      *                  getaccount
212      *********************************/
213     BOOST_CHECK_THROW(CallRPC("getaccount"), runtime_error);
214     BOOST_CHECK_NO_THROW(CallRPC("getaccount " + demoAddress.ToString()));
215
216     /*********************************
217      *  signmessage + verifymessage
218      *********************************/
219     BOOST_CHECK_NO_THROW(retValue = CallRPC("signmessage " + demoAddress.ToString() + " mymessage"));
220     BOOST_CHECK_THROW(CallRPC("signmessage"), runtime_error);
221     /* Should throw error because this address is not loaded in the wallet */
222     BOOST_CHECK_THROW(CallRPC("signmessage t1h8SqgtM3QM5e2M8EzhhT1yL2PXXtA6oqe mymessage"), runtime_error);
223
224     /* missing arguments */
225     BOOST_CHECK_THROW(CallRPC("verifymessage " + demoAddress.ToString()), runtime_error);
226     BOOST_CHECK_THROW(CallRPC("verifymessage " + demoAddress.ToString() + " " + retValue.get_str()), runtime_error);
227     /* Illegal address */
228     BOOST_CHECK_THROW(CallRPC("verifymessage t1VtArtnn1dGPiD2WFfMXYXW5mHM3q1Gpg " + retValue.get_str() + " mymessage"), runtime_error);
229     /* wrong address */
230     BOOST_CHECK(CallRPC("verifymessage t1VtArtnn1dGPiD2WFfMXYXW5mHM3q1GpgV " + retValue.get_str() + " mymessage").get_bool() == false);
231     /* Correct address and signature but wrong message */
232     BOOST_CHECK(CallRPC("verifymessage " + demoAddress.ToString() + " " + retValue.get_str() + " wrongmessage").get_bool() == false);
233     /* Correct address, message and signature*/
234     BOOST_CHECK(CallRPC("verifymessage " + demoAddress.ToString() + " " + retValue.get_str() + " mymessage").get_bool() == true);
235
236     /*********************************
237      *          getaddressesbyaccount
238      *********************************/
239     BOOST_CHECK_THROW(CallRPC("getaddressesbyaccount"), runtime_error);
240     BOOST_CHECK_NO_THROW(retValue = CallRPC("getaddressesbyaccount " + strAccount));
241     Array arr = retValue.get_array();
242     BOOST_CHECK_EQUAL(4, arr.size());
243     bool notFound = true;
244     for (auto a : arr) {
245         notFound &= CBitcoinAddress(a.get_str()).Get() != demoAddress.Get();
246     }
247     BOOST_CHECK(!notFound);
248
249     /*
250      * getblocksubsidy
251      */
252     BOOST_CHECK_THROW(CallRPC("getblocksubsidy too many args"), runtime_error);
253     BOOST_CHECK_THROW(CallRPC("getblocksubsidy -1"), runtime_error);
254     BOOST_CHECK_NO_THROW(retValue = CallRPC("getblocksubsidy 50000"));
255     Object obj = retValue.get_obj();
256     BOOST_CHECK(find_value(obj, "miner") == 10.0);
257     BOOST_CHECK(find_value(obj, "founders") == 2.5);
258     BOOST_CHECK_NO_THROW(retValue = CallRPC("getblocksubsidy 1000000"));
259     obj = retValue.get_obj();
260     BOOST_CHECK(find_value(obj, "miner") == 6.25);
261     BOOST_CHECK(find_value(obj, "founders") == 0.0);
262     BOOST_CHECK_NO_THROW(retValue = CallRPC("getblocksubsidy 2000000"));
263     obj = retValue.get_obj();
264     BOOST_CHECK(find_value(obj, "miner") == 3.125);
265     BOOST_CHECK(find_value(obj, "founders") == 0.0);
266 }
267
268 BOOST_AUTO_TEST_CASE(rpc_wallet_getbalance)
269 {
270     SelectParams(CBaseChainParams::TESTNET);
271
272     LOCK(pwalletMain->cs_wallet);
273
274     
275     BOOST_CHECK_THROW(CallRPC("z_getbalance too many args"), runtime_error);
276     BOOST_CHECK_THROW(CallRPC("z_getbalance invalidaddress"), runtime_error);
277     BOOST_CHECK_NO_THROW(CallRPC("z_getbalance tmC6YZnCUhm19dEXxh3Jb7srdBJxDawaCab"));
278     BOOST_CHECK_THROW(CallRPC("z_getbalance tmC6YZnCUhm19dEXxh3Jb7srdBJxDawaCab -1"), runtime_error);
279     BOOST_CHECK_NO_THROW(CallRPC("z_getbalance tmC6YZnCUhm19dEXxh3Jb7srdBJxDawaCab 0"));
280     BOOST_CHECK_THROW(CallRPC("z_getbalance tnRZ8bPq2pff3xBWhTJhNkVUkm2uhzksDeW5PvEa7aFKGT9Qi3YgTALZfjaY4jU3HLVKBtHdSXxoPoLA3naMPcHBcY88FcF 1"), runtime_error);
281    
282     
283     BOOST_CHECK_THROW(CallRPC("z_gettotalbalance too manyargs"), runtime_error);
284     BOOST_CHECK_THROW(CallRPC("z_gettotalbalance -1"), runtime_error);
285     BOOST_CHECK_NO_THROW(CallRPC("z_gettotalbalance 0"));
286     
287     
288     BOOST_CHECK_THROW(CallRPC("z_listreceivedbyaddress too many args"), runtime_error);
289     // negative minconf not allowed
290     BOOST_CHECK_THROW(CallRPC("z_listreceivedbyaddress tmC6YZnCUhm19dEXxh3Jb7srdBJxDawaCab -1"), runtime_error);
291     // invalid zaddr, taddr not allowed
292     BOOST_CHECK_THROW(CallRPC("z_listreceivedbyaddress tmC6YZnCUhm19dEXxh3Jb7srdBJxDawaCab 0"), runtime_error);
293     // don't have the spending key
294     BOOST_CHECK_THROW(CallRPC("z_listreceivedbyaddress tnRZ8bPq2pff3xBWhTJhNkVUkm2uhzksDeW5PvEa7aFKGT9Qi3YgTALZfjaY4jU3HLVKBtHdSXxoPoLA3naMPcHBcY88FcF 1"), runtime_error);
295 }
296
297 /*
298  * This test covers RPC command z_exportwallet
299  */
300 BOOST_AUTO_TEST_CASE(rpc_wallet_z_exportwallet)
301 {
302     LOCK2(cs_main, pwalletMain->cs_wallet);
303     
304     // wallet should be empty
305     std::set<libzcash::PaymentAddress> addrs;
306     pwalletMain->GetPaymentAddresses(addrs);
307     BOOST_CHECK(addrs.size()==0);
308
309     // wallet should have one key
310     CZCPaymentAddress paymentAddress = pwalletMain->GenerateNewZKey();
311     pwalletMain->GetPaymentAddresses(addrs);
312     BOOST_CHECK(addrs.size()==1);
313     
314     BOOST_CHECK_THROW(CallRPC("z_exportwallet"), runtime_error);
315     
316    
317     boost::filesystem::path temp = boost::filesystem::temp_directory_path() /
318             boost::filesystem::unique_path();
319     const std::string path = temp.native();
320
321     BOOST_CHECK_NO_THROW(CallRPC(string("z_exportwallet ") + path));
322
323     auto addr = paymentAddress.Get();
324     libzcash::SpendingKey key;
325     BOOST_CHECK(pwalletMain->GetSpendingKey(addr, key));
326
327     std::string s1 = paymentAddress.ToString();
328     std::string s2 = CZCSpendingKey(key).ToString();
329     
330     // There's no way to really delete a private key so we will read in the
331     // exported wallet file and search for the spending key and payment address.
332     
333     EnsureWalletIsUnlocked();
334
335     ifstream file;
336     file.open(path.c_str(), std::ios::in | std::ios::ate);
337     BOOST_CHECK(file.is_open());
338     bool fVerified = false;
339     int64_t nFilesize = std::max((int64_t)1, (int64_t)file.tellg());
340     file.seekg(0, file.beg);
341     while (file.good()) {
342         std::string line;
343         std::getline(file, line);
344         if (line.empty() || line[0] == '#')
345             continue;
346         if (line.find(s1) != std::string::npos && line.find(s2) != std::string::npos) {
347             fVerified = true;
348             break;
349         }
350     }
351     BOOST_CHECK(fVerified);
352 }
353
354
355 /*
356  * This test covers RPC command z_importwallet
357  */
358 BOOST_AUTO_TEST_CASE(rpc_wallet_z_importwallet)
359 {
360     LOCK2(cs_main, pwalletMain->cs_wallet);
361     
362     // error if no args
363     BOOST_CHECK_THROW(CallRPC("z_importwallet"), runtime_error);
364
365     // create a random key locally
366     auto testSpendingKey = libzcash::SpendingKey::random();
367     auto testPaymentAddress = testSpendingKey.address();
368     std::string testAddr = CZCPaymentAddress(testPaymentAddress).ToString();
369     std::string testKey = CZCSpendingKey(testSpendingKey).ToString();
370     
371     // create test data using the random key
372     std::string format_str = "# Wallet dump created by Zcash v0.11.2.0.z8-9155cc6-dirty (2016-08-11 11:37:00 -0700)\n"
373             "# * Created on 2016-08-12T21:55:36Z\n"
374             "# * Best block at time of backup was 0 (0de0a3851fef2d433b9b4f51d4342bdd24c5ddd793eb8fba57189f07e9235d52),\n"
375             "#   mined on 2009-01-03T18:15:05Z\n"
376             "\n"
377             "# Zkeys\n"
378             "\n"
379             "%s 2016-08-12T21:55:36Z # zaddr=%s\n"
380             "\n"
381             "\n# End of dump";
382     
383     boost::format formatobject(format_str);
384     std::string testWalletDump = (formatobject % testKey % testAddr).str();
385     
386     // write test data to file
387     boost::filesystem::path temp = boost::filesystem::temp_directory_path() /
388             boost::filesystem::unique_path();
389     const std::string path = temp.native();
390     std::ofstream file(path);
391     file << testWalletDump;
392     file << std::flush;
393
394     // wallet should currently be empty
395     std::set<libzcash::PaymentAddress> addrs;
396     pwalletMain->GetPaymentAddresses(addrs);
397     BOOST_CHECK(addrs.size()==0);
398     
399     // import test data from file into wallet
400     BOOST_CHECK_NO_THROW(CallRPC(string("z_importwallet ") + path));
401         
402     // wallet should now have one zkey
403     pwalletMain->GetPaymentAddresses(addrs);
404     BOOST_CHECK(addrs.size()==1);
405     
406     // check that we have the spending key for the address
407     CZCPaymentAddress address(testAddr);
408     auto addr = address.Get();
409     BOOST_CHECK(pwalletMain->HaveSpendingKey(addr));
410     
411     // Verify the spending key is the same as the test data
412     libzcash::SpendingKey k;
413     BOOST_CHECK(pwalletMain->GetSpendingKey(addr, k));
414     CZCSpendingKey spendingkey(k);
415     BOOST_CHECK_EQUAL(testKey, spendingkey.ToString());
416 }
417
418
419 /*
420  * This test covers RPC commands z_listaddresses, z_importkey, z_exportkey
421  */
422 BOOST_AUTO_TEST_CASE(rpc_wallet_z_importexport)
423 {
424     LOCK2(cs_main, pwalletMain->cs_wallet);
425     Value retValue;
426     int n1 = 1000; // number of times to import/export
427     int n2 = 1000; // number of addresses to create and list
428    
429     // error if no args
430     BOOST_CHECK_THROW(CallRPC("z_importkey"), runtime_error);   
431     BOOST_CHECK_THROW(CallRPC("z_exportkey"), runtime_error);   
432
433     // wallet should currently be empty
434     std::set<libzcash::PaymentAddress> addrs;
435     pwalletMain->GetPaymentAddresses(addrs);
436     BOOST_CHECK(addrs.size()==0);
437
438     // verify import and export key
439     for (int i = 0; i < n1; i++) {
440         // create a random key locally
441         auto testSpendingKey = libzcash::SpendingKey::random();
442         auto testPaymentAddress = testSpendingKey.address();
443         std::string testAddr = CZCPaymentAddress(testPaymentAddress).ToString();
444         std::string testKey = CZCSpendingKey(testSpendingKey).ToString();
445         BOOST_CHECK_NO_THROW(CallRPC(string("z_importkey ") + testKey));
446         BOOST_CHECK_NO_THROW(retValue = CallRPC(string("z_exportkey ") + testAddr));
447         BOOST_CHECK_EQUAL(retValue.get_str(), testKey);
448     }
449
450     // Verify we can list the keys imported
451     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_listaddresses"));
452     Array arr = retValue.get_array();
453     BOOST_CHECK(arr.size() == n1);
454
455     // Put addresses into a set
456     std::unordered_set<std::string> myaddrs;
457     for (Value element : arr) {
458         myaddrs.insert(element.get_str());
459     }
460     
461     // Make new addresses for the set
462     for (int i=0; i<n2; i++) {
463         myaddrs.insert((pwalletMain->GenerateNewZKey()).ToString());
464     }
465
466     // Verify number of addresses stored in wallet is n1+n2
467     int numAddrs = myaddrs.size();
468     BOOST_CHECK(numAddrs == n1+n2);
469     pwalletMain->GetPaymentAddresses(addrs);
470     BOOST_CHECK(addrs.size()==numAddrs);  
471     
472     // Ask wallet to list addresses
473     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_listaddresses"));
474     arr = retValue.get_array();
475     BOOST_CHECK(arr.size() == numAddrs);
476   
477     // Create a set from them
478     std::unordered_set<std::string> listaddrs;
479     for (Value element : arr) {
480         listaddrs.insert(element.get_str());
481     }
482     
483     // Verify the two sets of addresses are the same
484     BOOST_CHECK(listaddrs.size() == numAddrs);
485     BOOST_CHECK(myaddrs == listaddrs);
486
487     // Add one more address
488     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_getnewaddress"));
489     std::string newaddress = retValue.get_str();
490     CZCPaymentAddress pa(newaddress);
491     auto newAddr = pa.Get();
492     BOOST_CHECK(pwalletMain->HaveSpendingKey(newAddr));
493 }
494
495
496
497 /**
498  * Test Async RPC operations.
499  * Tip: Create mock operations by subclassing AsyncRPCOperation.
500  */
501
502 class MockSleepOperation : public AsyncRPCOperation {
503 public:
504     std::chrono::milliseconds naptime;
505     MockSleepOperation(int t=1000) {
506         this->naptime = std::chrono::milliseconds(t);
507     }
508     virtual ~MockSleepOperation() {
509     }
510     virtual void main() {
511         set_state(OperationStatus::EXECUTING);
512         start_execution_clock();
513         std::this_thread::sleep_for(std::chrono::milliseconds(naptime));
514         stop_execution_clock();
515         set_result(Value("done"));
516         set_state(OperationStatus::SUCCESS);
517     }
518 };
519
520
521 /*
522  * Test Aysnc RPC queue and operations.
523  */
524 BOOST_AUTO_TEST_CASE(rpc_wallet_async_operations)
525 {
526     std::shared_ptr<AsyncRPCQueue> q = std::make_shared<AsyncRPCQueue>();
527     BOOST_CHECK(q->getNumberOfWorkers() == 0);
528     std::vector<AsyncRPCOperationId> ids = q->getAllOperationIds();
529     BOOST_CHECK(ids.size()==0);
530
531     std::shared_ptr<AsyncRPCOperation> op1 = std::make_shared<AsyncRPCOperation>();
532     q->addOperation(op1);    
533     BOOST_CHECK(q->getOperationCount() == 1);
534     
535     OperationStatus status = op1->getState();
536     BOOST_CHECK(status == OperationStatus::READY);
537     
538     AsyncRPCOperationId id1 = op1->getId();
539     int64_t creationTime1 = op1->getCreationTime();
540  
541     q->addWorker();
542     BOOST_CHECK(q->getNumberOfWorkers() == 1);
543  
544     // an AsyncRPCOperation doesn't do anything so will finish immediately 
545     std::this_thread::sleep_for(std::chrono::seconds(1));
546     BOOST_CHECK(q->getOperationCount() == 0);
547
548     // operation should be a success
549     BOOST_CHECK_EQUAL(op1->isCancelled(), false);
550     BOOST_CHECK_EQUAL(op1->isExecuting(), false);
551     BOOST_CHECK_EQUAL(op1->isReady(), false);
552     BOOST_CHECK_EQUAL(op1->isFailed(), false);
553     BOOST_CHECK_EQUAL(op1->isSuccess(), true);
554     BOOST_CHECK(op1->getError() == Value::null );
555     BOOST_CHECK(op1->getResult().is_null() == false );
556     BOOST_CHECK_EQUAL(op1->getStateAsString(), "success");
557     BOOST_CHECK_NE(op1->getStateAsString(), "executing");
558     
559     // Create a second operation which just sleeps
560     std::shared_ptr<AsyncRPCOperation> op2(new MockSleepOperation(2500));
561     AsyncRPCOperationId id2 = op2->getId();
562     int64_t creationTime2 = op2->getCreationTime();
563
564     // it's different from the previous operation
565     BOOST_CHECK_NE(op1.get(), op2.get());
566     BOOST_CHECK_NE(id1, id2);
567     BOOST_CHECK_NE(creationTime1, creationTime2);
568
569     // Only the first operation has been added to the queue
570     std::vector<AsyncRPCOperationId> v = q->getAllOperationIds();
571     std::set<AsyncRPCOperationId> opids(v.begin(), v.end());
572     BOOST_CHECK(opids.size() == 1);
573     BOOST_CHECK(opids.count(id1)==1);
574     BOOST_CHECK(opids.count(id2)==0);
575     std::shared_ptr<AsyncRPCOperation> p1 = q->getOperationForId(id1);
576     BOOST_CHECK_EQUAL(p1.get(), op1.get());
577     std::shared_ptr<AsyncRPCOperation> p2 = q->getOperationForId(id2);
578     BOOST_CHECK(!p2); // null ptr as not added to queue yet
579
580     // Add operation 2 and 3 to the queue
581     q->addOperation(op2);
582     std::shared_ptr<AsyncRPCOperation> op3(new MockSleepOperation(1000));
583     q->addOperation(op3);
584     std::this_thread::sleep_for(std::chrono::milliseconds(500));
585     BOOST_CHECK_EQUAL(op2->isExecuting(), true);
586     op2->cancel();  // too late, already executing
587     op3->cancel();
588     std::this_thread::sleep_for(std::chrono::milliseconds(3000));
589     BOOST_CHECK_EQUAL(op2->isSuccess(), true);
590     BOOST_CHECK_EQUAL(op2->isCancelled(), false);
591     BOOST_CHECK_EQUAL(op3->isCancelled(), true);
592     
593    
594     v = q->getAllOperationIds();
595     std::copy( v.begin(), v.end(), std::inserter( opids, opids.end() ) );
596     BOOST_CHECK(opids.size() == 3);
597     BOOST_CHECK(opids.count(id1)==1);
598     BOOST_CHECK(opids.count(id2)==1);
599     BOOST_CHECK(opids.count(op3->getId())==1);
600     q->finishAndWait();
601 }
602
603
604 // The CountOperation will increment this global
605 std::atomic<int64_t> gCounter(0);
606
607 class CountOperation : public AsyncRPCOperation {
608 public:
609     CountOperation() {}
610     virtual ~CountOperation() {}
611     virtual void main() {  
612         set_state(OperationStatus::EXECUTING);
613         gCounter++;
614         std::this_thread::sleep_for(std::chrono::milliseconds(1000));
615         set_state(OperationStatus::SUCCESS);
616     }
617 };
618
619 // This tests the queue waiting for multiple workers to finish
620 BOOST_AUTO_TEST_CASE(rpc_wallet_async_operations_parallel_wait)
621 {
622     gCounter = 0;
623     
624     std::shared_ptr<AsyncRPCQueue> q = std::make_shared<AsyncRPCQueue>();
625     q->addWorker();
626     q->addWorker();
627     q->addWorker();
628     q->addWorker();
629     BOOST_CHECK(q->getNumberOfWorkers() == 4);
630
631     int64_t numOperations = 10;     // 10 * 1000ms / 4 = 2.5 secs to finish
632     for (int i=0; i<numOperations; i++) {
633         std::shared_ptr<AsyncRPCOperation> op(new CountOperation());
634         q->addOperation(op);
635     }
636
637     std::vector<AsyncRPCOperationId> ids = q->getAllOperationIds();
638     BOOST_CHECK(ids.size()==numOperations);
639     q->finishAndWait();
640     BOOST_CHECK_EQUAL(q->isFinishing(), true);
641     BOOST_CHECK_EQUAL(numOperations, gCounter.load());
642 }
643
644 // This tests the queue shutting down immediately
645 BOOST_AUTO_TEST_CASE(rpc_wallet_async_operations_parallel_cancel)
646 {
647     gCounter = 0;
648     
649     std::shared_ptr<AsyncRPCQueue> q = std::make_shared<AsyncRPCQueue>();
650     q->addWorker();
651     q->addWorker();
652     BOOST_CHECK(q->getNumberOfWorkers() == 2);
653
654     int numOperations = 10000;  // 10000 seconds to complete
655     for (int i=0; i<numOperations; i++) {
656         std::shared_ptr<AsyncRPCOperation> op(new CountOperation());
657         q->addOperation(op);
658     }
659     std::vector<AsyncRPCOperationId> ids = q->getAllOperationIds();
660     BOOST_CHECK(ids.size()==numOperations);
661     q->closeAndWait();
662
663     int numSuccess = 0;
664     int numCancelled = 0; 
665     for (auto & id : ids) {
666         std::shared_ptr<AsyncRPCOperation> ptr = q->popOperationForId(id);
667         if (ptr->isCancelled()) {
668             numCancelled++;
669         } else if (ptr->isSuccess()) {
670             numSuccess++;
671         }
672     }
673         
674     BOOST_CHECK_EQUAL(numOperations, numSuccess+numCancelled);
675     BOOST_CHECK_EQUAL(gCounter.load(), numSuccess);
676     BOOST_CHECK(q->getOperationCount() == 0);
677     ids = q->getAllOperationIds();
678     BOOST_CHECK(ids.size()==0);
679 }
680
681 // This tests z_getoperationstatus, z_getoperationresult, z_listoperationids
682 BOOST_AUTO_TEST_CASE(rpc_z_getoperations)
683 {
684     std::shared_ptr<AsyncRPCQueue> q = getAsyncRPCQueue();
685     std::shared_ptr<AsyncRPCQueue> sharedInstance = AsyncRPCQueue::sharedInstance();
686     BOOST_CHECK(q == sharedInstance);
687
688     BOOST_CHECK_NO_THROW(CallRPC("z_getoperationstatus"));
689     BOOST_CHECK_NO_THROW(CallRPC("z_getoperationstatus []"));
690     BOOST_CHECK_NO_THROW(CallRPC("z_getoperationstatus [\"opid-1234\"]"));
691     BOOST_CHECK_THROW(CallRPC("z_getoperationstatus [] toomanyargs"), runtime_error);
692     BOOST_CHECK_THROW(CallRPC("z_getoperationstatus not_an_array"), runtime_error);
693
694     BOOST_CHECK_NO_THROW(CallRPC("z_getoperationresult"));
695     BOOST_CHECK_NO_THROW(CallRPC("z_getoperationresult []"));
696     BOOST_CHECK_NO_THROW(CallRPC("z_getoperationresult [\"opid-1234\"]"));
697     BOOST_CHECK_THROW(CallRPC("z_getoperationresult [] toomanyargs"), runtime_error);
698     BOOST_CHECK_THROW(CallRPC("z_getoperationresult not_an_array"), runtime_error);
699     
700     std::shared_ptr<AsyncRPCOperation> op1 = std::make_shared<AsyncRPCOperation>();
701     q->addOperation(op1);
702     std::shared_ptr<AsyncRPCOperation> op2 = std::make_shared<AsyncRPCOperation>();
703     q->addOperation(op2);
704     
705     BOOST_CHECK(q->getOperationCount() == 2);
706     BOOST_CHECK(q->getNumberOfWorkers() == 0);
707     q->addWorker();
708     BOOST_CHECK(q->getNumberOfWorkers() == 1);
709     std::this_thread::sleep_for(std::chrono::milliseconds(1000));
710     BOOST_CHECK(q->getOperationCount() == 0);
711     
712     Value retValue;
713     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_listoperationids"));
714     BOOST_CHECK(retValue.get_array().size() == 2);
715
716     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_getoperationstatus"));
717     Array array = retValue.get_array();
718     BOOST_CHECK(array.size() == 2);
719
720     // idempotent
721     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_getoperationstatus"));
722     array = retValue.get_array();
723     BOOST_CHECK(array.size() == 2);   
724     
725     for (Value v : array) {
726         Object obj = v.get_obj();
727         Value id = find_value(obj, "id");
728         
729         Value result;
730         // removes result from internal storage
731         BOOST_CHECK_NO_THROW(result = CallRPC("z_getoperationresult [\"" + id.get_str() + "\"]"));
732         Array resultArray = result.get_array();
733         BOOST_CHECK(resultArray.size() == 1);
734         
735         Object resultObj = resultArray.front().get_obj();
736         Value resultId = find_value(resultObj, "id");
737         BOOST_CHECK_EQUAL(id.get_str(), resultId.get_str());
738         
739         // verify the operation has been removed 
740         BOOST_CHECK_NO_THROW(result = CallRPC("z_getoperationresult [\"" + id.get_str() + "\"]"));
741         resultArray = result.get_array();
742         BOOST_CHECK(resultArray.size() == 0);
743     }
744     
745     // operations removed
746     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_getoperationstatus"));
747     array = retValue.get_array();
748     BOOST_CHECK(array.size() == 0);
749
750     q->close();
751 }
752
753 BOOST_AUTO_TEST_CASE(rpc_z_sendmany_parameters)
754 {
755     SelectParams(CBaseChainParams::TESTNET);
756
757     LOCK(pwalletMain->cs_wallet);
758
759     BOOST_CHECK_THROW(CallRPC("z_sendmany"), runtime_error);
760     BOOST_CHECK_THROW(CallRPC("z_sendmany toofewargs"), runtime_error);
761     BOOST_CHECK_THROW(CallRPC("z_sendmany too many args here"), runtime_error);
762
763     // bad from address
764     BOOST_CHECK_THROW(CallRPC("z_sendmany "
765             "INVALIDtmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ []"), runtime_error);
766     // empty amounts
767     BOOST_CHECK_THROW(CallRPC("z_sendmany "
768             "tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ []"), runtime_error);
769
770     // don't have the spending key for this address
771     BOOST_CHECK_THROW(CallRPC("z_sendmany "
772             "tnpoQJVnYBZZqkFadj2bJJLThNCxbADGB5gSGeYTAGGrT5tejsxY9Zc1BtY8nnHmZkB"
773             "UkJ1oSfbhTJhm72WiZizvkZz5aH1 []"), runtime_error);
774
775     // duplicate address
776     BOOST_CHECK_THROW(CallRPC("z_sendmany "
777             "tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ "
778             "[{\"address\":\"tmQP9L3s31cLsghVYf2Jb5MhKj1jRBPoeQn\", \"amount\":50.0},"
779             " {\"address\":\"tmQP9L3s31cLsghVYf2Jb5MhKj1jRBPoeQn\", \"amount\":12.0} ]"
780             ), runtime_error);
781
782     // memo bigger than allowed length of ZC_MEMO_SIZE
783     std::vector<char> v (2 * (ZC_MEMO_SIZE+1));     // x2 for hexadecimal string format
784     std::fill(v.begin(),v.end(), 'A');
785     std::string badmemo(v.begin(), v.end());
786     CZCPaymentAddress pa = pwalletMain->GenerateNewZKey();
787     std::string zaddr1 = pa.ToString();
788     BOOST_CHECK_THROW(CallRPC(string("z_sendmany tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ ")
789             + "[{\"address\":\"" + zaddr1 + "\", \"amount\":123.456}]"), runtime_error);
790     
791     // Test constructor of AsyncRPCOperation_sendmany 
792     try {
793         std::shared_ptr<AsyncRPCOperation> operation(new AsyncRPCOperation_sendmany("",{}, {}, -1));
794     } catch (const Object& objError) {
795         BOOST_CHECK( find_error(objError, "Minconf cannot be negative"));
796     }
797
798     try {
799         std::shared_ptr<AsyncRPCOperation> operation(new AsyncRPCOperation_sendmany("",{}, {}, 1));
800     } catch (const Object& objError) {
801         BOOST_CHECK( find_error(objError, "From address parameter missing"));
802     }
803
804     try {
805         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany("tmRr6yJonqGK23UVhrKuyvTpF8qxQQjKigJ", {}, {}, 1) );
806     } catch (const Object& objError) {
807         BOOST_CHECK( find_error(objError, "No recipients"));
808     }
809
810     try {
811         std::vector<SendManyRecipient> recipients = { SendManyRecipient("dummy",1.0, "") };
812         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany("INVALID", recipients, {}, 1) );
813     } catch (const Object& objError) {
814         BOOST_CHECK( find_error(objError, "payment address is invalid"));
815     }
816
817     // Testnet payment addresses begin with 'zt'.  This test detects an incorrect prefix.
818     try {
819         std::vector<SendManyRecipient> recipients = { SendManyRecipient("dummy",1.0, "") };
820         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany("zcMuhvq8sEkHALuSU2i4NbNQxshSAYrpCExec45ZjtivYPbuiFPwk6WHy4SvsbeZ4siy1WheuRGjtaJmoD1J8bFqNXhsG6U", recipients, {}, 1) );
821     } catch (const Object& objError) {
822         BOOST_CHECK( find_error(objError, "payment address is for wrong network type"));
823     }
824
825     // Note: The following will crash as a google test because AsyncRPCOperation_sendmany
826     // invokes a method on pwalletMain, which is undefined in the google test environment.
827     try {
828         std::vector<SendManyRecipient> recipients = { SendManyRecipient("dummy",1.0, "") };
829         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany("ztjiDe569DPNbyTE6TSdJTaSDhoXEHLGvYoUnBU1wfVNU52TEyT6berYtySkd21njAeEoh8fFJUT42kua9r8EnhBaEKqCpP", recipients, {}, 1) );
830     } catch (const Object& objError) {
831         BOOST_CHECK( find_error(objError, "no spending key found for zaddr"));
832     }
833 }
834
835
836 // TODO: test private methods
837 BOOST_AUTO_TEST_CASE(rpc_z_sendmany_internals)
838 {
839     SelectParams(CBaseChainParams::TESTNET);
840
841     LOCK(pwalletMain->cs_wallet);
842
843     Value retValue;
844  
845     // add keys manually
846     BOOST_CHECK_NO_THROW(retValue = CallRPC("getnewaddress"));
847     std::string taddr1 = retValue.get_str();
848     CZCPaymentAddress pa = pwalletMain->GenerateNewZKey();
849     std::string zaddr1 = pa.ToString();
850     
851     // there are no utxos to spend
852     {
853         std::vector<SendManyRecipient> recipients = { SendManyRecipient(zaddr1,100.0, "DEADBEEF") };
854         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(taddr1, {}, recipients, 1) );
855         operation->main();
856         BOOST_CHECK(operation->isFailed());
857         std::string msg = operation->getErrorMessage();
858         BOOST_CHECK( msg.find("Insufficient funds, no UTXOs found") != string::npos);
859     }
860     
861     // there are no unspent notes to spend
862     {
863         std::vector<SendManyRecipient> recipients = { SendManyRecipient(taddr1,100.0, "DEADBEEF") };
864         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(zaddr1, recipients, {}, 1) );
865         operation->main();
866         BOOST_CHECK(operation->isFailed());
867         std::string msg = operation->getErrorMessage();
868         BOOST_CHECK( msg.find("Insufficient funds, no unspent notes") != string::npos);
869     }
870
871     // get_memo_from_hex_string())
872     {
873         std::vector<SendManyRecipient> recipients = { SendManyRecipient(zaddr1,100.0, "DEADBEEF") };
874         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(zaddr1, recipients, {}, 1) );
875         std::shared_ptr<AsyncRPCOperation_sendmany> ptr = std::dynamic_pointer_cast<AsyncRPCOperation_sendmany> (operation);
876         TEST_FRIEND_AsyncRPCOperation_sendmany proxy(ptr);
877         
878         std::string memo = "DEADBEEF";
879         boost::array<unsigned char, ZC_MEMO_SIZE> array = proxy.get_memo_from_hex_string(memo);
880         BOOST_CHECK_EQUAL(array[0], 0xDE);
881         BOOST_CHECK_EQUAL(array[1], 0xAD);
882         BOOST_CHECK_EQUAL(array[2], 0xBE);
883         BOOST_CHECK_EQUAL(array[3], 0xEF);
884         for (int i=4; i<ZC_MEMO_SIZE; i++) {
885             BOOST_CHECK_EQUAL(array[i], 0x00);  // zero padding
886         }
887         
888         // memo is longer than allowed
889         std::vector<char> v (2 * (ZC_MEMO_SIZE+1));
890         std::fill(v.begin(),v.end(), 'A');
891         std::string bigmemo(v.begin(), v.end());
892         
893         try {
894             proxy.get_memo_from_hex_string(bigmemo);
895         } catch (const Object& objError) {
896             BOOST_CHECK( find_error(objError, "too big"));
897         }
898         
899         // invalid hexadecimal string
900         std::fill(v.begin(),v.end(), '@'); // not a hex character
901         std::string badmemo(v.begin(), v.end());
902         
903         try {
904             proxy.get_memo_from_hex_string(badmemo);
905         } catch (const Object& objError) {
906             BOOST_CHECK( find_error(objError, "hexadecimal format"));
907         }
908         
909         // odd length hexadecimal string
910         std::fill(v.begin(),v.end(), 'A');
911         v.resize(v.size() - 1);
912         assert(v.size() %2 == 1); // odd length
913         std::string oddmemo(v.begin(), v.end());
914         try {
915             proxy.get_memo_from_hex_string(oddmemo);
916         } catch (const Object& objError) {
917             BOOST_CHECK( find_error(objError, "hexadecimal format"));
918         }
919     }
920     
921     
922     // add_taddr_change_output_to_tx() will append a vout to a raw transaction
923     {
924         std::vector<SendManyRecipient> recipients = { SendManyRecipient(zaddr1,100.0, "DEADBEEF") };
925         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(zaddr1, recipients, {}, 1) );
926         std::shared_ptr<AsyncRPCOperation_sendmany> ptr = std::dynamic_pointer_cast<AsyncRPCOperation_sendmany> (operation);
927         TEST_FRIEND_AsyncRPCOperation_sendmany proxy(ptr);
928         
929         CTransaction tx = proxy.getTx();
930         BOOST_CHECK(tx.vout.size() == 0);
931         
932         CAmount amount = 123.456;
933         proxy.add_taddr_change_output_to_tx(amount);
934         tx = proxy.getTx();
935         BOOST_CHECK(tx.vout.size() == 1);
936         CTxOut out = tx.vout[0];
937         BOOST_CHECK_EQUAL(out.nValue, amount);
938         
939         amount = 1.111;
940         proxy.add_taddr_change_output_to_tx(amount);
941         tx = proxy.getTx();
942         BOOST_CHECK(tx.vout.size() == 2);
943         out = tx.vout[1];
944         BOOST_CHECK_EQUAL(out.nValue, amount);
945     }
946     
947     // add_taddr_outputs_to_tx() will append many vouts to a raw transaction
948     {
949         std::vector<SendManyRecipient> recipients = {
950             SendManyRecipient("tmTGScYwiLMzHe4uGZtBYmuqoW4iEoYNMXt",CAmount(1.23), ""),
951             SendManyRecipient("tmUSbHz3vxnwLvRyNDXbwkZxjVyDodMJEhh",CAmount(4.56), ""),
952             SendManyRecipient("tmYZAXYPCP56Xa5JQWWPZuK7o7bfUQW6kkd",CAmount(7.89), ""),
953         };
954         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(zaddr1, recipients, {}, 1) );
955         std::shared_ptr<AsyncRPCOperation_sendmany> ptr = std::dynamic_pointer_cast<AsyncRPCOperation_sendmany> (operation);
956         TEST_FRIEND_AsyncRPCOperation_sendmany proxy(ptr);
957         
958         proxy.add_taddr_outputs_to_tx();
959         
960         CTransaction tx = proxy.getTx();
961         BOOST_CHECK(tx.vout.size() == 3);
962         BOOST_CHECK_EQUAL(tx.vout[0].nValue, CAmount(1.23));
963         BOOST_CHECK_EQUAL(tx.vout[1].nValue, CAmount(4.56));
964         BOOST_CHECK_EQUAL(tx.vout[2].nValue, CAmount(7.89));
965     }
966     
967
968     // Raw joinsplit is a zaddr->zaddr
969     {
970         std::string raw = "020000000000000000000100000000000000001027000000000000183a0d4c46c369078705e39bcfebee59a978dbd210ce8de3efc9555a03fbabfd3cea16693d730c63850d7e48ccde79854c19adcb7e9dcd7b7d18805ee09083f6b16e1860729d2d4a90e2f2acd009cf78b5eb0f4a6ee4bdb64b1262d7ce9eb910c460b02022991e968d0c50ee44908e4ccccbc591d0053bcca154dd6d6fc400a29fa686af4682339832ccea362a62aeb9df0d5aa74f86a1e75ac0f48a8ccc41e0a940643c6c33e1d09223b0a46eaf47a1bb4407cfc12b1dcf83a29c0cef51e45c7876ca5b9e5bae86d92976eb3ef68f29cd29386a8be8451b50f82bf9da10c04651868655194da8f6ed3d241bb5b5ff93a3e2bbe44644544d88bcde5cc35978032ee92699c7a61fcbb395e7583f47e698c4d53ede54f956629400bf510fb5e22d03158cc10bdcaaf29e418ef18eb6480dd9c8b9e2a377809f9f32a556ef872febd0021d4ad013aa9f0b7255e98e408d302abefd33a71180b720271835b487ab309e160b06dfe51932120fb84a7ede16b20c53599a11071592109e10260f265ee60d48c62bfe24074020e9b586ce9e9356e68f2ad1a9538258234afe4b83a209f178f45202270eaeaeecaf2ce3100b2c5a714f75f35777a9ebff5ebf47059d2bbf6f3726190216468f2b152673b766225b093f3a2f827c86d6b48b42117fec1d0ac38dd7af700308dcfb02eba821612b16a2c164c47715b9b0c93900893b1aba2ea03765c94d87022db5be06ab338d1912e0936dfe87586d0a8ee49144a6cd2e306abdcb652faa3e0222739deb23154d778b50de75069a4a2cce1208cd1ced3cb4744c9888ce1c2fcd2e66dc31e62d3aa9e423d7275882525e9981f92e84ac85975b8660739407efbe1e34c2249420fde7e17db3096d5b22e83d051d01f0e6e7690dca7d168db338aadf0897fedac10de310db2b1bff762d322935dddbb60c2efb8b15d231fa17b84630371cb275c209f0c4c7d0c68b150ea5cd514122215e3f7fcfb351d69514788d67c2f3c8922581946e3a04bdf1f07f15696ca76eb95b10698bf1188fd882945c57657515889d042a6fc45d38cbc943540c4f0f6d1c45a1574c81f3e42d1eb8702328b729909adee8a5cfed7c79d54627d1fd389af941d878376f7927b9830ca659bf9ab18c5ca5192d52d02723008728d03701b8ab3e1c4a3109409ec0b13df334c7deec3523eeef4c97b5603e643de3a647b873f4c1b47fbfc6586ba66724f112e51fc93839648005043620aa3ce458e246d77977b19c53d98e3e812de006afc1a79744df236582943631d04cc02941ac4be500e4ed9fb9e3e7cc187b1c4050fad1d9d09d5fd70d5d01d615b439d8c0015d2eb10398bcdbf8c4b2bd559dbe4c288a186aed3f86f608da4d582e120c4a896e015e2241900d1daeccd05db968852677c71d752bec46de9962174b46f980e8cc603654daf8b98a3ee92dac066033954164a89568b70b1780c2ce2410b2f816dbeddb2cd463e0c8f21a52cf6427d9647a6fd4bafa8fb4cd4d47ac057b0160bee86c6b2fb8adce214c2bcdda277512200adf0eaa5d2114a2c077b009836a68ec254bfe56f51d147b9afe2ddd9cb917c0c2de19d81b7b8fd9f4574f51fa1207630dc13976f4d7587c962f761af267de71f3909a576e6bedaf6311633910d291ac292c467cc8331ef577aef7646a5d949322fa0367a49f20597a13def53136ee31610395e3e48d291fd8f58504374031fe9dcfba5e06086ebcf01a9106f6a4d6e16e19e4c5bb893f7da79419c94eca31a384be6fa1747284dee0fc3bbc8b1b860172c10b29c1594bb8c747d7fe05827358ff2160f49050001625ffe2e880bd7fc26cd0ffd89750745379a8e862816e08a5a2008043921ab6a4976064ac18f7ee37b6628bc0127d8d5ebd3548e41d8881a082d86f20b32e33094f15a0e6ea6074b08c6cd28142de94713451640a55985051f5577eb54572699d838cb34a79c8939e981c0c277d06a6e2ce69ccb74f8a691ff08f81d8b99e6a86223d29a2b7c8e7b041aba44ea678ae654277f7e91cbfa79158b989164a3d549d9f4feb0cc43169699c13e321fe3f4b94258c75d198ff9184269cd6986c55409e07528c93f64942c6c283ce3917b4bf4c3be2fe3173c8c38cccb35f1fbda0ca88b35a599c0678cb22aa8eabea8249dbd2e4f849fffe69803d299e435ebcd7df95854003d8eda17a74d98b4be0e62d45d7fe48c06a6f464a14f8e0570077cc631279092802a89823f031eef5e1028a6d6fdbd502869a731ee7d28b4d6c71b419462a30d31442d3ee444ffbcbd16d558c9000c97e949c2b1f9d6f6d8db7b9131ebd963620d3fc8595278d6f8fdf49084325373196d53e64142fa5a23eccd6ef908c4d80b8b3e6cc334b7f7012103a3682e4678e9b518163d262a39a2c1a69bf88514c52b7ccd7cc8dc80e71f7c2ec0701cff982573eb0c2c4daeb47fa0b586f4451c10d1da2e5d182b03dd067a5e971b3a6138ca6667aaf853d2ac03b80a1d5870905f2cfb6c78ec3c3719c02f973d638a0f973424a2b0f2b0023f136d60092fe15fba4bc180b9176bd0ff576e053f1af6939fe9ca256203ffaeb3e569f09774d2a6cbf91873e56651f4d6ff77e0b5374b0a1a201d7e523604e0247644544cc571d48c458a4f96f45580b";
971         Object obj;
972         obj.push_back(Pair("rawtxn", raw));
973         
974         // we have the spending key for the dummy recipient zaddr1
975         std::vector<SendManyRecipient> recipients = { SendManyRecipient(zaddr1, 0.0005, "ABCD") };
976         
977         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(zaddr1, {}, recipients, 1) );
978         std::shared_ptr<AsyncRPCOperation_sendmany> ptr = std::dynamic_pointer_cast<AsyncRPCOperation_sendmany> (operation);
979         TEST_FRIEND_AsyncRPCOperation_sendmany proxy(ptr);
980         
981         // Enable test mode so tx is not sent
982         static_cast<AsyncRPCOperation_sendmany *>(operation.get())->testmode = true;
983         
984         // Pretend that the operation completed successfully
985         proxy.set_state(OperationStatus::SUCCESS);
986
987         // Verify test mode is returning output (since no input taddrs, signed and unsigned are the same).
988         BOOST_CHECK_NO_THROW( proxy.sign_send_raw_transaction(obj) );
989         Value result = operation->getResult();
990         BOOST_CHECK(!result.is_null());
991         Object resultObj = result.get_obj();
992         std::string hex = find_value(resultObj, "hex").get_str();
993         BOOST_CHECK_EQUAL(hex, raw);
994     }
995     
996     
997     // Test the perform_joinsplit methods.
998     {
999         // Dummy input so the operation object can be instantiated.
1000         std::vector<SendManyRecipient> recipients = { SendManyRecipient(zaddr1, 0.0005, "ABCD") };
1001         
1002         std::shared_ptr<AsyncRPCOperation> operation( new AsyncRPCOperation_sendmany(zaddr1, {}, recipients, 1) );
1003         std::shared_ptr<AsyncRPCOperation_sendmany> ptr = std::dynamic_pointer_cast<AsyncRPCOperation_sendmany> (operation);
1004         TEST_FRIEND_AsyncRPCOperation_sendmany proxy(ptr); 
1005
1006         // Enable test mode so tx is not sent and proofs are not generated
1007         static_cast<AsyncRPCOperation_sendmany *>(operation.get())->testmode = true;
1008         
1009         AsyncJoinSplitInfo info;        
1010         std::vector<boost::optional < ZCIncrementalWitness>> witnesses;
1011         uint256 anchor;
1012         try {
1013             proxy.perform_joinsplit(info, witnesses, anchor);
1014         } catch (const std::runtime_error & e) {
1015             BOOST_CHECK( string(e.what()).find("anchor is null")!= string::npos);
1016         }
1017
1018         try {
1019             std::vector<JSOutPoint> v;
1020             proxy.perform_joinsplit(info, v);
1021         } catch (const std::runtime_error & e) {
1022             BOOST_CHECK( string(e.what()).find("anchor is null")!= string::npos);
1023         }
1024
1025         info.notes.push_back(Note());
1026         try {
1027             proxy.perform_joinsplit(info);
1028         } catch (const std::runtime_error & e) {
1029             BOOST_CHECK( string(e.what()).find("number of notes")!= string::npos);
1030         }
1031         
1032         info.notes.clear();
1033         info.vjsin.push_back(JSInput());
1034         info.vjsin.push_back(JSInput());
1035         info.vjsin.push_back(JSInput());
1036         try {
1037             proxy.perform_joinsplit(info);
1038         } catch (const std::runtime_error & e) {
1039             BOOST_CHECK( string(e.what()).find("unsupported joinsplit input")!= string::npos);
1040         }
1041         
1042         info.vjsin.clear();
1043         try {
1044             proxy.perform_joinsplit(info);
1045         } catch (const std::runtime_error & e) {
1046             BOOST_CHECK( string(e.what()).find("JoinSplit verifying key not loaded")!= string::npos);
1047         }
1048     }
1049     
1050 }
1051
1052
1053 /*
1054  * This test covers storing encrypted zkeys in the wallet.
1055  */
1056 BOOST_AUTO_TEST_CASE(rpc_wallet_encrypted_wallet_zkeys)
1057 {
1058     LOCK2(cs_main, pwalletMain->cs_wallet);
1059     Value retValue;
1060     int n = 100;
1061
1062     // wallet should currently be empty
1063     std::set<libzcash::PaymentAddress> addrs;
1064     pwalletMain->GetPaymentAddresses(addrs);
1065     BOOST_CHECK(addrs.size()==0);
1066
1067     // create keys
1068     for (int i = 0; i < n; i++) {
1069         CallRPC("z_getnewaddress");
1070     }
1071
1072     // Verify we can list the keys imported
1073     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_listaddresses"));
1074     Array arr = retValue.get_array();
1075     BOOST_CHECK(arr.size() == n);
1076
1077     // Encrypt the wallet (we can't call RPC encryptwallet as that shuts down node)
1078     SecureString strWalletPass;
1079     strWalletPass.reserve(100);
1080     strWalletPass = "hello";
1081     BOOST_CHECK(pwalletMain->EncryptWallet(strWalletPass));
1082     
1083     // Verify we can still list the keys imported
1084     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_listaddresses"));
1085     arr = retValue.get_array();
1086     BOOST_CHECK(arr.size() == n);
1087     
1088     // Try to add a new key, but we can't as the wallet is locked
1089     BOOST_CHECK_THROW(CallRPC("z_getnewaddress"), runtime_error);
1090     
1091     // We can't call RPC walletpassphrase as that invokes RPCRunLater which breaks tests.
1092     // So we manually unlock.
1093     BOOST_CHECK(pwalletMain->Unlock(strWalletPass));
1094     
1095     // Now add a key
1096     BOOST_CHECK_NO_THROW(CallRPC("z_getnewaddress"));
1097     
1098     // Verify the key has been added
1099     BOOST_CHECK_NO_THROW(retValue = CallRPC("z_listaddresses"));
1100     arr = retValue.get_array();
1101     BOOST_CHECK(arr.size() == n+1);    
1102
1103     // We can't simulate over RPC the wallet closing and being reloaded
1104     // but there are tests for this in gtest.
1105 }
1106
1107 BOOST_AUTO_TEST_SUITE_END()
This page took 0.090995 seconds and 4 git commands to generate.