Skip to content

Commit

Permalink
Fix wallet load error on incomplete gettransaction
Browse files Browse the repository at this point in the history
The error happens when the wallet has some locked outputs in
`listlockunspent` and the corresponding `gettransaction` doesn't contain
the details of this particular output.

It may be related to bitcoin/bitcoin#28555. It
is at least the same symptom.

The locked outputs were locked by creating a transaction in Specter.
This has happened to me multiple times.

The UI says it failed to load the wallet with the following error:
Failed to load utxos, IndexError: list index out of range

The raw transaction does contain the missing output so we use that
instead.

Here's an example that generates the error:
listlockunspent:
[
  {
    "txid": "<some txid>",
    "vout": 0
  }
]

gettransaction <some txid>:
{
  "amount": <redacted>,
  "fee": <redacted>,
  "confirmations": <redacted>,
  "blockhash": "<redacted>",
  "blockheight": <redacted>,
  "blockindex": <redacted>,
  "blocktime": <redacted>,
  "txid": "<some tx id>",
  "wtxid": "<redacted>",
  "walletconflicts": [
  ],
  "time": <redacted>,
  "timereceived": <redacted>,
  "bip125-replaceable": "no",
  "details": [
    {
      "address": "<redacted>",
      "category": "send",
      "amount": <redacted>,
      "label": "<redacted>",
      "vout": 1,
      "fee": <redacted>,
      "abandoned": false
    }
  ],
  "hex": "<some raw tx hex>",
  "lastprocessedblock": {
    "hash": "<redacted>",
    "height": <redacted>
  }
}

decoderawtransaction <some raw tx hex>:
{
  "txid": "<some txid>",
  "hash": "<redacted>",
  "version": 2,
  "size": <redacted>,
  "vsize": <redacted>,
  "weight": <redacted>,
  "locktime": <redacted>,
  "vin": [
    {
      "txid": "<some other txid>",
      "vout": 0,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "txinwitness": [
        "<redacted>",
        "<redacted>"
      ],
      "sequence": <redacted>
    }
  ],
  "vout": [
    {
      "value": <redacted>,
      "n": 0,
      "scriptPubKey": {
        "asm": "<redacted>",
        "desc": "<redacted>",
        "hex": "<redacted>",
        "address": "<redacted>",
        "type": "<redacted>"
      }
    },
    {
      "value": <redacted>,
      "n": 1,
      "scriptPubKey": {
        "asm": "<redacted>",
        "desc": "<redacted>",
        "hex": "<redacted>",
        "address": "<redacted>",
        "type": "<redacted>"
      }
    }
  ]
}
  • Loading branch information
leon-costa committed Aug 1, 2024
1 parent 0ac4640 commit d74d4ab
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/cryptoadvance/specter/wallet/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,13 +718,28 @@ def check_utxo(self):
else:
tx_from_core = self.rpc.gettransaction(tx_copy["txid"])
# in the case of locked amounts, the stupid listlockunspent call does not contain reasonable utxo-data
searched_vout = [
searched_vouts = [
_tx
for _tx in tx_from_core["details"]
if _tx["vout"] == utxo_vout
][0]
tx_copy["amount"] = searched_vout["amount"]
tx_copy["address"] = searched_vout["address"]
]
if len(searched_vouts) > 0:
searched_vout = searched_vouts[0]
tx_copy["amount"] = searched_vout["amount"]
tx_copy["address"] = searched_vout["address"]
else:
# Sometimes gettransaction doesn't return the complete list of outputs.
# It may be related to https://github.com/bitcoin/bitcoin/issues/28555
# In this case we decode the raw transaction to get the data we want.
raw_transaction_hex = tx_from_core["hex"]
raw_transaction = self.rpc.decoderawtransaction(raw_transaction_hex)
searched_raw_vout = [
_output
for _output in raw_transaction["vout"]
if _output["n"] == utxo_vout
][0]
tx_copy["amount"] = searched_raw_vout["value"]
tx_copy["address"] = searched_raw_vout["scriptPubKey"]["address"]

# Append the copy to the _full_utxo list
_full_utxo.append(tx_copy)
Expand Down

0 comments on commit d74d4ab

Please sign in to comment.