Browse Source

local tx: restructure exception handling wrt wallet.add_transaction and QT

3.1
SomberNight 7 years ago
parent
commit
6f5751977b
  1. 18
      gui/qt/history_list.py
  2. 20
      gui/qt/main_window.py
  3. 16
      gui/qt/transaction_dialog.py
  4. 17
      lib/wallet.py

18
gui/qt/history_list.py

@ -26,7 +26,7 @@
import webbrowser import webbrowser
import datetime import datetime
from electrum.wallet import UnrelatedTransactionException, TX_HEIGHT_LOCAL from electrum.wallet import AddTransactionException, TX_HEIGHT_LOCAL
from .util import * from .util import *
from electrum.i18n import _ from electrum.i18n import _
from electrum.util import block_explorer_URL from electrum.util import block_explorer_URL
@ -356,16 +356,12 @@ class HistoryList(MyTreeWidget, AcceptFileDragDrop):
self.parent.need_update.set() self.parent.need_update.set()
def onFileAdded(self, fn): def onFileAdded(self, fn):
with open(fn) as f: try:
tx = self.parent.tx_from_text(f.read()) with open(fn) as f:
try: tx = self.parent.tx_from_text(f.read())
self.wallet.add_transaction(tx.txid(), tx) self.parent.save_transaction_into_wallet(tx)
except UnrelatedTransactionException as e: except IOError as e:
self.parent.show_error(e) self.parent.show_error(e)
else:
self.wallet.save_transactions(write=True)
# need to update at least: history_list, utxo_list, address_list
self.parent.need_update.set()
def export_history_dialog(self): def export_history_dialog(self):
d = WindowModalDialog(self, _('Export History')) d = WindowModalDialog(self, _('Export History'))

20
gui/qt/main_window.py

@ -51,7 +51,7 @@ from electrum.util import (format_time, format_satoshis, PrintError,
from electrum import Transaction from electrum import Transaction
from electrum import util, bitcoin, commands, coinchooser from electrum import util, bitcoin, commands, coinchooser
from electrum import paymentrequest from electrum import paymentrequest
from electrum.wallet import Multisig_Wallet from electrum.wallet import Multisig_Wallet, AddTransactionException
from .amountedit import AmountEdit, BTCAmountEdit, MyLineEdit, FeerateEdit from .amountedit import AmountEdit, BTCAmountEdit, MyLineEdit, FeerateEdit
from .qrcodewidget import QRCodeWidget, QRDialog from .qrcodewidget import QRCodeWidget, QRDialog
@ -3125,3 +3125,21 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
if is_final: if is_final:
new_tx.set_rbf(False) new_tx.set_rbf(False)
self.show_transaction(new_tx, tx_label) self.show_transaction(new_tx, tx_label)
def save_transaction_into_wallet(self, tx):
try:
if not self.wallet.add_transaction(tx.txid(), tx):
self.show_error(_("Transaction could not be saved.") + "\n" +
_("It conflicts with current history."))
return False
except AddTransactionException as e:
self.show_error(e)
return False
else:
self.wallet.save_transactions(write=True)
# need to update at least: history_list, utxo_list, address_list
self.need_update.set()
self.show_message(_("Transaction saved successfully"))
return True

16
gui/qt/transaction_dialog.py

@ -35,7 +35,7 @@ from electrum.i18n import _
from electrum.plugins import run_hook from electrum.plugins import run_hook
from electrum.util import bfh from electrum.util import bfh
from electrum.wallet import UnrelatedTransactionException from electrum.wallet import AddTransactionException
from .util import * from .util import *
@ -179,17 +179,9 @@ class TxDialog(QDialog, MessageBoxMixin):
self.main_window.sign_tx(self.tx, sign_done) self.main_window.sign_tx(self.tx, sign_done)
def save(self): def save(self):
if not self.wallet.add_transaction(self.tx.txid(), self.tx): if self.main_window.save_transaction_into_wallet(self.tx):
self.show_error(_("Transaction could not be saved. It conflicts with current history.")) self.save_button.setDisabled(True)
return self.saved = True
self.wallet.save_transactions(write=True)
# need to update at least: history_list, utxo_list, address_list
self.main_window.need_update.set()
self.save_button.setDisabled(True)
self.show_message(_("Transaction saved successfully"))
self.saved = True
def export(self): def export(self):

17
lib/wallet.py

@ -157,9 +157,18 @@ def sweep(privkeys, network, config, recipient, fee=None, imax=100):
return tx return tx
class UnrelatedTransactionException(Exception): class AddTransactionException(Exception):
def __init__(self): pass
self.args = ("Transaction is unrelated to this wallet ", )
class UnrelatedTransactionException(AddTransactionException):
def __str__(self):
return _("Transaction is unrelated to this wallet.")
class NotIsMineTransactionException(AddTransactionException):
def __str__(self):
return _("Only transactions with inputs owned by the wallet can be added.")
class Abstract_Wallet(PrintError): class Abstract_Wallet(PrintError):
@ -768,7 +777,7 @@ class Abstract_Wallet(PrintError):
# do not save if tx is local and not mine # do not save if tx is local and not mine
if tx_height == TX_HEIGHT_LOCAL and not is_mine: if tx_height == TX_HEIGHT_LOCAL and not is_mine:
# FIXME the test here should be for "not all is_mine"; cannot detect conflict in some cases # FIXME the test here should be for "not all is_mine"; cannot detect conflict in some cases
return False raise NotIsMineTransactionException()
# raise exception if unrelated to wallet # raise exception if unrelated to wallet
is_for_me = any([self.is_mine(self.get_txout_address(txo)) for txo in tx.outputs()]) is_for_me = any([self.is_mine(self.get_txout_address(txo)) for txo in tx.outputs()])
if not is_mine and not is_for_me: if not is_mine and not is_for_me:

Loading…
Cancel
Save