Skip to content

Commit 4f1cc8b

Browse files
committed
tests: set low wallet.gap_limit_for_change to speed up tests
1 parent 4800a2d commit 4f1cc8b

File tree

9 files changed

+141
-112
lines changed

9 files changed

+141
-112
lines changed

tests/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
import threading
55
import tempfile
66
import shutil
7+
import functools
78

89
import electrum
910
import electrum.logging
1011
from electrum import constants
1112
from electrum import util
1213
from electrum.logging import Logger
14+
from electrum.wallet import restore_wallet_from_text
1315

1416

1517
# Set this locally to make the test suite run faster.
@@ -92,3 +94,14 @@ def run_test(*args, **kwargs):
9294
finally:
9395
constants.net = old_net
9496
return run_test
97+
98+
99+
@functools.wraps(restore_wallet_from_text)
100+
def restore_wallet_from_text__for_unittest(*args, gap_limit=2, gap_limit_for_change=1, **kwargs):
101+
"""much lower default gap limits (to save compute time)"""
102+
return restore_wallet_from_text(
103+
*args,
104+
gap_limit=gap_limit,
105+
gap_limit_for_change=gap_limit_for_change,
106+
**kwargs,
107+
)

tests/test_commands.py

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from electrum.commands import Commands, eval_bool
88
from electrum import storage, wallet
99
from electrum.lnworker import RecvMPPResolution
10-
from electrum.wallet import restore_wallet_from_text, Abstract_Wallet
10+
from electrum.wallet import Abstract_Wallet
1111
from electrum.address_synchronizer import TX_HEIGHT_UNCONFIRMED
1212
from electrum.simple_config import SimpleConfig
1313
from electrum.transaction import Transaction, TxOutput, tx_from_any
@@ -16,6 +16,7 @@
1616
from electrum.lnaddr import lndecode
1717

1818
from . import ElectrumTestCase
19+
from . import restore_wallet_from_text__for_unittest
1920
from .test_wallet_vertical import WalletIntegrityHelper
2021

2122

@@ -91,9 +92,10 @@ async def test_convert_xkey(self):
9192

9293
@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
9394
async def test_encrypt_decrypt(self, mock_save_db):
94-
wallet = restore_wallet_from_text('p2wpkh:L4rYY5QpfN6wJEF4SEKDpcGhTPnCe9zcGs6hiSnhpprZqVywFifN',
95-
path='if_this_exists_mocking_failed_648151893',
96-
config=self.config)['wallet']
95+
wallet = restore_wallet_from_text__for_unittest(
96+
'p2wpkh:L4rYY5QpfN6wJEF4SEKDpcGhTPnCe9zcGs6hiSnhpprZqVywFifN',
97+
path='if_this_exists_mocking_failed_648151893',
98+
config=self.config)['wallet']
9799
cmds = Commands(config=self.config)
98100
cleartext = "asdasd this is the message"
99101
pubkey = "021f110909ded653828a254515b58498a6bafc96799fb0851554463ed44ca7d9da"
@@ -102,9 +104,10 @@ async def test_encrypt_decrypt(self, mock_save_db):
102104

103105
@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
104106
async def test_export_private_key_imported(self, mock_save_db):
105-
wallet = restore_wallet_from_text('p2wpkh:L4rYY5QpfN6wJEF4SEKDpcGhTPnCe9zcGs6hiSnhpprZqVywFifN p2wpkh:L4jkdiXszG26SUYvwwJhzGwg37H2nLhrbip7u6crmgNeJysv5FHL',
106-
path='if_this_exists_mocking_failed_648151893',
107-
config=self.config)['wallet']
107+
wallet = restore_wallet_from_text__for_unittest(
108+
'p2wpkh:L4rYY5QpfN6wJEF4SEKDpcGhTPnCe9zcGs6hiSnhpprZqVywFifN p2wpkh:L4jkdiXszG26SUYvwwJhzGwg37H2nLhrbip7u6crmgNeJysv5FHL',
109+
path='if_this_exists_mocking_failed_648151893',
110+
config=self.config)['wallet']
108111
cmds = Commands(config=self.config)
109112
# single address tests
110113
with self.assertRaises(UserFacingException):
@@ -121,10 +124,10 @@ async def test_export_private_key_imported(self, mock_save_db):
121124

