diff --git a/electrum/commands.py b/electrum/commands.py index e4601497a..084a9326a 100644 --- a/electrum/commands.py +++ b/electrum/commands.py @@ -1412,6 +1412,14 @@ class Commands(Logger): """ return wallet synchronization status """ return wallet.is_up_to_date() + @command('wn') + async def wait_for_sync(self, wallet: Abstract_Wallet = None): + """Block until the wallet synchronization finishes.""" + while True: + if wallet.is_up_to_date(): + return True + await wallet.up_to_date_changed_event.wait() + @command('n') async def getfeerate(self): """ diff --git a/electrum/wallet.py b/electrum/wallet.py index f7ef09c9f..c232fd47f 100644 --- a/electrum/wallet.py +++ b/electrum/wallet.py @@ -438,6 +438,7 @@ class Abstract_Wallet(ABC, Logger, EventListener): # true when synchronized. this is stricter than adb.is_up_to_date(): # to-be-generated (HD) addresses are also considered here (gap-limit-roll-forward) self._up_to_date = False + self.up_to_date_changed_event = asyncio.Event() self.test_addresses_sanity() if self.storage and self.has_storage_encryption(): @@ -588,6 +589,8 @@ class Abstract_Wallet(ABC, Logger, EventListener): if status_changed or up_to_date: # suppress False->False transition, as it is spammy util.trigger_callback('wallet_updated', self) util.trigger_callback('status') + self.up_to_date_changed_event.set() + self.up_to_date_changed_event.clear() if status_changed: self.logger.info(f'set_up_to_date: {up_to_date}') diff --git a/tests/regtest/regtest.sh b/tests/regtest/regtest.sh index d60465ef5..3725dc77e 100755 --- a/tests/regtest/regtest.sh +++ b/tests/regtest/regtest.sh @@ -114,7 +114,7 @@ if [[ $1 == "start" ]]; then agent="./run_electrum --regtest -D /tmp/$2" $agent daemon -d $agent load_wallet - sleep 1 # give time to synchronize + $agent wait_for_sync fi if [[ $1 == "stop" ]]; then @@ -170,6 +170,7 @@ if [[ $1 == "backup" ]]; then $alice daemon -d $alice load_wallet $alice import_channel_backup $backup + $alice wait_for_sync echo "request force close $channel1" $alice request_force_close $channel1 echo "request force close $channel2"