DEV Community

Blockchain: what is in a block?

Damien Cosset on December 27, 2017

Introduction In my previous article, I tried to described the concept of a blockchain with code. This time, I'll try to describe the str...
Collapse
 
peter profile image
Peter Kim Frank

Damien, this is absolutely brilliant. I've re-read twice now and it's just beginning to click. Thanks for writing this up. I especially love the graphic to explain the merkle root concept.

Collapse
 
damcosset profile image
Damien Cosset

Thank you for the kind words. Glad it helped.

Collapse
 
rlahiri profile image
Rahul Lahiri

This really helped -- so clearly written!

I have a rookie question: how are the transactions included in a block determined? I expect that at any given time, all the miners are working with a similar but non-identical set of transactions to create the next block. Do they have to check the transactions included in every new block while they are computing the hashes, and discard all the work if a newly added block includes any transaction they were working with?

Collapse
 
damcosset profile image
Damien Cosset

Yes, whenever a new block is mined, miners have to create a candidate block. This candidate block includes all the transactions that have not been mined yet. If a miner was trying to mine a block and failed, she will check which transactions have been included in the winning block. Whatever transactions is leftover becomes part of this new candidate block.

If you want a deeper explaination on this, I wrote an article called Blockchain: what is mining? That explains this concept :)

Collapse
 
rlahiri profile image
Rahul Lahiri

Thank you! I will read up that article!

Collapse
 
chamarapw profile image
ChamaraPW

There are 16 leaves in this tree. We construct our tree from the bottom up by pairing each leaf. Now, anybody can prove that the leaf I ( in orange ) is part of this block by having the path given in green. We have only 4 hashes, but that is enough to know if the leaf I belongs here. That is because with those informations, we are able to construct every single leaf we need( in yellow ). We can create IJ, IJKL, IJKLMNOP and the root and check if those hashes correspond. This is why it is very complicated to cheat a blockchain. To change one thing means you must change everything.
How can we prove that I is belong to this block.is it mathematical way or any other ways?can you explain little ?

Collapse
 
damcosset profile image
Damien Cosset

In this example, by having the path J, KL, MNOP and ABCDEFGH, you can re-created the hash of each pair.

You have the I hash and J hash, so you can create a hash IJ. Because you also have the hash KL, you can create the hash IJKL.... If one hash doesn't match the original, you know the I hash is corrupt.

Collapse
 
arvindpdmn profile image
Arvind Padmanabhan

Do we have I hash, J hash and so on? I thought a block contains only the Merkle root? When you say I hash and J hash, do you mean the transaction IDs that are included in the block? If so, they we can compute IJ hash, etc. until we arrive at the Merkle root? Does the block include IJ hash for validation purpose? Or it is just the Merkle root that can be used for validation? Thx

Thread Thread
 
damcosset profile image
Damien Cosset

Yes, I J K ... are transactions hashes that are included in the block here.

Whenever a transaction ( in this case I ) claims to be a part of a block, we can control if the hashes we get are the same.

Thread Thread
 
arvindpdmn profile image
Arvind Padmanabhan

One more doubt. Are intermediate hashes (eg. IJ hash) included in the block or is it only the Merkle root that is included?

Thread Thread
 
damcosset profile image
Damien Cosset

Intermediate hashes are included. If I understood this part correctly, the client wants to verify a transaction is part of a block. The client gets a bloom filter that will give him the necessary hashes to verify whether or not this transaction is part of the block.

This saves a lot of resources, because you only need a few "leaves" in the tree, and not the entire merkle tree. With the path you get, you can control if you get the same hashes.

Collapse
 
chamarapw profile image
ChamaraPW

thank you very much

Collapse
 
lilredindy profile image
lilredindy

but let's say i'm a full-node and i modify an existing block/transaction. specifically, i modify a transaction's unspent output. then i make another new transaction on top of this where i use the previously modified output as an input. let's assume i am the owner of this unspent output that i will be using as input for the new transaction. yes, the hash info for the transaction and thus the block will no longer be correct. when a peer node goes to verify the transaction they notice the transaction has been signed by a valid private key and thus proving i am the owner of the modified unspent output. how long before I actually get caught? am i missing some part of the verification process?