122125
@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
123126
async def test_export_private_key_deterministic(self, mock_save_db):
124-
wallet = restore_wallet_from_text('bitter grass shiver impose acquire brush forget axis eager alone wine silver',
125-
gap_limit=2,
126-
path='if_this_exists_mocking_failed_648151893',
127-
config=self.config)['wallet']
127+
wallet = restore_wallet_from_text__for_unittest(
128+
'bitter grass shiver impose acquire brush forget axis eager alone wine silver',
129+
path='if_this_exists_mocking_failed_648151893',
130+
config=self.config)['wallet']
128131
cmds = Commands(config=self.config)
129132
# single address tests
130133
with self.assertRaises(UserFacingException):
@@ -150,10 +153,10 @@ async def test_verifymessage_enforces_strict_base64(self):
150153
@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
151154
async def test_decrypt_enforces_strict_base64(self, mock_save_db):
152155
cmds = Commands(config=self.config)
153-
wallet = restore_wallet_from_text('9dk',
154-
gap_limit=2,
155-
path='if_this_exists_mocking_failed_648151893',
156-
config=self.config)['wallet'] # type: Abstract_Wallet
156+
wallet = restore_wallet_from_text__for_unittest(
157+
'9dk',
158+
path='if_this_exists_mocking_failed_648151893',
159+
config=self.config)['wallet'] # type: Abstract_Wallet
157160
plaintext = "hello there"
158161
ciphertext = "QklFMQJEFgxfkXj+UNblbHR+4y6ZA2rGEeEhWo7h84lBFjlRY5JOPfV1zyC1fw5YmhIr7+3ceIV11lpf/Yv7gSqQCQ5Wuf1aGXceHZO0GjKVxBsuew=="
159162
pubkey = "02a0507c8bb3d96dfd7731bafb0ae30e6ed10bbadd6a9f9f88eaf0602b9cc99adc"
@@ -234,10 +237,10 @@ async def test_serialize_custom_nsequence(self):
234237

235238
@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
236239
async def test_getprivatekeyforpath(self, mock_save_db):
237-
wallet = restore_wallet_from_text('north rent dawn bunker hamster invest wagon market romance pig either squeeze',
238-
gap_limit=2,
239-
path='if_this_exists_mocking_failed_648151893',
240-
config=self.config)['wallet']
240+
wallet = restore_wallet_from_text__for_unittest(
241+
'north rent dawn bunker hamster invest wagon market romance pig either squeeze',
242+
path='if_this_exists_mocking_failed_648151893',
243+
config=self.config)['wallet']
241244
cmds = Commands(config=self.config)
242245
self.assertEqual("p2wpkh:cUzm7zPpWgLYeURgff4EsoMjhskCpsviBH4Y3aZcrBX8UJSRPjC2",
243246
await cmds.getprivatekeyforpath([0, 10000], wallet=wallet))
@@ -248,10 +251,10 @@ async def test_getprivatekeyforpath(self, mock_save_db):
248251

