Skip to content

Commit 72eaab0

Browse files
committed
tests: functional watch-only wallet tests
These test the new watch-only defaults for rpcs with include_watchonly and includeWatching options. Signed-off-by: William Casarin <[email protected]>
1 parent 72ffbdc commit 72eaab0

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

test/functional/test_runner.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@
127127
'wallet_multiwallet.py --usecli',
128128
'wallet_createwallet.py',
129129
'wallet_createwallet.py --usecli',
130+
'wallet_watchonly.py',
131+
'wallet_watchonly.py --usecli',
130132
'interface_http.py',
131133
'interface_rpc.py',
132134
'rpc_psbt.py',

test/functional/wallet_watchonly.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2018-2019 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Test createwallet arguments.
6+
"""
7+
8+
from test_framework.test_framework import BitcoinTestFramework
9+
from test_framework.util import (
10+
assert_equal,
11+
assert_raises_rpc_error
12+
)
13+
14+
15+
class CreateWalletWatchonlyTest(BitcoinTestFramework):
16+
def set_test_params(self):
17+
self.setup_clean_chain = False
18+
self.num_nodes = 1
19+
self.supports_cli = True
20+
21+
def skip_test_if_missing_module(self):
22+
self.skip_if_no_wallet()
23+
24+
def run_test(self):
25+
node = self.nodes[0]
26+
27+
self.nodes[0].createwallet(wallet_name='default')
28+
def_wallet = node.get_wallet_rpc('default')
29+
30+
a1 = def_wallet.getnewaddress()
31+
wo_change = def_wallet.getnewaddress()
32+
wo_addr = def_wallet.getnewaddress()
33+
34+
self.nodes[0].createwallet(wallet_name='wo', disable_private_keys=True)
35+
wo_wallet = node.get_wallet_rpc('wo')
36+
37+
wo_wallet.importpubkey(pubkey=def_wallet.getaddressinfo(wo_addr)['pubkey'])
38+
wo_wallet.importpubkey(pubkey=def_wallet.getaddressinfo(wo_change)['pubkey'])
39+
40+
# generate some btc for testing
41+
node.generatetoaddress(101, a1)
42+
43+
# send 1 btc to our watch-only address
44+
txid = def_wallet.sendtoaddress(wo_addr, 1)
45+
self.nodes[0].generate(1)
46+
47+
# getbalance
48+
self.log.info('include_watchonly should default to true for watch-only wallets')
49+
self.log.info('Testing getbalance watch-only defaults')
50+
assert_equal(wo_wallet.getbalance(), 1)
51+
assert_equal(len(wo_wallet.listtransactions()), 1)
52+
assert_equal(wo_wallet.getbalance(include_watchonly=False), 0)
53+
54+
self.log.info('Testing listreceivedbyaddress watch-only defaults')
55+
result = wo_wallet.listreceivedbyaddress()
56+
assert_equal(len(result), 1)
57+
assert_equal(result[0]["involvesWatchonly"], True)
58+
result = wo_wallet.listreceivedbyaddress(include_watchonly=False)
59+
assert_equal(len(result), 0)
60+
61+
self.log.info('Testing listreceivedbylabel watch-only defaults')
62+
result = wo_wallet.listreceivedbylabel()
63+
assert_equal(len(result), 1)
64+
assert_equal(result[0]["involvesWatchonly"], True)
65+
result = wo_wallet.listreceivedbylabel(include_watchonly=False)
66+
assert_equal(len(result), 0)
67+
68+
self.log.info('Testing listtransactions watch-only defaults')
69+
result = wo_wallet.listtransactions()
70+
assert_equal(len(result), 1)
71+
assert_equal(result[0]["involvesWatchonly"], True)
72+
result = wo_wallet.listtransactions(include_watchonly=False)
73+
assert_equal(len(result), 0)
74+
75+
self.log.info('Testing listsinceblock watch-only defaults')
76+
result = wo_wallet.listsinceblock()
77+
assert_equal(len(result["transactions"]), 1)
78+
assert_equal(result["transactions"][0]["involvesWatchonly"], True)
79+
result = wo_wallet.listsinceblock(include_watchonly=False)
80+
assert_equal(len(result["transactions"]), 0)
81+
82+
self.log.info('Testing gettransaction watch-only defaults')
83+
result = wo_wallet.gettransaction(txid)
84+
assert_equal(result["details"][0]["involvesWatchonly"], True)
85+
result = wo_wallet.gettransaction(txid=txid, include_watchonly=False)
86+
assert_equal(len(result["details"]), 0)
87+
88+
self.log.info('Testing walletcreatefundedpsbt watch-only defaults')
89+
inputs = []
90+
outputs = [{a1: 0.5}]
91+
options = {'changeAddress': wo_change}
92+
no_wo_options = {'changeAddress': wo_change, 'includeWatching': False}
93+
94+
result = wo_wallet.walletcreatefundedpsbt(inputs=inputs, outputs=outputs, options=options)
95+
assert_equal("psbt" in result, True)
96+
assert_raises_rpc_error(-4, "Insufficient funds", wo_wallet.walletcreatefundedpsbt, inputs, outputs, 0, no_wo_options)
97+
98+
self.log.info('Testing fundrawtransaction watch-only defaults')
99+
rawtx = wo_wallet.createrawtransaction(inputs=inputs, outputs=outputs)
100+
result = wo_wallet.fundrawtransaction(hexstring=rawtx, options=options)
101+
assert_equal("hex" in result, True)
102+
assert_raises_rpc_error(-4, "Insufficient funds", wo_wallet.fundrawtransaction, rawtx, no_wo_options)
103+
104+
105+
106+
if __name__ == '__main__':
107+
CreateWalletWatchonlyTest().main()

0 commit comments

Comments
 (0)