1

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?
1
  • 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
    Commented Jul 8, 2022 at 18:00

2 Answers 2

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. In it the miner pays out the block reward to themselves. The other transactions are a selection 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 of their block template with different values for the nonce and the extranonce. Eventually one miner will find 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 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 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, so there is no question 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".

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.