wallet: some performance optimisations for get_receiving_addresses

jsondb takes a copy of the whole self.receiving_addresses
good for avoiding race conditions but horrible for performance...
this significantly speeds up at least
- synchronize_sequence, and
- is_beyond_limit (used by Qt AddressList)
This commit is contained in:
SomberNight 2019-06-28 20:20:24 +02:00
parent a2bffb9137
commit c6a54f05f5
No known key found for this signature in database
GPG key ID: B33B5F232C6271E9
3 changed files with 38 additions and 21 deletions

View file

@ -31,7 +31,7 @@ from PyQt5.QtGui import QStandardItemModel, QStandardItem, QFont
from PyQt5.QtWidgets import QAbstractItemView, QComboBox, QLabel, QMenu from PyQt5.QtWidgets import QAbstractItemView, QComboBox, QLabel, QMenu
from electrum.i18n import _ from electrum.i18n import _
from electrum.util import block_explorer_URL from electrum.util import block_explorer_URL, profiler
from electrum.plugin import run_hook from electrum.plugin import run_hook
from electrum.bitcoin import is_address from electrum.bitcoin import is_address
from electrum.wallet import InternalAddressCorruption from electrum.wallet import InternalAddressCorruption
@ -107,6 +107,7 @@ class AddressList(MyTreeView):
self.show_used = state self.show_used = state
self.update() self.update()
@profiler
def update(self): def update(self):
self.wallet = self.parent.wallet self.wallet = self.parent.wallet
current_address = self.current_item_user_role(col=self.Columns.LABEL) current_address = self.current_item_user_role(col=self.Columns.LABEL)

View file

@ -691,12 +691,14 @@ class JsonDB(Logger):
return len(self.receiving_addresses) return len(self.receiving_addresses)
@locked @locked
def get_change_addresses(self): def get_change_addresses(self, *, slice_start=None, slice_stop=None):
return list(self.change_addresses) # note: slicing makes a shallow copy
return self.change_addresses[slice_start:slice_stop]
@locked @locked
def get_receiving_addresses(self): def get_receiving_addresses(self, *, slice_start=None, slice_stop=None):
return list(self.receiving_addresses) # note: slicing makes a shallow copy
return self.receiving_addresses[slice_start:slice_stop]
@modifier @modifier
def add_change_address(self, addr): def add_change_address(self, addr):

View file

