<p>When a node receives a transaction, they perform the following steps at a high level.</p>

<p>Inductively, they already have a set of validated transactions; these belong to the blockchain they have adopted as well as their current <a href="https://99bitcoins.com/what-is-bitcoin-mempool/" rel="nofollow noreferrer">mempool</a>. From this, the node has extracted a <a href="https://bitcoin.org/en/glossary/unspent-transaction-output" rel="nofollow noreferrer">UTXO</a>.</p>

<ol>
<li><p>Ensure that the inputs of the new transaction reference valid outpoints in the UTXO. This ensures that <strong>the money being spent is real</strong>.</p></li>
<li><p>Verify the Law of Conservation, i.e., that the sum of value of the outputs does not exceed the sum of the value of the inputs. This ensures that <strong>no new money is being created</strong>. The one exception to this is when a new coinbase transaction is received with a newly mined block; in that case, new money can really be created.</p></li>
<li><p>For each input of the new transaction, ensure that running the scriptPubKey script attached to the outpoint it connects to with the parameters specified in the scriptSig of the input, leaves the <a href="https://bitcoin.org/en/glossary/pubkey-script" rel="nofollow noreferrer">Bitcoin Script</a> stack with a non-zero value at the top. Most of the time, this is the step where the digital signature validation happens and ensures <strong>the money is being spent by its rightful owner</strong>.</p></li>
</ol>

<p>The last step above has many subcases and can function in various ways depending on whether the transaction involves a <a href="https://bitcoincore.org/en/segwit_wallet_dev/" rel="nofollow noreferrer">segregated witness</a> or an <a href="https://en.bitcoin.it/wiki/Pay_to_script_hash" rel="nofollow noreferrer">attached indirect script</a>, for example.</p>

<p>Lastly, once a transaction is validated by the above steps, the UTXO of the node is updated so that they can validate further transactions.</p>

<p>None of the above has anything to do with block validation. Block validation is a separate mechanism from transaction validation. Transactions are validated even when they don't belong to blocks. One of the block validation steps is to validate all block transactions with the above algorithm. Transaction validation does not require calculating any block hashes.</p>

CC BY-SA 4.0
2018-08-05T07:42:35.147Z
2018-08-05T07:47:35.670Z
2018-08-05T07:47:35.670Z
dionyziz
<blockquote>
  <p>When a node receives a transaction, what steps does it do to verify that transaction?</p>
</blockquote>

<p>This reference may be a bit obsolete (and even incorrect), but it is a good start: 
<a href="https://en.bitcoin.it/wiki/Protocol_rules#.22tx.22_messages" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/Protocol_rules#.22tx.22_messages</a></p>

<blockquote>
  <p>I know that it verifies the block hash by calculating each hash of the contiguous blocks in order, but does it verify the balance of each wallet sending the transactions as well?</p>
</blockquote>

<p>No sense in this question. Verifying transactions has nothing with checking block hash. And there is no such thing as <code>balance of each wallet</code> in bitcoin protocol.</p>

<blockquote>
  <p>And does it do the block hash calculation every time, or does it just do it when it starts up?</p>
</blockquote>

<p>Checking the hash of the <code>header</code> of block performed only one time when the node receives the block. No need to check it more than once, because the header and its hash can not be changed later.</p>

CC BY-SA 4.0
2018-08-05T06:22:09.043Z
2018-08-05T06:22:09.043Z
amaclin
<blockquote>
  <p>I know that it verifies the block hash by calculating each hash of the contiguous blocks in order</p>
</blockquote>

<p>This alone just verifies that the chain is unbroken. The node will also hash the block header to verify the that hash meets the difficulty requirements for that block height. It also builds the transaction merkle tree, and verifies that the merkle root in the header matches the merkle root of the transaction data.</p>

<blockquote>
  <p>does it verify the balance of each wallet sending the transactions as well</p>
</blockquote>

<p>A wallet is a collection of addresses. As far as the blockchain is concerned, there is no such thing as a wallet, all addresses are treated individually.</p>

<p>A transaction consumes one or more outputs that were previously created to certain addresses. Each address can be associate with multiple outputs. When spending coins, a transaction must reference specific outputs in the past (identified by a transaction ID + output index). The node will then verify that these outputs have not previously been spent by another transaction. It will also verified that the provided scriptSig in the transaction satisfies the requirements for spending an output (usually a signature by a specific private key).</p>

<blockquote>
  <p>And does it do the block hash calculation every time, or does it just do it when it starts up?</p>
</blockquote>

<p>Block verification is only performed as new blocks are received. If you shutdown a node and start it again, it will only run a complete validation for new blocks. </p>

