feature_notifications.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 2014-2022 The Limenka 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 the -alertnotify, -blocknotify and -walletnotify options."""
   6  import os
   7  import platform
   8  
   9  from test_framework.address import ADDRESS_BCRT1_UNSPENDABLE
  10  from test_framework.descriptors import descsum_create
  11  from test_framework.test_framework import LimenkaTestFramework
  12  from test_framework.util import (
  13      assert_equal,
  14  )
  15  
  16  # Linux allow all characters other than \x00
  17  # Windows disallow control characters (0-31) and /\?%:|"<>
  18  FILE_CHAR_START = 32 if platform.system() == 'Windows' else 1
  19  FILE_CHAR_END = 128
  20  FILE_CHARS_DISALLOWED = '/\\?%*:|"<>' if platform.system() == 'Windows' else '/'
  21  UNCONFIRMED_HASH_STRING = 'unconfirmed'
  22  
  23  def notify_outputname(walletname, txid):
  24      return txid if platform.system() == 'Windows' else f'{walletname}_{txid}'
  25  
  26  
  27  class NotificationsTest(LimenkaTestFramework):
  28      def add_options(self, parser):
  29          self.add_wallet_options(parser)
  30  
  31      def set_test_params(self):
  32          self.num_nodes = 2
  33          self.setup_clean_chain = True
  34  
  35      def setup_network(self):
  36          self.wallet = ''.join(chr(i) for i in range(FILE_CHAR_START, FILE_CHAR_END) if chr(i) not in FILE_CHARS_DISALLOWED)
  37          self.alertnotify_dir = os.path.join(self.options.tmpdir, "alertnotify")
  38          self.blocknotify_dir = os.path.join(self.options.tmpdir, "blocknotify")
  39          self.walletnotify_dir = os.path.join(self.options.tmpdir, "walletnotify")
  40          self.shutdownnotify_dir = os.path.join(self.options.tmpdir, "shutdownnotify")
  41          self.shutdownnotify_file = os.path.join(self.shutdownnotify_dir, "shutdownnotify.txt")
  42          os.mkdir(self.alertnotify_dir)
  43          os.mkdir(self.blocknotify_dir)
  44          os.mkdir(self.walletnotify_dir)
  45          os.mkdir(self.shutdownnotify_dir)
  46  
  47          # -alertnotify and -blocknotify on node0, walletnotify on node1
  48          self.extra_args = [[
  49              f"-alertnotify=echo > {os.path.join(self.alertnotify_dir, '%s')}",
  50              f"-blocknotify=echo > {os.path.join(self.blocknotify_dir, '%s')}",
  51              f"-shutdownnotify=echo > {self.shutdownnotify_file}",
  52          ], [
  53              "-blockversion=211",
  54              f"-walletnotify=echo %h_%b > {os.path.join(self.walletnotify_dir, notify_outputname('%w', '%s'))}",
  55          ]]
  56          self.wallet_names = [self.default_wallet_name, self.wallet]
  57          super().setup_network()
  58  
  59      def run_test(self):
  60          if self.is_wallet_compiled():
  61              # Setup the descriptors to be imported to the wallet
  62              seed = "cTdGmKFWpbvpKQ7ejrdzqYT2hhjyb3GPHnLAK7wdi5Em67YLwSm9"
  63              xpriv = "tprv8ZgxMBicQKsPfHCsTwkiM1KT56RXbGGTqvc2hgqzycpwbHqqpcajQeMRZoBD35kW4RtyCemu6j34Ku5DEspmgjKdt2qe4SvRch5Kk8B8A2v"
  64              desc_imports = [{
  65                  "desc": descsum_create(f"wpkh({xpriv}/0/*)"),
  66                  "timestamp": 0,
  67                  "active": True,
  68                  "keypool": True,
  69              },{
  70                  "desc": descsum_create(f"wpkh({xpriv}/1/*)"),
  71                  "timestamp": 0,
  72                  "active": True,
  73                  "keypool": True,
  74                  "internal": True,
  75              }]
  76              # Make the wallets and import the descriptors
  77              # Ensures that node 0 and node 1 share the same wallet for the conflicting transaction tests below.
  78              for i, name in enumerate(self.wallet_names):
  79                  self.nodes[i].createwallet(wallet_name=name, descriptors=self.options.descriptors, blank=True, load_on_startup=True)
  80                  if self.options.descriptors:
  81                      self.nodes[i].importdescriptors(desc_imports)
  82                  else:
  83                      self.nodes[i].sethdseed(True, seed)
  84  
  85          self.log.info("test -blocknotify")
  86          block_count = 10
  87          blocks = self.generatetoaddress(self.nodes[1], block_count, self.nodes[1].getnewaddress() if self.is_wallet_compiled() else ADDRESS_BCRT1_UNSPENDABLE)
  88  
  89          # wait at most 10 seconds for expected number of files before reading the content
  90          self.wait_until(lambda: len(os.listdir(self.blocknotify_dir)) == block_count, timeout=10)
  91  
  92          # directory content should equal the generated blocks hashes
  93          assert_equal(sorted(blocks), sorted(os.listdir(self.blocknotify_dir)))
  94  
  95          if self.is_wallet_compiled():
  96              self.log.info("test -walletnotify")
  97              # wait at most 10 seconds for expected number of files before reading the content
  98              self.wait_until(lambda: len(os.listdir(self.walletnotify_dir)) == block_count, timeout=10)
  99  
 100              # directory content should equal the generated transaction hashes
 101              tx_details = list(map(lambda t: (t['txid'], t['blockheight'], t['blockhash']), self.nodes[1].listtransactions("*", block_count)))
 102              self.expect_wallet_notify(tx_details)
 103  
 104              self.log.info("test -walletnotify after rescan")
 105              # rescan to force wallet notifications
 106              self.nodes[1].rescanblockchain()
 107              self.wait_until(lambda: len(os.listdir(self.walletnotify_dir)) == block_count, timeout=10)
 108  
 109              self.connect_nodes(0, 1)
 110  
 111              # directory content should equal the generated transaction hashes
 112              tx_details = list(map(lambda t: (t['txid'], t['blockheight'], t['blockhash']), self.nodes[1].listtransactions("*", block_count)))
 113              self.expect_wallet_notify(tx_details)
 114  
 115              # Conflicting transactions tests.
 116              # Generate spends from node 0, and check notifications
 117              # triggered by node 1
 118              self.log.info("test -walletnotify with conflicting transactions")
 119              self.nodes[0].rescanblockchain()
 120              self.generatetoaddress(self.nodes[0], 100, ADDRESS_BCRT1_UNSPENDABLE)
 121  
 122              # Generate transaction on node 0, sync mempools, and check for
 123              # notification on node 1.
 124              tx1 = self.nodes[0].sendtoaddress(address=ADDRESS_BCRT1_UNSPENDABLE, amount=1, replaceable=True)
 125              assert_equal(tx1 in self.nodes[0].getrawmempool(), True)
 126              self.sync_mempools()
 127              self.expect_wallet_notify([(tx1, -1, UNCONFIRMED_HASH_STRING)])
 128  
 129              # Generate bump transaction, sync mempools, and check for bump1
 130              # notification. In the future, per
 131              # https://github.com/limenka/limenka/pull/9371, it might be better
 132              # to have notifications for both tx1 and bump1.
 133              bump1 = self.nodes[0].bumpfee(tx1)["txid"]
 134              assert_equal(bump1 in self.nodes[0].getrawmempool(), True)
 135              self.sync_mempools()
 136              self.expect_wallet_notify([(bump1, -1, UNCONFIRMED_HASH_STRING)])
 137  
 138              # Add bump1 transaction to new block, checking for a notification
 139              # and the correct number of confirmations.
 140              blockhash1 = self.generatetoaddress(self.nodes[0], 1, ADDRESS_BCRT1_UNSPENDABLE)[0]
 141              blockheight1 = self.nodes[0].getblockcount()
 142              self.sync_blocks()
 143              self.expect_wallet_notify([(bump1, blockheight1, blockhash1)])
 144              assert_equal(self.nodes[1].gettransaction(bump1)["confirmations"], 1)
 145  
 146              # Generate a second transaction to be bumped.
 147              tx2 = self.nodes[0].sendtoaddress(address=ADDRESS_BCRT1_UNSPENDABLE, amount=1, replaceable=True)
 148              assert_equal(tx2 in self.nodes[0].getrawmempool(), True)
 149              self.sync_mempools()
 150              self.expect_wallet_notify([(tx2, -1, UNCONFIRMED_HASH_STRING)])
 151  
 152              # Bump tx2 as bump2 and generate a block on node 0 while
 153              # disconnected, then reconnect and check for notifications on node 1
 154              # about newly confirmed bump2 and newly conflicted tx2.
 155              self.disconnect_nodes(0, 1)
 156              bump2 = self.nodes[0].bumpfee(tx2)["txid"]
 157              blockhash2 = self.generatetoaddress(self.nodes[0], 1, ADDRESS_BCRT1_UNSPENDABLE, sync_fun=self.no_op)[0]
 158              blockheight2 = self.nodes[0].getblockcount()
 159              assert_equal(self.nodes[0].gettransaction(bump2)["confirmations"], 1)
 160              assert_equal(tx2 in self.nodes[1].getrawmempool(), True)
 161              self.connect_nodes(0, 1)
 162              self.sync_blocks()
 163              self.expect_wallet_notify([(bump2, blockheight2, blockhash2), (tx2, -1, UNCONFIRMED_HASH_STRING)])
 164              assert_equal(self.nodes[1].gettransaction(bump2)["confirmations"], 1)
 165  
 166          # TODO: add test for `-alertnotify` large fork notifications
 167  
 168          # Mine 51 unknown-version blocks. -alertnotify should trigger on the 51st.
 169          self.log.info("test -alertnotify")
 170          self.generatetoaddress(self.nodes[1], 51, ADDRESS_BCRT1_UNSPENDABLE)
 171  
 172          # Give limenkad 10 seconds to write the alert notification
 173          self.wait_until(lambda: len(os.listdir(self.alertnotify_dir)), timeout=10)
 174  
 175          for notify_file in os.listdir(self.alertnotify_dir):
 176              os.remove(os.path.join(self.alertnotify_dir, notify_file))
 177  
 178          # Mine more up-version blocks, should not get more alerts:
 179          self.generatetoaddress(self.nodes[1], 2, ADDRESS_BCRT1_UNSPENDABLE)
 180  
 181          self.log.info("-alertnotify should not continue notifying for more unknown version blocks")
 182          assert_equal(len(os.listdir(self.alertnotify_dir)), 0)
 183  
 184          self.log.info("test -shutdownnotify")
 185          self.stop_nodes()
 186          self.wait_until(lambda: os.path.isfile(self.shutdownnotify_file), timeout=10)
 187  
 188      def expect_wallet_notify(self, tx_details):
 189          self.wait_until(lambda: len(os.listdir(self.walletnotify_dir)) >= len(tx_details), timeout=10)
 190          # Should have no more and no less files than expected
 191          assert_equal(sorted(notify_outputname(self.wallet, tx_id) for tx_id, _, _ in tx_details), sorted(os.listdir(self.walletnotify_dir)))
 192          # Should now verify contents of each file
 193          for tx_id, blockheight, blockhash in tx_details:
 194              fname = os.path.join(self.walletnotify_dir, notify_outputname(self.wallet, tx_id))
 195              # Wait for the cached writes to hit storage
 196              self.wait_until(lambda: os.path.getsize(fname) > 0, timeout=10)
 197              with open(fname, 'rt', encoding='utf-8') as f:
 198                  text = f.read()
 199                  # Universal newline ensures '\n' on 'nt'
 200                  assert_equal(text[-1], '\n')
 201                  text = text[:-1]
 202                  if platform.system() == 'Windows':
 203                      # On Windows, echo as above will append a whitespace
 204                      assert_equal(text[-1], ' ')
 205                      text = text[:-1]
 206                  expected = str(blockheight) + '_' + blockhash
 207                  assert_equal(text, expected)
 208  
 209          for tx_file in os.listdir(self.walletnotify_dir):
 210              os.remove(os.path.join(self.walletnotify_dir, tx_file))
 211  
 212  
 213  if __name__ == '__main__':
 214      NotificationsTest(__file__).main()
 215