249252
@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
250253
async def test_payto(self, mock_save_db):
251-
wallet = restore_wallet_from_text('disagree rug lemon bean unaware square alone beach tennis exhibit fix mimic',
252-
gap_limit=2,
253-
path='if_this_exists_mocking_failed_648151893',
254-
config=self.config)['wallet']
254+
wallet = restore_wallet_from_text__for_unittest(
255+
'disagree rug lemon bean unaware square alone beach tennis exhibit fix mimic',
256+
path='if_this_exists_mocking_failed_648151893',
257+
config=self.config)['wallet']
255258
# bootstrap wallet
256259
funding_tx = Transaction('0200000000010165806607dd458280cb57bf64a16cf4be85d053145227b98c28932e953076b8e20000000000fdffffff02ac150700000000001600147e3ddfe6232e448a8390f3073c7a3b2044fd17eb102908000000000016001427fbe3707bc57e5bb63d6f15733ec88626d8188a02473044022049ce9efbab88808720aa563e2d9bc40226389ab459c4390ea3e89465665d593502206c1c7c30a2f640af1e463e5107ee4cfc0ee22664cfae3f2606a95303b54cdef80121026269e54d06f7070c1f967eb2874ba60de550dfc327a945c98eb773672d9411fd77181e00')
257260
funding_txid = funding_tx.txid()
@@ -276,10 +279,10 @@ async def test_payto(self, mock_save_db):
276279
@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
277280
async def test_payto__confirmed_only(self, mock_save_db):
278281
"""test that payto respects 'confirmed_only' config var"""
279-
wallet = restore_wallet_from_text('disagree rug lemon bean unaware square alone beach tennis exhibit fix mimic',
280-
gap_limit=2,
281-
path='if_this_exists_mocking_failed_648151893',
282-
config=self.config)['wallet']
282+
wallet = restore_wallet_from_text__for_unittest(
283+
'disagree rug lemon bean unaware square alone beach tennis exhibit fix mimic',
284+
path='if_this_exists_mocking_failed_648151893',
285+
config=self.config)['wallet']
283286
# bootstrap wallet
284287
funding_tx = Transaction('0200000000010165806607dd458280cb57bf64a16cf4be85d053145227b98c28932e953076b8e20000000000fdffffff02ac150700000000001600147e3ddfe6232e448a8390f3073c7a3b2044fd17eb102908000000000016001427fbe3707bc57e5bb63d6f15733ec88626d8188a02473044022049ce9efbab88808720aa563e2d9bc40226389ab459c4390ea3e89465665d593502206c1c7c30a2f640af1e463e5107ee4cfc0ee22664cfae3f2606a95303b54cdef80121026269e54d06f7070c1f967eb2874ba60de550dfc327a945c98eb773672d9411fd77181e00')
285288
funding_txid = funding_tx.txid()
@@ -310,10 +313,10 @@ async def create_tx():
310313

311314
@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
312315
async def test_paytomany_multiple_max_spends(self, mock_save_db):
313-
wallet = restore_wallet_from_text('kit virtual quantum festival fortune inform ladder saddle filter soldier start ghost',
314-
gap_limit=2,
315-
path='if_this_exists_mocking_failed_648151893',
316-
config=self.config)['wallet']
316+
wallet = restore_wallet_from_text__for_unittest(
317+
'kit virtual quantum festival fortune inform ladder saddle filter soldier start ghost',
318+
path='if_this_exists_mocking_failed_648151893',
319+
config=self.config)['wallet']
317320
# bootstrap wallet
318321
funding_tx = Transaction('02000000000101f59876b1c65bbe3e182ccc7ea7224fe397bb9b70aadcbbf4f4074c75c8a074840000000000fdffffff021f351f00000000001600144eec851dd980cc36af1f629a32325f511604d6af56732d000000000016001439267bc7f3e3fabeae3bc3f73880de22d8b01ba50247304402207eac5f639806a00878488d58ca651d690292145bca5511531845ae21fab309d102207162708bd344840cc1bacff1092e426eb8484f83f5c068ba4ca579813de324540121020e0798c267ff06ee8b838cd465f3cfa6c843a122a04917364ce000c29ca205cae5f31f00')
319322
funding_txid = funding_tx.txid()
@@ -345,10 +348,10 @@ async def test_signtransaction_without_wallet(self, mock_save_db):
345348

346349
@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
347350
async def test_signtransaction_with_wallet(self, mock_save_db):
348-
wallet = restore_wallet_from_text('bitter grass shiver impose acquire brush forget axis eager alone wine silver',
349-
gap_limit=2,
350-
path='if_this_exists_mocking_failed_648151893',
351-
config=self.config)['wallet']
351+
wallet = restore_wallet_from_text__for_unittest(
352+
'bitter grass shiver impose acquire brush forget axis eager alone wine silver',
353+
path='if_this_exists_mocking_failed_648151893',
354+
config=self.config)['wallet']
352355

