Browse Source

add JMManager.stopped prop, fix tests patcher stop

add-joinmarket
zebra-lucky 1 year ago
parent
commit
4fe17a2899
  1. 8
      electrum/plugins/joinmarket/jm_main.py
  2. 6
      electrum/plugins/joinmarket/jm_wallet.py
  3. 2
      electrum/plugins/joinmarket/plugin.py
  4. 10
      electrum/plugins/joinmarket/tests/__init__.py
  5. 12
      electrum/plugins/joinmarket/tests/test_jm_main.py

8
electrum/plugins/joinmarket/jm_main.py

@ -35,6 +35,7 @@ class JMManager(Logger):
self.tumble_log = get_tumble_log(self, logsdir, config) self.tumble_log = get_tumble_log(self, logsdir, config)
self._state = JMStates.Unsupported self._state = JMStates.Unsupported
self._stopped = False
self.jmw = JMWallet(self) self.jmw = JMWallet(self)
self.jmconf = JMConf(self) self.jmconf = JMConf(self)
self.jmw.jmconf = self.jmconf self.jmw.jmconf = self.jmconf
@ -111,6 +112,10 @@ class JMManager(Logger):
'''JM was enabled on this wallet''' '''JM was enabled on this wallet'''
return self.state not in [JMStates.Unsupported, JMStates.Disabled] return self.state not in [JMStates.Unsupported, JMStates.Disabled]
@property
def stopped(self):
return self._stopped
def enable_jm(self): def enable_jm(self):
'''Enables JM on this wallet, store changes in db.''' '''Enables JM on this wallet, store changes in db.'''
if not self.enabled: if not self.enabled:
@ -151,8 +156,9 @@ class JMManager(Logger):
self.jmw.on_network_start(network) self.jmw.on_network_start(network)
asyncio.ensure_future(self.trigger_postponed_notifications()) asyncio.ensure_future(self.trigger_postponed_notifications())
def on_stop_threads(self): def stop(self):
'''Run when the wallet is unloaded/stopped''' '''Run when the wallet is unloaded/stopped'''
self._stopped = True
self.jmw.unregister_callbacks() self.jmw.unregister_callbacks()
def postpone_notification(self, event, *args): def postpone_notification(self, event, *args):

6
electrum/plugins/joinmarket/jm_wallet.py

@ -430,7 +430,7 @@ class JMWallet(KeyPairsMixin, WalletDBMixin, JMBaseCodeMixin, EventListener):
result = [] result = []
w = self.wallet w = self.wallet
with w.lock: with w.lock:
while len(result) < addrs_count: while len(result) < addrs_count and not self.jmman.stopped:
if internal: if internal:
unused = w.calc_unused_change_addresses() unused = w.calc_unused_change_addresses()
else: else:
@ -467,7 +467,7 @@ class JMWallet(KeyPairsMixin, WalletDBMixin, JMBaseCodeMixin, EventListener):
gen_cnt = 0 # num new addresses we generated gen_cnt = 0 # num new addresses we generated
limit = self.jmconf.gaplimit limit = self.jmconf.gaplimit
while True: while True and not self.jmman.stopped:
jm_addrs = self.get_jm_addresses(mixdepth=mixdepth, jm_addrs = self.get_jm_addresses(mixdepth=mixdepth,
internal=internal) internal=internal)
addr_cnt = len(jm_addrs) addr_cnt = len(jm_addrs)
@ -515,7 +515,7 @@ class JMWallet(KeyPairsMixin, WalletDBMixin, JMBaseCodeMixin, EventListener):
self.logger.info("taskgroup stopped.") self.logger.info("taskgroup stopped.")
async def do_synchronize_loop(self): async def do_synchronize_loop(self):
while True: while True and not self.jmman.stopped:
if self.jmman.enabled: if self.jmman.enabled:
# note: we only generate new HD addresses if the existing ones # note: we only generate new HD addresses if the existing ones
# have history that are mined and SPV-verified. # have history that are mined and SPV-verified.

2
electrum/plugins/joinmarket/plugin.py

