tsimplify get_tx_fee - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit 7b828359c6049ab1910154e80490fbb1baaf5fa3 DIR parent 482605edbbe43aa27cbea58729d38585b75e4ee7 HTML Author: ThomasV <thomasv@electrum.org> Date: Thu, 12 Sep 2019 12:26:49 +0200 simplify get_tx_fee Diffstat: M electrum/address_synchronizer.py | 32 +++++++++++++++---------------- M electrum/json_db.py | 8 +++++--- M electrum/wallet.py | 8 ++++---- 3 files changed, 24 insertions(+), 24 deletions(-) --- DIR diff --git a/electrum/address_synchronizer.py b/electrum/address_synchronizer.py t@@ -451,7 +451,7 @@ class AddressSynchronizer(Logger): for tx_hash in tx_deltas: delta = tx_deltas[tx_hash] tx_mined_status = self.get_tx_height(tx_hash) - fee, is_calculated_by_us = self.get_tx_fee(tx_hash) + fee = self.get_tx_fee(tx_hash) history.append((tx_hash, tx_mined_status, delta, fee)) history.sort(key = lambda x: self.get_txpos(x[0]), reverse=True) # 3. add balance t@@ -689,39 +689,37 @@ class AddressSynchronizer(Logger): fee = None return is_relevant, is_mine, v, fee - def get_tx_fee(self, txid: str) -> Tuple[Optional[int], bool]: - """Returns (tx_fee, is_calculated_by_us).""" + def get_tx_fee(self, txid: str) -> Optional[int]: + """ Returns tx_fee or None. Use server fee only if tx is unconfirmed and not mine""" # check if stored fee is available - # return that, if is_calc_by_us - fee = None - fee_and_bool = self.db.get_tx_fee(txid) - if fee_and_bool is not None: - fee, is_calc_by_us = fee_and_bool - if is_calc_by_us: - return fee, is_calc_by_us - elif self.get_tx_height(txid).conf > 0: - # delete server-sent fee for confirmed txns - self.db.add_tx_fee_from_server(txid, None) - fee = None + fee = self.db.get_tx_fee(txid, trust_server=False) + if fee is not None: + return fee + # delete server-sent fee for confirmed txns + confirmed = self.get_tx_height(txid).conf > 0 + if confirmed: + self.db.add_tx_fee_from_server(txid, None) # if all inputs are ismine, try to calc fee now; # otherwise, return stored value num_all_inputs = self.db.get_num_all_inputs_of_tx(txid) if num_all_inputs is not None: + # check if tx is mine num_ismine_inputs = self.db.get_num_ismine_inputs_of_tx(txid) assert num_ismine_inputs <= num_all_inputs, (num_ismine_inputs, num_all_inputs) + # trust server if tx is unconfirmed and not mine if num_ismine_inputs < num_all_inputs: - return fee, False + return None if confirmed else self.db.get_tx_fee(txid, trust_server=True) # lookup tx and deserialize it. # note that deserializing is expensive, hence above hacks tx = self.db.get_transaction(txid) if not tx: - return None, False + return None with self.lock, self.transaction_lock: is_relevant, is_mine, v, fee = self.get_wallet_delta(tx) # save result self.db.add_tx_fee_we_calculated(txid, fee) self.db.add_num_inputs_to_tx(txid, len(tx.inputs())) - return fee, True + return fee def get_addr_io(self, address): with self.lock, self.transaction_lock: DIR diff --git a/electrum/json_db.py b/electrum/json_db.py t@@ -699,12 +699,14 @@ class JsonDB(Logger): self.tx_fees[txid] = self.tx_fees[txid]._replace(fee=fee_sat, is_calculated_by_us=True) @locked - def get_tx_fee(self, txid: str) -> Optional[Tuple[Optional[int], bool]]: - """Returns (tx_fee, is_calculated_by_us).""" + def get_tx_fee(self, txid: str, *, trust_server=False) -> Optional[int]: + """Returns tx_fee.""" tx_fees_value = self.tx_fees.get(txid) if tx_fees_value is None: return None - return tx_fees_value.fee, tx_fees_value.is_calculated_by_us + if not trust_server and not tx_fees_value.is_calculated_by_us: + return None + return tx_fees_value.fee @modifier def add_num_inputs_to_tx(self, txid: str, num_inputs: int) -> None: DIR diff --git a/electrum/wallet.py b/electrum/wallet.py t@@ -409,7 +409,7 @@ class Abstract_Wallet(AddressSynchronizer): elif tx_mined_status.height in (TX_HEIGHT_UNCONF_PARENT, TX_HEIGHT_UNCONFIRMED): status = _('Unconfirmed') if fee is None: - fee, _calc_by_us = self.get_tx_fee(tx_hash) + fee = self.get_tx_fee(tx_hash, trust_server=True) if fee and self.network and self.config.has_fee_mempool(): size = tx.estimated_size() fee_per_byte = fee / size t@@ -722,7 +722,7 @@ class Abstract_Wallet(AddressSynchronizer): is_final = tx and tx.is_final() if not is_final: extra.append('rbf') - fee, _calc_by_us = self.get_tx_fee(tx_hash) + fee = self.get_tx_fee(tx_hash, trust_server=True) if fee is not None: size = tx.estimated_size() fee_per_byte = fee / size t@@ -1004,8 +1004,8 @@ class Abstract_Wallet(AddressSynchronizer): old_tx_size = tx.estimated_size() old_txid = tx.txid() assert old_txid - old_fee, is_calc_by_us = self.get_tx_fee(old_txid) - if old_fee is None or not is_calc_by_us: + old_fee = self.get_tx_fee(old_txid) + if old_fee is None: raise CannotBumpFee(_('Cannot bump fee') + ': ' + _('current fee unknown')) old_fee_rate = old_fee / old_tx_size # sat/vbyte if new_fee_rate <= old_fee_rate: