2017-10-11 11:25:41 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2014-2016 The Bitcoin Core developers
|
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
"""Test the -alertnotify, -blocknotify and -walletnotify options."""
|
|
|
|
import os
|
|
|
|
|
2018-10-08 06:39:53 +02:00
|
|
|
from test_framework.address import ADDRESS_BCRT1_UNSPENDABLE
|
2017-10-11 11:25:41 +02:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2019-09-18 20:41:14 +02:00
|
|
|
from test_framework.util import (
|
|
|
|
assert_equal,
|
|
|
|
wait_until,
|
|
|
|
)
|
2017-10-11 11:25:41 +02:00
|
|
|
|
2018-05-29 15:37:53 +02:00
|
|
|
# Linux allow all characters other than \x00
|
|
|
|
# Windows disallow control characters (0-31) and /\?%:|"<>
|
|
|
|
FILE_CHAR_START = 32 if os.name == 'nt' else 1
|
|
|
|
FILE_CHAR_END = 128
|
|
|
|
FILE_CHAR_BLACKLIST = '/\\?%*:|"<>' if os.name == 'nt' else '/'
|
|
|
|
|
|
|
|
|
|
|
|
def notify_outputname(walletname, txid):
|
|
|
|
return txid if os.name == 'nt' else '{}_{}'.format(walletname, txid)
|
|
|
|
|
2018-10-08 06:39:53 +02:00
|
|
|
|
2017-10-11 11:25:41 +02:00
|
|
|
class NotificationsTest(BitcoinTestFramework):
|
|
|
|
def set_test_params(self):
|
|
|
|
self.num_nodes = 2
|
|
|
|
self.setup_clean_chain = True
|
|
|
|
|
|
|
|
def setup_network(self):
|
2018-05-29 15:37:53 +02:00
|
|
|
self.wallet = ''.join(chr(i) for i in range(FILE_CHAR_START, FILE_CHAR_END) if chr(i) not in FILE_CHAR_BLACKLIST)
|
2021-08-12 08:28:30 +02:00
|
|
|
self.alertnotify_dir = os.path.join(self.options.tmpdir, "alertnotify")
|
|
|
|
self.blocknotify_dir = os.path.join(self.options.tmpdir, "blocknotify")
|
|
|
|
self.walletnotify_dir = os.path.join(self.options.tmpdir, "walletnotify")
|
|
|
|
os.mkdir(self.alertnotify_dir)
|
|
|
|
os.mkdir(self.blocknotify_dir)
|
|
|
|
os.mkdir(self.walletnotify_dir)
|
2017-10-11 11:25:41 +02:00
|
|
|
|
|
|
|
# -alertnotify and -blocknotify on node0, walletnotify on node1
|
2018-10-08 06:39:53 +02:00
|
|
|
self.extra_args = [[
|
2021-08-12 08:28:30 +02:00
|
|
|
"-alertnotify=echo > {}".format(os.path.join(self.alertnotify_dir, '%s')),
|
|
|
|
"-blocknotify=echo > {}".format(os.path.join(self.blocknotify_dir, '%s'))],
|
2017-10-11 11:25:41 +02:00
|
|
|
["-blockversion=211",
|
|
|
|
"-rescan",
|
2018-05-29 15:37:53 +02:00
|
|
|
"-walletnotify=echo > {}".format(os.path.join(self.walletnotify_dir, notify_outputname('%w', '%s')))]]
|
2022-11-30 20:23:48 +01:00
|
|
|
self.wallet_names = [self.default_wallet_name, self.wallet]
|
2017-10-11 11:25:41 +02:00
|
|
|
super().setup_network()
|
|
|
|
|
|
|
|
def run_test(self):
|
2023-02-14 09:48:36 +01:00
|
|
|
if self.is_wallet_compiled():
|
|
|
|
# Make the wallets
|
|
|
|
# Ensures that node 0 and node 1 share the same wallet for the conflicting transaction tests below.
|
|
|
|
for i, name in enumerate(self.wallet_names):
|
|
|
|
self.nodes[i].createwallet(wallet_name=name, load_on_startup=True)
|
|
|
|
|
2017-10-11 11:25:41 +02:00
|
|
|
self.log.info("test -blocknotify")
|
|
|
|
block_count = 10
|
2018-10-08 06:39:53 +02:00
|
|
|
blocks = self.nodes[1].generatetoaddress(block_count, self.nodes[1].getnewaddress() if self.is_wallet_compiled() else ADDRESS_BCRT1_UNSPENDABLE)
|
2017-10-11 11:25:41 +02:00
|
|
|
|
2021-08-12 08:28:30 +02:00
|
|
|
# wait at most 10 seconds for expected number of files before reading the content
|
|
|
|
wait_until(lambda: len(os.listdir(self.blocknotify_dir)) == block_count, timeout=10)
|
2017-10-11 11:25:41 +02:00
|
|
|
|
2021-08-12 08:28:30 +02:00
|
|
|
# directory content should equal the generated blocks hashes
|
|
|
|
assert_equal(sorted(blocks), sorted(os.listdir(self.blocknotify_dir)))
|
2017-10-11 11:25:41 +02:00
|
|
|
|
2018-10-08 06:39:53 +02:00
|
|
|
if self.is_wallet_compiled():
|
|
|
|
self.log.info("test -walletnotify")
|
|
|
|
# wait at most 10 seconds for expected number of files before reading the content
|
|
|
|
wait_until(lambda: len(os.listdir(self.walletnotify_dir)) == block_count, timeout=10)
|
|
|
|
|
|
|
|
# directory content should equal the generated transaction hashes
|
2018-05-29 15:37:53 +02:00
|
|
|
txids_rpc = list(map(lambda t: notify_outputname(self.wallet, t['txid']), self.nodes[1].listtransactions("*", block_count)))
|
2018-10-08 06:39:53 +02:00
|
|
|
assert_equal(sorted(txids_rpc), sorted(os.listdir(self.walletnotify_dir)))
|
|
|
|
self.stop_node(1)
|
2017-10-11 11:25:41 +02:00
|
|
|
|
2018-10-08 06:39:53 +02:00
|
|
|
for tx_file in os.listdir(self.walletnotify_dir):
|
|
|
|
os.remove(os.path.join(self.walletnotify_dir, tx_file))
|
2017-10-11 11:25:41 +02:00
|
|
|
|
2018-10-08 06:39:53 +02:00
|
|
|
self.log.info("test -walletnotify after rescan")
|
|
|
|
# restart node to rescan to force wallet notifications
|
|
|
|
self.start_node(1)
|
2022-09-24 14:36:35 +02:00
|
|
|
self.connect_nodes(0, 1)
|
2017-10-11 11:25:41 +02:00
|
|
|
|
2018-10-08 06:39:53 +02:00
|
|
|
wait_until(lambda: len(os.listdir(self.walletnotify_dir)) == block_count, timeout=10)
|
2017-10-11 11:25:41 +02:00
|
|
|
|
2018-10-08 06:39:53 +02:00
|
|
|
# directory content should equal the generated transaction hashes
|
2018-05-29 15:37:53 +02:00
|
|
|
txids_rpc = list(map(lambda t: notify_outputname(self.wallet, t['txid']), self.nodes[1].listtransactions("*", block_count)))
|
2018-10-08 06:39:53 +02:00
|
|
|
assert_equal(sorted(txids_rpc), sorted(os.listdir(self.walletnotify_dir)))
|
2017-10-11 11:25:41 +02:00
|
|
|
|
2019-02-26 15:20:25 +01:00
|
|
|
# TODO: add test for `-alertnotify` large fork notifications
|
2017-10-11 11:25:41 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
NotificationsTest().main()
|