utxo_ledger (reference): failed apply_transaction leaks already-created outputs (supply inflation)

Open Beginner friendly
#8,766 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 10 days

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
79/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
blockchain

Research direction

Read rips/rustchain-core/utxo_ledger.py, especially UtxoSet.apply_transaction around lines 303–330 and add_transaction. Start by checking the duplicate-output probe and how failed applies restore spent inputs; add a regression test that verifies total supply is unchanged after failure. Done means failed applies leave no outputs behind, inputs remain spendable, and the mempool rejects duplicate outputs.

Written by the indexing model from the issue text.

Description

Found while reviewing #8302. It already exists on main and is not caused by that PR.

Where: rips/rustchain-core reference ledger, utxo_ledger.py UtxoSet.apply_transaction (around lines 303–330). This is reference code; the live node ledger (node/utxo_db.py) is not affected.

Bug: if one output's box_id collides with an earlier output in the same transaction, or with an existing UTXO, add_box raises after the earlier outputs were already inserted. The rollback only restores the spent inputs. The result:

  • apply_transaction returns False;
  • the input is still spendable;
  • the outputs created before the failure stay in the set.

In other words, value is minted. A probe with a 100-unit input went from supply 100 to 150.

Suggested fix (verified with the duplicate-box_id probe: supply stays 100):

  • Before applying, reject the transaction if output ids are not unique, or if any of them already exists or was already spent.
  • Record the outputs as they are created. On failure, remove them as well as restoring the inputs.
  • Give add_transaction (the mempool) the same duplicate-output check.
+        output_ids = [out.box_id for out in tx.outputs]
+        if len(output_ids) != len(set(output_ids)):
+            return False
+        if any(oid in self._boxes or oid in self._spent for oid in output_ids):
+            return False
         spent_boxes = []
+        created_boxes = []
 ...
                 self.add_box(output, owner)
+                created_boxes.append((output.box_id, owner))
 ...
         except Exception as e:
+            for box_id, owner in created_boxes:
+                self._boxes.pop(box_id, None)
+                self._by_address.get(owner, set()).discard(box_id)

A regression test should check that total supply is unchanged after a failed apply.

Dominant language
Python
Stars
850
Forks
589
Avg merge
11d 7h
Merged PRs (30d)
29

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Scottcjn/Rustchain

All issues in Scottcjn/Rustchain

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.