{
 "number": 36313,
 "repo": "bitcoin/bitcoin",
 "url": "https://github.com/bitcoin/bitcoin/pull/36313",
 "title": "wallet: fix persistent coin lock upgrade",
 "author": "ArvinFarrelP",
 "author_association": "CONTRIBUTOR",
 "created_at": "2026-09-22T02:27:59Z",
 "updated_at": "2026-10-04T01:42:24Z",
 "description_hash": "089332851cabe246",
 "age_days": 40,
 "draft": false,
 "labels": [
  "Wallet"
 ],
 "milestone": null,
 "base": "master",
 "head_sha": "23dbc1fe5cc02bb3afa681b530f8ebdecdd9a51a",
 "head_ref": "fix/36304-persistent-coin-lock",
 "head_repo": "ArvinFarrelP/bitcoin",
 "head_history": [
  {
   "t": "2026-09-30T03:57:58Z",
   "sha": "bb5dc52082129001b2c711d3dc1022c380940215"
  },
  {
   "t": "2026-10-03T23:38:32Z",
   "sha": "23dbc1fe5cc02bb3afa681b530f8ebdecdd9a51a"
  }
 ],
 "additions": 43,
 "deletions": 1,
 "changed_files": 2,
 "commit_count": 1,
 "size_bucket": "S",
 "mergeable_state": "clean",
 "bot": {
  "drahtbot": {
   "present": true,
   "reviews": {
    "ack": [
     {
      "login": "jcastros",
      "url": "https://github.com/bitcoin/bitcoin/pull/36313#issuecomment-5975483009"
     }
    ],
    "concept_ack": [
     {
      "login": "vicjuma",
      "url": "https://github.com/bitcoin/bitcoin/pull/36313#pullrequestreview-5291418009"
     }
    ]
   },
   "conflicts": []
  }
 },
 "acks_parsed": {
  "vicjuma": {
   "kind": "concept_ack",
   "hash": null,
   "t": "2026-09-23T13:22:12Z",
   "stale": false
  },
  "jcastros": {
   "kind": "ack",
   "hash": "23dbc1fe5c",
   "t": "2026-10-04T01:42:19Z",
   "stale": false
  }
 },
 "acks_tally": {
  "ack": 1,
  "stale_ack": 0,
  "concept_ack": 1,
  "approach_ack": 0,
  "nack": 0,
  "concept_nack": 0,
  "approach_nack": 0
 },
 "reviews": {
  "approved": 0,
  "changes_requested": 0,
  "distinct_reviewers": [
   "achow101",
   "jcastros",
   "vicjuma"
  ]
 },
 "signals": {
  "needs_rebase": false,
  "ci_failed": false,
  "mergeable_state": "clean",
  "last_author_activity": "2026-10-04T01:05:35Z",
  "last_reviewer_activity": "2026-10-04T01:42:19Z",
  "last_reviewer": "jcastros",
  "author_silent_days": 29,
  "waiting_on_author_days": 28,
  "days_since_update": 28
 },
 "refs": {
  "mentioned": [
   36304
  ],
  "depends_on": [],
  "fixes": [
   36304
  ],
  "linked_issues": [
   {
    "number": 36304,
    "type": "issue",
    "state": "open",
    "merged": false,
    "merged_at": null,
    "title": "wallet: upgrading a non-persistent coin lock to persistent leaves a stale lock on the spent output after restart"
   }
  ],
  "references": [
   {
    "number": 36304,
    "type": "issue",
    "state": "open",
    "merged": false,
    "merged_at": null,
    "title": "wallet: upgrading a non-persistent coin lock to persistent leaves a stale lock on the spent output after restart"
   }
  ],
  "conflicts": []
 },
 "stack": {
  "shares_commits_with": [],
  "based_on": [],
  "base_for": []
 },
 "review_paths": [
  "src/wallet/wallet.cpp"
 ],
 "body": "Fix a stale persistent coin lock when a temporary coin lock is upgraded to persistent.\n\nPreviously, upgrading an existing non-persistent lock to a persistent lock used `emplace()` without updating the existing value. As a result, the in-memory lock remained non-persistent.\n\nWhen the coin was later spent, `UnlockCoin()` therefore did not erase the persistent `LOCKED_UTXO` database record. After restarting the wallet, the stale lock was loaded again.\n\nThis change updates the existing lock state when the coin is already present.\n\nAdds a functional test covering:\n- temporary coin lock\n- upgrade to persistent lock\n- spending the locked coin\n- wallet reload\n- verification that the spent lock is not restored\n\nFixes #36304",
 "commits": [
  {
   "sha": "23dbc1fe5cc02bb3afa681b530f8ebdecdd9a51a",
   "date": "2026-10-03T23:36:44Z",
   "message": "wallet: fix persistent coin lock upgrade"
  }
 ],
 "timeline": [
  {
   "t": "2026-09-23T13:11:45Z",
   "kind": "review_comment",
   "who": "vicjuma",
   "assoc": "CONTRIBUTOR",
   "path": "src/wallet/wallet.cpp",
   "commit": "8efd73f405bb1c62ef2d2736552f0cfd78cd62eb",
   "in_reply_to": null,
   "text": "### Testing\n\nYour solution works as expected\n\n**Before:**\n```console\nratedg@0xratedg:~/projects/contributions/bitcoin/build/bin$ ./bitcoin-cli -regtest lockunspent false '[{\"txid\":\"'\"56e8518c816e3e0f4f7c3320bb7fc8cdebc4997ff8bb46777a2e2955283dd9a5\"'\",\"vout\":0}]' true\ntrue\nratedg@0xratedg:~/projects/contributions/bitcoin/build/bin$  RAW=$(./bitcoin-cli -regtest createrawtransaction \\\n  '[{\"txid\":\"'\"56e8518c816e3e0f4f7c3320bb7fc8cdebc4997ff8bb46777a2e2955283dd9a5\"'\",\"vout\":0}]' \\\n  '{\"'\"$ADDR\"'\":49.999}')\nratedg@0xratedg:~/projects/contributions/bitcoin/build/bin$ SIGNED=$(./bitcoin-cli -regtest signrawtransactionwithwallet $RAW | jq -r .hex)\nratedg@0xratedg:~/projects/contributions/bitcoin/build/bin$ ./bitcoin-cli -regtest sendrawtransaction $SIGNED\nc5ceb2a49687731fc359f9bfa4bc4deb0353bac100b2267325dc149d6d85720c\nratedg@0xratedg:~/projects/contributions/bitcoin/build/bin$ ./bitcoin-cli -regtest generatetoaddress 1 $ADDR\n[\n  \"54a452dcaa240214de9de207fa470cbaad9e037b6da66280c019abd2ed9ea868\"\n]\nratedg@0xratedg:~/projects/contributions/bitcoin/build/bin$ ./bitcoin-cli -regtest listlockunspent\n[\n]\nratedg@0xratedg:~/projects/contributions/bitcoin/build/bin$ ./bitcoin-cli stop\nBitcoin Core stopping\nratedg@0xratedg:~/projects/contributions/bitcoin/build/bin$ ./bitcoind\nBitcoin Core starting\nratedg@0xratedg:~/projects/contributions/bitcoin/build/bin$ ./bitcoin-cli loadwallet locktest\n{\n  \"name\": \"locktest\"\n}\nratedg@0xratedg:~/projects/contributions/bitcoin/build/bin$ ./bitcoin-cli -regtest listlockunspent\n[\n  {\n    \"txid\": \"56e8518c816e3e0f4f7c3320bb7fc8cdebc4997ff8bb46777a2e2955283dd9a5\",\n    \"vout\": 0\n  }\n]\n```\n\n**After:**\n\n```console\nratedg@0xratedg:~/projects/contributions/bitcoin/build2/bin$ ./bitcoin-cli -regtest lockunspent false '[{\"txid\":\"'\"642c8da8bd3c98c34a9fcbbc6361ebb33847efb60fc37b4f0dbee5779fde8caf\"'\",\"vout\":0}]' true\ntrue\nratedg@0xratedg:~/projects/contributions/bitcoin/build2/bin$ ./bitcoin-cli getbalance\n50.00000000\nratedg@0xratedg:~/projects/contributions/bitcoin/build2/bin$ RAW=$(./bitcoin-cli -regtest createrawtransaction \\\n  '[{\"txid\":\"'\"$TXID\"'\",\"vout\":0}]' \\\n  '{\"'\"$ADDR\"'\":49.999}')\nerror code: -8\nerror message:\ntxid must be of length 64 (not 0, for '')\nratedg@0xratedg:~/projects/contributions/bitcoin/build2/bin$ RAW=$(./bitcoin-cli -regtest createrawtransaction \\\n  '[{\"txid\":\"'\"642c8da8bd3c98c34a9fcbbc6361ebb33847efb60fc37b4f0dbee5779fde8caf\"'\",\"vout\":0}]' \\\n  '{\"'\"$ADDR\"'\":49.999}')\nratedg@0xratedg:~/projects/contributions/bitcoin/build2/bin$ SIGNED=$(./bitcoin-cli -regtest signrawtransactionwithwallet $RAW | jq -r .hex)\nratedg@0xratedg:~/projects/contributions/bitcoin/build2/bin$ ./bitcoin-cli -regtest sendrawtransaction $SIGNED\nbf2dbc5d32806197c9beddd750a5b90dd50f855fe2736ed8a7101db1618664aa\nratedg@0xratedg:~/projects/contributions/bitcoin/build2/bin$ ./bitcoin-cli -regtest generatetoaddress 1 $ADDR\n[\n  \"1821ed455cf0c0b5c5b5dbd537922cef873b802e732ae1b2acb53835a5588e06\"\n]\nratedg@0xratedg:~/projects/contributions/bitcoin/build2/bin$ ./bitcoin-cli -regtest listlockunspent\n[\n]\nratedg@0xratedg:~/projects/contributions/bitcoin/build2/bin$ ./bitcoin-cli stop\nBitcoin Core stopping\nratedg@0xratedg:~/projects/contributions/bitcoin/build2/bin$ ./bitcoind\nBitcoin Core starting\nratedg@0xratedg:~/projects/contributions/bitcoin/build2/bin$ ./bitcoin-cli loadwallet locktest\n{\n  \"name\": \"locktest\"\n}\nratedg@0xratedg:~/projects/contributions/bitcoin/build2/bin$ ./bitcoin-cli -regtest listlockunspent\n[\n]\n\n```",
   "id": "rc:4082789218",
   "url": "https://github.com/bitcoin/bitcoin/pull/36313#discussion_r4082789218",
   "hash": "74b514fb6b6740c2",
   "edited": "2026-09-23T13:22:12Z"
  },
  {
   "t": "2026-09-23T13:22:12Z",
   "kind": "review",
   "who": "vicjuma",
   "assoc": "CONTRIBUTOR",
   "state": "COMMENTED",
   "commit": "8efd73f405bb1c62ef2d2736552f0cfd78cd62eb",
   "text": "Concept ACK.\n\nBut why won't the user just unlock then lock it again with persistence:-)? So that after\n\n```console\nratedg@0xratedg:~/projects/contributions/bitcoin/build/bin$ ./bitcoin-cli lockunspent false '[{\"txid\":\"'\"ad83e72a347518506d6043643b42a7be682d2178aa2905d63c918e7eeb800feb\"'\",\"vout\":0}]'\ntrue\nratedg@0xratedg:~/projects/contributions/bitcoin/build/bin$ ./bitcoin-cli listlockunspent\n[\n  {\n    \"txid\": \"ad83e72a347518506d6043643b42a7be682d2178aa2905d63c918e7eeb800feb\",\n    \"vout\": 0\n  }\n]\n```\n\nthey can\n\n```console\nratedg@0xratedg:~/projects/contributions/bitcoin/build/bin$ ./bitcoin-cli lockunspent true '[{\"txid\":\"'\"ad83e72a347518506d6043643b42a7be682d2178aa2905d63c918e7eeb800feb\"'\",\"vout\":0}]'\ntrue\nratedg@0xratedg:~/projects/contributions/bitcoin/build/bin$ ./bitcoin-cli listlockunspent\n[\n]\nratedg@0xratedg:~/projects/contributions/bitcoin/build/bin$ ./bitcoin-cli lockunspent false '[{\"txid\":\"'\"ad83e72a347518506d6043643b42a7be682d2178aa2905d63c918e7eeb800feb\"'\",\"vout\":0}]' true\ntrue\nratedg@0xratedg:~/projects/contributions/bitcoin/build/bin$ ./bitcoin-cli listlockunspent\n[\n  {\n    \"txid\": \"ad83e72a347518506d6043643b42a7be682d2178aa2905d63c918e7eeb800feb\",\n    \"vout\": 0\n  }\n]\n\n```\n\nMaybe your version offers a great UX. This code `src/wallet/rpc/coins.cpp:328:            throw JSONRPCError(RPC_INVALID_PARAMETER, \"Invalid parameter, output already locked\");`\n\n```cpp\n    if (!fUnlock && is_locked && !persistent) {\n            throw JSONRPCError(RPC_INVALID_PARAMETER, \"Invalid parameter, output already locked\");\n        }\n```\nalso supports your solution cause it dictates that we could go from non-persistence to persistence in a single call but not the other way around",
   "id": "r:5291418009",
   "url": "https://github.com/bitcoin/bitcoin/pull/36313#pullrequestreview-5291418009",
   "hash": "9e5ba31210f5803e",
   "edited": null
  },
  {
   "t": "2026-09-24T01:02:30Z",
   "kind": "review_comment",
   "who": "ArvinFarrelP",
   "assoc": "CONTRIBUTOR",
   "path": "src/wallet/wallet.cpp",
   "commit": "8efd73f405bb1c62ef2d2736552f0cfd78cd62eb",
   "in_reply_to": 4082789218,
   "text": "Thanks for the Concept ACK and testing! I didn't go with unlock-then-lock because it requires two RPC calls and isn't atomic \u2014 another process could lock the coin in between. The fix updates the existing entry when `emplace` reports the coin was already present, handling the upgrade in a single call and matching the existing behavior in `coins.cpp:328`.",
   "id": "rc:4088801045",
   "url": "https://github.com/bitcoin/bitcoin/pull/36313#discussion_r4088801045",
   "hash": "5b26536e36f7527d",
   "edited": null
  },
  {
   "t": "2026-09-29T22:01:50Z",
   "kind": "review_comment",
   "who": "achow101",
   "assoc": "MEMBER",
   "path": "src/wallet/wallet.cpp",
   "commit": "8efd73f405bb1c62ef2d2736552f0cfd78cd62eb",
   "in_reply_to": null,
   "text": "In 8efd73f405bb1c62ef2d2736552f0cfd78cd62eb \"wallet: fix persistent coin lock upgrade\"\n\nThis can use `insert_or_assign` rather than setting the fields afterwards on failed emplacement.",
   "id": "rc:4138795110",
   "url": "https://github.com/bitcoin/bitcoin/pull/36313#discussion_r4138795110",
   "hash": "9469dcd2348eab7e",
   "edited": "2026-09-29T22:01:53Z"
  },
  {
   "t": "2026-09-30T03:57:58Z",
   "kind": "force_push",
   "who": "ArvinFarrelP",
   "commit": "bb5dc52082129001b2c711d3dc1022c380940215",
   "id": "push:bb5dc52082129001b2c711d3dc1022c380940215"
  },
  {
   "t": "2026-09-30T05:58:03Z",
   "kind": "review_comment",
   "who": "ArvinFarrelP",
   "assoc": "CONTRIBUTOR",
   "path": "src/wallet/wallet.cpp",
   "commit": "8efd73f405bb1c62ef2d2736552f0cfd78cd62eb",
   "in_reply_to": 4138795110,
   "text": "Thanks for the suggestion! I've updated the code to use `insert_or_assign` as suggested.",
   "id": "rc:4141254523",
   "url": "https://github.com/bitcoin/bitcoin/pull/36313#discussion_r4141254523",
   "hash": "fa5faa98fe350baa",
   "edited": null
  },
  {
   "t": "2026-10-03T15:57:46Z",
   "kind": "comment",
   "who": "jcastros",
   "assoc": "NONE",
   "text": "Concept ACK on fixing #36304.\n\nTested `bb5dc52082` against its base, `bb5296576e`.\n\nOn the base, the #36304 steps still show the spent coin in `listlockunspent` after `unloadwallet` / `loadwallet`, and `wallet_basic.py` fails with `not(1 == 0)`. On this commit the list stays empty and the test passes.\n\nThe other direction looks broken. Lock the coin with `lockunspent` persistent, then spend that same coin. On the base, `listlockunspent` is empty before and after a reload. On this commit it is empty before the reload and the spent output is back afterwards (`gettxout` is null). I got that from both of these:\n\n- `createrawtransaction` of that input, then `fundrawtransaction` with `lockUnspents=true`\n- `sendall` with that coin in `inputs` and `lock_unspents=true` (`\"complete\": true`)\n\n`fundrawtransaction` and `sendall` call `LockCoin(prevout, false)` ([spend.cpp:1543](https://github.com/bitcoin/bitcoin/blob/bb5dc52082129001b2c711d3dc1022c380940215/src/wallet/spend.cpp#L1543), [rpc/spend.cpp:1581](https://github.com/bitcoin/bitcoin/blob/bb5dc52082129001b2c711d3dc1022c380940215/src/wallet/rpc/spend.cpp#L1581)). `insert_or_assign` sets the in-memory flag to false and leaves the database record. The spend goes through `AddToSpends` to [UnlockCoin](https://github.com/bitcoin/bitcoin/blob/bb5dc52082129001b2c711d3dc1022c380940215/src/wallet/wallet.cpp#L801), which skips the erase because the flag is no longer persistent.\n\nA later non-persistent lock should not clear a persistent one. A test for this path would have caught it.",
   "id": "c:5970820999",
   "url": "https://github.com/bitcoin/bitcoin/pull/36313#issuecomment-5970820999",
   "hash": "f7b2f520e3a1df8b",
   "edited": null
  },
  {
   "t": "2026-10-03T23:38:32Z",
   "kind": "force_push",
   "who": "ArvinFarrelP",
   "commit": "23dbc1fe5cc02bb3afa681b530f8ebdecdd9a51a",
   "id": "push:23dbc1fe5cc02bb3afa681b530f8ebdecdd9a51a"
  },
  {
   "t": "2026-10-04T01:05:35Z",
   "kind": "comment",
   "who": "ArvinFarrelP",
   "assoc": "CONTRIBUTOR",
   "text": "Good catch. `insert_or_assign` could downgrade an existing persistent lock when `LockCoin()` was called non-persistently. I've changed the implementation to preserve the persistent state when the coin is already locked, and added a regression test for this case. `wallet_basic.py` passes with the new test.",
   "id": "c:5975240001",
   "url": "https://github.com/bitcoin/bitcoin/pull/36313#issuecomment-5975240001",
   "hash": "40103f93194ca46a",
   "edited": null
  },
  {
   "t": "2026-10-04T01:42:19Z",
   "kind": "comment",
   "who": "jcastros",
   "assoc": "NONE",
   "text": "Tested ACK 23dbc1fe5c.\n\nThe downgrade is gone. I locked a coin persistently, spent it with `fundrawtransaction` `lockUnspents=true`, and `listlockunspent` stayed empty across `unloadwallet` / `loadwallet` (`gettxout` null). Same with `sendall`, that coin in `inputs`, and `lock_unspents=true` (`\"complete\": true`).\n\n`wallet_basic.py` passes. Putting `insert_or_assign` back in `LoadLockedCoin` fails the new check at [wallet_basic.py:239](https://github.com/bitcoin/bitcoin/blob/23dbc1fe5cc02bb3afa681b530f8ebdecdd9a51a/test/functional/wallet_basic.py#L239) with `not(1 == 0)`.",
   "id": "c:5975483009",
   "url": "https://github.com/bitcoin/bitcoin/pull/36313#issuecomment-5975483009",
   "hash": "4a910d49581eb501",
   "edited": null
  }
 ],
 "labels_log": [
  {
   "t": "2026-09-22T02:28:02Z",
   "action": "labeled",
   "label": "Wallet",
   "who": "DrahtBot"
  }
 ],
 "state_log": [],
 "text_chars": 9035,
 "text_tokens_estimate": 2258,
 "changed_paths": [
  "src/wallet/wallet.cpp",
  "test/functional/wallet_basic.py"
 ],
 "files": [
  {
   "path": "src/wallet/wallet.cpp",
   "add": 4,
   "del": 1
  },
  {
   "path": "test/functional/wallet_basic.py",
   "add": 39,
   "del": 0
  }
 ],
 "test_lines": 39,
 "git": {
  "head": "23dbc1fe5cc02bb3afa681b530f8ebdecdd9a51a",
  "head_matches_backup": true,
  "base": "bb5296576e8f1a9fc11c19d9a25ba02ed4547e24",
  "patch_id": "3cdb6c83df0ebf98",
  "commits": [
   {
    "sha": "23dbc1fe5c",
    "subject": "wallet: fix persistent coin lock upgrade",
    "files": 2,
    "add": 43,
    "del": 1
   }
  ],
  "patch_truncated": false
 },
 "patch_id": "3cdb6c83df0ebf98",
 "input_hash": "523365ee8824b615",
 "extracted_at": "2026-11-02T01:09:49+00:00"
}