Changeset View
Changeset View
Standalone View
Standalone View
test/functional/feature_notifications.py
Show All 10 Lines | |||||
from test_framework.util import ( | from test_framework.util import ( | ||||
assert_equal, | assert_equal, | ||||
connect_nodes, | connect_nodes, | ||||
wait_until | wait_until | ||||
) | ) | ||||
FORK_WARNING_MESSAGE = "Warning: Large-work fork detected, forking after block {}" | FORK_WARNING_MESSAGE = "Warning: Large-work fork detected, forking after block {}" | ||||
# 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) | |||||
class NotificationsTest(BitcoinTestFramework): | class NotificationsTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.num_nodes = 2 | self.num_nodes = 2 | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
def setup_network(self): | def setup_network(self): | ||||
self.wallet = ''.join( | |||||
chr(i) for i in range( | |||||
FILE_CHAR_START, | |||||
FILE_CHAR_END) if chr(i) not in FILE_CHAR_BLACKLIST) | |||||
self.alertnotify_dir = os.path.join(self.options.tmpdir, "alertnotify") | self.alertnotify_dir = os.path.join(self.options.tmpdir, "alertnotify") | ||||
self.blocknotify_dir = os.path.join(self.options.tmpdir, "blocknotify") | self.blocknotify_dir = os.path.join(self.options.tmpdir, "blocknotify") | ||||
self.walletnotify_dir = os.path.join( | self.walletnotify_dir = os.path.join( | ||||
self.options.tmpdir, "walletnotify") | self.options.tmpdir, "walletnotify") | ||||
os.mkdir(self.alertnotify_dir) | os.mkdir(self.alertnotify_dir) | ||||
os.mkdir(self.blocknotify_dir) | os.mkdir(self.blocknotify_dir) | ||||
os.mkdir(self.walletnotify_dir) | os.mkdir(self.walletnotify_dir) | ||||
# -alertnotify and -blocknotify on node0, walletnotify on node1 | # -alertnotify and -blocknotify on node0, walletnotify on node1 | ||||
self.extra_args = [["-alertnotify=echo > {}".format( | self.extra_args = [["-alertnotify=echo > {}".format( | ||||
os.path.join(self.alertnotify_dir, '%s')), | os.path.join(self.alertnotify_dir, '%s')), | ||||
"-blocknotify=echo > {}".format(os.path.join(self.blocknotify_dir, '%s'))], | "-blocknotify=echo > {}".format(os.path.join(self.blocknotify_dir, '%s'))], | ||||
["-blockversion=211", | ["-blockversion=211", | ||||
"-rescan", | "-rescan", | ||||
"-walletnotify=echo > {}".format(os.path.join(self.walletnotify_dir, '%s'))]] | "-wallet={}".format(self.wallet), | ||||
"-walletnotify=echo > {}".format(os.path.join(self.walletnotify_dir, notify_outputname('%w', '%s')))]] | |||||
super().setup_network() | super().setup_network() | ||||
def run_test(self): | def run_test(self): | ||||
self.log.info("test -blocknotify") | self.log.info("test -blocknotify") | ||||
block_count = 10 | block_count = 10 | ||||
blocks = self.nodes[1].generatetoaddress( | blocks = self.nodes[1].generatetoaddress( | ||||
block_count, | block_count, | ||||
self.nodes[1].getnewaddress() if self.is_wallet_compiled() | self.nodes[1].getnewaddress() if self.is_wallet_compiled() | ||||
Show All 17 Lines | def run_test(self): | ||||
# the content | # the content | ||||
wait_until( | wait_until( | ||||
lambda: len( | lambda: len( | ||||
os.listdir( | os.listdir( | ||||
self.walletnotify_dir)) == block_count, | self.walletnotify_dir)) == block_count, | ||||
timeout=10) | timeout=10) | ||||
# directory content should equal the generated transaction hashes | # directory content should equal the generated transaction hashes | ||||
txids_rpc = list( | txids_rpc = list(map(lambda t: notify_outputname( | ||||
map(lambda t: t['txid'], self.nodes[1].listtransactions("*", block_count))) | self.wallet, t['txid']), self.nodes[1].listtransactions("*", block_count))) | ||||
assert_equal( | assert_equal( | ||||
sorted(txids_rpc), sorted( | sorted(txids_rpc), sorted( | ||||
os.listdir( | os.listdir( | ||||
self.walletnotify_dir))) | self.walletnotify_dir))) | ||||
self.stop_node(1) | self.stop_node(1) | ||||
for tx_file in os.listdir(self.walletnotify_dir): | for tx_file in os.listdir(self.walletnotify_dir): | ||||
os.remove(os.path.join(self.walletnotify_dir, tx_file)) | os.remove(os.path.join(self.walletnotify_dir, tx_file)) | ||||
self.log.info("test -walletnotify after rescan") | self.log.info("test -walletnotify after rescan") | ||||
# restart node to rescan to force wallet notifications | # restart node to rescan to force wallet notifications | ||||
self.start_node(1) | self.start_node(1) | ||||
connect_nodes(self.nodes[0], self.nodes[1]) | connect_nodes(self.nodes[0], self.nodes[1]) | ||||
wait_until( | wait_until( | ||||
lambda: len( | lambda: len( | ||||
os.listdir( | os.listdir( | ||||
self.walletnotify_dir)) == block_count, | self.walletnotify_dir)) == block_count, | ||||
timeout=10) | timeout=10) | ||||
# directory content should equal the generated transaction hashes | # directory content should equal the generated transaction hashes | ||||
txids_rpc = list( | txids_rpc = list(map(lambda t: notify_outputname( | ||||
map(lambda t: t['txid'], self.nodes[1].listtransactions("*", block_count))) | self.wallet, t['txid']), self.nodes[1].listtransactions("*", block_count))) | ||||
assert_equal( | assert_equal( | ||||
sorted(txids_rpc), sorted( | sorted(txids_rpc), sorted( | ||||
os.listdir( | os.listdir( | ||||
self.walletnotify_dir))) | self.walletnotify_dir))) | ||||
# Create an invalid chain and ensure the node warns. | # Create an invalid chain and ensure the node warns. | ||||
self.log.info("test -alertnotify for forked chain") | self.log.info("test -alertnotify for forked chain") | ||||
fork_block = self.nodes[0].getbestblockhash() | fork_block = self.nodes[0].getbestblockhash() | ||||
Show All 22 Lines |