Skip to content

Commit 59779fc

Browse files
authored
Chore: Refactoring of wallet tests (#1943)
* refactoring of fixtures and wallet tests * bottleneck mark
1 parent 07cd038 commit 59779fc

File tree

7 files changed

+205
-176
lines changed

7 files changed

+205
-176
lines changed

pytest.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ addopts = --bitcoind-version v22.0.0 --elementsd-version v0.21.0.2
55
markers =
66
slow: mark test as slow.
77
elm: mark test as elementsd dependent
8+
bottleneck: mark a test as so ressource intensive that it can create a bottleneck where the test just fails due to a lack of ressources
89
# If you need live logging to debug, uncomment the next line
910
# log_cli = True
1011
# Then set the desired logging level on the command line, for example:

tests/fix_devices_and_wallets.py

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,20 @@ def hot_wallet_device_2(specter_regtest_configured):
5151
return create_hot_wallet_device(specter_regtest_configured)
5252

5353

54-
def create_hot_wallet_with_ID(
55-
specter_regtest_configured: Specter, hot_wallet_device_1, wallet_id
54+
@pytest.fixture
55+
def hot_ghost_machine_device(
56+
specter_regtest_configured: Specter, mnemonic_ghost_machine: str
57+
) -> Device:
58+
return create_hot_wallet_device(
59+
specter_regtest_configured,
60+
name="Ghost machine device",
61+
mnemonic=mnemonic_ghost_machine,
62+
)
63+
64+
65+
def create_hot_segwit_wallet(
66+
specter_regtest_configured: Specter, device: Device, wallet_id
5667
) -> Wallet:
57-
device = hot_wallet_device_1
5868
wallet_manager = specter_regtest_configured.wallet_manager
5969
assert device.taproot_available(specter_regtest_configured.rpc)
6070

@@ -64,24 +74,56 @@ def create_hot_wallet_with_ID(
6474
return source_wallet
6575

6676

77+
def create_hot_taproot_wallet(
78+
specter_regtest_configured: Specter, device: Device, wallet_id
79+
) -> Wallet:
80+
wallet_manager = specter_regtest_configured.wallet_manager
81+
assert device.taproot_available(specter_regtest_configured.rpc)
82+
keys = [key for key in device.keys if key.key_type == "tr"]
83+
source_wallet = wallet_manager.create_wallet(wallet_id, 1, "tr", keys, [device])
84+
return source_wallet
85+
86+
6787
@pytest.fixture
6888
def unfunded_hot_wallet_1(specter_regtest_configured, hot_wallet_device_1) -> Wallet:
69-
return create_hot_wallet_with_ID(
89+
return create_hot_segwit_wallet(
7090
specter_regtest_configured,
7191
hot_wallet_device_1,
7292
f"a_hotwallet_{random.randint(0, 999999)}",
7393
)
7494

7595

7696
@pytest.fixture
77-
def unfunded_hot_wallet_2(specter_regtest_configured, hot_wallet_device_1) -> Wallet:
78-
return create_hot_wallet_with_ID(
97+
def unfunded_hot_wallet_2(specter_regtest_configured, hot_wallet_device_2) -> Wallet:
98+
return create_hot_segwit_wallet(
7999
specter_regtest_configured,
80100
hot_wallet_device_1,
81101
f"a_hotwallet_{random.randint(0, 999999)}",
82102
)
83103

84104

105+
@pytest.fixture
106+
def unfunded_ghost_machine_wallet(
107+
specter_regtest_configured: Specter, hot_ghost_machine_device: Device
108+
) -> Wallet:
109+
return create_hot_segwit_wallet(
110+
specter_regtest_configured,
111+
hot_ghost_machine_device,
112+
f"ghost_machine",
113+
)
114+
115+
116+
@pytest.fixture
117+
def unfunded_taproot_wallet(
118+
specter_regtest_configured: Specter, hot_ghost_machine_device: Device
119+
) -> Wallet:
120+
return create_hot_taproot_wallet(
121+
specter_regtest_configured,
122+
hot_ghost_machine_device,
123+
f"taproot",
124+
)
125+
126+
85127
@pytest.fixture
86128
def funded_hot_wallet_1(
87129
bitcoin_regtest: NodeController, unfunded_hot_wallet_1: Wallet
@@ -116,3 +158,25 @@ def funded_hot_wallet_2(
116158
funded_hot_wallet_2 = unfunded_hot_wallet_2
117159
bitcoin_regtest.testcoin_faucet(funded_hot_wallet_2.getnewaddress())
118160
return funded_hot_wallet_2
161+
162+
163+
@pytest.fixture
164+
def funded_ghost_machine_wallet(
165+
bitcoin_regtest: NodeController, unfunded_ghost_machine_wallet: Wallet
166+
) -> Wallet:
167+
funded_ghost_machine_wallet = unfunded_ghost_machine_wallet
168+
bitcoin_regtest.testcoin_faucet(
169+
funded_ghost_machine_wallet.getnewaddress()
170+
) # default value are 20 BTC
171+
return funded_ghost_machine_wallet
172+
173+
174+
@pytest.fixture
175+
def funded_taproot_wallet(
176+
bitcoin_regtest: NodeController, unfunded_taproot_wallet: Wallet
177+
) -> Wallet:
178+
funded_taproot_wallet = unfunded_taproot_wallet
179+
bitcoin_regtest.testcoin_faucet(
180+
funded_taproot_wallet.getnewaddress()
181+
) # default value are 20 BTC
182+
return funded_taproot_wallet

tests/fix_keys_and_seeds.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,26 @@
77
from embit.bip32 import HDKey, NETWORKS
88
from embit import script
99

10-
mnemonic_ghost_machine = (
11-
"ghost ghost ghost ghost ghost ghost ghost ghost ghost ghost ghost machine"
12-
)
1310

14-
mnemonic_zoo_when = (
15-
"zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo when"
16-
)
11+
@pytest.fixture
12+
def mnemonic_ghost_machine():
13+
return 11 * "ghost " + "machine"
1714

1815

1916
@pytest.fixture
2017
def mnemonic_keen_join():
2118
return 11 * "keen " + "join"
2219

2320

24-
# hold hold hold hold hold hold hold hold hold hold hold accident
25-
# This is a formal creation of all major bitcoin artifacts from the
26-
# hold accident mnemonic
27-
28-
2921
@pytest.fixture
3022
def mnemonic_hold_accident():
3123
return 11 * "hold " + "accident"
3224

3325

26+
# The following is a formal creation of all major bitcoin artifacts from the
27+
# hold accident mnemonic
28+
29+
3430
@pytest.fixture
3531
def seed_hold_accident(mnemonic_hold_accident):
3632
seed = mnemonic_to_seed(mnemonic_hold_accident)

tests/test_commands_utxo_scanner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from cryptoadvance.specter.commands.utxo_scanner import UtxoScanner
77
from cryptoadvance.specter.specter import Specter
88
from cryptoadvance.specter.wallet import Wallet
9-
from fix_devices_and_wallets import create_hot_wallet_device, create_hot_wallet_with_ID
9+
from fix_devices_and_wallets import create_hot_wallet_device, create_hot_segwit_wallet
1010

1111

1212
@pytest.mark.skip()
@@ -25,7 +25,7 @@ def test_rescan_utxo(specter_testnet_configured: Specter, caplog):
2525
11 * "hold " + "accident",
2626
)
2727
assert hot_device
28-
wallet: Wallet = create_hot_wallet_with_ID(
28+
wallet: Wallet = create_hot_segwit_wallet(
2929
specter_testnet_configured, hot_device, "hold_accident"
3030
)
3131

tests/test_managers_wallet.py

Lines changed: 1 addition & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def test_WalletManager(
9292

9393

9494
@pytest.mark.slow
95+
@pytest.mark.bottleneck
9596
def test_WalletManager_2_nodes(
9697
docker,
9798
request,
@@ -142,92 +143,6 @@ def test_WalletManager_2_nodes(
142143
wm.update(chain="regtest3")
143144
with pytest.raises(Exception, match="can only be changed with one another") as e:
144145
wm.update(rpc=bitcoin_regtest2.get_rpc())
145-
# assert False
146-
147-
148-
@pytest.mark.slow
149-
def test_wallet_createpsbt(
150-
docker, request, devices_filled_data_folder, device_manager, bitcoin_regtest
151-
):
152-
wm = WalletManager(
153-
200100,
154-
devices_filled_data_folder,
155-
bitcoin_regtest.get_rpc(),
156-
"regtest",
157-
device_manager,
158-
allow_threading=False,
159-
)
160-
# A wallet-creation needs a device
161-
device = device_manager.get_by_alias("specter")
162-
key = Key.from_json(
163-
{
164-
"derivation": "m/48h/1h/0h/2h",
165-
"original": "Vpub5n9kKePTPPGtw3RddeJWJe29epEyBBcoHbbPi5HhpoG2kTVsSCUzsad33RJUt3LktEUUPPofcZczuudnwR7ZgkAkT6N2K2Z7wdyjYrVAkXM",
166-
"fingerprint": "08686ac6",
167-
"type": "wsh",
168-
"xpub": "tpubDFHpKypXq4kwUrqLotPs6fCic5bFqTRGMBaTi9s5YwwGymE8FLGwB2kDXALxqvNwFxB1dLWYBmmeFVjmUSdt2AsaQuPmkyPLBKRZW8BGCiL",
169-
}
170-
)
171-
wallet = wm.create_wallet("a_second_test_wallet", 1, "wpkh", [key], [device])
172-
# Let's fund the wallet
173-
address = wallet.getnewaddress()
174-
assert address == "bcrt1qtnrv2jpygx2ef3zqfjhqplnycxak2m6ljnhq6z"
175-
bitcoin_regtest.testcoin_faucet(address, amount=5)
176-
address = wallet.getnewaddress()
177-
bitcoin_regtest.testcoin_faucet(address, amount=10)
178-
address = wallet.getnewaddress()
179-
bitcoin_regtest.testcoin_faucet(address, amount=15)
180-
# update the wallet data
181-
wallet.update_balance()
182-
assert wallet.amount_total == 30
183-
unspents = wallet.rpc.listunspent(0)
184-
# Let's take 3 UTXO:
185-
selected_coins = [
186-
{"txid": u["txid"], "vout": u["vout"]}
187-
for u in [unspents[0], unspents[1], unspents[2]]
188-
]
189-
selected_coins_amount_sum = (
190-
unspents[0]["amount"] + unspents[1]["amount"] + unspents[2]["amount"]
191-
)
192-
number_of_coins_to_spend = (
193-
selected_coins_amount_sum - 0.1
194-
) # Let's spend almost all of them
195-
random_address = "bcrt1qwxhn49vkppudjcgrd4mylxczgjtuwayg5ckp5s"
196-
psbt = wallet.createpsbt(
197-
[random_address],
198-
[number_of_coins_to_spend],
199-
True,
200-
0,
201-
10,
202-
selected_coins=selected_coins,
203-
)
204-
assert len(psbt["tx"]["vin"]) == 3
205-
psbt_txs = [tx["txid"] for tx in psbt["tx"]["vin"]]
206-
for coin in selected_coins:
207-
assert coin["txid"] in psbt_txs
208-
209-
# Now let's spend more coins than we have selected. This should result in an exception:
210-
try:
211-
psbt = wallet.createpsbt(
212-
[random_address],
213-
[number_of_coins_to_spend + 1],
214-
True,
215-
0,
216-
10,
217-
selected_coins=selected_coins,
218-
)
219-
assert False, "should throw an exception!"
220-
except SpecterError as e:
221-
pass
222-
223-
assert wallet.amount_locked_unsigned == selected_coins_amount_sum
224-
assert len(wallet.rpc.listlockunspent()) == 3
225-
assert wallet.amount_available == wallet.amount_total - selected_coins_amount_sum
226-
227-
wallet.delete_pending_psbt(psbt["tx"]["txid"])
228-
assert wallet.amount_locked_unsigned == 0
229-
assert len(wallet.rpc.listlockunspent()) == 0
230-
assert wallet.amount_total == wallet.amount_available
231146

232147

233148
def test_WalletManager_check_duplicate_keys(empty_data_folder):
@@ -779,47 +694,3 @@ def test_multisig_wallet_backup_and_restore(
779694

780695
# We restored the wallet's utxos
781696
assert wallet.amount_total == 3.3
782-
783-
784-
@pytest.mark.slow
785-
def test_taproot_wallet(bitcoin_regtest, caplog, specter_regtest_configured):
786-
"""
787-
Taproot m/86h/ derivation path xpubs should be able to create wallets and
788-
receive funds
789-
"""
790-
caplog.set_level(logging.INFO)
791-
792-
device_manager = specter_regtest_configured.device_manager
793-
wallet_manager = specter_regtest_configured.wallet_manager
794-
795-
taproot_device = device_manager.add_device(
796-
name="hot_key", device_type=DeviceTypes.BITCOINCORE, keys=[]
797-
)
798-
taproot_device.setup_device(file_password=None, wallet_manager=wallet_manager)
799-
taproot_device.add_hot_wallet_keys(
800-
mnemonic=generate_mnemonic(strength=128),
801-
passphrase="",
802-
paths=["m/86h/1h/0h"], # Taproot for testnet/regtest
803-
file_password=None,
804-
wallet_manager=wallet_manager,
805-
testnet=True,
806-
keys_range=[0, 1000],
807-
keys_purposes=[],
808-
)
809-
810-
taproot_wallet = wallet_manager.create_wallet(
811-
name="my_taproot_test_wallet",
812-
sigs_required=1,
813-
key_type=taproot_device.keys[0].key_type,
814-
keys=[taproot_device.keys[0]],
815-
devices=[taproot_device],
816-
)
817-
818-
# Fund the wallet
819-
address = taproot_wallet.getnewaddress()
820-
bitcoin_regtest.testcoin_faucet(address, amount=2.2)
821-
taproot_wallet.update_balance()
822-
assert taproot_wallet.amount_total == 2.2
823-
824-
# Taproot test addrs are bcrt1p vs native segwit bcrt1q
825-
assert address.startswith("bcrt1p")

tests/test_rest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from cryptoadvance.specter.specter_error import SpecterError
1414
from cryptoadvance.specter.util.wallet_importer import WalletImporter
1515
from cryptoadvance.specter.user import User
16-
from fix_devices_and_wallets import create_hot_wallet_with_ID
1716

1817
logger = logging.getLogger(__name__)
1918

0 commit comments

Comments
 (0)