Skip to content

Commit

Permalink
Fix some database errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryp Toon committed May 15, 2024
1 parent 44b0ba3 commit 48e9b2f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions bitcoinlib/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
9 changes: 8 additions & 1 deletion bitcoinlib/wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
2 changes: 2 additions & 0 deletions tests/test_wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#

import unittest
import time
from random import shuffle

try:
Expand Down Expand Up @@ -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)

0 comments on commit 48e9b2f

Please sign in to comment.