<p>The nodes form a gossip network. Every time a node hears about a new (unconfirmed¹) transaction, they check whether the transaction would be eligible to be added to a block, and if they deem it valid, they forward it to their peers.</p>
<p>Each miner builds their own block template. The first transaction in the block template must be a <a href="https://bitcoin.stackexchange.com/a/96442/5406">coinbase transaction</a>. In it the miner pays out the block reward to themselves. The other transactions are <a href="https://bitcoin.stackexchange.com/q/57823/5406">a selection</a> of unconfirmed transactions the miner has heard from the network about. The miner usually selects the unconfirmed transactions that offer the most transaction fees in sum, as the miner gets to keep the transaction fees.</p>
<p>Each miner tries hashing <a href="https://bitcoin.stackexchange.com/a/96442/5406">many variants</a> of their block template with different values for the nonce and the extranonce. Eventually one miner <a href="https://bitcoin.stackexchange.com/a/22984/5406">will find</a> a block header that passes the difficulty requirement—a valid block. The successful miner publishes their valid block to their peers, who forward in turn to their peers and so forth. Since the successful miner added the instruction to pay themselves in their block template's coinbase transaction, the block pays the reward to the successful miner.</p>
<p>Every other node validates that the block and all transactions in it follow the protocol rules. If anything is wrong, they drop the block. Otherwise, they update their <a href="https://bitcoin.stackexchange.com/q/4301/5406">UTXO set</a> to mark all transaction outputs that were used as spent and add the new transaction outputs that were created.</p>
<p>Then everything restarts from the top: miners build a block template, hash many variations of it, someone finds a block, it gets relayed, nodes validate it, apply the changes to their UTXO set, the miner that found it gets paid.</p>
<p>Since this is a random uncoordinated process, <a href="https://bitcoin.stackexchange.com/a/97197/5406">occasionally two miners will get lucky at the same time</a> and publish competing blocks at the same height. Each node will see one or the other first and the network will not have a shared ground-truth. The next block found then breaks the tie: whatever chain has more total work becomes canon, the other becomes extinct. The best chain can only have <a href="https://bitcoin.stackexchange.com/q/12427/5406">one block at each height</a>, so there is <a href="https://bitcoin.stackexchange.com/q/59257/5406">no question</a> of &quot;what happens to the transactions verified in the competing block&quot;. From the perspective of the best chain, that block might as well not exist, it's irrelevant.</p>
<p>And from top: tick tock, next block.</p>
<hr />
<p><sup>¹ Unconfirmed means &quot;not added to a block yet&quot;.</sup></p>

CC BY-SA 4.0
2022-07-08T17:45:29.023Z
2022-07-08T18:10:40.093Z
2022-07-08T18:10:40.093Z
Murch
<blockquote>
<p><strong>How do transaction verification and adding a block to the blockchain fit together?</strong></p>
</blockquote>
<p><em><strong>Verification</strong></em> of transactions is performed by every participant in the Bitcoin network. Participants include normal wallets and are usually referred to as nodes.</p>
<p>Only miners create new blocks. Thereby <em><strong>confirming</strong></em> the transactions in them.</p>
<p>Note the important distinction between verification and confirmation.</p>
<blockquote>
<p><strong>How is a transaction added to a block?</strong></p>
</blockquote>
<p>A miner selects unconfirmed transactions and adds them to a block template. When they find an arrangement whose hash is less than the current target they send the new block to some other network nodes.</p>
<blockquote>
<p><strong>Do miners get a reward for adding a transaction to a block?</strong></p>
</blockquote>
<p>Yes, the difference between sum of inputs and sum of outputs is a transaction fee that the miner claims.</p>
<blockquote>
<p><strong>How is that block then added to the blockchain?</strong></p>
</blockquote>
<p>Each node validates the new blocks it receives and decides whether to add the new block to their own copy of the blockchain. If a node has tested a block and found it to be valid, it will also pass on the block to other nodes.</p>
<blockquote>
<p><strong>If everyone (miners) are solving the block and adding a transaction, then how is it decided whose block will be added to the blockchain?</strong></p>
</blockquote>
<p>Every miner's draft block is different because the first transaction in a block is one that pays rewards/fees to that miner's own address.</p>
<p>First block received by a node is added to the current branch. Then by comparing total &quot;work&quot; in competing branches a node may choose another branch as the current branch.</p>
<p>Work is essentially an assessment of the <em>average</em> number of rearrangements of a block that would <em>theoretically</em> have to be attempted to find one whose hash is less than the target applicable to the block-height of the block.  It doesn't measure how many attempts a miner actually made.</p>
<p>The target is reassessed every 2016 blocks and is given a value that will tend to make the average time between blocks be 10 minutes. Every node calculates these targets independently using common rules applied to blockchain data for those 2016 blocks.</p>
<blockquote>
<p><strong>Also, for example, if there are two miners A and B, and if miner A's block is added then what will happen to the transactions [<strike>verified</strike> <em>confirmed</em>] by miner B?</strong></p>
</blockquote>
<p>Nodes that follow A's branch will consider transactions that are only in B's block to be unconfirmed. A or another miner will probably include them in a later block that references A's block (i.e. one that builds on a chain that includes A's block but not B's).</p>

CC BY-SA 4.0
2022-07-08T18:37:32.017Z
2022-07-08T18:53:50.400Z
2022-07-08T18:53:50.400Z
RedGrittyBrick
<p>I have the below questions:</p>
<ol>
<li>How is a transaction added to a block?</li>
<li>Do miners get a reward for adding a transaction to a block?</li>
<li>How is that block then added to the blockchain?</li>
<li>If everyone (miners) are solving the block and adding a transaction, then how is it decided whose block will be added to the blockchain? Also, for example, if there are two miners A and B, and if miner A's block is added then what will happen to the transactions verified by miner B?</li>
</ol>

