|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2024 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 | +from test_framework.blocktools import COINBASE_MATURITY |
| 7 | +from test_framework.test_framework import BitcoinTestFramework |
| 8 | +from test_framework.util import ( |
| 9 | + assert_greater_than_or_equal, |
| 10 | +) |
| 11 | +from test_framework.wallet import ( |
| 12 | + MiniWallet, |
| 13 | + MiniWalletMode, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class FeatureFrameworkMiniWalletTest(BitcoinTestFramework): |
| 18 | + def set_test_params(self): |
| 19 | + self.num_nodes = 1 |
| 20 | + |
| 21 | + def test_tx_padding(self): |
| 22 | + """Verify that MiniWallet's transaction padding (`target_weight` parameter) |
| 23 | + works accurately enough (i.e. at most 3 WUs higher) with all modes.""" |
| 24 | + for mode_name, wallet in self.wallets: |
| 25 | + self.log.info(f"Test tx padding with MiniWallet mode {mode_name}...") |
| 26 | + utxo = wallet.get_utxo(mark_as_spent=False) |
| 27 | + for target_weight in [1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 4000000, |
| 28 | + 989, 2001, 4337, 13371, 23219, 49153, 102035, 223419, 3999989]: |
| 29 | + tx = wallet.create_self_transfer(utxo_to_spend=utxo, target_weight=target_weight)["tx"] |
| 30 | + self.log.debug(f"-> target weight: {target_weight}, actual weight: {tx.get_weight()}") |
| 31 | + assert_greater_than_or_equal(tx.get_weight(), target_weight) |
| 32 | + assert_greater_than_or_equal(target_weight + 3, tx.get_weight()) |
| 33 | + |
| 34 | + def run_test(self): |
| 35 | + node = self.nodes[0] |
| 36 | + self.wallets = [ |
| 37 | + ("ADDRESS_OP_TRUE", MiniWallet(node, mode=MiniWalletMode.ADDRESS_OP_TRUE)), |
| 38 | + ("RAW_OP_TRUE", MiniWallet(node, mode=MiniWalletMode.RAW_OP_TRUE)), |
| 39 | + ("RAW_P2PK", MiniWallet(node, mode=MiniWalletMode.RAW_P2PK)), |
| 40 | + ] |
| 41 | + for _, wallet in self.wallets: |
| 42 | + self.generate(wallet, 10) |
| 43 | + self.generate(wallet, COINBASE_MATURITY) |
| 44 | + |
| 45 | + self.test_tx_padding() |
| 46 | + |
| 47 | + |
| 48 | +if __name__ == '__main__': |
| 49 | + FeatureFrameworkMiniWalletTest().main() |
0 commit comments