Browse Source

Have network_proxy use ThreadJob framework

Rename WalletSynchronizer to Synchronizer so that diagnostic messages,
which are based on class name, still refer to Synchronizer.
283
Neil Booth 9 years ago
parent
commit
01491dd1d0
  1. 2
      lib/__init__.py
  2. 4
      lib/network_proxy.py
  3. 12
      lib/synchronizer.py
  4. 8
      lib/wallet.py

2
lib/__init__.py

@ -1,6 +1,6 @@
from version import ELECTRUM_VERSION from version import ELECTRUM_VERSION
from util import format_satoshis, print_msg, print_json, print_error, set_verbosity from util import format_satoshis, print_msg, print_json, print_error, set_verbosity
from wallet import WalletSynchronizer, WalletStorage from wallet import Synchronizer, WalletStorage
from wallet import Wallet, Imported_Wallet from wallet import Wallet, Imported_Wallet
from network import Network, DEFAULT_SERVERS, DEFAULT_PORTS, pick_random_server from network import Network, DEFAULT_SERVERS, DEFAULT_PORTS, pick_random_server
from interface import Interface from interface import Interface

4
lib/network_proxy.py

@ -60,15 +60,13 @@ class NetworkProxy(util.DaemonThread):
self.blockchain_height = 0 self.blockchain_height = 0
self.server_height = 0 self.server_height = 0
self.interfaces = [] self.interfaces = []
self.jobs = []
# value returned by estimatefee # value returned by estimatefee
self.fee = None self.fee = None
def run(self): def run(self):
while self.is_running(): while self.is_running():
for job in self.jobs: self.run_jobs() # Synchronizer, for now
job()
try: try:
response = self.pipe.get() response = self.pipe.get()
except util.timeout: except util.timeout:

12
lib/synchronizer.py

@ -21,10 +21,10 @@ from threading import Lock
from bitcoin import Hash, hash_encode from bitcoin import Hash, hash_encode
from transaction import Transaction from transaction import Transaction
from util import print_error, print_msg from util import print_error, print_msg, ThreadJob
class WalletSynchronizer(): class Synchronizer(ThreadJob):
'''The synchronizer keeps the wallet up-to-date with its set of '''The synchronizer keeps the wallet up-to-date with its set of
addresses and their transactions. It subscribes over the network addresses and their transactions. It subscribes over the network
to wallet addresses, gets the wallet to generate new addresses to wallet addresses, gets the wallet to generate new addresses
@ -46,12 +46,6 @@ class WalletSynchronizer():
self.lock = Lock() self.lock = Lock()
self.initialize() self.initialize()
def print_error(self, *msg):
print_error("[Synchronizer]", *msg)
def print_msg(self, *msg):
print_msg("[Synchronizer]", *msg)
def parse_response(self, response): def parse_response(self, response):
if response.get('error'): if response.get('error'):
self.print_error("response error:", response) self.print_error("response error:", response)
@ -165,7 +159,7 @@ class WalletSynchronizer():
self.print_error("missing tx", self.requested_tx) self.print_error("missing tx", self.requested_tx)
self.subscribe_to_addresses(set(self.wallet.addresses(True))) self.subscribe_to_addresses(set(self.wallet.addresses(True)))
def main_loop(self): def run(self):
'''Called from the network proxy thread main loop.''' '''Called from the network proxy thread main loop.'''
# 1. Create new addresses # 1. Create new addresses
self.wallet.synchronize() self.wallet.synchronize()

8
lib/wallet.py

@ -38,7 +38,7 @@ from version import *
from transaction import Transaction from transaction import Transaction
from plugins import run_hook from plugins import run_hook
import bitcoin import bitcoin
from synchronizer import WalletSynchronizer from synchronizer import Synchronizer
from mnemonic import Mnemonic from mnemonic import Mnemonic
import paymentrequest import paymentrequest
@ -1110,8 +1110,8 @@ class Abstract_Wallet(object):
self.verifier = SPV(self.network, self) self.verifier = SPV(self.network, self)
self.verifier.start() self.verifier.start()
self.set_verifier(self.verifier) self.set_verifier(self.verifier)
self.synchronizer = WalletSynchronizer(self, network) self.synchronizer = Synchronizer(self, network)
network.jobs.append(self.synchronizer.main_loop) network.add_job(self.synchronizer)
else: else:
self.verifier = None self.verifier = None
self.synchronizer = None self.synchronizer = None
@ -1119,7 +1119,7 @@ class Abstract_Wallet(object):
def stop_threads(self): def stop_threads(self):
if self.network: if self.network:
self.verifier.stop() self.verifier.stop()
self.network.jobs.remove(self.synchronizer.main_loop) self.network.remove_job(self.synchronizer)
self.synchronizer = None self.synchronizer = None
self.storage.put('stored_height', self.get_local_height(), True) self.storage.put('stored_height', self.get_local_height(), True)

Loading…
Cancel
Save