CC BY-SA 4.0
2022-07-08T18:00:15.747Z
Hi Tanzeel Malik, I've copy edited your post, retagged it and tried to rephrase your title into a sentence. Please feel free to edit the title further if you feel that I've not captured your intended question well.
Murch
<mining-reward><transaction-selection><chain-reorganization><best-chain>
How do transaction verification and adding a block to the blockchain fit together?
Tanzeel Malik
# How do transaction verification and adding a block to the blockchain fit together?

(Asked by: username_0 on 2022-07-08)

I have the below questions:

1. How is a transaction added to a block?
2. Do miners get a reward for adding a transaction to a block?
3. How is that block then added to the blockchain?
4. If everyone (miners) are solving the block and adding a transaction, then how is it decided whose block will be added to the blockchain? Also, for example, if there are two miners A and B, and if miner A's block is added then what will happen to the transactions verified by miner B?

**Tags:** mining-reward, transaction-selection, chain-reorganization, best-chain

> **Comments:**
> **username_1**: Hi username_0, I've copy edited your post, retagged it and tried to rephrase your title into a sentence. Please feel free to edit the title further if you feel that I've not captured your intended question well.  

---

## 2 answers

### Answer by username_1 on 2022-07-08. Score: 3

The nodes form a gossip network. Every time a node hears about a new (unconfirmed¹) transaction, they check whether the transaction would be eligible to be added to a block, and if they deem it valid, they forward it to their peers.

Each miner builds their own block template. The first transaction in the block template must be a [coinbase transaction](https://bitcoin.stackexchange.com/a/96442/5406). In it the miner pays out the block reward to themselves. The other transactions are [a selection](https://bitcoin.stackexchange.com/q/57823/5406) of unconfirmed transactions the miner has heard from the network about. The miner usually selects the unconfirmed transactions that offer the most transaction fees in sum, as the miner gets to keep the transaction fees.

Each miner tries hashing [many variants](https://bitcoin.stackexchange.com/a/96442/5406) of their block template with different values for the nonce and the extranonce. Eventually one miner [will find](https://bitcoin.stackexchange.com/a/22984/5406) a block header that passes the difficulty requirement—a valid block. The successful miner publishes their valid block to their peers, who forward in turn to their peers and so forth. Since the successful miner added the instruction to pay themselves in their block template's coinbase transaction, the block pays the reward to the successful miner.

Every other node validates that the block and all transactions in it follow the protocol rules. If anything is wrong, they drop the block. Otherwise, they update their [UTXO set](https://bitcoin.stackexchange.com/q/4301/5406) to mark all transaction outputs that were used as spent and add the new transaction outputs that were created.

Then everything restarts from the top: miners build a block template, hash many variations of it, someone finds a block, it gets relayed, nodes validate it, apply the changes to their UTXO set, the miner that found it gets paid.

Since this is a random uncoordinated process, [occasionally two miners will get lucky at the same time](https://bitcoin.stackexchange.com/a/97197/5406) and publish competing blocks at the same height. Each node will see one or the other first and the network will not have a shared ground-truth. The next block found then breaks the tie: whatever chain has more total work becomes canon, the other becomes extinct. The best chain can only have [one block at each height](https://bitcoin.stackexchange.com/q/12427/5406), so there is [no question](https://bitcoin.stackexchange.com/q/59257/5406) of "what happens to the transactions verified in the competing block". From the perspective of the best chain, that block might as well not exist, it's irrelevant.

And from top: tick tock, next block.

---

¹ Unconfirmed means "not added to a block yet".


---

### Answer by username_2 on 2022-07-08. Score: 1

> **How do transaction verification and adding a block to the blockchain fit together?**

***Verification*** of transactions is performed by every participant in the Bitcoin network. Participants include normal wallets and are usually referred to as nodes.

Only miners create new blocks. Thereby ***confirming*** the transactions in them.

Note the important distinction between verification and confirmation.

> **How is a transaction added to a block?**

A miner selects unconfirmed transactions and adds them to a block template. When they find an arrangement whose hash is less than the current target they send the new block to some other network nodes.

> **Do miners get a reward for adding a transaction to a block?**

Yes, the difference between sum of inputs and sum of outputs is a transaction fee that the miner claims.

> **How is that block then added to the blockchain?**

Each node validates the new blocks it receives and decides whether to add the new block to their own copy of the blockchain. If a node has tested a block and found it to be valid, it will also pass on the block to other nodes.

> **If everyone (miners) are solving the block and adding a transaction, then how is it decided whose block will be added to the blockchain?**

Every miner's draft block is different because the first transaction in a block is one that pays rewards/fees to that miner's own address.

First block received by a node is added to the current branch. Then by comparing total "work" in competing branches a node may choose another branch as the current branch.

Work is essentially an assessment of the *average* number of rearrangements of a block that would *theoretically* have to be attempted to find one whose hash is less than the target applicable to the block-height of the block. It doesn't measure how many attempts a miner actually made.

The target is reassessed every 2016 blocks and is given a value that will tend to make the average time between blocks be 10 minutes. Every node calculates these targets independently using common rules applied to blockchain data for those 2016 blocks.

> **Also, for example, if there are two miners A and B, and if miner A's block is added then what will happen to the transactions [verified *confirmed*] by miner B?**

Nodes that follow A's branch will consider transactions that are only in B's block to be unconfirmed. A or another miner will probably include them in a later block that references A's block (i.e. one that builds on a chain that includes A's block but not B's).


---

