From 48e9b2f77c5c362ea16c453d1f39f851276fe6fa Mon Sep 17 00:00:00 2001 From: Cryp Toon Date: Wed, 15 May 2024 22:03:01 +0200 Subject: [PATCH] Fix some database errors --- bitcoinlib/db.py | 4 ++-- bitcoinlib/wallets.py | 9 ++++++++- tests/test_wallets.py | 2 ++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/bitcoinlib/db.py b/bitcoinlib/db.py index 6c863b02..99256f69 100644 --- a/bitcoinlib/db.py +++ b/bitcoinlib/db.py @@ -443,7 +443,7 @@ class DbTransactionInput(Base): transaction = relationship("DbTransaction", back_populates='inputs', doc="Related DbTransaction object") index_n = Column(Integer, primary_key=True, doc="Index number of transaction input") key_id = Column(Integer, ForeignKey('keys.id'), index=True, doc="ID of key used in this input") - key = relationship("DbKey", back_populates="transaction_inputs", doc="Related DbKey object") + key = relationship("DbKey", doc="Related DbKey object") address = Column(String(255), doc="Address string of input, used if no key is associated. " "An cryptocurrency address is a hash of the public key or a redeemscript") @@ -484,7 +484,7 @@ class DbTransactionOutput(Base): doc="Link to transaction object") output_n = Column(BigInteger, primary_key=True, doc="Sequence number of transaction output") key_id = Column(Integer, ForeignKey('keys.id'), index=True, doc="ID of key used in this transaction output") - key = relationship("DbKey", back_populates="transaction_outputs", doc="List of DbKey object used in this output") + key = relationship("DbKey", doc="List of DbKey object used in this output") address = Column(String(255), doc="Address string of output, used if no key is associated. " "An cryptocurrency address is a hash of the public key or a redeemscript") diff --git a/bitcoinlib/wallets.py b/bitcoinlib/wallets.py index 2129a94f..4de9f11e 100644 --- a/bitcoinlib/wallets.py +++ b/bitcoinlib/wallets.py @@ -3748,7 +3748,14 @@ def select_inputs(self, amount, variance=None, input_key_id=None, account_id=Non utxo_query = utxo_query.filter(DbKey.id.in_(input_key_id)) if skip_dust_amounts: utxo_query = utxo_query.filter(DbTransactionOutput.value >= dust_amount) - utxos = utxo_query.order_by(DbTransaction.confirmations.desc()).all() + utxo_query = utxo_query.order_by(DbTransaction.confirmations.desc()) + try: + utxos = utxo_query.all() + except Exception as e: + self.session.close() + logger.warning("Error when querying database, retry: %s" % str(e)) + utxos = utxo_query.all() + if not utxos: raise WalletError("Create transaction: No unspent transaction outputs found or no key available for UTXO's") diff --git a/tests/test_wallets.py b/tests/test_wallets.py index c9fecd8c..daf30106 100644 --- a/tests/test_wallets.py +++ b/tests/test_wallets.py @@ -19,6 +19,7 @@ # import unittest +import time from random import shuffle try: @@ -2943,6 +2944,7 @@ def test_wallet_transaction_remove_unconfirmed(self): w.transactions_remove_unconfirmed(1) self.assertEqual(len(w.utxos()), 5) self.assertEqual(w.balance(), 104441651) + time.sleep(3) w.transactions_remove_unconfirmed(0) self.assertEqual(len(w.utxos()), 3) self.assertEqual(w.balance(), 102057170)