@ -36,6 +36,6 @@ class JoinMarketPlugin(BasePlugin):
def close_wallet(self, wallet): def close_wallet(self, wallet):
if wallet not in self._wallets: if wallet not in self._wallets:
return return
wallet.jmman.on_stop_threads() wallet.jmman.stop()
del wallet.jmman del wallet.jmman
self._wallets.remove(wallet) self._wallets.remove(wallet)

10
electrum/plugins/joinmarket/tests/__init__.py

@ -283,9 +283,8 @@ class JMTestCase(ElectrumTestCase):
async def asyncSetUp(self): async def asyncSetUp(self):
await super().asyncSetUp() await super().asyncSetUp()
patcher = mock.patch.object(storage.WalletStorage, 'write') self.patcher = mock.patch.object(storage.WalletStorage, 'write')
self.mock_save_db = patcher.start() self.patcher.start()
self.addCleanup(patcher.stop)
self.asyncio_loop = util.get_asyncio_loop() self.asyncio_loop = util.get_asyncio_loop()
self.config = SimpleConfig({'electrum_path': self.electrum_path}) self.config = SimpleConfig({'electrum_path': self.electrum_path})
@ -318,3 +317,8 @@ class JMTestCase(ElectrumTestCase):
w.adb.add_transaction(Transaction(tx1_str)) w.adb.add_transaction(Transaction(tx1_str))
w.adb.add_verified_tx(tx1_txid, util.TxMinedInfo( w.adb.add_verified_tx(tx1_txid, util.TxMinedInfo(
int(1e6), '', '', '', '')) int(1e6), '', '', '', ''))
async def asyncTearDown(self):
self.jmman.stop()
await self.w.stop()
self.patcher.stop()

12
electrum/plugins/joinmarket/tests/test_jm_main.py

@ -21,9 +21,8 @@ class JMManagerInitTestCase(ElectrumTestCase):
async def asyncSetUp(self): async def asyncSetUp(self):
await super().asyncSetUp() await super().asyncSetUp()
patcher = mock.patch.object(storage.WalletStorage, 'write') self.patcher = mock.patch.object(storage.WalletStorage, 'write')
self.mock_save_db = patcher.start() self.patcher.start()
self.addCleanup(patcher.stop)
self.asyncio_loop = util.get_asyncio_loop() self.asyncio_loop = util.get_asyncio_loop()
self.config = SimpleConfig({'electrum_path': self.electrum_path}) self.config = SimpleConfig({'electrum_path': self.electrum_path})
@ -42,6 +41,10 @@ class JMManagerInitTestCase(ElectrumTestCase):
self.w.db.put('stored_height', int(1e7)) self.w.db.put('stored_height', int(1e7))
self.network = NetworkMock(self.asyncio_loop, self.config, w) self.network = NetworkMock(self.asyncio_loop, self.config, w)
async def asyncTearDown(self):
await self.w.stop()
self.patcher.stop()
async def test_init(self): async def test_init(self):
w = self.w w = self.w
keystore = w.db.get('keystore') keystore = w.db.get('keystore')
@ -73,6 +76,7 @@ class JMManagerInitTestCase(ElectrumTestCase):
await jmman._enable_jm() await jmman._enable_jm()
assert jmman.enabled assert jmman.enabled
assert jmman.state == JMStates.Ready assert jmman.state == JMStates.Ready
jmman.stop()
async def test_init_on_mainnet(self): async def test_init_on_mainnet(self):
w = self.w w = self.w
@ -105,6 +109,7 @@ class JMManagerInitTestCase(ElectrumTestCase):
assert db.get('jm_addresses') is not None assert db.get('jm_addresses') is not None
assert db.get('jm_commitments') is not None assert db.get('jm_commitments') is not None
assert db.get('jm_txs') is not None assert db.get('jm_txs') is not None
jmman.stop()
async def test_enable_jm(self): async def test_enable_jm(self):
w = self.w w = self.w
@ -123,3 +128,4 @@ class JMManagerInitTestCase(ElectrumTestCase):
enabled = await jmman._enable_jm() enabled = await jmman._enable_jm()
assert jmman.enabled assert jmman.enabled
assert not enabled assert not enabled
jmman.stop()

Loading…
Cancel
Save