<p>In the case of a fork, you have to <a href="https://github.com/bitcoin/bitcoin/blob/v0.12.0rc2/src/main.cpp#L1792-L1858" rel="nofollow"><code>Disconnect</code></a> the blocks that are on the losing side of the fork, and then <a href="https://github.com/bitcoin/bitcoin/blob/v0.12.0rc2/src/main.cpp#L1962-L2166" rel="nofollow"><code>Connect</code></a> the blocks on the winning side of the fork, in order. If you look at the <code>CCoinsViewCache</code> input into the function, you can see how the UTXO set is updated.</p>

<p>Essentially, though, you have to reverse the effects of all of the transactions in the losing blocks and apply the effects of the transactions in the new blocks. Reversing means processing inputs to mark STXOs as UTXOs and removing UTXOs created in the block. Processing new transactions into the UTXO set means, in addition to verifying the transactions are valid, you mark UTXOs spent by the inputs of transactions as STXOs (or remove them, depending on how your system works), and you create new UTXOs for the outputs in the block. </p>

<p>Keep in mind that you have apply these processes for each transaction in the block in order (or reverse order if Disconnecting a block), because transaction B can spend from outputs created in transaction A, where A &amp; B are in the same block.</p>

<hr>

<blockquote>
  <p>My goal is to maintain an index of addresses to UTXOs.</p>
</blockquote>

<p>If I were you, I would keep an index of the <code>scriptPubKey</code> and then convert an address to the scriptPubKey you need when you need to do a lookup.</p>

CC BY-SA 3.0
2016-01-26T15:45:53.767Z
2016-01-26T15:45:53.767Z
morsecoder
<p>I am looking for a filename and a line, or multiple ones if necessary. My goal is to maintain an index of addresses to UTXOs.</p>

<bitcoind><bitcoin-core><address><utxo>
Where can I see the code of how the UTXO set is updated in case of a fork in Bitcoin Core?
stanm87
# Where can I see the code of how the UTXO set is updated in case of a fork in Bitcoin Core?

(Asked by: username_0 on 2016-01-26)

I am looking for a filename and a line, or multiple ones if necessary. My goal is to maintain an index of addresses to UTXOs.

**Tags:** bitcoind, bitcoin-core, address, utxo


---

## 1 answers

### Answer by username_1 on 2016-01-26. Score: 2 | Accepted answer

In the case of a fork, you have to [`Disconnect`](https://github.com/bitcoin/bitcoin/blob/v0.12.0rc2/src/main.cpp#L1792-L1858) the blocks that are on the losing side of the fork, and then [`Connect`](https://github.com/bitcoin/bitcoin/blob/v0.12.0rc2/src/main.cpp#L1962-L2166) the blocks on the winning side of the fork, in order. If you look at the `CCoinsViewCache` input into the function, you can see how the UTXO set is updated.

Essentially, though, you have to reverse the effects of all of the transactions in the losing blocks and apply the effects of the transactions in the new blocks. Reversing means processing inputs to mark STXOs as UTXOs and removing UTXOs created in the block. Processing new transactions into the UTXO set means, in addition to verifying the transactions are valid, you mark UTXOs spent by the inputs of transactions as STXOs (or remove them, depending on how your system works), and you create new UTXOs for the outputs in the block.

Keep in mind that you have apply these processes for each transaction in the block in order (or reverse order if Disconnecting a block), because transaction B can spend from outputs created in transaction A, where A & B are in the same block.

---

> My goal is to maintain an index of addresses to UTXOs.

If I were you, I would keep an index of the `scriptPubKey` and then convert an address to the scriptPubKey you need when you need to do a lookup.


---

