{"id":2825,"date":"2022-12-02T06:06:14","date_gmt":"2022-12-02T06:06:14","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=2825"},"modified":"2023-01-06T08:29:18","modified_gmt":"2023-01-06T08:29:18","slug":"merkle-tree-blockchain","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/merkle-tree-blockchain\/","title":{"rendered":"Merkle tree in blockchain, explained"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_56_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/metaschool.so\/articles\/merkle-tree-blockchain\/#What_is_a_Merkle_tree\" title=\"What is a Merkle tree?\">What is a Merkle tree?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/metaschool.so\/articles\/merkle-tree-blockchain\/#The_importance_of_Merkle_tree_in_blockchain\" title=\"The importance of Merkle tree in blockchain\">The importance of Merkle tree in blockchain<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/metaschool.so\/articles\/merkle-tree-blockchain\/#Merkle_tree_chart_explanation\" title=\"Merkle tree chart explanation\">Merkle tree chart explanation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/metaschool.so\/articles\/merkle-tree-blockchain\/#The_protocol_of_a_Merkle_tree\" title=\"The protocol of a Merkle tree\">The protocol of a Merkle tree<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/metaschool.so\/articles\/merkle-tree-blockchain\/#Merkle_tree_use_cases\" title=\"Merkle tree use cases\">Merkle tree use cases<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/metaschool.so\/articles\/merkle-tree-blockchain\/#Git\" title=\"Git\">Git<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/metaschool.so\/articles\/merkle-tree-blockchain\/#Bitcoin\" title=\"Bitcoin\">Bitcoin<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-merkle-tree\"><span class=\"ez-toc-section\" id=\"What_is_a_Merkle_tree\"><\/span>What is a Merkle tree?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>A Merkle tree, also known as<em> \u201cbinary hash tree\u201d <\/em>is a hash-based data structures popularly used in computer science applications.<\/p>\n\n\n\n<p>It\u2019s a tree-like structure where each leaf node is a hash of a block data, and each non-leaf node is a hash of its children. Typically, in Merkle trees, each node has 2 children extending in branches.<\/p>\n\n\n\n<p>Merkle trees enable efficient data verification in distributed systems. They are efficient as they use hashes instead of entire files. Hashes are basically a method of holding encoded files that are much bigger in size in a small, encrypted code called hash code. Currently, Merkle trees are widely used in peer-to-peer networks such as blockchain technology like <a href=\"https:\/\/metaschool.so\/blockchains\/bitcoin\">Bitcoin<\/a>, Git, Tor, etc.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-importance-of-merkle-tree-in-blockchain\"><span class=\"ez-toc-section\" id=\"The_importance_of_Merkle_tree_in_blockchain\"><\/span>The importance of Merkle tree in blockchain<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Merkle trees are used to efficiently verify data. For example, if Bitcoin didn\u2019t have Merkle trees, each node would have to keep a complete copy of all the data and each transaction that ever occurred on Bitcoin. <\/p>\n\n\n\n<p>Can you imagine how tedious that would have been?<\/p>\n\n\n\n<p>Any verification request would have required an extremely large pack of data to be sent over to the network in order to verify it. Since the data is not hash coded, each computer would have to use a lot of computing power to process the data and verify it.&nbsp;<\/p>\n\n\n\n<p>Merkle trees solve this issue. They hold large data records into small hash codes, offering a small amount of data across networks to prove its validity as that&#8217;s all that is needed.<\/p>\n\n\n\n<figure class=\"wp-block-embed aligncenter is-type-rich is-provider-twitter wp-block-embed-twitter\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"twitter-tweet\" data-width=\"550\" data-dnt=\"true\"><p lang=\"en\" dir=\"ltr\">All crypto exchanges should do merkle-tree proof-of-reserves. <br><br>Banks run on fractional reserves. <br>Crypto exchanges should not.<a href=\"https:\/\/twitter.com\/binance?ref_src=twsrc%5Etfw\" target=\"_blank\" rel=\"noopener\">@Binance<\/a> will start to do proof-of-reserves soon. Full transparency.<\/p>&mdash; CZ \ud83d\udd36 Binance (@cz_binance) <a href=\"https:\/\/twitter.com\/cz_binance\/status\/1590055819416330240?ref_src=twsrc%5Etfw\" target=\"_blank\" rel=\"noopener\">November 8, 2022<\/a><\/blockquote><script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script>\n<\/div><\/figure>\n\n\n\n<p>Post the <a href=\"https:\/\/metaschool.so\/articles\/ftx-collapse-timeline\/\" target=\"_blank\" rel=\"noreferrer noopener\">FTX Crash<\/a> (a centralized crypto exchange), having a Merkle tree verification of proof-of-reserves has become a need of the hour. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"merkle-tree-chart-explanation\"><span class=\"ez-toc-section\" id=\"Merkle_tree_chart_explanation\"><\/span>Merkle tree chart explanation<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In various distributed and peer-to-peer systems, data verification is very important. As the same data exists everywhere &#8211; if a piece of data is modified in one location, it must be changed everywhere. This is why it\u2019s important to ensure the same data is everywhere.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"651\" height=\"537\" src=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2022\/11\/merkletreediagram.jpg\" alt=\"Merkle Tree Diagram\n\" class=\"wp-image-2845\" srcset=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2022\/11\/merkletreediagram.jpg 651w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2022\/11\/merkletreediagram-300x247.jpg 300w\" sizes=\"auto, (max-width: 651px) 100vw, 651px\" \/><figcaption>Please source metaschool.so if you intend to use images<\/figcaption><\/figure><\/div>\n\n\n\n<p>In a Merkle tree, the top-most node is called the \u201croot\u201d node like that of a tree. Each node has 2 children branches called \u201cleaf\u201d as shown in the image above. These nodes carry all the data of the blockchain in a secure hash function.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"472\" height=\"41\" src=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2022\/11\/hash-code.png\" alt=\"Example of a Hash code\" class=\"wp-image-2833\" srcset=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2022\/11\/hash-code.png 472w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2022\/11\/hash-code-300x26.png 300w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2022\/11\/hash-code-470x41.png 470w\" sizes=\"auto, (max-width: 472px) 100vw, 472px\" \/><figcaption>Example of a hash code<\/figcaption><\/figure><\/div>\n\n\n\n<p>Since the intention is to limit the amount of big data being sent over the network. Instead of sending an entire file we just sent the hash of the file to check if it matches. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-protocol-of-a-merkle-tree\"><span class=\"ez-toc-section\" id=\"The_protocol_of_a_Merkle_tree\"><\/span>The protocol of a Merkle tree<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>Computer A sends the hash of the file to computer B.<br><\/li><li>Computer B checks and verifies that hash against the root of the Merkle tree.<br><\/li><li>If it finds no difference, the job is done! Otherwise go to step 4<br><\/li><li>If there is a difference in a hash, the computer will request the hash of the root of the two children leaves.<br><\/li><li>Computer A will find the necessary hash and send it back to computer B<br><\/li><li>The computer repeats step 4 &amp; 5 till it has found the data block(s) that are inconsistent. It&#8217;s possible to discover more than 1 incorrect data block.<\/li><\/ol>\n\n\n\n<p>Merkle trees are useful in a peer-to-peer network system to verify information, as some information comes from untrusted sources (which is a concern in peer-to-peer systems).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"merkle-tree-use-cases\"><span class=\"ez-toc-section\" id=\"Merkle_tree_use_cases\"><\/span>Merkle tree use cases<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>As talked about earlier, a Merkle tree is specifically useful in distributed systems where the same data should exist in multiple locations.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"git\"><span class=\"ez-toc-section\" id=\"Git\"><\/span>Git<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Git is a version control system popularly used by programmers. All the saved files are saved on the computer of every user. Hence, it&#8217;s imperative to check that these changes are consistent and applied across everyone\u2019s computers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"bitcoin\"><span class=\"ez-toc-section\" id=\"Bitcoin\"><\/span>Bitcoin<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><a href=\"https:\/\/metaschool.so\/blockchains\/bitcoin\">Bitcoin <\/a>is a blockchain-based anonymous currency. All transactions that happen on the blockchain in bitcoin are stored in blocks. This blockchain exists on every bitcoin user\u2019s computer.\u00a0<\/p>\n\n\n\n<p>The leaves of the bitcoin Merke tree are typically hashes of single blocks. Every time someone wants to alter the blockchain for example: add a transaction in the chain, this change needs to be reflected everywhere.&nbsp;<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p>This is particularly difficult in Bitcoin\u2019s blockchain. Let&#8217;s say an intruder wants to modify the chain for their personal benefit, it is not possible in case of bitcoin due to the 51% attack resistance of a blockchain as proposed by Satoshi Nakamoto in the <a href=\"https:\/\/bitcoin.org\/bitcoin.pdf\" target=\"_blank\" rel=\"noreferrer noopener\">Bitcoin Whitepaper.<\/a><\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"nv-iframe-embed\"><iframe loading=\"lazy\" title=\"What is a 51% Attack\uff5cExplained For Beginners\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/BuTj9raHQOU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe><\/div>\n<\/div><\/figure>\n<\/div>\n<\/div>\n<\/div><\/div>\n\n\n\n<p id=\"finally\"><strong>finally,<\/strong><\/p>\n\n\n\n<p>Merkle trees are significant for <a href=\"https:\/\/metaschool.so\/blockchains\" target=\"_blank\" rel=\"noreferrer noopener\">blockchain technology<\/a> and a concept used by many developers in the web3 ecosystem. <\/p>\n\n\n\n<p>I hope you benefited from this explanation, in case of any doubts feel free to drop your questions in the comment section below. <\/p>\n\n\n\n<p>Bharvi out \ud83e\udd19\ud83c\udffc<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":7,"featured_media":3864,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[17,3],"tags":[51,258,257],"class_list":["post-2825","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blockchain","category-blog","tag-blockchain","tag-cryptography","tag-merkle-tree"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/2825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=2825"}],"version-history":[{"count":18,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/2825\/revisions"}],"predecessor-version":[{"id":4489,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/2825\/revisions\/4489"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/3864"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=2825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=2825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=2825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}