2018-12-18 12:00:46 -08:00
|
|
|
#!/usr/bin/env python
|
2018-09-13 14:22:00 -07:00
|
|
|
# Copyright (c) 2018 The Zcash developers
|
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2018-12-18 12:00:46 -08:00
|
|
|
import sys; assert sys.version_info < (3,), ur"This script does not run under Python 3. Please use Python 2.7.x."
|
|
|
|
|
2018-09-13 14:22:00 -07:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2018-10-06 22:53:24 -07:00
|
|
|
from test_framework.util import (
|
|
|
|
assert_equal, assert_true,
|
2018-03-10 02:19:44 -08:00
|
|
|
get_coinbase_address,
|
2018-10-06 22:53:24 -07:00
|
|
|
start_nodes, stop_nodes,
|
|
|
|
initialize_chain_clean, connect_nodes_bi, wait_bitcoinds,
|
|
|
|
wait_and_assert_operationid_status
|
|
|
|
)
|
|
|
|
from decimal import Decimal
|
2018-09-13 14:22:00 -07:00
|
|
|
|
|
|
|
class WalletPersistenceTest (BitcoinTestFramework):
|
|
|
|
|
2018-10-06 22:53:24 -07:00
|
|
|
def setup_chain(self):
|
|
|
|
print("Initializing test directory " + self.options.tmpdir)
|
2018-10-11 21:52:56 -07:00
|
|
|
initialize_chain_clean(self.options.tmpdir, 3)
|
2018-10-06 22:53:24 -07:00
|
|
|
|
2018-09-13 14:22:00 -07:00
|
|
|
def setup_network(self, split=False):
|
2018-10-11 21:52:56 -07:00
|
|
|
self.nodes = start_nodes(3, self.options.tmpdir,
|
2018-10-06 22:53:24 -07:00
|
|
|
extra_args=[[
|
|
|
|
'-nuparams=5ba81b19:100', # Overwinter
|
|
|
|
'-nuparams=76b809bb:201', # Sapling
|
2018-10-11 21:52:56 -07:00
|
|
|
]] * 3)
|
2018-10-06 22:53:24 -07:00
|
|
|
connect_nodes_bi(self.nodes,0,1)
|
2018-10-11 21:52:56 -07:00
|
|
|
connect_nodes_bi(self.nodes,1,2)
|
2018-10-06 22:53:24 -07:00
|
|
|
self.is_network_split=False
|
2018-09-13 14:22:00 -07:00
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
def run_test(self):
|
2018-10-06 22:53:24 -07:00
|
|
|
# Sanity-check the test harness
|
|
|
|
self.nodes[0].generate(200)
|
|
|
|
assert_equal(self.nodes[0].getblockcount(), 200)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
# Verify Sapling address is persisted in wallet (even when Sapling is not yet active)
|
2018-09-13 14:22:00 -07:00
|
|
|
sapling_addr = self.nodes[0].z_getnewaddress('sapling')
|
2018-10-06 22:53:24 -07:00
|
|
|
|
|
|
|
# Make sure the node has the addresss
|
2018-09-13 14:22:00 -07:00
|
|
|
addresses = self.nodes[0].z_listaddresses()
|
|
|
|
assert_true(sapling_addr in addresses, "Should contain address before restart")
|
2018-10-06 22:53:24 -07:00
|
|
|
|
|
|
|
# Restart the nodes
|
2018-09-13 14:22:00 -07:00
|
|
|
stop_nodes(self.nodes)
|
|
|
|
wait_bitcoinds()
|
2018-10-06 22:53:24 -07:00
|
|
|
self.setup_network()
|
|
|
|
|
|
|
|
# Make sure we still have the address after restarting
|
2018-09-13 14:22:00 -07:00
|
|
|
addresses = self.nodes[0].z_listaddresses()
|
|
|
|
assert_true(sapling_addr in addresses, "Should contain address after restart")
|
|
|
|
|
2018-10-06 22:53:24 -07:00
|
|
|
# Activate Sapling
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
# Node 0 shields funds to Sapling address
|
2018-03-10 02:19:44 -08:00
|
|
|
taddr0 = get_coinbase_address(self.nodes[0], 101)
|
2018-10-06 22:53:24 -07:00
|
|
|
recipients = []
|
|
|
|
recipients.append({"address": sapling_addr, "amount": Decimal('20')})
|
|
|
|
myopid = self.nodes[0].z_sendmany(taddr0, recipients, 1, 0)
|
|
|
|
wait_and_assert_operationid_status(self.nodes[0], myopid)
|
|
|
|
|
|
|
|
self.sync_all()
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
# Verify shielded balance
|
|
|
|
assert_equal(self.nodes[0].z_getbalance(sapling_addr), Decimal('20'))
|
|
|
|
|
2018-11-16 08:14:52 -08:00
|
|
|
# Verify size of shielded pools
|
|
|
|
pools = self.nodes[0].getblockchaininfo()['valuePools']
|
|
|
|
assert_equal(pools[0]['chainValue'], Decimal('0')) # Sprout
|
|
|
|
assert_equal(pools[1]['chainValue'], Decimal('20')) # Sapling
|
|
|
|
|
2018-10-07 16:22:33 -07:00
|
|
|
# Restart the nodes
|
|
|
|
stop_nodes(self.nodes)
|
|
|
|
wait_bitcoinds()
|
|
|
|
self.setup_network()
|
|
|
|
|
2018-11-16 08:14:52 -08:00
|
|
|
# Verify size of shielded pools
|
|
|
|
pools = self.nodes[0].getblockchaininfo()['valuePools']
|
|
|
|
assert_equal(pools[0]['chainValue'], Decimal('0')) # Sprout
|
|
|
|
assert_equal(pools[1]['chainValue'], Decimal('20')) # Sapling
|
|
|
|
|
2018-10-06 22:53:24 -07:00
|
|
|
# Node 0 sends some shielded funds to Node 1
|
|
|
|
dest_addr = self.nodes[1].z_getnewaddress('sapling')
|
|
|
|
recipients = []
|
|
|
|
recipients.append({"address": dest_addr, "amount": Decimal('15')})
|
|
|
|
myopid = self.nodes[0].z_sendmany(sapling_addr, recipients, 1, 0)
|
|
|
|
wait_and_assert_operationid_status(self.nodes[0], myopid)
|
|
|
|
|
|
|
|
self.sync_all()
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
# Verify balances
|
|
|
|
assert_equal(self.nodes[0].z_getbalance(sapling_addr), Decimal('5'))
|
|
|
|
assert_equal(self.nodes[1].z_getbalance(dest_addr), Decimal('15'))
|
|
|
|
|
|
|
|
# Restart the nodes
|
|
|
|
stop_nodes(self.nodes)
|
|
|
|
wait_bitcoinds()
|
|
|
|
self.setup_network()
|
|
|
|
|
|
|
|
# Verify balances
|
|
|
|
assert_equal(self.nodes[0].z_getbalance(sapling_addr), Decimal('5'))
|
|
|
|
assert_equal(self.nodes[1].z_getbalance(dest_addr), Decimal('15'))
|
|
|
|
|
2018-10-11 21:52:56 -07:00
|
|
|
# Verify importing a spending key will update and persist the nullifiers and witnesses correctly
|
|
|
|
sk0 = self.nodes[0].z_exportkey(sapling_addr)
|
|
|
|
self.nodes[2].z_importkey(sk0, "yes")
|
|
|
|
assert_equal(self.nodes[2].z_getbalance(sapling_addr), Decimal('5'))
|
|
|
|
|
|
|
|
# Restart the nodes
|
|
|
|
stop_nodes(self.nodes)
|
|
|
|
wait_bitcoinds()
|
|
|
|
self.setup_network()
|
|
|
|
|
|
|
|
# Verify nullifiers persisted correctly by checking balance
|
|
|
|
# Prior to PR #3590, there will be an error as spent notes are considered unspent:
|
|
|
|
# Assertion failed: expected: <25.00000000> but was: <5>
|
|
|
|
assert_equal(self.nodes[2].z_getbalance(sapling_addr), Decimal('5'))
|
|
|
|
|
|
|
|
# Verity witnesses persisted correctly by sending shielded funds
|
|
|
|
recipients = []
|
|
|
|
recipients.append({"address": dest_addr, "amount": Decimal('1')})
|
|
|
|
myopid = self.nodes[2].z_sendmany(sapling_addr, recipients, 1, 0)
|
|
|
|
wait_and_assert_operationid_status(self.nodes[2], myopid)
|
|
|
|
|
|
|
|
self.sync_all()
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
# Verify balances
|
|
|
|
assert_equal(self.nodes[2].z_getbalance(sapling_addr), Decimal('4'))
|
|
|
|
assert_equal(self.nodes[1].z_getbalance(dest_addr), Decimal('16'))
|
|
|
|
|
2018-09-13 14:22:00 -07:00
|
|
|
if __name__ == '__main__':
|
|
|
|
WalletPersistenceTest().main()
|