From 3dbc10bca0d2292c366d6ef547d816332e43ea3c Mon Sep 17 00:00:00 2001 From: Neil Booth Date: Tue, 17 Jul 2018 11:44:15 +0800 Subject: [PATCH] Move address handlers from controller to session --- electrumx/server/controller.py | 29 +-------------------- electrumx/server/session.py | 46 ++++++++++++++++++++++++++-------- 2 files changed, 36 insertions(+), 39 deletions(-) diff --git a/electrumx/server/controller.py b/electrumx/server/controller.py index 10bf9eb..7306082 100644 --- a/electrumx/server/controller.py +++ b/electrumx/server/controller.py @@ -563,7 +563,7 @@ class Controller(ServerBase): '''Replace the daemon URL.''' daemon_url = daemon_url or self.env.daemon_url try: - self.daemon.set_urls(self.env.coin.daemon_urls(daemon_url)) + self.daemon.set_urls(self.coin.daemon_urls(daemon_url)) except Exception as e: raise RPCError(BAD_REQUEST, f'an error occured: {e}') return 'now using daemon at {}'.format(self.daemon.logged_url()) @@ -601,13 +601,6 @@ class Controller(ServerBase): # Helpers for RPC "blockchain" command handlers - def address_to_hashX(self, address): - try: - return self.coin.address_to_hashX(address) - except Exception: - pass - raise RPCError(BAD_REQUEST, f'{address} is not a valid address') - def assert_tx_hash(self, value): '''Raise an RPCError if the value is not a valid transaction hash.''' @@ -702,21 +695,6 @@ class Controller(ServerBase): # Client RPC "blockchain" command handlers - async def address_get_balance(self, address): - '''Return the confirmed and unconfirmed balance of an address.''' - hashX = self.address_to_hashX(address) - return await self.get_balance(hashX) - - async def address_get_history(self, address): - '''Return the confirmed and unconfirmed history of an address.''' - hashX = self.address_to_hashX(address) - return await self.confirmed_and_unconfirmed_history(hashX) - - async def address_get_mempool(self, address): - '''Return the mempool transactions touching an address.''' - hashX = self.address_to_hashX(address) - return await self.unconfirmed_history(hashX) - async def hashX_listunspent(self, hashX): '''Return the list of UTXOs of a script hash, including mempool effects.''' @@ -731,11 +709,6 @@ class Controller(ServerBase): for utxo in utxos if (utxo.tx_hash, utxo.tx_pos) not in spends] - async def address_listunspent(self, address): - '''Return the list of UTXOs of an address.''' - hashX = self.address_to_hashX(address) - return await self.hashX_listunspent(hashX) - def block_get_header(self, height): '''The deserialized header at a given height. diff --git a/electrumx/server/session.py b/electrumx/server/session.py index 6a6cfa0..c648329 100644 --- a/electrumx/server/session.py +++ b/electrumx/server/session.py @@ -69,6 +69,7 @@ class SessionBase(ServerSession): self.controller = controller self.bp = controller.bp self.env = controller.env + self.coin = self.env.coin self.daemon = self.bp.daemon self.client = 'unknown' self.anon_logs = self.env.anon_logs @@ -325,11 +326,38 @@ class ElectrumX(SessionBase): self.hashX_subs[hashX] = alias return await self.address_status(hashX) + def address_to_hashX(self, address): + try: + return self.coin.address_to_hashX(address) + except Exception: + pass + raise RPCError(BAD_REQUEST, f'{address} is not a valid address') + + async def address_get_balance(self, address): + '''Return the confirmed and unconfirmed balance of an address.''' + hashX = self.address_to_hashX(address) + return await self.controller.get_balance(hashX) + + async def address_get_history(self, address): + '''Return the confirmed and unconfirmed history of an address.''' + hashX = self.address_to_hashX(address) + return await self.controller.confirmed_and_unconfirmed_history(hashX) + + async def address_get_mempool(self, address): + '''Return the mempool transactions touching an address.''' + hashX = self.address_to_hashX(address) + return await self.controller.unconfirmed_history(hashX) + + async def address_listunspent(self, address): + '''Return the list of UTXOs of an address.''' + hashX = self.address_to_hashX(address) + return await self.controller.hashX_listunspent(hashX) + async def address_subscribe(self, address): '''Subscribe to an address. address: the address to subscribe to''' - hashX = self.controller.address_to_hashX(address) + hashX = self.address_to_hashX(address) return await self.hashX_subscribe(hashX, address) async def scripthash_get_balance(self, scripthash): @@ -417,7 +445,7 @@ class ElectrumX(SessionBase): index: the chunk index''' index = self.controller.non_negative_integer(index) - chunk_size = self.controller.coin.CHUNK_SIZE + chunk_size = self.coin.CHUNK_SIZE start_height = index * chunk_size hex_str, n = self.controller.block_headers(start_height, chunk_size) return hex_str @@ -579,14 +607,10 @@ class ElectrumX(SessionBase): else: handlers.update({ 'blockchain.headers.subscribe': self.headers_subscribe_False, - 'blockchain.address.get_balance': - controller.address_get_balance, - 'blockchain.address.get_history': - controller.address_get_history, - 'blockchain.address.get_mempool': - controller.address_get_mempool, - 'blockchain.address.listunspent': - controller.address_listunspent, + 'blockchain.address.get_balance': self.address_get_balance, + 'blockchain.address.get_history': self.address_get_history, + 'blockchain.address.get_mempool': self.address_get_mempool, + 'blockchain.address.listunspent': self.address_listunspent, 'blockchain.address.subscribe': self.address_subscribe, }) @@ -752,7 +776,7 @@ class DashElectrumX(ElectrumX): balance = await self.controller.address_get_balance( mn_info['payee']) mn_info['balance'] = (sum(balance.values()) - / self.controller.coin.VALUE_PER_COIN) + / self.coin.VALUE_PER_COIN) mn_list.append(mn_info) self.controller.mn_cache = mn_list