Skip to content

Commit

Permalink
[wip] test dash specific notifications in feature_notifications.py
Browse files Browse the repository at this point in the history
  • Loading branch information
UdjinM6 committed Aug 2, 2023
1 parent e838ae3 commit d1461b0
Showing 1 changed file with 65 additions and 18 deletions.
83 changes: 65 additions & 18 deletions test/functional/feature_notifications.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#!/usr/bin/env python3
# Copyright (c) 2014-2016 The Bitcoin Core developers
# Copyright (c) 2023 The Dash 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."""
"""Test the -alertnotify, -blocknotify, -chainlocknotify, -instantsendnotify and -walletnotify options."""
import os

from test_framework.address import ADDRESS_BCRT1_UNSPENDABLE
from test_framework.test_framework import BitcoinTestFramework
from test_framework.test_framework import DashTestFramework
from test_framework.util import (
assert_equal,
force_finish_mnsync,
wait_until,
)

Expand All @@ -23,40 +25,50 @@ def notify_outputname(walletname, txid):
return txid if os.name == 'nt' else '{}_{}'.format(walletname, txid)


class NotificationsTest(BitcoinTestFramework):
class NotificationsTest(DashTestFramework):
def set_test_params(self):
self.num_nodes = 2
self.setup_clean_chain = True
self.set_dash_test_params(5, 3, fast_dip3_enforcement=True)

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_CHARS_DISALLOWED)
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")
self.chainlocknotify_dir = os.path.join(self.options.tmpdir, "chainlocknotify")
self.instantsendnotify_dir = os.path.join(self.options.tmpdir, "instantsendnotify")
os.mkdir(self.alertnotify_dir)
os.mkdir(self.blocknotify_dir)
os.mkdir(self.walletnotify_dir)
os.mkdir(self.chainlocknotify_dir)
os.mkdir(self.instantsendnotify_dir)

# -alertnotify and -blocknotify on node0, walletnotify on node1
self.extra_args = [[
"-alertnotify=echo > {}".format(os.path.join(self.alertnotify_dir, '%s')),
"-blocknotify=echo > {}".format(os.path.join(self.blocknotify_dir, '%s'))],
["-blockversion=211",
"-rescan",
"-walletnotify=echo > {}".format(os.path.join(self.walletnotify_dir, notify_outputname('%w', '%s')))]]
self.wallet_names = [self.default_wallet_name, self.wallet]
self.extra_args[0].append("-alertnotify=echo > {}".format(os.path.join(self.alertnotify_dir, '%s')))
self.extra_args[0].append("-blocknotify=echo > {}".format(os.path.join(self.blocknotify_dir, '%s')))
self.extra_args[1].append("-blockversion=211")
self.extra_args[1].append("-rescan")
self.extra_args[1].append("-walletnotify=echo > {}".format(os.path.join(self.walletnotify_dir, notify_outputname('%w', '%s'))))

# -chainlocknotify on node0, -instantsendnotify on node1
self.extra_args[0].append("-chainlocknotify=echo > {}".format(os.path.join(self.chainlocknotify_dir, '%s')))
self.extra_args[1].append("-instantsendnotify=echo > {}".format(os.path.join(self.instantsendnotify_dir, notify_outputname('%w', '%s'))))

super().setup_network()

def run_test(self):
# remove files created during network setup
for block_file in os.listdir(self.blocknotify_dir):
os.remove(os.path.join(self.blocknotify_dir, block_file))
for tx_file in os.listdir(self.walletnotify_dir):
os.remove(os.path.join(self.walletnotify_dir, tx_file))

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)
self.nodes[1].createwallet(wallet_name=self.wallet, load_on_startup=True)
rpc = self.nodes[1].get_wallet_rpc(self.wallet)

self.log.info("test -blocknotify")
block_count = 10
blocks = self.nodes[1].generatetoaddress(block_count, self.nodes[1].getnewaddress() if self.is_wallet_compiled() else ADDRESS_BCRT1_UNSPENDABLE)
blocks = rpc.generatetoaddress(block_count, rpc.getnewaddress() if self.is_wallet_compiled() else ADDRESS_BCRT1_UNSPENDABLE)

# 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)
Expand All @@ -70,7 +82,7 @@ def run_test(self):
wait_until(lambda: len(os.listdir(self.walletnotify_dir)) == block_count, timeout=10)

# directory content should equal the generated transaction hashes
txids_rpc = list(map(lambda t: notify_outputname(self.wallet, t['txid']), self.nodes[1].listtransactions("*", block_count)))
txids_rpc = list(map(lambda t: notify_outputname(self.wallet, t['txid']), rpc.listtransactions("*", block_count)))
assert_equal(sorted(txids_rpc), sorted(os.listdir(self.walletnotify_dir)))
self.stop_node(1)

Expand All @@ -80,6 +92,7 @@ def run_test(self):
self.log.info("test -walletnotify after rescan")
# restart node to rescan to force wallet notifications
self.start_node(1)
force_finish_mnsync(self.nodes[1])
self.connect_nodes(0, 1)

wait_until(lambda: len(os.listdir(self.walletnotify_dir)) == block_count, timeout=10)
Expand All @@ -88,6 +101,40 @@ def run_test(self):
txids_rpc = list(map(lambda t: notify_outputname(self.wallet, t['txid']), self.nodes[1].listtransactions("*", block_count)))
assert_equal(sorted(txids_rpc), sorted(os.listdir(self.walletnotify_dir)))

self.log.info("test -chainlocknotify")

self.activate_dip8()
self.nodes[0].sporkupdate("SPORK_17_QUORUM_DKG_ENABLED", 0)
self.nodes[0].sporkupdate("SPORK_19_CHAINLOCKS_ENABLED", 4070908800)
self.wait_for_sporks_same()
for i in range(4):
self.mine_quorum()
self.nodes[0].sporkupdate("SPORK_19_CHAINLOCKS_ENABLED", 0)
self.wait_for_sporks_same()

self.log.info("Mine single block, wait for chainlock")
self.bump_mocktime(1)
tip = self.nodes[0].generate(1)[-1]
self.wait_for_chainlocked_block_all_nodes(tip)
# directory content should equal the chainlocked block hash
assert_equal([tip], sorted(os.listdir(self.chainlocknotify_dir)))

if self.is_wallet_compiled():
self.log.info("test -instantsendnotify")
assert_equal(len(os.listdir(self.instantsendnotify_dir)), 0)

tx_count = 10
for _ in range(tx_count):
txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 1)
self.wait_for_instantlock(txid, self.nodes[1])

# wait at most 10 seconds for expected number of files before reading the content
wait_until(lambda: len(os.listdir(self.instantsendnotify_dir)) == tx_count, timeout=10)

# directory content should equal the generated transaction hashes
txids_rpc = list(map(lambda t: notify_outputname(self.wallet, t['txid']), self.nodes[1].listtransactions("*", tx_count)))
assert_equal(sorted(txids_rpc), sorted(os.listdir(self.instantsendnotify_dir)))

# TODO: add test for `-alertnotify` large fork notifications

if __name__ == '__main__':
Expand Down

0 comments on commit d1461b0

Please sign in to comment.