Skip to content

Commit 857b32b

Browse files
committed
[tests] Add -walletnotify functional test
1 parent df18d29 commit 857b32b

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

test/functional/notifications.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,29 @@
22
# Copyright (c) 2014-2016 The Bitcoin Core developers
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5-
"""Test the -alertnotify and -blocknotify options."""
5+
"""Test the -alertnotify, -blocknotify and -walletnotify options."""
66
import os
77

88
from test_framework.test_framework import BitcoinTestFramework
9-
from test_framework.util import assert_equal, wait_until
9+
from test_framework.util import assert_equal, wait_until, connect_nodes_bi
1010

1111
class NotificationsTest(BitcoinTestFramework):
1212
def set_test_params(self):
1313
self.num_nodes = 2
14+
self.setup_clean_chain = True
1415

1516
def setup_network(self):
1617
self.alert_filename = os.path.join(self.options.tmpdir, "alert.txt")
17-
self.block_filename = os.path.join(self.options.tmpdir, 'blocks.txt')
18+
self.block_filename = os.path.join(self.options.tmpdir, "blocks.txt")
19+
self.tx_filename = os.path.join(self.options.tmpdir, "transactions.txt")
20+
21+
# -alertnotify and -blocknotify on node0, walletnotify on node1
1822
self.extra_args = [["-blockversion=2",
1923
"-alertnotify=echo %%s >> %s" % self.alert_filename,
2024
"-blocknotify=echo %%s >> %s" % self.block_filename],
21-
["-blockversion=211"]]
25+
["-blockversion=211",
26+
"-rescan",
27+
"-walletnotify=echo %%s >> %s" % self.tx_filename]]
2228
super().setup_network()
2329

2430
def run_test(self):
@@ -33,6 +39,28 @@ def run_test(self):
3339
with open(self.block_filename, 'r') as f:
3440
assert_equal(sorted(blocks), sorted(f.read().splitlines()))
3541

42+
self.log.info("test -walletnotify")
43+
# wait at most 10 seconds for expected file size before reading the content
44+
wait_until(lambda: os.path.isfile(self.tx_filename) and os.stat(self.tx_filename).st_size >= (block_count * 65), timeout=10)
45+
46+
# file content should equal the generated transaction hashes
47+
txids_rpc = list(map(lambda t: t['txid'], self.nodes[1].listtransactions("*", block_count)))
48+
with open(self.tx_filename, 'r') as f:
49+
assert_equal(sorted(txids_rpc), sorted(f.read().splitlines()))
50+
os.remove(self.tx_filename)
51+
52+
self.log.info("test -walletnotify after rescan")
53+
# restart node to rescan to force wallet notifications
54+
self.restart_node(1)
55+
connect_nodes_bi(self.nodes, 0, 1)
56+
57+
wait_until(lambda: os.path.isfile(self.tx_filename) and os.stat(self.tx_filename).st_size >= (block_count * 65), timeout=10)
58+
59+
# file content should equal the generated transaction hashes
60+
txids_rpc = list(map(lambda t: t['txid'], self.nodes[1].listtransactions("*", block_count)))
61+
with open(self.tx_filename, 'r') as f:
62+
assert_equal(sorted(txids_rpc), sorted(f.read().splitlines()))
63+
3664
# Mine another 41 up-version blocks. -alertnotify should trigger on the 51st.
3765
self.log.info("test -alertnotify")
3866
self.nodes[1].generate(41)

0 commit comments

Comments
 (0)