<p>The UTXO set is updated every time a block is connected to the chain. So if you imagine during initial sync, you download block #1 and verify it, then add one UTXO to the database (the coinbase output). Block #2 gets downloaded and verified, and you add one more UTXO to the set and so on. Eventually you download block #1000 which contains a transaction that spends the coinbase output from block #1. If everything is valid, you delete that block #1 coinbase output from the UTXO set and replace it with whatever new coins are created by the transaction in block #1000.</p>
<p>So your assumption that &quot;Now, I synced with it. It's time for my node to create UTXO database&quot; is incorrect - the UTXO database is always under constant maintenance from the moment you launch bitcoind.</p>
<p>To start digging through the code, note <a href="https://github.com/bitcoin/bitcoin/blob/12a1c3ad1a43634d2a98717e49e3f02c4acea2fe/src/validation.cpp#L2193" rel="nofollow noreferrer">this call to <code>UpdateCoins()</code> in the function <code>ConnectBlock()</code></a>. This will eventually call <a href="https://github.com/bitcoin/bitcoin/blob/12a1c3ad1a43634d2a98717e49e3f02c4acea2fe/src/coins.cpp#L102" rel="nofollow noreferrer"><code>AddCoins()</code></a> and you can see the UTXO are actually added to a cache first (which is eventually flushed to disk). But this is the mechanism you are looking for.</p>
<p>You can see where coins are removed from the UTXO cache in <a href="https://github.com/bitcoin/bitcoin/blob/12a1c3ad1a43634d2a98717e49e3f02c4acea2fe/src/coins.cpp#L113" rel="nofollow noreferrer"><code>SpendCoin()</code></a></p>

CC BY-SA 4.0
2020-10-09T13:20:54.937Z
so let's say utxo database is key value and keys are txid+vout. node checks txid+vout in inputs and if it's present in utxo , then it removes it from utxo. something like this ?
Nika Kurashvili
CC BY-SA 4.0
2020-10-09T13:25:17.913Z
yes, I added some links to code in the answer
pinhead
CC BY-SA 4.0
2020-10-09T13:27:35.343Z
Before I check the code ,quick question. how does node know if an input is coinbase transaction ?
Nika Kurashvili
CC BY-SA 4.0
2020-10-09T13:30:26.957Z
coinbase tx can be identified because their inputs are null (they "spend" from a txid of 32 `0x00` bytes) You can the function `isCoinbase()` in `transaction.h`
pinhead
CC BY-SA 4.0
2020-10-09T13:33:23.093Z
Thanks .  do nodes also try to download utxo sets from other nodes? because if i am a new node, syncing with other nodes might take lots of time(downloading blockchain, creating utxo) . does bitcoin do something like ethereum does ? downloading state trie and blockchain in parallel ?
Nika Kurashvili
CC BY-SA 4.0
2020-10-09T13:36:10.787Z
That is not currently possible because there would be no way to verify the data is accurate. There have been several proposals to enable this however: search for "UTXO commitments" on this site.
pinhead
CC BY-SA 4.0
2020-10-09T13:37:17.790Z
Also check out: https://dci.mit.edu/utreexo
pinhead
CC BY-SA 4.0
2020-10-09T13:17:14.547Z
2020-10-09T13:21:33.337Z
2020-10-09T13:21:33.337Z
pinhead
<p>Let's say I am a new node.</p>
<p>I try to synch with the full node to grab the whole blockchain. Now, I synced with it .It's time for my node to create UTXO database ..</p>
<p>How does it do this exactly so that it knows which one to put in UTXO database ? since it will need to know which one is unspent. By looking at the blockchain, it can't quickly figure out which one is unspent for the following Reason: 1st height block contains output which has 5btc sent to me. I only spend this in the 400,000th block. This kind of thing happens for every other output. So how does bitcoin build UTXO in an effective way ?</p>
<p>It would also be great if there could be any code from bitcoind or something so i can take a look. Couldn't find it myself.</p>

<bitcoin-core><transactions><blockchain><bitcoind><utxo>
how does node create UTXO from blockchain for the first time?
Nika Kurashvili
# how does node create UTXO from blockchain for the first time?

(Asked by: username_0 on 2020-10-09)

Let's say I am a new node.

I try to synch with the full node to grab the whole blockchain. Now, I synced with it .It's time for my node to create UTXO database ..

How does it do this exactly so that it knows which one to put in UTXO database ? since it will need to know which one is unspent. By looking at the blockchain, it can't quickly figure out which one is unspent for the following Reason: 1st height block contains output which has 5btc sent to me. I only spend this in the 400,000th block. This kind of thing happens for every other output. So how does bitcoin build UTXO in an effective way ?

It would also be great if there could be any code from bitcoind or something so i can take a look. Couldn't find it myself.

**Tags:** bitcoin-core, transactions, blockchain, bitcoind, utxo


---

## 1 answers

### Answer by username_1 on 2020-10-09. Score: 3 | Accepted answer

The UTXO set is updated every time a block is connected to the chain. So if you imagine during initial sync, you download block #1 and verify it, then add one UTXO to the database (the coinbase output). Block #2 gets downloaded and verified, and you add one more UTXO to the set and so on. Eventually you download block #1000 which contains a transaction that spends the coinbase output from block #1. If everything is valid, you delete that block #1 coinbase output from the UTXO set and replace it with whatever new coins are created by the transaction in block #1000.

So your assumption that "Now, I synced with it. It's time for my node to create UTXO database" is incorrect - the UTXO database is always under constant maintenance from the moment you launch bitcoind.

To start digging through the code, note [this call to `UpdateCoins()` in the function `ConnectBlock()`](https://github.com/bitcoin/bitcoin/blob/12a1c3ad1a43634d2a98717e49e3f02c4acea2fe/src/validation.cpp#L2193). This will eventually call [`AddCoins()`](https://github.com/bitcoin/bitcoin/blob/12a1c3ad1a43634d2a98717e49e3f02c4acea2fe/src/coins.cpp#L102) and you can see the UTXO are actually added to a cache first (which is eventually flushed to disk). But this is the mechanism you are looking for.

You can see where coins are removed from the UTXO cache in [`SpendCoin()`](https://github.com/bitcoin/bitcoin/blob/12a1c3ad1a43634d2a98717e49e3f02c4acea2fe/src/coins.cpp#L113)

> **Comments:**
> **username_0**: so let's say utxo database is key value and keys are txid+vout. node checks txid+vout in inputs and if it's present in utxo , then it removes it from utxo. something like this ?  
> **username_1**: yes, I added some links to code in the answer  
> **username_0**: Before I check the code ,quick question. how does node know if an input is coinbase transaction ?  
> **username_1**: coinbase tx can be identified because their inputs are null (they "spend" from a txid of 32 `0x00` bytes) You can the function `isCoinbase()` in `transaction.h`  
> **username_0**: Thanks .  do nodes also try to download utxo sets from other nodes? because if i am a new node, syncing with other nodes might take lots of time(downloading blockchain, creating utxo) . does bitcoin do something like ethereum does ? downloading state trie and blockchain in parallel ?  
> **username_1**: That is not currently possible because there would be no way to verify the data is accurate. There have been several proposals to enable this however: search for "UTXO commitments" on this site.  
> **username_1**: Also check out: https://dci.mit.edu/utreexo  

---