@ -38,7 +38,7 @@ import traceback
from functools import partial from functools import partial
from numbers import Number from numbers import Number
from decimal import Decimal from decimal import Decimal
from typing import TYPE_CHECKING, List, Optional, Tuple, Union, NamedTuple from typing import TYPE_CHECKING, List, Optional, Tuple, Union, NamedTuple, Sequence
from .i18n import _ from .i18n import _
from .util import (NotEnoughFunds, UserCancelled, profiler, from .util import (NotEnoughFunds, UserCancelled, profiler,
@ -434,8 +434,15 @@ class Abstract_Wallet(AddressSynchronizer):
utxos = [utxo for utxo in utxos if not self.is_frozen_coin(utxo)] utxos = [utxo for utxo in utxos if not self.is_frozen_coin(utxo)]
return utxos return utxos
def get_receiving_addresses(self, *, slice_start=None, slice_stop=None) -> Sequence:
raise NotImplementedError() # implemented by subclasses
def get_change_addresses(self, *, slice_start=None, slice_stop=None) -> Sequence:
raise NotImplementedError() # implemented by subclasses
def dummy_address(self): def dummy_address(self):
return self.get_receiving_addresses()[0] # first receiving address
return self.get_receiving_addresses(slice_start=0, slice_stop=1)[0]
def get_frozen_balance(self): def get_frozen_balance(self):
if not self.frozen_coins: # shortcut if not self.frozen_coins: # shortcut
@ -692,7 +699,7 @@ class Abstract_Wallet(AddressSynchronizer):
change_addrs = addrs change_addrs = addrs
else: else:
# if there are none, take one randomly from the last few # if there are none, take one randomly from the last few
addrs = self.get_change_addresses()[-self.gap_limit_for_change:] addrs = self.get_change_addresses(slice_start=-self.gap_limit_for_change)
change_addrs = [random.choice(addrs)] if addrs else [] change_addrs = [random.choice(addrs)] if addrs else []
for addr in change_addrs: for addr in change_addrs:
assert is_address(addr), f"not valid bitcoin address: {addr}" assert is_address(addr), f"not valid bitcoin address: {addr}"
@ -1506,10 +1513,10 @@ class Imported_Wallet(Simple_Wallet):
# note: overridden so that the history can be cleared # note: overridden so that the history can be cleared
return self.db.get_imported_addresses() return self.db.get_imported_addresses()
def get_receiving_addresses(self): def get_receiving_addresses(self, **kwargs):
return self.get_addresses() return self.get_addresses()
def get_change_addresses(self): def get_change_addresses(self, **kwargs):
return [] return []
def import_addresses(self, addresses: List[str], *, def import_addresses(self, addresses: List[str], *,
@ -1661,16 +1668,15 @@ class Deterministic_Wallet(Abstract_Wallet):
def get_addresses(self): def get_addresses(self):
# note: overridden so that the history can be cleared. # note: overridden so that the history can be cleared.
# addresses are ordered based on derivation # addresses are ordered based on derivation
out = [] out = self.get_receiving_addresses()
out += self.get_receiving_addresses()
out += self.get_change_addresses() out += self.get_change_addresses()
return out return out
def get_receiving_addresses(self): def get_receiving_addresses(self, *, slice_start=None, slice_stop=None):
return self.db.get_receiving_addresses() return self.db.get_receiving_addresses(slice_start=slice_start, slice_stop=slice_stop)
def get_change_addresses(self): def get_change_addresses(self, *, slice_start=None, slice_stop=None):
return self.db.get_change_addresses() return self.db.get_change_addresses(slice_start=slice_start, slice_stop=slice_stop)
@profiler @profiler
def try_detecting_internal_addresses_corruption(self): def try_detecting_internal_addresses_corruption(self):
@ -1748,11 +1754,15 @@ class Deterministic_Wallet(Abstract_Wallet):
def synchronize_sequence(self, for_change): def synchronize_sequence(self, for_change):
limit = self.gap_limit_for_change if for_change else self.gap_limit limit = self.gap_limit_for_change if for_change else self.gap_limit
while True: while True:
addresses = self.get_change_addresses() if for_change else self.get_receiving_addresses() num_addr = self.db.num_change_addresses() if for_change else self.db.num_receiving_addresses()
if len(addresses) < limit: if num_addr < limit:
self.create_new_address(for_change) self.create_new_address(for_change)
continue continue
if any(map(self.address_is_old, addresses[-limit:])): if for_change:
last_few_addresses = self.get_change_addresses(slice_start=-limit)
else:
last_few_addresses = self.get_receiving_addresses(slice_start=-limit)
if any(map(self.address_is_old, last_few_addresses)):
self.create_new_address(for_change) self.create_new_address(for_change)
else: else:
break break
@ -1764,11 +1774,15 @@ class Deterministic_Wallet(Abstract_Wallet):
def is_beyond_limit(self, address): def is_beyond_limit(self, address):
is_change, i = self.get_address_index(address) is_change, i = self.get_address_index(address)
addr_list = self.get_change_addresses() if is_change else self.get_receiving_addresses()
limit = self.gap_limit_for_change if is_change else self.gap_limit limit = self.gap_limit_for_change if is_change else self.gap_limit
if i < limit: if i < limit:
return False return False
prev_addresses = addr_list[max(0, i - limit):max(0, i)] slice_start = max(0, i - limit)
slice_stop = max(0, i)
if is_change:
prev_addresses = self.get_change_addresses(slice_start=slice_start, slice_stop=slice_stop)
else:
prev_addresses = self.get_receiving_addresses(slice_start=slice_start, slice_stop=slice_stop)
for addr in prev_addresses: for addr in prev_addresses:
if self.db.get_addr_history(addr): if self.db.get_addr_history(addr):
return False return False