diff --git a/node/test_utxo_db.py b/node/test_utxo_db.py index c41aba40c..2063f6efa 100644 --- a/node/test_utxo_db.py +++ b/node/test_utxo_db.py @@ -812,6 +812,32 @@ def _conn(self): }, block_height=10) self.assertFalse(ok) + def test_user_supplied_mining_reward_preserves_external_conn(self): + """Rejected mint attempts must not close a caller-owned connection.""" + conn = self.db._conn() + try: + conn.execute("BEGIN IMMEDIATE") + ok = self.db.apply_transaction({ + 'tx_type': 'mining_reward', + 'inputs': [], + 'outputs': [{'address': 'attacker', 'value_nrtc': 1 * UNIT}], + 'fee_nrtc': 0, + 'timestamp': int(time.time()), + }, block_height=10, conn=conn) + self.assertFalse(ok) + + try: + self.assertEqual(conn.execute("SELECT 1").fetchone()[0], 1) + except Exception as exc: + self.fail(f"apply_transaction closed caller-owned conn: {exc}") + finally: + try: + if conn.in_transaction: + conn.execute("ROLLBACK") + conn.close() + except Exception: + pass + def test_mempool_empty_inputs_rejected_for_transfer(self): """Mempool must also reject non-minting txs with empty inputs.""" tx = { diff --git a/node/utxo_db.py b/node/utxo_db.py index 36c61984a..0faba5538 100644 --- a/node/utxo_db.py +++ b/node/utxo_db.py @@ -446,8 +446,6 @@ def apply_transaction(self, tx: dict, block_height: int, # Require _allow_minting=True (internal flag) to permit mining_reward. MINTING_TX_TYPES = {'mining_reward'} if tx_type in MINTING_TX_TYPES and not tx.get('_allow_minting'): - if conn: - conn.close() return False if own: conn = self._conn()