feature_framework_miniwallet.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 2024-present The Bitcoin Core 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 MiniWallet."""
   6  import random
   7  import string
   8  
   9  from test_framework.blocktools import COINBASE_MATURITY
  10  from test_framework.test_framework import BitcoinTestFramework
  11  from test_framework.util import (
  12      assert_equal,
  13  )
  14  from test_framework.wallet import (
  15      MiniWallet,
  16      MiniWalletMode,
  17  )
  18  
  19  
  20  class FeatureFrameworkMiniWalletTest(BitcoinTestFramework):
  21      def set_test_params(self):
  22          self.num_nodes = 1
  23  
  24      def test_tx_padding(self):
  25          """Verify that MiniWallet's transaction padding (`target_vsize` parameter)
  26             works accurately with all modes."""
  27          for mode_name, wallet in self.wallets:
  28              self.log.info(f"Test tx padding with MiniWallet mode {mode_name}...")
  29              utxo = wallet.get_utxo(mark_as_spent=False)
  30              for target_vsize in [250, 500, 1250, 2500, 5000, 12500, 25000, 50000, 1000000,
  31                                   248, 501, 1085, 3343, 5805, 12289, 25509, 55855,  999998]:
  32                  tx = wallet.create_self_transfer(utxo_to_spend=utxo, target_vsize=target_vsize)
  33                  assert_equal(tx['tx'].get_vsize(), target_vsize)
  34                  child_tx = wallet.create_self_transfer_multi(utxos_to_spend=[tx["new_utxo"]], target_vsize=target_vsize)
  35                  assert_equal(child_tx['tx'].get_vsize(), target_vsize)
  36  
  37  
  38      def test_wallet_tagging(self):
  39          """Verify that tagged wallet instances are able to send funds."""
  40          self.log.info("Test tagged wallet instances...")
  41          node = self.nodes[0]
  42          untagged_wallet = self.wallets[0][1]
  43          for i in range(10):
  44              tag = ''.join(random.choice(string.ascii_letters) for _ in range(20))
  45              self.log.debug(f"-> ({i}) tag name: {tag}")
  46              tagged_wallet = MiniWallet(node, tag_name=tag)
  47              untagged_wallet.send_to(from_node=node, scriptPubKey=tagged_wallet.get_output_script(), amount=100000)
  48              tagged_wallet.rescan_utxos()
  49              tagged_wallet.send_self_transfer(from_node=node)
  50          self.generate(node, 1)  # clear mempool
  51  
  52      def run_test(self):
  53          node = self.nodes[0]
  54          self.wallets = [
  55              ("ADDRESS_OP_TRUE", MiniWallet(node, mode=MiniWalletMode.ADDRESS_OP_TRUE)),
  56              ("RAW_OP_TRUE",     MiniWallet(node, mode=MiniWalletMode.RAW_OP_TRUE)),
  57              ("RAW_P2PK",        MiniWallet(node, mode=MiniWalletMode.RAW_P2PK)),
  58          ]
  59          for _, wallet in self.wallets:
  60              self.generate(wallet, 10)
  61          self.generate(wallet, COINBASE_MATURITY)
  62  
  63          self.test_tx_padding()
  64          self.test_wallet_tagging()
  65  
  66  
  67  if __name__ == '__main__':
  68      FeatureFrameworkMiniWalletTest(__file__).main()
  69