Collapse
 
damcosset profile image
Damien Cosset

If I understand correctly what your scenario is:

  • A block has been mined
  • In this block, you modify one transaction to your benefit. In this transaction, you give yourself 10 BTC instead of 0.01 BTC for example.

For this new transaction to be part of the blockchain, you, or another miner, would have to find a new Proof-of-Work for this block. This is an entirely new block now.

You mine it and find a valid Proof-of-Work. You propagate your finding to the network and they have to validate this new block.

Here, the problem is much bigger for this block to be accepted.

1) The network sees you tried to cheat the system. The block is rejected and you wasted your resources mining this new block for nothing.

2) The network accepts this new block ( for whatever reasons ). Now, we have to mine every single block that was validated after the one you just proposed because they are all invalid now.

I believe in this case, you won't get caught, because your block will never be part of the blockchain. You can't modify a block that is already part of the blockchain. You will create a new block that will act as a replacement of an existing block. So, for this block to be accepted, the network would also have to provide valid proof-of-work for every single block after the one you want to change.

  • In the case where you are mining the block and decide to modify a transaction in the block you are mining in your favor:

When you find a valid Proof-of-Work to your block, it is propagated to other nodes. These nodes will verify that the Proof-of-Work is valid, but they will also verify every single transaction in the block. There is a very long list of parameters that nodes must control in order to call a transaction valid ( inputs, outputs...). In this scenario, this is where your fraud will be stopped. Your block won't be accepted by the network.

I hope I answered your question. Let me know if anything wasn't clear.

Collapse
 
lilredindy profile image
lilredindy

ok that makes enough sense. from your type-up i get that the consensus from other peer nodes will not be in my favor due to this proof-of-work algorithm that is performed on new blocks.

Collapse
 
bertilmuth profile image
Bertil Muth

Very good. I like your article series - I wish I would have read it when I got started, you make it really easy to understand (or at least: as easy as possible).

One small thing I noticed: you wrote "Mining is another crucial part of the blockchain technology, but it is outside the scope of this article".

I have heard many people speak about "the blockchain", implying there is only one, when in fact there are many. The most popular one is the Bitcoin blockchain, and for that chain, your statement is correct. It uses "Proof-of-work" a.k.a. mining for creating new blocks/coins.

There are other blockchains that use "Proof-of-stake", so instead of mining, new blocks are created based on stake, i.e. already owned coins, and random distribution.

Am I correct?

Collapse
 
damcosset profile image
Damien Cosset

Yes, you are absolutely right. Proof-of-stake is a mechanism that also requires a lot less computational power, so it could probably be used in future to help blockchains scale.

Collapse
 
mayukhg07 profile image
Mayukh Ghosh

Great and very simplistic article....Thanks

Collapse
 
ddnelaka profile image
ddnelaka

Reallu useful article but one question.
How is data stored in a block exactly. For example if we consider a medical health record system, do many health records are stored in one block or one medical record is only stored in one block. If there can be many records in one block, does one record (transaction) has a unique key or hash.
And if we need to update one record how can it happen?

Collapse
 
intheroom profile image
intheroom

Hi Damien,

Thank you for your very helpful article. However, I have one question: What would be the transaction we choose to validate? In the above article you choose the transaction I to validate if it exists in the block. So how about other transactions? Is there any specific rules to choose which transactions need to be validated?

Collapse
 
mosteo profile image
Álex RM

Very clear, thanks. Just a small nickpick, I believe that you refer to the pink I leaf when you say the orange one.

Collapse
 
shirintahmasebi profile image
شیرین طهماسبی

Well-structured article! It was very clear. Thanks

Collapse
 
lndr27 profile image
Leandro

Excellent article. You make it really simple to understand.

Collapse
 
txd481 profile image
Tanya Daskova

Again, where is the plain message? How can I access the plain String? Thank you!

Collapse
 
afendoulis profile image
George Afendoulis

Question please= A block can be updated with new transactions coming in or once has been created cannot accommodate more transactions? thanks, sorry might be a very basic Q

Collapse
 
zahra_ahadi74 profile image
zahra ahadi

Thanks for writing this up.Does the input data size of a miner is the same block size?