[Tut] Blockchain Basics of Smart Contracts and Solidity - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] Blockchain Basics of Smart Contracts and Solidity (/thread-99779.html) |
[Tut] Blockchain Basics of Smart Contracts and Solidity - xSicKxBot - 08-07-2022 Blockchain Basics of Smart Contracts and Solidity <div> <div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{"align":"left","id":"537705","slug":"default","valign":"top","reference":"auto","class":"","count":"1","readonly":"","score":"5","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})","font_factor":"1.25"}"> <div class="kksr-stars"> <div class="kksr-stars-inactive"> <div class="kksr-star" data-star="1" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="2" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="3" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="4" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="5" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> <div class="kksr-stars-active" style="width: 142.5px;"> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> </div> <div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div> </div> <p>This article will give you an overview of the blockchain basics, transactions, and blocks.</p> <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> <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 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> <h2>Blockchain B<strong>asics</strong></h2> <p>Blockchain technology enables us to write programs without having to think about the details of the underlying communication infrastructure. </p> <p>However, just to be aware of some of the keywords which are commonly used when studying the infrastructure, we’ll name just a few among others (borrowed from the Solidity documentation): </p> <ul> <li><em>mining, </em></li> <li><em>elliptic-curve cryptography, </em></li> <li><em>peer-to-peer network</em>. </li> </ul> <p>Although it’s interesting to see how these technologies work <em>“under the blockchain’s hood”</em>, the beneficial fact is that you don’t have to be closely familiar with them. You can just implicitly utilize them and maybe even forget they’re 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> <p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" 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> <h2><a></a>Transactions</h2> <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> <p>A blockchain network is <strong>globally shared</strong> because any party in the network can access and read its contents.</p> <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> <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> <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> <h2>Blockchain Security</h2> <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> <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> <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> <p>The latter would require immense computing power and energy, making the entire effort unfeasible in theory. </p> <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> <h2>Example Application</h2> <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> <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> <p>What’s important in the context of such transactions is that the same amount of currency should always be “simultaneously” deducted from the sender’s account and added to the receiver’s account. </p> <p>What we mean by “simultaneously” 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’s account, it has to be added to the receiver’s account. </p> <p>If an error occurs after the amount is decreased from the sender’s account, but before the amount is added to the receiver’s account, the operation should revert to the smart contract’s previous state, as it was before the transaction started. </p> <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> <p>In the context of everything said so far, with our subcurrency example in mind, we may ask ourselves: </p> <p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" 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> <p>The answer to the question is relatively simple: </p> <p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Answer</strong>: <em>A transaction always bears the sender’s 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 – receiver of the currency.</em></p> <h2><a></a>Blocks</h2> <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 “double-spend attack” (<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> <p>Some paragraphs ago, we mentioned a blockchain attack implying the majority of the network members’ acceptance, popularly known as the “51% attack”. </p> <p>Let’s assume there are two transactions in the blockchain network, and each of them attempts to transfer all the account’s currency to another account, i.e. they will both attempt to empty the account. </p> <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> <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> <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> <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’s successor blocks just get returned to the memory pool. </p> <p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" 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> <p>For block reversal to happen, two conditions should occur. </p> <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> <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> <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> <p>(2) <strong>Second</strong>, one of the miners will mine and broadcast a new block, e.g. C<sub>1</sub> to the network before the other miners mine blocks A<sub>1</sub> or B<sub>1</sub> (these don’t exist as yet). </p> <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> <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> <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> <h2>Conclusion</h2> <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> <p>First, we made friends with an event listener example based on <code>web3.js</code> library.</p> <p>Second, we glanced at the blockchain basics. You already know the works: some basic terms, dry notes, etc. It’s just a scratch, really.</p> <p>Third, we learned about how blockchain treats transactions and how it makes them feel secure, shared and cared for. That’s why we mentioned some properties that make blockchain look like a database.</p> <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> <hr class="wp-block-separator has-alpha-channel-opacity"/> <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> <p>Solidity is the programming language of the future.</p> <p>It gives you the rare and sought-after superpower to program against the “Internet Computer”, 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> – to mention just a few Blockchain infrastructures that support Solidity.</p> <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> <p>NFTs, DeFi, DAOs, and Blockchain-based games are all based on smart contracts.</p> <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 – without fluff, significant upfront costs to purchase ETH, or unnecessary complexity.</p> <div class="wp-block-image"> <figure class="aligncenter size-full is-resized"><a href="https://academy.finxter.com/university/solidity-basics/" target="_blank" rel="noopener"><img loading="lazy" 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="(max-width: 491px) 100vw, 491px" /></a></figure> </div> </div> https://www.sickgaming.net/blog/2022/08/03/blockchain-basics-of-smart-contracts-and-solidity/ |