CC BY-SA 4.0
2018-08-05T06:16:52.903Z
2018-08-05T06:16:52.903Z
Raghav Sood
<p>I know that it verifies the block hash by calculating each hash of the contiguous blocks in order, but does it verify the balance of each wallet sending the transactions as well? And does it do the block hash calculation every time, or does it just do it when it starts up? </p>

<bitcoin-core><hash><nodes><transaction-verification>
When a node receives a transaction, what steps does it do to verify that transaction?
Clay O'Neil
# When a node receives a transaction, what steps does it do to verify that transaction?

(Asked by: username_0 on 2018-08-05)

I know that it verifies the block hash by calculating each hash of the contiguous blocks in order, but does it verify the balance of each wallet sending the transactions as well? And does it do the block hash calculation every time, or does it just do it when it starts up?

**Tags:** bitcoin-core, hash, nodes, transaction-verification


---

## 3 answers

### Answer by username_1 on 2018-08-05. Score: 3 | Accepted answer

When a node receives a transaction, they perform the following steps at a high level.

Inductively, they already have a set of validated transactions; these belong to the blockchain they have adopted as well as their current [mempool](https://99bitcoins.com/what-is-bitcoin-mempool/). From this, the node has extracted a [UTXO](https://bitcoin.org/en/glossary/unspent-transaction-output).

1. Ensure that the inputs of the new transaction reference valid outpoints in the UTXO. This ensures that **the money being spent is real**.
2. Verify the Law of Conservation, i.e., that the sum of value of the outputs does not exceed the sum of the value of the inputs. This ensures that **no new money is being created**. The one exception to this is when a new coinbase transaction is received with a newly mined block; in that case, new money can really be created.
3. For each input of the new transaction, ensure that running the scriptPubKey script attached to the outpoint it connects to with the parameters specified in the scriptSig of the input, leaves the [Bitcoin Script](https://bitcoin.org/en/glossary/pubkey-script) stack with a non-zero value at the top. Most of the time, this is the step where the digital signature validation happens and ensures **the money is being spent by its rightful owner**.

The last step above has many subcases and can function in various ways depending on whether the transaction involves a [segregated witness](https://bitcoincore.org/en/segwit_wallet_dev/) or an [attached indirect script](https://en.bitcoin.it/wiki/Pay_to_script_hash), for example.

Lastly, once a transaction is validated by the above steps, the UTXO of the node is updated so that they can validate further transactions.

None of the above has anything to do with block validation. Block validation is a separate mechanism from transaction validation. Transactions are validated even when they don't belong to blocks. One of the block validation steps is to validate all block transactions with the above algorithm. Transaction validation does not require calculating any block hashes.


---

### Answer by username_2 on 2018-08-05. Score: 1

> When a node receives a transaction, what steps does it do to verify that transaction?

This reference may be a bit obsolete (and even incorrect), but it is a good start:
<https://en.bitcoin.it/wiki/Protocol_rules#.22tx.22_messages>

> I know that it verifies the block hash by calculating each hash of the contiguous blocks in order, but does it verify the balance of each wallet sending the transactions as well?

No sense in this question. Verifying transactions has nothing with checking block hash. And there is no such thing as `balance of each wallet` in bitcoin protocol.

> And does it do the block hash calculation every time, or does it just do it when it starts up?

Checking the hash of the `header` of block performed only one time when the node receives the block. No need to check it more than once, because the header and its hash can not be changed later.


---

### Answer by username_3 on 2018-08-05. Score: 0

> I know that it verifies the block hash by calculating each hash of the contiguous blocks in order

This alone just verifies that the chain is unbroken. The node will also hash the block header to verify the that hash meets the difficulty requirements for that block height. It also builds the transaction merkle tree, and verifies that the merkle root in the header matches the merkle root of the transaction data.

> does it verify the balance of each wallet sending the transactions as well

A wallet is a collection of addresses. As far as the blockchain is concerned, there is no such thing as a wallet, all addresses are treated individually.

A transaction consumes one or more outputs that were previously created to certain addresses. Each address can be associate with multiple outputs. When spending coins, a transaction must reference specific outputs in the past (identified by a transaction ID + output index). The node will then verify that these outputs have not previously been spent by another transaction. It will also verified that the provided scriptSig in the transaction satisfies the requirements for spending an output (usually a signature by a specific private key).

> And does it do the block hash calculation every time, or does it just do it when it starts up?

Block verification is only performed as new blocks are received. If you shutdown a node and start it again, it will only run a complete validation for new blocks.


---