353356
# bootstrap wallet1
354357
funding_tx = Transaction('01000000014576dacce264c24d81887642b726f5d64aa7825b21b350c7b75a57f337da6845010000006b483045022100a3f8b6155c71a98ad9986edd6161b20d24fad99b6463c23b463856c0ee54826d02200f606017fd987696ebbe5200daedde922eee264325a184d5bbda965ba5160821012102e5c473c051dae31043c335266d0ef89c1daab2f34d885cc7706b267f3269c609ffffffff0240420f00000000001600148a28bddb7f61864bdcf58b2ad13d5aeb3abc3c42a2ddb90e000000001976a914c384950342cb6f8df55175b48586838b03130fad88ac00000000')
@@ -366,10 +369,10 @@ async def test_signtransaction_with_wallet(self, mock_save_db):
366369

367370
@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
368371
async def test_bumpfee(self, mock_save_db):
369-
wallet = restore_wallet_from_text('right nominee cheese afford exotic pilot mask illness rug fringe degree pottery',
370-
gap_limit=2,
371-
path='if_this_exists_mocking_failed_648151893',
372-
config=self.config)['wallet'] # type: Abstract_Wallet
372+
wallet = restore_wallet_from_text__for_unittest(
373+
'right nominee cheese afford exotic pilot mask illness rug fringe degree pottery',
374+
path='if_this_exists_mocking_failed_648151893',
375+
config=self.config)['wallet'] # type: Abstract_Wallet
373376

374377
funding_tx = Transaction("02000000000102789e8aa8caa79d87241ff9df0e3fd757a07c85a30195d76e8efced1d57c56b670000000000fdffffff7ee2b6abd52b332f797718ae582f8d3b979b83b1799e0a3bfb2c90c6e070c29e0100000000fdffffff020820000000000000160014c0eb720c93a61615d2d66542d381be8943ca553950c3000000000000160014d7dbd0196a2cbd76420f14a19377096cf6cddb75024730440220485b491ad8d3ce3b4da034a851882da84a06ec9800edff0d3fd6aa42eeba3b440220359ea85d32a05932ac417125e133fa54e54e7e9cd20ebc54b883576b8603fd65012103860f1fbf8a482b9d35d7d4d04be8fb33d856a514117cd8b73e372d36895feec60247304402206c2ca56cc030853fa59b4b3cb293f69a3378ead0f10cb76f640f8c2888773461022079b7055d0f6af6952a48e5b97218015b0723462d667765c142b41bd35e3d9c0a01210359e303f57647094a668d69e8ff0bd46c356d00aa7da6dc533c438e71c057f0793e721f00")
375378
funding_txid = funding_tx.txid()
@@ -397,9 +400,10 @@ async def test_bumpfee(self, mock_save_db):
397400

398401
@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
399402
async def test_importprivkey(self, mock_save_db):
400-
wallet = restore_wallet_from_text('p2wpkh:cQUdWZehnGDwGn7CSc911cJBcWTAcnyzpLoJYTsFNYW1w6iaq7Nw p2wpkh:cNHsDLo137ngrr2wGf3mwqpwTUvpuDVAZrqzan9heHcMTK4rP5JB',
401-
path='if_this_exists_mocking_failed_648151893',
402-
config=self.config)['wallet']
403+
wallet = restore_wallet_from_text__for_unittest(
404+
'p2wpkh:cQUdWZehnGDwGn7CSc911cJBcWTAcnyzpLoJYTsFNYW1w6iaq7Nw p2wpkh:cNHsDLo137ngrr2wGf3mwqpwTUvpuDVAZrqzan9heHcMTK4rP5JB',
405+
path='if_this_exists_mocking_failed_648151893',
406+
config=self.config)['wallet']
403407
cmds = Commands(config=self.config)
404408
self.assertEqual(2, len(wallet.get_addresses()))
405409
# try importing a single bad privkey
@@ -435,9 +439,8 @@ async def test_importprivkey(self, mock_save_db):
435439

