From 51d0b3f1d29372efd628a2fd25a912c7c76dc875 Mon Sep 17 00:00:00 2001 From: Andreas Griffin <116060138+andreasgriffin@users.noreply.github.com> Date: Sat, 25 Jan 2025 11:44:18 +0100 Subject: [PATCH] Bug fix when calling set_data_from_mempoolspace from timer (#72) --- bitcoin_safe/__init__.py | 2 +- bitcoin_safe/gui/qt/block_buttons.py | 31 ++++++++++++++++---- bitcoin_safe/gui/qt/fee_group.py | 12 ++++++-- bitcoin_safe/gui/qt/main.py | 6 ++-- bitcoin_safe/gui/qt/network_settings/main.py | 4 +-- bitcoin_safe/gui/qt/ui_tx.py | 20 +++++++++---- bitcoin_safe/network_config.py | 2 +- bitcoin_safe/wallet.py | 2 +- pyproject.toml | 2 +- 9 files changed, 59 insertions(+), 22 deletions(-) diff --git a/bitcoin_safe/__init__.py b/bitcoin_safe/__init__.py index 863a523..446fde1 100644 --- a/bitcoin_safe/__init__.py +++ b/bitcoin_safe/__init__.py @@ -1,2 +1,2 @@ # this is the source of the version information -__version__ = "1.0.2" +__version__ = "1.0.3" diff --git a/bitcoin_safe/gui/qt/block_buttons.py b/bitcoin_safe/gui/qt/block_buttons.py index 593334b..5411e05 100644 --- a/bitcoin_safe/gui/qt/block_buttons.py +++ b/bitcoin_safe/gui/qt/block_buttons.py @@ -29,7 +29,7 @@ import enum import logging -from typing import Callable, List +from typing import Callable, Dict, List from urllib.parse import urlparse import bdkpython as bdk @@ -44,6 +44,7 @@ ) from bitcoin_safe.config import MIN_RELAY_FEE, UserConfig +from bitcoin_safe.network_config import ProxyInfo from bitcoin_safe.util import block_explorer_URL_of_projected_block, unit_fee_str from ...html_utils import html_f @@ -300,18 +301,22 @@ def set_active(self, index: int, exclusive=True): class ObjectRequiringMempool(QObject): - def __init__(self, mempool_data: MempoolData, parent=None) -> None: + def __init__(self, proxies: Dict | None, mempool_data: MempoolData, parent=None) -> None: super().__init__(parent=parent) + self.proxies = proxies self.mempool_data = mempool_data self.timer = QTimer() - self.timer.timeout.connect(self.mempool_data.set_data_from_mempoolspace) + self.timer.timeout.connect(self.set_data_from_mempoolspace) self.timer.start(10 * 60 * 1000) # 10 minutes in milliseconds def set_mempool_block_unknown_fee_rate(self, i, confirmation_time: bdk.BlockTime | None = None) -> None: logger.error("This should not be called") + def set_data_from_mempoolspace(self): + self.mempool_data.set_data_from_mempoolspace(proxies=self.proxies) + class BaseBlock(QObject): signal_click: TypedPyQtSignal[float] = pyqtSignal(float) # type: ignore @@ -346,7 +351,12 @@ class MempoolButtons(BaseBlock, ObjectRequiringMempool): "Showing multiple buttons of the next, the 2. and the 3. block templates according to the mempool" def __init__( - self, mempool_data: MempoolData, fee_rate: float = 1, max_button_count=3, parent=None + self, + mempool_data: MempoolData, + proxies: Dict | None, + fee_rate: float = 1, + max_button_count=3, + parent=None, ) -> None: button_group = VerticalButtonGroup( network=mempool_data.network_config.network, button_count=max_button_count, parent=parent @@ -354,7 +364,7 @@ def __init__( BaseBlock.__init__( self, mempool_data=mempool_data, button_group=button_group, confirmation_time=None, parent=parent ) - ObjectRequiringMempool.__init__(self, mempool_data=mempool_data, parent=parent) + ObjectRequiringMempool.__init__(self, proxies=proxies, mempool_data=mempool_data, parent=parent) self.fee_rate = fee_rate self.refresh() @@ -411,7 +421,16 @@ def __init__( BaseBlock.__init__( self, mempool_data=mempool_data, button_group=button_group, confirmation_time=None, parent=parent ) - ObjectRequiringMempool.__init__(self, mempool_data=mempool_data, parent=parent) + ObjectRequiringMempool.__init__( + self, + proxies=( + ProxyInfo.parse(config.network_config.proxy_url).get_requests_proxy_dict() + if config.network_config.proxy_url + else None + ), + mempool_data=mempool_data, + parent=parent, + ) self.config = config self.fee_rate = fee_rate diff --git a/bitcoin_safe/gui/qt/fee_group.py b/bitcoin_safe/gui/qt/fee_group.py index b3b5025..79e1af8 100644 --- a/bitcoin_safe/gui/qt/fee_group.py +++ b/bitcoin_safe/gui/qt/fee_group.py @@ -33,6 +33,7 @@ from bitcoin_safe.gui.qt.notification_bar import NotificationBar from bitcoin_safe.gui.qt.util import icon_path from bitcoin_safe.html_utils import html_f, link +from bitcoin_safe.network_config import ProxyInfo from bitcoin_safe.psbt_util import FeeInfo from bitcoin_safe.typestubs import TypedPyQtSignal @@ -137,10 +138,17 @@ def __init__( fee_rate=fee_rate, ) self._mempool_projected_block = MempoolProjectedBlock( - mempool_data, config=self.config, fee_rate=fee_rate + mempool_data=mempool_data, config=self.config, fee_rate=fee_rate ) self._mempool_buttons = MempoolButtons( - fee_rate=fee_rate, mempool_data=mempool_data, max_button_count=5 + fee_rate=fee_rate, + mempool_data=mempool_data, + max_button_count=5, + proxies=( + ProxyInfo.parse(config.network_config.proxy_url).get_requests_proxy_dict() + if config.network_config.proxy_url + else None + ), ) self._all_mempool_buttons: List[BaseBlock] = [ diff --git a/bitcoin_safe/gui/qt/main.py b/bitcoin_safe/gui/qt/main.py index 082c078..1bd770e 100644 --- a/bitcoin_safe/gui/qt/main.py +++ b/bitcoin_safe/gui/qt/main.py @@ -138,7 +138,7 @@ def __init__( self.fx = FX( threading_parent=self.threading_manager, proxies=( - ProxyInfo.parse(self.config.network_config.proxy_url).get_requests_proxy_dist() + ProxyInfo.parse(self.config.network_config.proxy_url).get_requests_proxy_dict() if self.config.network_config.proxy_url else None ), @@ -158,7 +158,7 @@ def __init__( ) self.mempool_data.set_data_from_mempoolspace( proxies=( - ProxyInfo.parse(self.config.network_config.proxy_url).get_requests_proxy_dist() + ProxyInfo.parse(self.config.network_config.proxy_url).get_requests_proxy_dict() if self.config.network_config.proxy_url else None ) @@ -304,7 +304,7 @@ def setupUi(self) -> None: signals_min=self.signals, threading_parent=self.threading_manager, proxies=( - ProxyInfo.parse(self.config.network_config.proxy_url).get_requests_proxy_dist() + ProxyInfo.parse(self.config.network_config.proxy_url).get_requests_proxy_dict() if self.config.network_config.proxy_url else None ), diff --git a/bitcoin_safe/gui/qt/network_settings/main.py b/bitcoin_safe/gui/qt/network_settings/main.py index fe4d5f0..257553a 100644 --- a/bitcoin_safe/gui/qt/network_settings/main.py +++ b/bitcoin_safe/gui/qt/network_settings/main.py @@ -161,7 +161,7 @@ def test_connection(network_config: NetworkConfig) -> Optional[str]: f"{network_config.esplora_url}/blocks/tip/height", timeout=2, proxies=( - ProxyInfo.parse(network_config.proxy_url).get_requests_proxy_dist() + ProxyInfo.parse(network_config.proxy_url).get_requests_proxy_dict() if network_config.proxy_url else None ), @@ -473,7 +473,7 @@ def _test_connection(self, network_config: NetworkConfig) -> Tuple[str | None, b mempool_server = test_mempool_space_server( url=network_config.mempool_url, proxies=( - ProxyInfo.parse(network_config.proxy_url).get_requests_proxy_dist() + ProxyInfo.parse(network_config.proxy_url).get_requests_proxy_dict() if network_config.proxy_url else None ), diff --git a/bitcoin_safe/gui/qt/ui_tx.py b/bitcoin_safe/gui/qt/ui_tx.py index bea1780..39ac7c4 100644 --- a/bitcoin_safe/gui/qt/ui_tx.py +++ b/bitcoin_safe/gui/qt/ui_tx.py @@ -48,6 +48,7 @@ from bitcoin_safe.html_utils import html_f from bitcoin_safe.keystore import KeyStore from bitcoin_safe.labels import LabelType +from bitcoin_safe.network_config import ProxyInfo from bitcoin_safe.threading_manager import TaskThread, ThreadingManager from bitcoin_safe.typestubs import TypedPyQtSignal @@ -1002,6 +1003,15 @@ def set_tab_focus(self, focus_ui_element: UiElements): self.focus_ui_element = UiElements.none + def _get_height_from_mempool(self): + return self.mempool_data.fetch_block_tip_height( + proxies=( + ProxyInfo.parse(self.config.network_config.proxy_url).get_requests_proxy_dict() + if self.config.network_config.proxy_url + else None + ) + ) + def set_visibility(self, confirmation_time: bdk.BlockTime | None) -> None: is_psbt = self.data.data_type == DataType.PSBT self.export_widget_container.setVisible(not is_psbt) @@ -1011,10 +1021,10 @@ def set_visibility(self, confirmation_time: bdk.BlockTime | None) -> None: if not tx: return tx_status = TxStatus( - tx, - confirmation_time, - self.mempool_data.fetch_block_tip_height, - self.is_in_mempool(tx.txid()), + tx=tx, + confirmation_time=confirmation_time, + get_height=self._get_height_from_mempool, + is_in_mempool=self.is_in_mempool(tx.txid()), ) show_send = bool(tx_status.can_do_initial_broadcast() and self.data.data_type == DataType.Tx) @@ -1169,7 +1179,7 @@ def __init__( self.recipients.signal_clicked_send_max_button.connect(self.on_signal_amount_changed) self.recipients.add_recipient() - self.fee_group = FeeGroup(mempool_data, fx, self.config) + self.fee_group = FeeGroup(mempool_data=mempool_data, fx=fx, config=self.config) self.widget_right_top_layout.addWidget( self.fee_group.groupBox_Fee, alignment=Qt.AlignmentFlag.AlignHCenter ) diff --git a/bitcoin_safe/network_config.py b/bitcoin_safe/network_config.py index a476dd8..c84ba11 100644 --- a/bitcoin_safe/network_config.py +++ b/bitcoin_safe/network_config.py @@ -64,7 +64,7 @@ def get_socks_scheme(self) -> Literal[1] | Literal[2]: def get_url(self): return f"{self.scheme}://{self.host}:{self.port}" - def get_requests_proxy_dist(self): + def get_requests_proxy_dict(self): return {"http": self.get_url(), "https": self.get_url()} @classmethod diff --git a/bitcoin_safe/wallet.py b/bitcoin_safe/wallet.py index f2bfd77..506874d 100644 --- a/bitcoin_safe/wallet.py +++ b/bitcoin_safe/wallet.py @@ -99,7 +99,7 @@ def __init__( self, tx: bdk.Transaction | None, confirmation_time: bdk.BlockTime | None, - get_height: Callable, + get_height: Callable[[], int], is_in_mempool: bool, confirmation_status: Optional[TxConfirmationStatus] = None, ) -> None: diff --git a/pyproject.toml b/pyproject.toml index 22143c6..a875130 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,7 +12,7 @@ line-length = 110 name = "bitcoin-safe" # the version here and in all other places in this toml are updated automatically # from the source: bitcoin_safe/__init__.py -version = "1.0.2" +version = "1.0.3" description = "A bitcoin savings wallet for the entire family." authors = [ "andreasgriffin ",] license = "GPL-3.0"