Browse Source

qt utxo list: spend_list is now a set (and renamed)

this is a small performance improvement ("if x in spend_list" was linear)
and the "order" of selected coins does not matter anyway
master
SomberNight 6 years ago
parent
commit
11f54aee60
No known key found for this signature in database
GPG Key ID: B33B5F232C6271E9
  1. 20
      electrum/gui/qt/utxo_list.py

20
electrum/gui/qt/utxo_list.py

@ -23,7 +23,7 @@
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE. # SOFTWARE.
from typing import Optional, List, Dict, Sequence from typing import Optional, List, Dict, Sequence, Set
from enum import IntEnum from enum import IntEnum
from PyQt5.QtCore import Qt from PyQt5.QtCore import Qt
@ -58,7 +58,7 @@ class UTXOList(MyTreeView):
super().__init__(parent, self.create_menu, super().__init__(parent, self.create_menu,
stretch_column=self.Columns.LABEL, stretch_column=self.Columns.LABEL,
editable_columns=[]) editable_columns=[])
self.spend_list = [] # type: Sequence[str] self._spend_set = set() # type: Set[str] # coins selected by the user to spend from
self.setModel(QStandardItemModel(self)) self.setModel(QStandardItemModel(self))
self.setSelectionMode(QAbstractItemView.ExtendedSelection) self.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.setSortingEnabled(True) self.setSortingEnabled(True)
@ -75,12 +75,12 @@ class UTXOList(MyTreeView):
self.insert_utxo(idx, utxo) self.insert_utxo(idx, utxo)
self.filter() self.filter()
# update coincontrol status bar # update coincontrol status bar
coins = [self.utxo_dict[x] for x in self.spend_list] or utxos coins = [self.utxo_dict[x] for x in self._spend_set] or utxos
coins = self._filter_frozen_coins(coins) coins = self._filter_frozen_coins(coins)
amount = sum(x.value_sats() for x in coins) amount = sum(x.value_sats() for x in coins)
amount_str = self.parent.format_amount_and_units(amount) amount_str = self.parent.format_amount_and_units(amount)
num_outputs_str = _("{} outputs available ({} total)").format(len(coins), len(utxos)) num_outputs_str = _("{} outputs available ({} total)").format(len(coins), len(utxos))
if self.spend_list: if self._spend_set:
self.parent.set_coincontrol_msg(_("Coin control active") + f': {num_outputs_str}, {amount_str}') self.parent.set_coincontrol_msg(_("Coin control active") + f': {num_outputs_str}, {amount_str}')
else: else:
self.parent.set_coincontrol_msg(None) self.parent.set_coincontrol_msg(None)
@ -101,7 +101,7 @@ class UTXOList(MyTreeView):
utxo_item[self.Columns.OUTPOINT].setFont(QFont(MONOSPACE_FONT)) utxo_item[self.Columns.OUTPOINT].setFont(QFont(MONOSPACE_FONT))
utxo_item[self.Columns.ADDRESS].setData(name, Qt.UserRole) utxo_item[self.Columns.ADDRESS].setData(name, Qt.UserRole)
SELECTED_TO_SPEND_TOOLTIP = _('Coin selected to be spent') SELECTED_TO_SPEND_TOOLTIP = _('Coin selected to be spent')
if name in self.spend_list: if name in self._spend_set:
for col in utxo_item: for col in utxo_item:
col.setBackground(ColorScheme.GREEN.as_color(True)) col.setBackground(ColorScheme.GREEN.as_color(True))
if col != self.Columns.OUTPOINT: if col != self.Columns.OUTPOINT:
@ -113,7 +113,7 @@ class UTXOList(MyTreeView):
utxo_item[self.Columns.OUTPOINT].setBackground(ColorScheme.BLUE.as_color(True)) utxo_item[self.Columns.OUTPOINT].setBackground(ColorScheme.BLUE.as_color(True))
utxo_item[self.Columns.OUTPOINT].setToolTip(f"{name}\n{_('Coin is frozen')}") utxo_item[self.Columns.OUTPOINT].setToolTip(f"{name}\n{_('Coin is frozen')}")
else: else:
tooltip = ("\n" + SELECTED_TO_SPEND_TOOLTIP) if name in self.spend_list else "" tooltip = ("\n" + SELECTED_TO_SPEND_TOOLTIP) if name in self._spend_set else ""
utxo_item[self.Columns.OUTPOINT].setToolTip(name + tooltip) utxo_item[self.Columns.OUTPOINT].setToolTip(name + tooltip)
self.model().insertRow(idx, utxo_item) self.model().insertRow(idx, utxo_item)
@ -133,17 +133,17 @@ class UTXOList(MyTreeView):
def set_spend_list(self, coins: List[PartialTxInput]): def set_spend_list(self, coins: List[PartialTxInput]):
coins = self._filter_frozen_coins(coins) coins = self._filter_frozen_coins(coins)
self.spend_list = [utxo.prevout.to_str() for utxo in coins] self._spend_set = {utxo.prevout.to_str() for utxo in coins}
self.update() self.update()
def get_spend_list(self) -> Sequence[PartialTxInput]: def get_spend_list(self) -> Sequence[PartialTxInput]:
return [self.utxo_dict[x] for x in self.spend_list] return [self.utxo_dict[x] for x in self._spend_set]
def _maybe_reset_spend_list(self, current_wallet_utxos: Sequence[PartialTxInput]) -> None: def _maybe_reset_spend_list(self, current_wallet_utxos: Sequence[PartialTxInput]) -> None:
# if we spent one of the selected UTXOs, just reset selection # if we spent one of the selected UTXOs, just reset selection
utxo_set = {utxo.prevout.to_str() for utxo in current_wallet_utxos} utxo_set = {utxo.prevout.to_str() for utxo in current_wallet_utxos}
if not all([prevout_str in utxo_set for prevout_str in self.spend_list]): if not all([prevout_str in utxo_set for prevout_str in self._spend_set]):
self.spend_list = [] self._spend_set = set()
def create_menu(self, position): def create_menu(self, position):
selected = self.get_selected_outpoints() selected = self.get_selected_outpoints()

Loading…
Cancel
Save