<p>It is important to understand that the spent flag you are talking about, is not a field of an output that is stored in the blockchain. It is rather created internally by the client interacting with the blockchain. The only fields of information of an output that is permanently stored on the blockchain are:</p>

<ul>
<li>The Amount</li>
<li>The Locking-Script Size</li>
<li>The Locking-Script</li>
</ul>

<p>All the full nodes on the bitcoin network checks if an output is spent or not by checking if the output is part of a structure called the UTXO set. In this structures all nodes keep all outputs which has not yet been spent at the last block of the blockchain the nodes have validated. If an output is part of the UTXO set, it means that the output is unspent. </p>

<p>For every new block in the blockchain, the nodes validates the block and removes any previous outputs in the UTXO set, that has been used as inputs in the new block.</p>

<p>Since a new node validates the blockchain by starting from the first block ever, called the genesis block, and updates UTXO set for every block in the blockchain, the UTXO set will contain all current unspent outputs once the node is synced with the rest of the network.</p>

CC BY-SA 4.0
2018-06-28T15:04:30.230Z
2018-06-30T00:19:50.197Z
2018-06-30T00:19:50.197Z
user44404
<p>I just started getting interested in the blockchain a week ago, however there is something I can not just figure out.</p>

<p>Regarding the flag "Spent/Unspent" in transactions. I get that we flag "spent" the output of a transaction if this same output is used as an input in a future transaction.</p>

<p>-Suppose that we make a transaction (transaction 1), and this transaction get included in the blockchain (let's say block 1). We do not spent the output (consider a simple transaction single input &amp; a single output) until 100 blocks later (transaction 2) in the blockchain.</p>

<p>I understand that in transaction 2, in the inputs, there is the info of the previous transactions (since we are implementing hash pointers)like shown below in "prev_out" (JSON format of transaction extracted from Blockexplorer api) :</p>

<pre><code>'inputs': [{'prev_out': {'addr': '1PFtrRjbq4aLfM7k4tyLZ3ZAuTsgLr6Q8Q',
    'n': 0,
    'script': '76a914f423dfee34e4833104508103d89e316b4a49314088ac',
    'spent': True,
    'tx_index': 356882858,
</code></pre>

<p>Up to now, there is no problem, I can totally understand that we can add a flag spent/unspent for the previous outputs (inputs) in transaction 2.</p>

<p>Suppose now this is the output of transaction 1 (which we are going to spent 100 blocks later).</p>

<p>By the way, this is a real transaction, its hash: <code>a117a7aab7e2db8ef5fd40e74f72d689e13b733136454d2340a244236fbed9eb</code></p>

<pre><code>'out': {'addr': '1PFtrRjbq4aLfM7k4tyLZ3ZAuTsgLr6Q8Q',
   'n': 0,
   'script': '76a914f423dfee34e4833104508103d89e316b4a49314088ac',
   'spent': True,
   'tx_index': 356893407,
   'type': 0,
   'value': 16351264}
</code></pre>

<p><strong>My question is:</strong>
You can see that the flag "spent" is True, How is this even possible ? Because during 99 blocks the transaction was unspent (flag=False). There is only one case possible for the flag "spent" to be True:</p>

<p>-The transaction 2 has to be in the <strong>same block</strong> as transaction 1. Otherwise if transaction 2 is in block 100 and the flag "Spent" of transaction 1 is True, it means that:</p>

<p>For 99 Blocks the flag was False and in the block 100, we change the flag of transaction 1, <strong>which is in block 1</strong>.</p>

<p>That means that we changed block 1, hence changed its hash --> impossible given the structure of the Blockchain.</p>

<p>Thank you for enlightening me !</p>

<transactions><blockchain><spending>
Unspent/Spent Flag in Blockchain
Younes S
# Unspent/Spent Flag in Blockchain

(Asked by: username_0 on 2018-06-28)

I just started getting interested in the blockchain a week ago, however there is something I can not just figure out.

Regarding the flag "Spent/Unspent" in transactions. I get that we flag "spent" the output of a transaction if this same output is used as an input in a future transaction.

-Suppose that we make a transaction (transaction 1), and this transaction get included in the blockchain (let's say block 1). We do not spent the output (consider a simple transaction single input & a single output) until 100 blocks later (transaction 2) in the blockchain.

I understand that in transaction 2, in the inputs, there is the info of the previous transactions (since we are implementing hash pointers)like shown below in "prev\_out" (JSON format of transaction extracted from Blockexplorer api) :

```
'inputs': [{'prev_out': {'addr': '1PFtrRjbq4aLfM7k4tyLZ3ZAuTsgLr6Q8Q',
    'n': 0,
    'script': '76a914f423dfee34e4833104508103d89e316b4a49314088ac',
    'spent': True,
    'tx_index': 356882858,

```

Up to now, there is no problem, I can totally understand that we can add a flag spent/unspent for the previous outputs (inputs) in transaction 2.

Suppose now this is the output of transaction 1 (which we are going to spent 100 blocks later).

By the way, this is a real transaction, its hash: `a117a7aab7e2db8ef5fd40e74f72d689e13b733136454d2340a244236fbed9eb`

```
'out': {'addr': '1PFtrRjbq4aLfM7k4tyLZ3ZAuTsgLr6Q8Q',
   'n': 0,
   'script': '76a914f423dfee34e4833104508103d89e316b4a49314088ac',
   'spent': True,
   'tx_index': 356893407,
   'type': 0,
   'value': 16351264}

```

**My question is:**
You can see that the flag "spent" is True, How is this even possible ? Because during 99 blocks the transaction was unspent (flag=False). There is only one case possible for the flag "spent" to be True:

-The transaction 2 has to be in the **same block** as transaction 1. Otherwise if transaction 2 is in block 100 and the flag "Spent" of transaction 1 is True, it means that:

For 99 Blocks the flag was False and in the block 100, we change the flag of transaction 1, **which is in block 1**.

That means that we changed block 1, hence changed its hash --> impossible given the structure of the Blockchain.

Thank you for enlightening me !

**Tags:** transactions, blockchain, spending


---

## 1 answers

### Answer by username_1 on 2018-06-28. Score: 5 | Accepted answer

It is important to understand that the spent flag you are talking about, is not a field of an output that is stored in the blockchain. It is rather created internally by the client interacting with the blockchain. The only fields of information of an output that is permanently stored on the blockchain are:

* The Amount
* The Locking-Script Size
* The Locking-Script

All the full nodes on the bitcoin network checks if an output is spent or not by checking if the output is part of a structure called the UTXO set. In this structures all nodes keep all outputs which has not yet been spent at the last block of the blockchain the nodes have validated. If an output is part of the UTXO set, it means that the output is unspent.

For every new block in the blockchain, the nodes validates the block and removes any previous outputs in the UTXO set, that has been used as inputs in the new block.

Since a new node validates the blockchain by starting from the first block ever, called the genesis block, and updates UTXO set for every block in the blockchain, the UTXO set will contain all current unspent outputs once the node is synced with the rest of the network.


---

