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

This commit is contained in:
SomberNight 2018-02-21 03:58:08 +01:00
parent 363f3766d7
commit 6f5751977b
4 changed files with 43 additions and 28 deletions

View File

@ -26,7 +26,7 @@
import webbrowser
import datetime
from electrum.wallet import UnrelatedTransactionException, TX_HEIGHT_LOCAL
from electrum.wallet import AddTransactionException, TX_HEIGHT_LOCAL
from .util import *
from electrum.i18n import _
from electrum.util import block_explorer_URL
@ -356,16 +356,12 @@ class HistoryList(MyTreeWidget, AcceptFileDragDrop):
self.parent.need_update.set()
def onFileAdded(self, fn):
with open(fn) as f:
tx = self.parent.tx_from_text(f.read())
try:
self.wallet.add_transaction(tx.txid(), tx)
except UnrelatedTransactionException as 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()
try:
with open(fn) as f:
tx = self.parent.tx_from_text(f.read())
self.parent.save_transaction_into_wallet(tx)
except IOError as e:
self.parent.show_error(e)
def export_history_dialog(self):
d = WindowModalDialog(self, _('Export History'))

View File

@ -51,7 +51,7 @@ from electrum.util import (format_time, format_satoshis, PrintError,
from electrum import Transaction
from electrum import util, bitcoin, commands, coinchooser
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 .qrcodewidget import QRCodeWidget, QRDialog
@ -3125,3 +3125,21 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
if is_final:
new_tx.set_rbf(False)
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

View File

@ -35,7 +35,7 @@ from electrum.i18n import _
from electrum.plugins import run_hook
from electrum.util import bfh
from electrum.wallet import UnrelatedTransactionException
from electrum.wallet import AddTransactionException
from .util import *
@ -179,17 +179,9 @@ class TxDialog(QDialog, MessageBoxMixin):
self.main_window.sign_tx(self.tx, sign_done)
def save(self):
if not self.wallet.add_transaction(self.tx.txid(), self.tx):
self.show_error(_("Transaction could not be saved. It conflicts with current history."))
return
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
if self.main_window.save_transaction_into_wallet(self.tx):
self.save_button.setDisabled(True)
self.saved = True
def export(self):

View File

@ -157,9 +157,18 @@ def sweep(privkeys, network, config, recipient, fee=None, imax=100):
return tx
class UnrelatedTransactionException(Exception):
def __init__(self):
self.args = ("Transaction is unrelated to this wallet ", )
class AddTransactionException(Exception):
pass
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):
@ -768,7 +777,7 @@ class Abstract_Wallet(PrintError):
# do not save if tx is local and not 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
return False
raise NotIsMineTransactionException()
# raise exception if unrelated to wallet
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: