{"id":126943,"date":"2022-08-03T15:34:22","date_gmt":"2022-08-03T15:34:22","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=537705"},"modified":"2022-08-03T15:34:22","modified_gmt":"2022-08-03T15:34:22","slug":"blockchain-basics-of-smart-contracts-and-solidity","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/08\/03\/blockchain-basics-of-smart-contracts-and-solidity\/","title":{"rendered":"Blockchain Basics of Smart Contracts and Solidity"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;537705&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<p>This article will give you an overview of the blockchain basics, transactions, and blocks.<\/p>\n<p>In the <a href=\"https:\/\/blog.finxter.com\/creating-a-token-contract-in-solidity-easy-example\/\" data-type=\"post\" data-id=\"507889\" target=\"_blank\" rel=\"noreferrer noopener\">previous article<\/a> of this <a href=\"https:\/\/blog.finxter.com\/solidity-crash-course\/\" data-type=\"post\" data-id=\"445146\" target=\"_blank\" rel=\"noreferrer noopener\">series<\/a>, we looked at how to create your own token using Solidity and, specifically, events for transactions.<\/p>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/introduction-to-smart-contracts-and-solidity-part-3-blockchain-basics\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FIUYwzn_ys6U%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<h2>Blockchain B<strong>asics<\/strong><\/h2>\n<p>Blockchain technology enables us to write programs without having to think about the details of the underlying communication infrastructure. <\/p>\n<p>However, just to be aware of some of the keywords which are commonly used when studying the infrastructure, we\u2019ll name just a few among others (borrowed from the Solidity documentation): <\/p>\n<ul>\n<li><em>mining, <\/em><\/li>\n<li><em>elliptic-curve cryptography, <\/em><\/li>\n<li><em>peer-to-peer network<\/em>. <\/li>\n<\/ul>\n<p>Although it\u2019s interesting to see how these technologies work <em>\u201cunder the blockchain\u2019s hood\u201d<\/em>, the beneficial fact is that you don\u2019t have to be closely familiar with them. You can just implicitly utilize them and maybe even forget they\u2019re there. However, they represent some of the key elements which make up <a href=\"https:\/\/blog.finxter.com\/top-10-python-packages-for-crypto\/\" data-type=\"post\" data-id=\"527912\" target=\"_blank\" rel=\"noreferrer noopener\">Web3<\/a>.<\/p>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Note<\/strong>: Here, we need to establish a firm distinction between the terms <em>Web3<\/em> in the blockchain context that is the subject of our interest when discussing Solidity, and the <em><a href=\"https:\/\/en.wikipedia.org\/wiki\/Semantic_Web\" data-type=\"URL\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/Semantic_Web\" target=\"_blank\" rel=\"noreferrer noopener\">Semantic Web<\/a><\/em>, sometimes known as <em>Web 3.0<\/em>, which is an extension of the World Wide Web, intended to make Internet data machine-readable.<\/p>\n<h2><a><\/a>Transactions<\/h2>\n<p>One of the main roles of a blockchain is to preserve the data and make it temper-resistant. In that sense, we can easily consider a blockchain as a globally shared, transactional <a href=\"https:\/\/blog.finxter.com\/top-developer-jobs\/\" data-type=\"post\" data-id=\"475032\" target=\"_blank\" rel=\"noreferrer noopener\">database<\/a>.<\/p>\n<p>A blockchain network is <strong>globally shared<\/strong> because any party in the network can access and read its contents.<\/p>\n<p>It is <strong>transactional<\/strong> because any change to the blockchain has to be accepted by almost all, or at least the majority of other members, depending on the blockchain implementation. <\/p>\n<p>In database theory and practice having a transactional property means two things: the change to the database is either applied completely or not at all (see <a rel=\"noreferrer noopener\" href=\"https:\/\/www.geeksforgeeks.org\/acid-properties-in-dbms\/\" data-type=\"URL\" data-id=\"https:\/\/www.geeksforgeeks.org\/acid-properties-in-dbms\/\" target=\"_blank\">here<\/a>); no other transaction can modify the effect of a transaction being executed. <\/p>\n<p>We regularly ensure the equivalent behavior of our <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/smart-contracts-and-evm\/\" data-type=\"post\" data-id=\"92507\" target=\"_blank\">smart contracts<\/a> by using error handling and control structures available in Solidity (<a href=\"https:\/\/docs.soliditylang.org\/en\/v0.8.15\/control-structures.html?highlight=require#error-handling-assert-require-revert-and-exceptions\" data-type=\"URL\" data-id=\"https:\/\/docs.soliditylang.org\/en\/v0.8.15\/control-structures.html?highlight=require#error-handling-assert-require-revert-and-exceptions\" target=\"_blank\" rel=\"noreferrer noopener\">docs<\/a>). <\/p>\n<h2>Blockchain Security<\/h2>\n<p>There is also a strong security property that is inherent in the way how blockchain works: each new block header includes the hash of the previous block. <\/p>\n<p>To alter a block in a blockchain, an <a href=\"https:\/\/blog.finxter.com\/top-8-scary-smart-contract-hacks-that-exploit-your-dapp-video\/\" data-type=\"post\" data-id=\"437387\" target=\"_blank\" rel=\"noreferrer noopener\">attacker<\/a> would have to re-mine the targeted block and all the following blocks, therefore creating a chain fork. <\/p>\n<p>Also, an attacker would need to invest more total work than was invested in the original chain segment to get his chain fork accepted by the rest of the network (<a href=\"https:\/\/www.ledger.com\/academy\/blockchain\/what-is-proof-of-work\"><em>source<\/em><\/a>). <\/p>\n<p>The latter would require immense computing power and energy, making the entire effort unfeasible in theory. <\/p>\n<p>However, there have been successful attacks on blockchain networks, mostly due to the smaller scale of a particular network, where a fraudulent consensus (the verification process) was less difficult to fabricate, block creation errors, or insufficient security measures (<a href=\"https:\/\/www.epiqglobal.com\/en-us\/resource-center\/articles\/blockchain-can-be-hacked\" target=\"_blank\" rel=\"noreferrer noopener\">source<\/a>).<\/p>\n<h2>Example Application<\/h2>\n<p>An example to the story above is already given in part by the example we did in the <a href=\"https:\/\/blog.finxter.com\/creating-a-token-contract-in-solidity-easy-example\/\" data-type=\"post\" data-id=\"507889\" target=\"_blank\" rel=\"noreferrer noopener\">previous article<\/a>: a smart contract for (simulated) currency transfer between any two parties. <\/p>\n<p>There was a list of accounts holding balances in a cryptocurrency, <em>Wei<\/em>, and our smart contract supported transfers of a given amount of currency from the sender to the receiver. <\/p>\n<p>What\u2019s important in the context of such transactions is that the same amount of currency should always be \u201csimultaneously\u201d deducted from the sender\u2019s account and added to the receiver\u2019s account. <\/p>\n<p>What we mean by \u201csimultaneously\u201d is not a matter of happening at the same moment, but happening with the same, but the opposite consequence, i.e. if the amount gets successfully deducted from the sender\u2019s account, it has to be added to the receiver\u2019s account. <\/p>\n<p>If an error occurs after the amount is decreased from the sender\u2019s account, but before the amount is added to the receiver\u2019s account, the operation should revert to the smart contract\u2019s previous state, as it was before the transaction started. <\/p>\n<p class=\"has-base-background-color has-background\"><strong>This way, the blockchain behaves in a consistent, transactional manner and warrants that the transaction will be done entirely or not at all.<\/strong><\/p>\n<p>In the context of everything said so far, with our subcurrency example in mind, we may ask ourselves: <\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Question<\/strong>: How would we enable an account owner to transfer the currency only from the account in his ownership? <\/p>\n<p>The answer to the question is relatively simple: <\/p>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Answer<\/strong>: <em>A transaction always bears the sender\u2019s cryptographic signature, which serves as a seal in granting access to precisely defined modifications of (operations on) the database, such as currency transfer from the account owner originally holding the currency, to the account owner &#8211; receiver of the currency.<\/em><\/p>\n<h2><a><\/a>Blocks<\/h2>\n<p>There is no story about blockchain without actually mentioning the block itself. We will definitively return to talk about a block from some other angles, but with a security perspective in mind, we will focus on overcoming a significant obstacle known (in <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-value-bitcoin\/\" data-type=\"post\" data-id=\"31351\" target=\"_blank\">Bitcoin<\/a> terms) a \u201cdouble-spend attack\u201d (<a href=\"https:\/\/www.gemini.com\/cryptopedia\/double-spend-attacks-bitcoin\" data-type=\"URL\" data-id=\"https:\/\/www.gemini.com\/cryptopedia\/double-spend-attacks-bitcoin\">source<\/a>). <\/p>\n<p>Some paragraphs ago, we mentioned a blockchain attack implying the majority of the network members\u2019 acceptance, popularly known as the \u201c51% attack\u201d. <\/p>\n<p>Let\u2019s assume there are two transactions in the blockchain network, and each of them attempts to transfer all the account\u2019s currency to another account, i.e. they will both attempt to empty the account. <\/p>\n<p>Since there can be only one accepted, confirmed transaction (in contrast to two or more possible unconfirmed transactions), the transaction that gets confirmed first will end up bundled in a block. It is not under our control or, for that matter, not even a subject of our concern which block will be the first one.<\/p>\n<p>What matters is that the second block will be rejected and the contention between the blocks\/transactions will be resolved automatically, and the likelihood of the double-spend attack problem will decrease with each additional confirmation (<a href=\"https:\/\/www.chainbits.com\/cryptocurrency-terms\/blockchain-confirmation-definition\/\"><em>source<\/em><\/a>).<\/p>\n<p>Blocks are sequentially added to a blockchain, ordered by the time of their arrival. Adding a block to the blockchain is mostly done in regular intervals, which last about 17 seconds for the Ethereum network. <\/p>\n<p>Nonetheless, the blockchain tip can sometimes be reverted during the order selection mechanism. In that case, a confirmed block will get discarded and become a <em>stale block<\/em>, and the stale block\u2019s successor blocks just get returned to the memory pool. <\/p>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Note<\/strong>: discarded or stale blocks are popularly and <strong>wrongly<\/strong> called <em>orphan blocks<\/em>. (<a href=\"https:\/\/www.investopedia.com\/terms\/o\/orphan-block-cryptocurrency.asp\"><em>source<\/em><\/a>)<\/p>\n<p>For block reversal to happen, two conditions should occur. <\/p>\n<p>(1) <strong>First<\/strong>, multiple blocks should get created from the same parent block P simultaneously (by different miners) and get confirmed by the network, forming a fork with multiple subchains\/branches at the tip (block) P of the blockchain. <\/p>\n<p>As there are multiple successor block candidates (e.g. we will assume three blocks, A, B, and C), each block has an equal chance of becoming a permanent part of the blockchain. <\/p>\n<p>Because blocks propagate through the blockchain network differently, miners will receive one of the blocks (A, B, or C) before the other blocks and start mining a new block on the block they received first. <\/p>\n<p>(2) <strong>Second<\/strong>, one of the miners will mine and broadcast a new block, e.g.\u00a0 C<sub>1<\/sub> to the network before the other miners mine blocks A<sub>1<\/sub> or B<sub>1<\/sub> (these don\u2019t exist as yet). <\/p>\n<p>Since the fastest miner first received block C and then produced C<sub>1<\/sub>, it will extend the branch P-C, effectively making the branch P-C-C<sub>1<\/sub> the longest one. Once the network detects there is a chain longer than the chains P-A and P-B, it will disqualify the candidate blocks A and B as stale blocks, thus resolving the contention. <\/p>\n<p>All the miners who chose a different blockchain from the fork, e.g. P-A or P-B, and started building their blockchains on them, will experience a block reversal, as their chains will also get updated by the network with the blockchain P-C-C<sub>1<\/sub> as the longest one.<\/p>\n<p>The blockchain tip reversal gets less likely to happen as more blocks are confirmed and added to the blockchain. A common number of confirmations after we can be virtually certain that our block became a permanent part of a blockchain is six confirmations (<a href=\"https:\/\/www.buybitcoinworldwide.com\/confirmations\/\"><em>source<\/em><\/a>).<\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, we first shortly made a short detour (sorry about that) to a missing part about <strong>Solidity events<\/strong>, and then boldly stepped into the direction of blockchain basics, transactions, and blocks.<\/p>\n<p>First, we made friends with an event listener example based on <code>web3.js<\/code> library.<\/p>\n<p>Second, we glanced at the blockchain basics. You already know the works: some basic terms, dry notes, etc. It\u2019s just a scratch, really.<\/p>\n<p>Third, we learned about how blockchain treats transactions and how it makes them feel secure,\u00a0 shared and cared for. That\u2019s why we mentioned some properties that make blockchain look like a database.<\/p>\n<p>Fourth, we went into some light details on what a block is, what is its role in the blockchain ecosystem and what are some of the risks present in a blockchain network.<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2><a href=\"https:\/\/academy.finxter.com\/university\/solidity-basics\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/academy.finxter.com\/university\/solidity-basics\/\">Learn Solidity Course<\/a><\/h2>\n<p>Solidity is the programming language of the future.<\/p>\n<p>It gives you the rare and sought-after superpower to program against the &#8220;Internet Computer&#8221;, i.e., against decentralized Blockchains such as <em>Ethereum<\/em>, <em>Binance Smart Chain<\/em>, <em>Ethereum Classic<\/em>, <em>Tron<\/em>, and <em>Avalanche <\/em> &#8211; to mention just a few Blockchain infrastructures that support Solidity.<\/p>\n<p>In particular, Solidity allows you to<em> create smart contracts<\/em>, i.e., pieces of code that automatically execute on specific conditions in a completely decentralized environment. For example, smart contracts empower you to create your own <em>decentralized autonomous organizations<\/em> (DAOs) that run on Blockchains without being subject to centralized control.<\/p>\n<p>NFTs, DeFi, DAOs, and Blockchain-based games are all based on smart contracts.<\/p>\n<p><a href=\"https:\/\/academy.finxter.com\/university\/solidity-basics\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/academy.finxter.com\/university\/solidity-basics\/\">This course<\/a> is a simple, low-friction introduction to creating your first smart contract using the Remix IDE on the Ethereum testnet &#8211; without fluff, significant upfront costs to purchase ETH, or unnecessary complexity.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/academy.finxter.com\/university\/solidity-basics\/\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2021\/10\/image-121.png\" alt=\"\" class=\"wp-image-37299\" width=\"491\" height=\"287\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2021\/10\/image-121.png 982w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2021\/10\/image-121-300x175.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2021\/10\/image-121-768x448.png 768w\" sizes=\"auto, (max-width: 491px) 100vw, 491px\" \/><\/a><\/figure>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) This article will give you an overview of the blockchain basics, transactions, and blocks. In the previous article of this series, we looked at how to create your own token using Solidity and, specifically, events for transactions. Blockchain Basics Blockchain technology enables us to write programs without having to think about [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-126943","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/126943","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=126943"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/126943\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=126943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=126943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=126943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}