2 # Copyright (c) 2018 The Zcash developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 import sys; assert sys.version_info < (3,), ur"This script does not run under Python 3. Please use Python 2.7.x."
8 from test_framework.test_framework import BitcoinTestFramework
9 from test_framework.util import (
10 assert_equal, assert_true,
11 start_nodes, stop_nodes,
12 initialize_chain_clean, connect_nodes_bi, wait_bitcoinds,
13 wait_and_assert_operationid_status
15 from decimal import Decimal
17 class WalletPersistenceTest (BitcoinTestFramework):
19 def setup_chain(self):
20 print("Initializing test directory " + self.options.tmpdir)
21 initialize_chain_clean(self.options.tmpdir, 3)
23 def setup_network(self, split=False):
24 self.nodes = start_nodes(3, self.options.tmpdir,
26 '-nuparams=5ba81b19:100', # Overwinter
27 '-nuparams=76b809bb:201', # Sapling
29 connect_nodes_bi(self.nodes,0,1)
30 connect_nodes_bi(self.nodes,1,2)
31 self.is_network_split=False
35 # Sanity-check the test harness
36 self.nodes[0].generate(200)
37 assert_equal(self.nodes[0].getblockcount(), 200)
40 # Verify Sapling address is persisted in wallet (even when Sapling is not yet active)
41 sapling_addr = self.nodes[0].z_getnewaddress('sapling')
43 # Make sure the node has the addresss
44 addresses = self.nodes[0].z_listaddresses()
45 assert_true(sapling_addr in addresses, "Should contain address before restart")
48 stop_nodes(self.nodes)
52 # Make sure we still have the address after restarting
53 addresses = self.nodes[0].z_listaddresses()
54 assert_true(sapling_addr in addresses, "Should contain address after restart")
57 self.nodes[0].generate(1)
60 # Node 0 shields funds to Sapling address
61 taddr0 = self.nodes[0].getnewaddress()
63 recipients.append({"address": sapling_addr, "amount": Decimal('20')})
64 myopid = self.nodes[0].z_sendmany(taddr0, recipients, 1, 0)
65 wait_and_assert_operationid_status(self.nodes[0], myopid)
68 self.nodes[0].generate(1)
71 # Verify shielded balance
72 assert_equal(self.nodes[0].z_getbalance(sapling_addr), Decimal('20'))
74 # Verify size of shielded pools
75 pools = self.nodes[0].getblockchaininfo()['valuePools']
76 assert_equal(pools[0]['chainValue'], Decimal('0')) # Sprout
77 assert_equal(pools[1]['chainValue'], Decimal('20')) # Sapling
80 stop_nodes(self.nodes)
84 # Verify size of shielded pools
85 pools = self.nodes[0].getblockchaininfo()['valuePools']
86 assert_equal(pools[0]['chainValue'], Decimal('0')) # Sprout
87 assert_equal(pools[1]['chainValue'], Decimal('20')) # Sapling
89 # Node 0 sends some shielded funds to Node 1
90 dest_addr = self.nodes[1].z_getnewaddress('sapling')
92 recipients.append({"address": dest_addr, "amount": Decimal('15')})
93 myopid = self.nodes[0].z_sendmany(sapling_addr, recipients, 1, 0)
94 wait_and_assert_operationid_status(self.nodes[0], myopid)
97 self.nodes[0].generate(1)
101 assert_equal(self.nodes[0].z_getbalance(sapling_addr), Decimal('5'))
102 assert_equal(self.nodes[1].z_getbalance(dest_addr), Decimal('15'))
105 stop_nodes(self.nodes)
110 assert_equal(self.nodes[0].z_getbalance(sapling_addr), Decimal('5'))
111 assert_equal(self.nodes[1].z_getbalance(dest_addr), Decimal('15'))
113 # Verify importing a spending key will update and persist the nullifiers and witnesses correctly
114 sk0 = self.nodes[0].z_exportkey(sapling_addr)
115 self.nodes[2].z_importkey(sk0, "yes")
116 assert_equal(self.nodes[2].z_getbalance(sapling_addr), Decimal('5'))
119 stop_nodes(self.nodes)
123 # Verify nullifiers persisted correctly by checking balance
124 # Prior to PR #3590, there will be an error as spent notes are considered unspent:
125 # Assertion failed: expected: <25.00000000> but was: <5>
126 assert_equal(self.nodes[2].z_getbalance(sapling_addr), Decimal('5'))
128 # Verity witnesses persisted correctly by sending shielded funds
130 recipients.append({"address": dest_addr, "amount": Decimal('1')})
131 myopid = self.nodes[2].z_sendmany(sapling_addr, recipients, 1, 0)
132 wait_and_assert_operationid_status(self.nodes[2], myopid)
135 self.nodes[0].generate(1)
139 assert_equal(self.nodes[2].z_getbalance(sapling_addr), Decimal('4'))
140 assert_equal(self.nodes[1].z_getbalance(dest_addr), Decimal('16'))
142 if __name__ == '__main__':
143 WalletPersistenceTest().main()