From 7ffb2c3cb03c2e01fa9911779b7d7284840e4f03 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Thu, 15 Apr 2021 19:00:46 +0200 Subject: [PATCH] config: (trivial) add some type hints and rm unused variable --- electrum/interface.py | 5 +++-- electrum/network.py | 2 +- electrum/simple_config.py | 16 ++++++++-------- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/electrum/interface.py b/electrum/interface.py index 8e85733fd..34cbff1f7 100644 --- a/electrum/interface.py +++ b/electrum/interface.py @@ -29,7 +29,7 @@ import sys import traceback import asyncio import socket -from typing import Tuple, Union, List, TYPE_CHECKING, Optional, Set, NamedTuple, Any, Sequence +from typing import Tuple, Union, List, TYPE_CHECKING, Optional, Set, NamedTuple, Any, Sequence, Dict from collections import defaultdict from ipaddress import IPv4Network, IPv6Network, ip_address, IPv6Address, IPv4Address import itertools @@ -371,7 +371,7 @@ class Interface(Logger): self.tip_header = None self.tip = 0 - self.fee_estimates_eta = {} + self.fee_estimates_eta = {} # type: Dict[int, int] # Dump network messages (only for this interface). Set at runtime from the console. self.debug = False @@ -683,6 +683,7 @@ class Interface(Logger): for nblock_target, task in fee_tasks: fee = task.result() if fee < 0: continue + assert isinstance(fee, int) self.fee_estimates_eta[nblock_target] = fee self.network.update_fee_estimates() await asyncio.sleep(60) diff --git a/electrum/network.py b/electrum/network.py index 268a02700..1b8c25824 100644 --- a/electrum/network.py +++ b/electrum/network.py @@ -534,7 +534,7 @@ class Network(Logger, NetworkRetryManager[ServerAddr]): return {} return self.interface.fee_estimates_eta - def update_fee_estimates(self, *, fee_est: Dict = None): + def update_fee_estimates(self, *, fee_est: Dict[int, int] = None): if fee_est is None: fee_est = self.get_fee_estimates() for nblock_target, fee in fee_est.items(): diff --git a/electrum/simple_config.py b/electrum/simple_config.py index 394819165..3ab248da7 100644 --- a/electrum/simple_config.py +++ b/electrum/simple_config.py @@ -66,8 +66,7 @@ class SimpleConfig(Logger): self.lock = threading.RLock() self.mempool_fees = None # type: Optional[Sequence[Tuple[Union[float, int], int]]] - self.fee_estimates = {} - self.fee_estimates_last_updated = {} + self.fee_estimates = {} # type: Dict[int, int] self.last_time_fee_estimates_requested = 0 # zero ensures immediate fees # The following two functions are there for dependency injection when @@ -517,10 +516,10 @@ class SimpleConfig(Logger): def static_fee(self, i): return FEERATE_STATIC_VALUES[i] - def static_fee_index(self, value) -> int: - if value is None: + def static_fee_index(self, fee_per_kb: Optional[int]) -> int: + if fee_per_kb is None: raise TypeError('static fee cannot be None') - dist = list(map(lambda x: abs(x - value), FEERATE_STATIC_VALUES)) + dist = list(map(lambda x: abs(x - fee_per_kb), FEERATE_STATIC_VALUES)) return min(range(len(dist)), key=dist.__getitem__) def has_fee_etas(self): @@ -611,9 +610,10 @@ class SimpleConfig(Logger): fee_per_byte = quantize_feerate(fee_per_byte) return round(fee_per_byte * size) - def update_fee_estimates(self, key, value): - self.fee_estimates[key] = value - self.fee_estimates_last_updated[key] = time.time() + def update_fee_estimates(self, nblock_target: int, fee_per_kb: int): + assert isinstance(nblock_target, int), f"expected int, got {nblock_target!r}" + assert isinstance(fee_per_kb, int), f"expected int, got {fee_per_kb!r}" + self.fee_estimates[nblock_target] = fee_per_kb def is_fee_estimates_update_required(self): """Checks time since last requested and updated fee estimates.