From a036a2eb3f0490f2f678f11b8544077ec6e8e2cd Mon Sep 17 00:00:00 2001 From: Neil Booth Date: Mon, 6 Aug 2018 20:22:01 +0900 Subject: [PATCH] Rename get_utxos to all_utxos. - no longer takes a limit - runs in a thread to avoid blocking --- contrib/query.py | 7 ++++-- electrumx/server/chain_state.py | 15 +++++-------- electrumx/server/db.py | 39 +++++++++++++++------------------ electrumx/server/peers.py | 2 +- electrumx/server/session.py | 4 ++-- 5 files changed, 32 insertions(+), 35 deletions(-) diff --git a/contrib/query.py b/contrib/query.py index 955ff37..1a85dd7 100755 --- a/contrib/query.py +++ b/contrib/query.py @@ -80,13 +80,16 @@ async def query(args): if n is None: print('No history found') n = None - for n, utxo in enumerate(db.get_utxos(hashX, limit), start=1): + utxos = await db.all_utxos(hashX) + for n, utxo in enumerate(utxos, start=1): print(f'UTXO #{n:,d}: tx_hash {hash_to_hex_str(utxo.tx_hash)} ' f'tx_pos {utxo.tx_pos:,d} height {utxo.height:,d} ' f'value {utxo.value:,d}') + if n == limit: + break if n is None: print('No UTXOs found') - balance = db.get_balance(hashX) + balance = sum(utxo.value for utxo in utxos) print(f'Balance: {coin.decimal_value(balance):,f} {coin.SHORTNAME}') diff --git a/electrumx/server/chain_state.py b/electrumx/server/chain_state.py index 38b4e7b..b950bf4 100644 --- a/electrumx/server/chain_state.py +++ b/electrumx/server/chain_state.py @@ -27,6 +27,7 @@ class ChainState(object): self.force_chain_reorg = self._bp.force_chain_reorg self.tx_branch_and_root = self._bp.merkle.branch_and_root self.read_headers = self._bp.read_headers + self.all_utxos = self._bp.all_utxos async def broadcast_transaction(self, raw_tx): return await self._daemon.sendrawtransaction([raw_tx]) @@ -57,13 +58,6 @@ class ChainState(object): return await run_in_thread(job) - async def get_utxos(self, hashX): - '''Get UTXOs asynchronously to reduce latency.''' - def job(): - return list(self._bp.get_utxos(hashX, limit=None)) - - return await run_in_thread(job) - def header_branch_and_root(self, length, height): return self._bp.header_mc.branch_and_root(length, height) @@ -115,15 +109,18 @@ class ChainState(object): if n is None: lines.append('No history found') n = None - for n, utxo in enumerate(db.get_utxos(hashX, limit), start=1): + utxos = await db.all_utxos(hashX) + for n, utxo in enumerate(utxos, start=1): lines.append(f'UTXO #{n:,d}: tx_hash ' f'{hash_to_hex_str(utxo.tx_hash)} ' f'tx_pos {utxo.tx_pos:,d} height ' f'{utxo.height:,d} value {utxo.value:,d}') + if n == limit: + break if n is None: lines.append('No UTXOs found') - balance = db.get_balance(hashX) + balance = sum(utxo.value for utxo in utxos) lines.append(f'Balance: {coin.decimal_value(balance):,f} ' f'{coin.SHORTNAME}') diff --git a/electrumx/server/db.py b/electrumx/server/db.py index 8abb0d8..c288ff3 100644 --- a/electrumx/server/db.py +++ b/electrumx/server/db.py @@ -375,28 +375,25 @@ class DB(object): with self.utxo_db.write_batch() as batch: self.write_utxo_state(batch) - def get_balance(self, hashX): - '''Returns the confirmed balance of an address.''' - return sum(utxo.value for utxo in self.get_utxos(hashX, limit=None)) - - def get_utxos(self, hashX, limit=1000): - '''Generator that yields all UTXOs for an address sorted in no - particular order. By default yields at most 1000 entries. - Set limit to None to get them all. + async def all_utxos(self, hashX): + '''Return all UTXOs for an address sorted in no particular order. By + default yields at most 1000 entries. ''' - limit = util.resolve_limit(limit) - s_unpack = unpack - # Key: b'u' + address_hashX + tx_idx + tx_num - # Value: the UTXO value as a 64-bit unsigned integer - prefix = b'u' + hashX - for db_key, db_value in self.utxo_db.iterator(prefix=prefix): - if limit == 0: - return - limit -= 1 - tx_pos, tx_num = s_unpack('