436440
@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
437441
async def test_hold_invoice_commands(self, mock_save_db):
438-
wallet: Abstract_Wallet = restore_wallet_from_text(
442+
wallet: Abstract_Wallet = restore_wallet_from_text__for_unittest(
439443
'disagree rug lemon bean unaware square alone beach tennis exhibit fix mimic',
440-
gap_limit=2,
441444
path='if_this_exists_mocking_failed_648151893',
442445
config=self.config)['wallet']
443446

tests/test_contacts.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import os
22

3-
from . import ElectrumTestCase
4-
53
from electrum.simple_config import SimpleConfig
6-
from electrum.wallet import restore_wallet_from_text, Abstract_Wallet
4+
from electrum.wallet import Abstract_Wallet
75
from electrum.daemon import Daemon
86

7+
from . import ElectrumTestCase
8+
from . import restore_wallet_from_text__for_unittest
9+
910

1011
class TestContacts(ElectrumTestCase):
1112
TESTNET = True
@@ -17,7 +18,7 @@ def setUp(self):
1718

1819
async def test_saving_contacts(self):
1920
text = 'cross end slow expose giraffe fuel track awake turtle capital ranch pulp'
20-
d = restore_wallet_from_text(text, path=self.wallet_path, gap_limit=2, config=self.config)
21+
d = restore_wallet_from_text__for_unittest(text, path=self.wallet_path, config=self.config)
2122
w = d['wallet'] # type: Abstract_Wallet
2223
w.contacts["myNNuLYNgHE92nGQuJd5mXo6gy9gKXEDyQ"] = ("address", "alice")
2324
w.contacts["tb1q4syjltptqwhe62t3u5gwz9nsw87kmcwx003z05"] = ("address", "bob")

tests/test_daemon.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
from electrum.commands import Commands
55
from electrum.daemon import Daemon
66
from electrum.simple_config import SimpleConfig
7-
from electrum.wallet import restore_wallet_from_text, Abstract_Wallet
7+
from electrum.wallet import Abstract_Wallet
88
from electrum import util
99

10-
from . import ElectrumTestCase, as_testnet
10+
from . import ElectrumTestCase, as_testnet, restore_wallet_from_text__for_unittest
1111

1212

1313
class DaemonTestCase(ElectrumTestCase):
@@ -34,12 +34,11 @@ def _restore_wallet_from_text(self, text, *, password: Optional[str], encrypt_fi
3434
"""Returns path for created wallet."""
3535
basename = util.get_new_wallet_name(self.wallet_dir)
3636
path = os.path.join(self.wallet_dir, basename)
37-
wallet_dict = restore_wallet_from_text(
37+
wallet_dict = restore_wallet_from_text__for_unittest(
3838
text,
3939
path=path,
4040
password=password,
4141
encrypt_file=encrypt_file,
42-
gap_limit=2,
4342
config=self.config,
4443
)
4544
# We return the path instead of the wallet object, as extreme
@@ -193,19 +192,19 @@ class TestCommandsWithDaemon(DaemonTestCase):
193192

194193
async def test_wp_command_with_inmemory_wallet_has_password(self):
195194
cmds = Commands(config=self.config, daemon=self.daemon)
196-
wallet = restore_wallet_from_text(self.SEED,
197-
gap_limit=2,
198-
path=None,
199-
password="123456",
200-
config=self.config)['wallet']
195+
wallet = restore_wallet_from_text__for_unittest(
196+
self.SEED,
197+
path=None,
198+
password="123456",
199+
config=self.config)['wallet']
201200
self.assertEqual(self.SEED, await cmds.getseed(wallet=wallet, password="123456"))
202201

203202
async def test_wp_command_with_inmemory_wallet_no_password(self):
204203
cmds = Commands(config=self.config, daemon=self.daemon)
205-
wallet = restore_wallet_from_text(self.SEED,
206-
gap_limit=2,
207-
path=None,
208-
config=self.config)['wallet']
204+
wallet = restore_wallet_from_text__for_unittest(
205+
self.SEED,
206+
path=None,
207+
config=self.config)['wallet']
209208
self.assertEqual(self.SEED, await cmds.getseed(wallet=wallet))
210209

211210
async def test_wp_command_with_diskfile_wallet_has_password(self):

0 commit comments

Comments
 (0)