{"id":2697,"date":"2018-12-17T23:05:20","date_gmt":"2018-12-17T17:35:20","guid":{"rendered":"http:\/\/navveenbalani.dev\/?p=2697"},"modified":"2020-10-30T16:34:47","modified_gmt":"2020-10-30T11:04:47","slug":"building-blockchain-applications-using-ethereum","status":"publish","type":"post","link":"https:\/\/navveenbalani.dev\/index.php\/articles\/building-blockchain-applications-using-ethereum\/","title":{"rendered":"Building Blockchain applications using Ethereum"},"content":{"rendered":"\n<p>In this article, we will introduce a permissionless blockchain implementation called Ethereum. The chapter will cover the core features of Ethereum platform, how to setup a local instance of Ethereum, deploy smart contracts and build an end-to-end sample application. <\/p>\n\n\n\n<p>As per the Ethereum website &#8211; <\/p>\n\n\n\n<p>\u201cEthereum is a decentralized platform\nthat runs smart contracts, applications that run exactly as programmed without\nthe possibility of downtime, censorship, fraud or third party interference.\u201d<\/p>\n\n\n\n<p>Let\u2019s understand this definition in a\ndetail by understanding the terminology and core features of an Ethereum\nplatform. <\/p>\n\n\n\n<h3>Core Features of Ethereum\nplatform<\/h3>\n\n\n\n<p>In this section, we will talk about the core features of the Ethereum\nplatform.<\/p>\n\n\n\n<h4><a>Application<\/a><\/h4>\n\n\n\n<p>An application in an Ethereum platform is\nreferred to as a DApp (decentralized application). The DApp typically comprises\nof a frontend and a backend code. The frontend code is developed using\nprogramming languages like JavaScript. The frontend code communicates with the\nbackend code on the blockchain network. The backend code is referred to as a\nsmart contract and is typically developed using a higher-level language like\nSolidity.&nbsp; The smart contract runs on the\ndecentralized Ethereum network. The DApp maps to the application layer of our\nreference architecture.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><em>Note &#8211; The\nfrontend part of the DApp does not run on Ethereum network. So in an Ethereum context,\na DApp could be simply referred to as the smart contract running in the\nEthereum network.<\/em><em>\n\n\n\n<\/em><\/p>\n\n\n\n<h4>Smart Contract<\/h4>\n\n\n\n<p>We had described smart contract earlier\nin Chapter 1. Smart Contract defines and executes transactions in the form of a\nsoftware code. It comprises of operations that act on the state of the\nblockchain.<\/p>\n\n\n\n<p>This smart contract acts as a digital\ncontract, which can be used to move value and ownership of assets between\nparties in a given network or across networks. These contracts can be between a\nconsumer to consumer &#8211; like a wire transfer between individuals using their\ncell phones without any intermediary, a business to business &#8211; like a trade\nfinance contract where multiple parties are involved viz. exporter bank,\nimporter bank and shipment vendor, a business to consumer &#8211; like an insurance\nor a health contract which rewards people for their good health behaviors or\neven between system to system &#8211; which enables system to work in a secure and\nautonomous way, for instance, a contract that defines whether a new device can\nconnect seamlessly to your connected home.<\/p>\n\n\n\n<p>The smart contract in an Ethereum network\nruns inside an Ethereum Virtual Machine (EVM) and its available on every node\nin the Ethereum network. Every node has the same copy of the smart contract and\nruns the same code\/logic. We would go through the process of developing and\ndeploying a smart contract during the course of this chapter.<\/p>\n\n\n\n<p>The Smart contract maps to the smart\ncontract layer of our reference architecture.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><em>Note \u2013 Solidity\nis compiled into bytecode that is executed on EVM.<\/em> <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4>Ether and Gas<\/h4>\n\n\n\n<p>To run applications on the public Ethereum blockchain network, you\nneed ethers. Ether is a form of payment \u2013 a digital fuel that client applications\nneed to pay in order to deploy the smart contract and execute its\noperations.&nbsp; Each operation requires some\ncomputation; this computation work is referred to as a gas. Different\noperations require different gas units, which is auto calculated by the system.\nBased on the gas required, the client needs to pay the appropriate ethers.\nThink of ether being paid for a gas, as a software service fee for executing\nthe code on the network.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Note &#8211; Ether is a form of digital currency,\nsimilar to Bitcoin. But instead of using ether as a market currency, the design\nof Ethereum platform uses ether as a means to run the decentralized\napplications. <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4>Ethereum Wallet<\/h4>\n\n\n\n<p>The Ethereum Wallet allows you to hold\nand secure ethers and other crypto-assets created on Ethereum. It also allows\nyou to write, deploy and use smart contracts. We would soon see Ethereum Wallet\nin action as part of our sample application demonstration. The Ethereum Wallet\ncan be mapped to the application layer of our reference architecture, which\nprovides a capability to interact with our Ethereum network.<\/p>\n\n\n\n<p>One important point is to understand how\nconsensus is reached across the Ethereum network and data (i.e., blocks)\ncommitted to Ethereum. In the next section, we will talk about this in detail.<\/p>\n\n\n\n<h2><a>Consensus Algorithm in Ethereum<\/a><\/h2>\n\n\n\n<p>We briefly discussed in chapter 1,\ndifferent consensus algorithms like Proof-of-Work (PoW), Proof-of-Stake (PoS)\nand Practical Byzantine Fault Tolerance (PBFT). Ethereum currently uses the PoW\nalgorithm and the plan is to move to PoS in a later release. Let\u2019s understand\nthe PoW consensus algorithm in more detail and what problem it is trying to\nsolve in a decentralized public network.<\/p>\n\n\n\n<p>Ethereum blockchain uses permissionless\nconsensus.&nbsp; Permissionless here means\nthat any node can join and participate in the network and execute or validate\nthe transactions. Out of all the public nodes in the network, majority of the\nnodes, say 51% out of 100 must agree on the validity of the transaction to\narrive at a consensus. Once consensus is reached, the transaction is committed\nto Ethereum. Now, in the absence of a central governing authority, how can one\nassure that 51% of these nodes are genuine and no one has added or manipulated\nnodes to drive transaction in their favor. That\u2019s where the PoW algorithm is\napplied to drive consensus throughout the network.<\/p>\n\n\n\n<p>The following points explains the PoW\nalgorithm in simple terms<\/p>\n\n\n\n<ul><li>In a PoW based Ethereum network, each node needs to solve a\nmathematical puzzle to propose their transaction or to be more precise, an\nintent to create and commit the block. To solve this mathematical puzzle, it\nrequires lot of computation, power (energy) and time in terms of CPU resources.\nThe nodes solving this puzzle are referred to as miners.<\/li><\/ul>\n\n\n\n<ul><li>The mathematical puzzle is all about finding a message digest based on\ninput message + the digest of the previous block.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><em>Note \u2013 A\nmessage&nbsp;<strong>digest<\/strong>&nbsp;is a\ncryptographic&nbsp;<strong>hash<\/strong>&nbsp;function\ncontaining a set of digits created by a one-way&nbsp;<strong>hashing<\/strong>&nbsp;formula. Digest&nbsp;basically ensures data is not\naltered and the integrity of the message is maintained between the sender and\nreceiver.<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul><li>The mathematical puzzle should be less than the difficulty level of\nthe system. The difficulty level is an arithmetically derived number set by the\nsystem. For more details, refer to the example below. <\/li><\/ul>\n\n\n\n<ul><li>Once the puzzle is solved, all other nodes should agree to the\nsolution and apply the same formula to arrive at a consensus.<\/li><\/ul>\n\n\n\n<ul><li>The node that is the first to solve the mathematical puzzle, and\neventually verified and agreed by all other nodes is rewarded for its work. In\nthe Ethereum network, this would imply a node being rewarded with some ethers\nfor solving the puzzle. <\/li><\/ul>\n\n\n\n<p>Given below is an example of PoW algorithm.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"333\" height=\"143\" src=\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2019\/12\/image-46.png\" alt=\"\" class=\"wp-image-2698\" srcset=\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2019\/12\/image-46.png 333w, https:\/\/navveenbalani.dev\/wp-content\/uploads\/2019\/12\/image-46-300x129.png 300w\" sizes=\"(max-width: 333px) 100vw, 333px\" \/><\/figure>\n\n\n\n<p>Let\u2019s assume there is a chain of 2 blocks already committed on the\nnetwork. A new block 3 needs to be added to the existing chain. Let\u2019s\nunderstand the sequence of flow as to how this third block of transaction would\nbe committed:<\/p>\n\n\n\n<p>Every node in the network will take up the challenge of creating the\nthird block and committing the same to the blockchain. For this to achieve,\neach of the nodes will try to solve a mathematical equation. In the context of\nEthereum, it is a digest value that needs to be created and it has to be less\nthan some difficulty level preset by the system. The mathematical equation could\nbe something like the following:<\/p>\n\n\n\n<p>Problem to solve: Calculate digest (3) which should be &lt; (less\nthan) the difficulty level<\/p>\n\n\n\n<p><em>The equation and\nits context:<\/em><\/p>\n\n\n\n<p>Let\u2019s assume the difficulty level is preset as 000000000000000\u2026.. 781.\nNow the <em>digest (3)<\/em> is calculated\nusing hash algorithm (say <em>sha256<\/em>) and\nby creating the hash of the values: block 2, digest 2 and nonce<\/p>\n\n\n\n<p>Note: Nonce can be any arbitrary value that is incremented every time\nthe node makes an attempt to find the correct <em>digest(3)<\/em> value. In short, nonce is incremented to find a solution\nto the equation.<\/p>\n\n\n\n<p><em>Overview of the\nsolution:<\/em><\/p>\n\n\n\n<p>Now, each node would start solving this equation by incrementing the\nnonce value. Let\u2019s say B3 is the new block to be committed, B2 is the previous\nblock i.e. block 2 and D is the difficulty level. If equation is solved the\nresult is Yes, else it is No. The following snippet shows how a node tries to\nsolve the equation:<\/p>\n\n\n\n<ul><li>Iteration 1 &#8211; Sha256\n(Block 3, Block 2, 1) &lt; D Outcome \u2013 No<\/li><li>Iteration 2 &#8211; Sha256\n(Block 3, Block 2, 2) &lt; D Outcome \u2013 No<\/li><\/ul>\n\n\n\n<p><strong>&#8230;<\/strong><\/p>\n\n\n\n<ul><li>Iteration 2000008\n&#8211; Sha256 (Block 3, Block 2, 4000008) &lt; D Outcome \u2013 Yes<\/li><\/ul>\n\n\n\n<p>The miner node will keep trying until the outcome is Yes. It is like a\nrat race where every node attempts to be the first to get the outcome as \u2018Yes\u2019.\nAs you can see from the above snippet, at nonce value 4000008, the equation is\nsolved, i.e., the <em>digest(3)<\/em> value is\nless than the difficulty level. The node that solves this equation propagates\nthe nonce value of 4000008 to all the other nodes in the network for\nverification. All other nodes would simply take this nonce value and verify if\nit is indeed less than the difficulty level. The other nodes just need one-step\nto verify this equation with least computing power. <\/p>\n\n\n\n<p>Once verified, the block will get committed, i.e., chained to the\nprevious block and the miner is rewarded with ethers for the work done.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><em>Note \u2013 <\/em>As you can see in above example, the blockchain is a chain of hashes, each linked to previous one, i.e., Digest of 3 contains Digest 2, Digest of 2 contains Digest 1 and so on. Adding a new block in between by an intruder implies changing the entire hash sequence till the very beginning, which seems virtually impossible.  <\/p>\n\n\n\n<p>In the <a href=\"https:\/\/navveenbalani.dev\/index.php\/articles\/building-crowdsourcing-application-using-an-ethereum-platform\/\">next part of the article<\/a>, we will build a  crowdsourcing application using an Ethereum platform. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will introduce a permissionless blockchain implementation called Ethereum. The chapter will cover the core features of Ethereum platform, how to setup a local instance of Ethereum, deploy smart contracts and build an end-to-end sample application. As per the Ethereum website &#8211; \u201cEthereum is a decentralized platform that runs smart contracts, applications [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1740,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,174,156],"tags":[286],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.0.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Building Blockchain applications using Ethereum - Current and Future Technology Trends by Navveen Balani<\/title>\n<meta name=\"description\" content=\"Building Blockchain applications using Ethereum - Articles\" \/>\n<link rel=\"canonical\" href=\"https:\/\/navveenbalani.dev\/index.php\/articles\/building-blockchain-applications-using-ethereum\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Building Blockchain applications using Ethereum - Current and Future Technology Trends by Navveen Balani\" \/>\n<meta property=\"og:description\" content=\"Building Blockchain applications using Ethereum - Articles\" \/>\n<meta property=\"og:url\" content=\"https:\/\/navveenbalani.dev\/index.php\/articles\/building-blockchain-applications-using-ethereum\/\" \/>\n<meta property=\"og:site_name\" content=\"Current and Future Technology Trends by Navveen Balani\" \/>\n<meta property=\"article:published_time\" content=\"2018-12-17T17:35:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-10-30T11:04:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2015\/05\/cloud_docker.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"450\" \/>\n\t<meta property=\"og:image:height\" content=\"330\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"8 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/navveenbalani.dev\/#website\",\"url\":\"https:\/\/navveenbalani.dev\/\",\"name\":\"Current and Future Technology Trends by Navveen Balani\",\"description\":\"Current and Future Technology Trends by Navveen Balani\",\"publisher\":{\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/navveenbalani.dev\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/building-blockchain-applications-using-ethereum\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2015\/05\/cloud_docker.jpg\",\"width\":450,\"height\":330},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/building-blockchain-applications-using-ethereum\/#webpage\",\"url\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/building-blockchain-applications-using-ethereum\/\",\"name\":\"Building Blockchain applications using Ethereum - Current and Future Technology Trends by Navveen Balani\",\"isPartOf\":{\"@id\":\"https:\/\/navveenbalani.dev\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/building-blockchain-applications-using-ethereum\/#primaryimage\"},\"datePublished\":\"2018-12-17T17:35:20+00:00\",\"dateModified\":\"2020-10-30T11:04:47+00:00\",\"description\":\"Building Blockchain applications using Ethereum - Articles\",\"breadcrumb\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/building-blockchain-applications-using-ethereum\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/navveenbalani.dev\/index.php\/articles\/building-blockchain-applications-using-ethereum\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/building-blockchain-applications-using-ethereum\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/navveenbalani.dev\/\",\"url\":\"https:\/\/navveenbalani.dev\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/building-blockchain-applications-using-ethereum\/\",\"url\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/building-blockchain-applications-using-ethereum\/\",\"name\":\"Building Blockchain applications using Ethereum\"}}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/building-blockchain-applications-using-ethereum\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/building-blockchain-applications-using-ethereum\/#webpage\"},\"author\":{\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\"},\"headline\":\"Building Blockchain applications using Ethereum\",\"datePublished\":\"2018-12-17T17:35:20+00:00\",\"dateModified\":\"2020-10-30T11:04:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/building-blockchain-applications-using-ethereum\/#webpage\"},\"publisher\":{\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\"},\"image\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/building-blockchain-applications-using-ethereum\/#primaryimage\"},\"keywords\":\"blockchain-guide\",\"articleSection\":\"Articles,Blockchain,IOT\",\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\",\"name\":\"Navveen\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/navveenbalani.dev\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2019\/07\/navveen_balani.jpeg\",\"width\":200,\"height\":200,\"caption\":\"Navveen\"},\"logo\":{\"@id\":\"https:\/\/navveenbalani.dev\/#personlogo\"},\"sameAs\":[\"http:\/\/naveenbalani.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts\/2697"}],"collection":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/comments?post=2697"}],"version-history":[{"count":2,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts\/2697\/revisions"}],"predecessor-version":[{"id":2777,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts\/2697\/revisions\/2777"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/media\/1740"}],"wp:attachment":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/media?parent=2697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/categories?post=2697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/tags?post=2697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}