better synchronize method
This commit is contained in:
parent
4e3c9de1d0
commit
12d65f5e52
|
@ -566,6 +566,7 @@ class Wallet:
|
||||||
if h == ['*']: continue
|
if h == ['*']: continue
|
||||||
for tx_hash, tx_height in h:
|
for tx_hash, tx_height in h:
|
||||||
tx = self.transactions.get(tx_hash)
|
tx = self.transactions.get(tx_hash)
|
||||||
|
if tx is None: raise BaseException("Wallet not synchronized")
|
||||||
for output in tx.d.get('outputs'):
|
for output in tx.d.get('outputs'):
|
||||||
if output.get('address') != addr: continue
|
if output.get('address') != addr: continue
|
||||||
key = tx_hash + ":%d" % output.get('index')
|
key = tx_hash + ":%d" % output.get('index')
|
||||||
|
@ -1157,22 +1158,6 @@ class WalletSynchronizer(threading.Thread):
|
||||||
def is_running(self):
|
def is_running(self):
|
||||||
with self.lock: return self.running
|
with self.lock: return self.running
|
||||||
|
|
||||||
def synchronize_wallet(self):
|
|
||||||
new_addresses = self.wallet.synchronize()
|
|
||||||
if new_addresses:
|
|
||||||
self.subscribe_to_addresses(new_addresses)
|
|
||||||
self.wallet.up_to_date = False
|
|
||||||
return
|
|
||||||
|
|
||||||
if not self.interface.is_up_to_date('synchronizer'):
|
|
||||||
if self.wallet.is_up_to_date():
|
|
||||||
self.wallet.set_up_to_date(False)
|
|
||||||
self.was_updated = True
|
|
||||||
return
|
|
||||||
|
|
||||||
self.wallet.set_up_to_date(True)
|
|
||||||
self.was_updated = True
|
|
||||||
|
|
||||||
|
|
||||||
def subscribe_to_addresses(self, addresses):
|
def subscribe_to_addresses(self, addresses):
|
||||||
messages = []
|
messages = []
|
||||||
|
@ -1204,15 +1189,30 @@ class WalletSynchronizer(threading.Thread):
|
||||||
self.subscribe_to_addresses(self.wallet.addresses(True))
|
self.subscribe_to_addresses(self.wallet.addresses(True))
|
||||||
|
|
||||||
while self.is_running():
|
while self.is_running():
|
||||||
# 1. send new requests
|
# 1. create new addresses
|
||||||
self.synchronize_wallet()
|
new_addresses = self.wallet.synchronize()
|
||||||
|
|
||||||
|
# request missing addresses
|
||||||
|
if new_addresses:
|
||||||
|
self.subscribe_to_addresses(new_addresses)
|
||||||
|
|
||||||
|
# request missing transactions
|
||||||
for tx_hash, tx_height in missing_tx:
|
for tx_hash, tx_height in missing_tx:
|
||||||
if (tx_hash, tx_height) not in requested_tx:
|
if (tx_hash, tx_height) not in requested_tx:
|
||||||
self.interface.send([ ('blockchain.transaction.get',[tx_hash, tx_height]) ], 'synchronizer')
|
self.interface.send([ ('blockchain.transaction.get',[tx_hash, tx_height]) ], 'synchronizer')
|
||||||
requested_tx.append( (tx_hash, tx_height) )
|
requested_tx.append( (tx_hash, tx_height) )
|
||||||
missing_tx = []
|
missing_tx = []
|
||||||
|
|
||||||
|
# detect if situation has changed
|
||||||
|
if not self.interface.is_up_to_date('synchronizer'):
|
||||||
|
if self.wallet.is_up_to_date():
|
||||||
|
self.wallet.set_up_to_date(False)
|
||||||
|
self.was_updated = True
|
||||||
|
else:
|
||||||
|
if not self.wallet.is_up_to_date():
|
||||||
|
self.wallet.set_up_to_date(True)
|
||||||
|
self.was_updated = True
|
||||||
|
|
||||||
if self.was_updated:
|
if self.was_updated:
|
||||||
self.interface.trigger_callback('updated')
|
self.interface.trigger_callback('updated')
|
||||||
self.was_updated = False
|
self.was_updated = False
|
||||||
|
|
Loading…
Reference in New Issue