<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: SK Sabiruddin</title>
    <description>The latest articles on DEV Community by SK Sabiruddin (@sksabir).</description>
    <link>https://dev.to/sksabir</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1142434%2Fc04c7fdc-a121-4c62-b24a-376af695e12d.jpeg</url>
      <title>DEV Community: SK Sabiruddin</title>
      <link>https://dev.to/sksabir</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sksabir"/>
    <language>en</language>
    <item>
      <title>Solidity Gas Optimization Techniques</title>
      <dc:creator>SK Sabiruddin</dc:creator>
      <pubDate>Wed, 07 Feb 2024 05:09:29 +0000</pubDate>
      <link>https://dev.to/sksabir/ethereum-gas-optimization-techniques-5297</link>
      <guid>https://dev.to/sksabir/ethereum-gas-optimization-techniques-5297</guid>
      <description>&lt;h2&gt;
  
  
  Gas Units:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Making transactions on Ethereum or any other Ethereum backed blockchain will cost you a gas fee. The total gas fee consists of two parts: &lt;code&gt;Gas Units x Gas Price&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Gas units are a number that depends on the amount of computation required for a transaction. E.g. If you send some Ether (Ethereum’s native cryptocurrency) to someone, it requires &lt;code&gt;21,000 gas units&lt;/code&gt;. It’s the &lt;code&gt;minimum number of units required for any transaction&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The number of required gas units increases as the complexity of the transaction increases. E.g. Sending an &lt;code&gt;ERC-20 token&lt;/code&gt; might cost around &lt;code&gt;60,000 gas units&lt;/code&gt;. &lt;code&gt;Minting an NFT might cost around 120,000 gas units&lt;/code&gt;, and so on.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It all depends on how many computations need to be made while processing the transaction.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Gas Price:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Gas price is determined by the demand for making transactions. The more the traffic, the higher the price.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It’s usually expressed in &lt;code&gt;Gwei&lt;/code&gt; which is a billionth part of an Ether. If the gas price is 100 Gwei, it means 1 gas unit currently costs 100 Gwei as per the demand. The gas price will vary throughout the day depending on the total traffic.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Let’s combine these two now. If you need to send some Ether to someone at a time when gas price for 1 gas unit is 100 Gwei, you’ll have to pay a total of: &lt;code&gt;21,000 gas units x 100 Gwei = 2,100,000 Gwei&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since Gwei is &lt;code&gt;1/1bn of Ether, you’ll pay 0.0021 Ether&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To find the amount in dollars, multiply it with the current price of ether i.e. 1200 USD. Hence &lt;code&gt;0.0021 Ether x 1200 USD = 2.52 USD&lt;/code&gt; total gas fee. To summarise, the gas fee depends on: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Complexity of transaction (gas unit)&lt;/li&gt;
&lt;li&gt;Demand for making transactions (gas price)&lt;/li&gt;
&lt;li&gt;Price of Ether (to calculate dollar value)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  Where does my gas fee go?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;So, we know that we need to pay a gas fee when making a transaction. But where does it go now? Is it burnt? Or is it given to the validator?&lt;/li&gt;
&lt;li&gt;Actually, it’s both, after the &lt;code&gt;EIP-1559&lt;/code&gt; (an upgrade to Ethereum that happened in September 2022).&lt;/li&gt;
&lt;li&gt;Total gas fee is made up of:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Base Fee&lt;/em&gt;&lt;/strong&gt; (fixed for every transaction of a block) – which gets burnt entirely.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Max Priority Fee&lt;/em&gt;&lt;/strong&gt; (decided by the user) – which goes to the validator partially or entirely depending on the Max Total Fee which is again decided by the user.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Let’s run through an example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Suppose a user wants to pay a Max Total Fee of &lt;code&gt;100 Gwei&lt;/code&gt; per &lt;code&gt;gas unit consumed&lt;/code&gt; for the transaction. It’s inclusive of the Base Fee (burnt) and Max Priority Fee (for validator).&lt;/li&gt;
&lt;li&gt;While making the transaction, the user doesn’t know what the exact Base Fee will be for the transaction/block.&lt;/li&gt;
&lt;li&gt;During peak time, a lot of people will be trying to make a transaction, thus congesting the network. This will cause your transaction to take a &lt;code&gt;long time before going through&lt;/code&gt;. In order to prioritise his/her transaction, a user may want to &lt;code&gt;pay some (extra) fee to the block validator&lt;/code&gt;. This fee is called the &lt;code&gt;Max Priority Fee&lt;/code&gt;. Suppose the user wants it to be 30 Gwei per gas unit.&lt;/li&gt;
&lt;li&gt;Hence, the user enters the values of &lt;code&gt;100 Gwei&lt;/code&gt; and &lt;code&gt;30 Gwei&lt;/code&gt; respectively.&lt;/li&gt;
&lt;li&gt;When the block is confirmed, there are 2 possibilities depending on what is the final value of Base Fee:
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;Case&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Base&lt;/span&gt; &lt;span class="nc"&gt;Fee&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Max&lt;/span&gt; &lt;span class="nc"&gt;Priority&lt;/span&gt; &lt;span class="nc"&gt;Fee&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nc"&gt;Max&lt;/span&gt; &lt;span class="nc"&gt;Total&lt;/span&gt; &lt;span class="nc"&gt;Fee&lt;/span&gt;
    &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="nc"&gt;Base&lt;/span&gt; &lt;span class="nc"&gt;Fee&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Max&lt;/span&gt; &lt;span class="nc"&gt;Priority&lt;/span&gt; &lt;span class="nc"&gt;Fee&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Max&lt;/span&gt; &lt;span class="nc"&gt;Total&lt;/span&gt; &lt;span class="nc"&gt;Fee&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
    &lt;span class="nc"&gt;Hence&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="nc"&gt;Gwei&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;burnt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;per&lt;/span&gt; &lt;span class="n"&gt;gas&lt;/span&gt; &lt;span class="n"&gt;unit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="nc"&gt;Gwei&lt;/span&gt; &lt;span class="n"&gt;goes&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;validator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;per&lt;/span&gt; &lt;span class="n"&gt;gas&lt;/span&gt; &lt;span class="n"&gt;unit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nc"&gt;The&lt;/span&gt; &lt;span class="n"&gt;remaining&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="nc"&gt;Gwei&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;refunded&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="nf"&gt;user&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;per&lt;/span&gt; &lt;span class="n"&gt;gas&lt;/span&gt; &lt;span class="n"&gt;unit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;Case&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Base&lt;/span&gt; &lt;span class="nc"&gt;Fee&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Max&lt;/span&gt; &lt;span class="nc"&gt;Priority&lt;/span&gt; &lt;span class="nc"&gt;Fee&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Max&lt;/span&gt; &lt;span class="nc"&gt;Total&lt;/span&gt; &lt;span class="nc"&gt;Fee&lt;/span&gt;
    &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="nc"&gt;Base&lt;/span&gt; &lt;span class="nc"&gt;Fee&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Max&lt;/span&gt; &lt;span class="nc"&gt;Priority&lt;/span&gt; &lt;span class="nc"&gt;Fee&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Max&lt;/span&gt; &lt;span class="nc"&gt;Total&lt;/span&gt; &lt;span class="nc"&gt;Fee&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
    &lt;span class="nc"&gt;Hence&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt; &lt;span class="nc"&gt;Gwei&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;burnt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;per&lt;/span&gt; &lt;span class="n"&gt;gas&lt;/span&gt; &lt;span class="n"&gt;unit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nc"&gt;Only&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="nc"&gt;Gwei&lt;/span&gt; &lt;span class="n"&gt;goes&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="nf"&gt;validator&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;per&lt;/span&gt; &lt;span class="n"&gt;gas&lt;/span&gt; &lt;span class="n"&gt;unit&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;since&lt;/span&gt; &lt;span class="nc"&gt;Max&lt;/span&gt; &lt;span class="nc"&gt;Total&lt;/span&gt; &lt;span class="nc"&gt;Fee&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="nc"&gt;Gwei&lt;/span&gt;
        &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="n"&gt;refund&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="n"&gt;since&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt; &lt;span class="n"&gt;limit&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;consumed&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Note: &lt;code&gt;Base Fee&lt;/code&gt; can’t be &amp;gt;= &lt;code&gt;Max Total Fee&lt;/code&gt;. Your Metamask wallet will let you know if the Max Total Fee entered by you is not sufficient as per the network conditions.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  How to save Gas Cost:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2yxgtut0ao34kw5yy5zo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2yxgtut0ao34kw5yy5zo.png" alt="Image description" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Important: This document contains useful information to optimize your Gas Cost in Ethereum Blockchain. The following document contains &lt;code&gt;90%&lt;/code&gt; of the gas optimization techniques in Ethereum.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We will see which method is useful and which method is useless. (or event harmful)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We will see how much gas you can save by using each method.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We will rank all the methods by using a tier list. (&lt;code&gt;A = excellent, D= useless/harmful&lt;/code&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; &lt;strong&gt;Deployment optimization&lt;/strong&gt; vs &lt;strong&gt;Call optimization&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;At first, you need to know that there are 2 ways to optimize gas on a Decentralized Application.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;A).&lt;/strong&gt;&lt;/em&gt; &lt;strong&gt;&lt;em&gt;Optimizing Deployment costs&lt;/em&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You can optimize the cost to deploy a smart contract.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;cost = 21000 gas (for creating transaction) + 32000 gas (for creating a contract) + CONSTRUCTOR EXECUTION + BYTES DEPLOYED * 78.125&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So, for deploying a smart contract, you will spend &lt;code&gt;at least 53000 gas&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To minimize gas costs you need to &lt;code&gt;minimize the size of a smart contract and the cost of the constructor&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;B). Optimize every function call:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You can also optimize the &lt;code&gt;cost of calling every function&lt;/code&gt; in your smart contract.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The cost is equal to&lt;code&gt;21000 gas&lt;/code&gt; (for creating transaction) + the function execution cost).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So, the objective will be to reduce the function execution cost.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Note that these 2 means of optimization are not the same!!&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A lot of times, you may deploy a larger solidity code (so with higher deployment gas cost) but more optimized for every function call. (So with a smaller calling gas cost)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you need to choose which type of optimization, you need to use it wisely.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I strongly advise you to &lt;code&gt;optimize every function call&lt;/code&gt; as the &lt;code&gt;deployment is made only one time&lt;/code&gt; and the &lt;code&gt;user experience will be enhanced&lt;/code&gt;. (unless your smart contract &lt;code&gt;size exceeds the limit of 24Kb&lt;/code&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;2. Ranking Gas optimization methods:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;We will rank all the gas optimization methods by using a tier list. (&lt;code&gt;Because there is a difference between saving 2 gas on a transaction of 21000 gas and saving 10000 gas.&lt;/code&gt;)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tier A:&lt;/strong&gt; If you don’t know these rules, you’re not a &lt;em&gt;TRUE&lt;/em&gt; solidity developer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tier B:&lt;/strong&gt; Essential, should be known by all the developers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tier C:&lt;/strong&gt; may be useful sometime, should be known by developers too.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Tier D:&lt;/strong&gt; Useless/harmful, don’t use them (unless you know what you are doing). You may lose, either your time or the security of the smart contract.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Tier A:&lt;/em&gt;&lt;/strong&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;A1 : Knowing how much each &lt;code&gt;EVM opcodes&lt;/code&gt; cost:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Here is the 6 of the &lt;code&gt;most costly&lt;/code&gt; gas opcodes (among the most used is smart contracts):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;CREATE/CREATE2&lt;/em&gt;&lt;/strong&gt; -&amp;gt; Deploys a smart contract. (&lt;code&gt;32,000 gas&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;SSTORE&lt;/em&gt;&lt;/strong&gt; -&amp;gt; Stores a value in storage. (&lt;code&gt;20,000 gas&lt;/code&gt; if the slot hasn’t been accessed, &lt;code&gt;2900&lt;/code&gt;otherwise.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;EXTCODESIZE&lt;/em&gt;&lt;/strong&gt; -&amp;gt; Get the size in bytes of a smart contract code. (&lt;code&gt;2600&lt;/code&gt; if not accessed before, &lt;code&gt;100&lt;/code&gt; gas otherwise)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;BALANCE&lt;/em&gt;&lt;/strong&gt; (&lt;code&gt;address(this).balance&lt;/code&gt;), same as &lt;code&gt;EXTCODESIZE&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;SLOAD&lt;/em&gt;&lt;/strong&gt; -&amp;gt; Access a value in storage. (&lt;code&gt;2100 gas&lt;/code&gt; if not accessed before &lt;code&gt;100 gas&lt;/code&gt; otherwise)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;LOG4&lt;/em&gt;&lt;/strong&gt; -&amp;gt; Create an event with &lt;code&gt;4 topics&lt;/code&gt;. (&lt;code&gt;1875 gas&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;(This is in fact an estimation. Some of them may vary depending on the situation.)&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;The best website which describes how much each opcode is &lt;a href="https://www.evm.codes/" rel="noopener noreferrer"&gt;&lt;/a&gt;&lt;a href="https://www.evm.codes/" rel="noopener noreferrer"&gt;https://www.evm.codes/&lt;/a&gt;, it’s very well explained.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;If you know that, you’ll better understand solidity and you’ll get a “better” intuition of how much each line of code costs you.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;A2: Use &lt;code&gt;view&lt;/code&gt; the modifier:&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;      &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getBalance&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;You can easily cut your gas costs to 0 &lt;code&gt;when you call the smart contract if you don’t write to the storage&lt;/code&gt;. (moreover, you don’t need to wait &lt;code&gt;5–20 seconds the response&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;A3:&lt;/strong&gt; Understand the solidity programming language &amp;amp; &lt;strong&gt;A4:&lt;/strong&gt; Become good at computer science/algorithms.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Tier B:&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These tips and tricks can save you a good amount of gas and should be implemented as much as possible. &lt;em&gt;IMPORTANT NOTE BEFORE STARTING:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;As the &lt;code&gt;not_optimized()&lt;/code&gt; function is situated before &lt;code&gt;optimized()&lt;/code&gt; function in the byte code, Calling &lt;code&gt;optimized()&lt;/code&gt; costs &lt;code&gt;more than not_optimized()&lt;/code&gt; with the same code (&lt;code&gt;22 more gas&lt;/code&gt;), so I will subtract &lt;code&gt;22 gas&lt;/code&gt; on each call to&lt;code&gt;optimized() function&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;B1: Batching operations (Saved gas: &lt;code&gt;21000 gas * batched transactions&lt;/code&gt;):&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Submitting a transaction alone on the blockchain costs a lot of gas (&lt;code&gt;21,000&lt;/code&gt; gas to be precise), so if you can find a way to &lt;code&gt;batch transactions&lt;/code&gt; for your users, it can save you a good amount of gas.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;B2: change &lt;code&gt;orders&lt;/code&gt; of storage slot (&lt;code&gt;20,000&lt;/code&gt; gas saved at deployment):&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Ethereum storage is composed of &lt;code&gt;slots of 32 bytes&lt;/code&gt;, the problem is that writing is expensive. (Up to &lt;code&gt;20,000 gas&lt;/code&gt; using “cold” writing)&lt;/li&gt;
&lt;li&gt;Let’s say you have 3 variables:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;
    &lt;span class="c1"&gt;// These vars use 3 different storage slots&lt;/span&gt;
         &lt;span class="n"&gt;unit128&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// slot 0(16 bytes)&lt;/span&gt;
         &lt;span class="n"&gt;unit256&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// slot 1(32 bytes)&lt;/span&gt;
         &lt;span class="n"&gt;unit128&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// slot 2(16 bytes)&lt;/span&gt;

    &lt;span class="c1"&gt;// These var use 2 different storage slots&lt;/span&gt;
         &lt;span class="n"&gt;unit256&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// slot 0(32 bytes)&lt;/span&gt;
         &lt;span class="n"&gt;unit128&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// slot 1(16 bytes)&lt;/span&gt;
         &lt;span class="n"&gt;unit128&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// slot 1(16 bytes)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;But if we align the &lt;code&gt;3 slots well&lt;/code&gt;, we can economize &lt;code&gt;1 slot&lt;/code&gt;. (&lt;code&gt;a&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; variable will be in the &lt;code&gt;same slot&lt;/code&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Therefore, there are &lt;code&gt;1&lt;/code&gt; less used slot at the deployment. (So &lt;code&gt;20,000 gas&lt;/code&gt; saved)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Moreover, if you want to access the &lt;code&gt;c&lt;/code&gt; variable, but the &lt;code&gt;b&lt;/code&gt; variable is already accessed. It will count as a warm access (&lt;code&gt;accessing a storage slot already accessed&lt;/code&gt;) so it will cost you &lt;code&gt;1000&lt;/code&gt;instead of &lt;code&gt;2900 gas&lt;/code&gt; which is pretty significant.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you don’t understand the storage, the documentation may help you: &lt;a href="https://docs.soliditylang.org/en/v0.8.13/internals/layout_in_storage.html" rel="noopener noreferrer"&gt;https://docs.soliditylang.org/en/v0.8.13/internals/layout_in_storage.html&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;B3: Use the &lt;code&gt;optimizer&lt;/code&gt; (Saved gas: &lt;code&gt;10–50%&lt;/code&gt; on deployment &amp;amp; call):&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;You can use the solidity &lt;code&gt;built-in optimizer&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;This is a very simple way to save a lot of gas without learning any new concepts. You need to check the “&lt;code&gt;enable optimization&lt;/code&gt;” checkbox under Advance configuration section.&lt;/li&gt;
&lt;li&gt;A value near to &lt;code&gt;1&lt;/code&gt; will optimize gas cost, but a value near to the &lt;code&gt;max (2³²-1)&lt;/code&gt; will optimize calls to the function. In most of the cases you can use the &lt;code&gt;default value (200)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;B4: use &lt;code&gt;mapping&lt;/code&gt; instead of &lt;code&gt;array&lt;/code&gt; (&lt;code&gt;5000 gas&lt;/code&gt; saved per value):&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Mappings are usually less expensive than arrays, but you can’t iterate over them.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;      &lt;span class="nf"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unit&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;unit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;unit&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;map2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;not_optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="n"&gt;map2&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;not_optimised()&lt;/code&gt; function gas usage: &lt;code&gt;48,409 gas&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;optimised()&lt;/code&gt; function gas usage: &lt;code&gt;43,400 gas&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;B5: Use &lt;code&gt;require&lt;/code&gt; instead of &lt;code&gt;assert&lt;/code&gt;:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Assert&lt;/code&gt; should NOT be used by other means than testing purposes. (because when the &lt;code&gt;assertion fails, gas is NOT refunded&lt;/code&gt; contrary to require)&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;B6: Use &lt;code&gt;self-destruct&lt;/code&gt; (save up to &lt;code&gt;24,000&lt;/code&gt; gas):&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;selfdestruct()&lt;/code&gt; function destroys the smart contract and refunds &lt;code&gt;24,000 gas&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In a more complex transaction like deploying another smart contract, you can call &lt;code&gt;selfdestruct()&lt;/code&gt; on the smart contract to economize some gas.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;B7: Make fewer &lt;code&gt;external&lt;/code&gt; calls (amount saved: variable):&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Make a call to another contract only when you’re obligated to do so.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;B8: Look for &lt;code&gt;dead code&lt;/code&gt; (saves a variable amount of gas on deployment):&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Sometimes, developers forget to remove useless code like:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;      &lt;span class="k"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This is a dump example because it shows you that some devs can forget to remove &lt;code&gt;useless parts&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;B9: use &lt;code&gt;immutable&lt;/code&gt; variables (saves &lt;code&gt;15,000&lt;/code&gt; gas on deployment)":&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Use immutable storage variables whenever it’s possible.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;       &lt;span class="n"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;not_optimised&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="n"&gt;unit256&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
           &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unit256&lt;/span&gt; &lt;span class="n"&gt;_a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
             &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="p"&gt;}&lt;/span&gt; 
      &lt;span class="p"&gt;}&lt;/span&gt;

       &lt;span class="n"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;optimised&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="n"&gt;unit256&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;immutable&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unit256&lt;/span&gt; &lt;span class="n"&gt;_a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="p"&gt;}&lt;/span&gt; 
       &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;not_optimised&lt;/code&gt;contract gas usage: &lt;code&gt;11,6800&lt;/code&gt; gas&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;optimised&lt;/code&gt; contract gas usage: &lt;code&gt;10,1013&lt;/code&gt; gas&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;B10: Storing data in &lt;code&gt;events&lt;/code&gt; (up to &lt;code&gt;20,000&lt;/code&gt; gas saved on function call):&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;If you don’t need to access the data on-chain in solidity, you can store it using events.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;          &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="nf"&gt;Store&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;indexed&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                      &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="n"&gt;indexed&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

          &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;not_optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="n"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xaaC532&lt;/span&gt;&lt;span class="mf"&gt;..&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
           &lt;span class="p"&gt;}&lt;/span&gt;

          &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="n"&gt;emit&lt;/span&gt; &lt;span class="nf"&gt;Store&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0xaaC532&lt;/span&gt;&lt;span class="mf"&gt;..&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
           &lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;not_optimised()&lt;/code&gt; function gas usage: &lt;code&gt;43,353&lt;/code&gt; gas&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;optimised()&lt;/code&gt; function gas usage: &lt;code&gt;22,747&lt;/code&gt; gas&lt;/li&gt;
&lt;/ul&gt;




&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Tier C:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;These tips can save you a fair amount of gas and cost you nothing.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C1: Use &lt;code&gt;static size&lt;/code&gt; type in solidity (about &lt;code&gt;50&lt;/code&gt; gas saved):&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Static size types (like &lt;code&gt;bool&lt;/code&gt;, &lt;code&gt;uint256&lt;/code&gt;, &lt;code&gt;bytes5&lt;/code&gt;) are cheaper than dynamic size type (like &lt;code&gt;string&lt;/code&gt; or &lt;code&gt;bytes&lt;/code&gt;).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;         &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;not_optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="n"&gt;bytes4&lt;/span&gt; &lt;span class="n"&gt;memory&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"abcd"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;

          &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="n"&gt;bytes4&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x61626364&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt; &lt;code&gt;not_optimised()&lt;/code&gt; function gas usage: &lt;code&gt;21,255&lt;/code&gt; gas&lt;/li&gt;
&lt;li&gt; &lt;code&gt;optimised()&lt;/code&gt; function gas usage: &lt;code&gt;21,227&lt;/code&gt; gas&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C2: &lt;code&gt;Cold access&lt;/code&gt; vs &lt;code&gt;warm access&lt;/code&gt; (&lt;code&gt;70&lt;/code&gt; saved gas):&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Don’t access the same storage variable twice.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;            &lt;span class="n"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;not_optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;tmp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="n"&gt;tmp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="n"&gt;tmp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
           &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;not_optimised()&lt;/code&gt; function gas usage: &lt;code&gt;23,412&lt;/code&gt; gas&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;optimised()&lt;/code&gt; function gas usage: &lt;code&gt;23,347&lt;/code&gt; gas&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C3: Using &lt;code&gt;calldata&lt;/code&gt; instead of &lt;code&gt;memory&lt;/code&gt; (&lt;code&gt;450&lt;/code&gt; saved gas per call):&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;        &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;not_optimized&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;memory&lt;/span&gt; &lt;span class="n"&gt;_str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;//code ...&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;optimized&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;calldata&lt;/span&gt; &lt;span class="n"&gt;_str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// code...&lt;/span&gt;
         &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;not_optimised()&lt;/code&gt; function gas usage: &lt;code&gt;22,442&lt;/code&gt; gas&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;optimised()&lt;/code&gt; function gas usage: &lt;code&gt;21,994&lt;/code&gt; gas&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C4: Use &lt;code&gt;Indexed events&lt;/code&gt; (&lt;code&gt;62&lt;/code&gt; gas saved on function call per topic):&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;You can mark each event as &lt;code&gt;indexed&lt;/code&gt; as bellow:&lt;/li&gt;
&lt;li&gt;Indexed events allow events to be &lt;code&gt;searched&lt;/code&gt; more easily.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;       &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="nf"&gt;Test1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
       &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="nf"&gt;Test2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="n"&gt;indexed&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="n"&gt;indexed&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

       &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;optimized&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;calldata&lt;/span&gt; &lt;span class="n"&gt;_str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="c1"&gt;// code ...&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

       &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;not_optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;emit&lt;/span&gt; &lt;span class="nf"&gt;Test1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="mf"&gt;..&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="mf"&gt;..&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

       &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;emit&lt;/span&gt; &lt;span class="nf"&gt;Test2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="mf"&gt;..&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="mf"&gt;..&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Here the function &lt;code&gt;optimised()&lt;/code&gt; uses &lt;code&gt;135&lt;/code&gt; less gas than &lt;code&gt;not_optimised()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C5: Changing the &lt;code&gt;order of calls&lt;/code&gt; (&lt;code&gt;20–2000&lt;/code&gt; saved gas per call):&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We can call every smart contract, you supply in &lt;code&gt;msg.data&lt;/code&gt; the signature of the function. (the first &lt;code&gt;4 bytes&lt;/code&gt; of the function &lt;code&gt;keccak256&lt;/code&gt; hash).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Firstly, the EVM will do a “&lt;code&gt;switch&lt;/code&gt;” of this signature, to see which function to execute.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;      &lt;span class="k"&gt;switch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// compare to signature&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mh"&gt;0x01234567&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;go&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;functionA&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// cost 22 more gas..&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mh"&gt;0x11111111&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;go&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;functionB&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// cost 22+22 more gas... &lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mh"&gt;0x4913aaaa&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;go&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;functionC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
              &lt;span class="mf"&gt;...&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;As &lt;code&gt;comparing to signature computation&lt;/code&gt; use a bit of gas (&lt;code&gt;22&lt;/code&gt; gas), you need to place the most called function in the first place of the &lt;code&gt;switch()&lt;/code&gt; by assuring that the signature is in the first place (by changing its name)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can learn more about this par is my reversing EVM articles: &lt;a href="https://trustchain.medium.com/reversing-and-debugging-evm-smart-contracts-392fdadef32d" rel="noopener noreferrer"&gt;https://trustchain.medium.com/reversing-and-debugging-evm-smart-contracts-392fdadef32d&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C6: Use &lt;code&gt;++i&lt;/code&gt; instead of &lt;code&gt;i = i+1&lt;/code&gt; (&lt;code&gt;62&lt;/code&gt; gas saved on each call):&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;This sounds like a joke, but it seems to work.:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;        &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;not_optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
             &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
             &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="p"&gt;}&lt;/span&gt;

         &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
              &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;not_optimised()&lt;/code&gt; function gas usage: &lt;code&gt;21,401&lt;/code&gt; gas&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;optimised()&lt;/code&gt; function gas usage: &lt;code&gt;21,339&lt;/code&gt; gas&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C7: Use &lt;code&gt;uint256&lt;/code&gt; instead of &lt;code&gt;uintXX&lt;/code&gt; on storage (&lt;code&gt;55&lt;/code&gt; gas saved per call):&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;To &lt;code&gt;write/access&lt;/code&gt; data on storage, the EVM uses &lt;code&gt;32 bytes (= 256 bits slots)&lt;/code&gt;, by using smaller types than uint256, the EVM needs to perform additional operations to assure the conversion. So better to use &lt;code&gt;uint256&lt;/code&gt; instead of &lt;code&gt;uint8&lt;/code&gt; for storing data.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;         &lt;span class="n"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="n"&gt;uint8&lt;/span&gt; &lt;span class="n"&gt;balance2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;not_optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;balance2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;

         &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;not_optimised()&lt;/code&gt; function gas usage: &lt;code&gt;43,353&lt;/code&gt; gas&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;optimised()&lt;/code&gt; function gas usage: &lt;code&gt;43,298&lt;/code&gt; gas&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C8: Create a &lt;code&gt;custom error&lt;/code&gt; (&lt;code&gt;24&lt;/code&gt; gas saved per call):&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;You can create a custom error on solidity and revert with it like below:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;          &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="nf"&gt;err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;not_optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nf"&gt;revert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
           &lt;span class="p"&gt;}&lt;/span&gt;

          &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;revert&lt;/span&gt; &lt;span class="nf"&gt;err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
           &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;not_optimised()&lt;/code&gt; function gas usage: &lt;code&gt;21,476&lt;/code&gt; gas&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;optimised()&lt;/code&gt; function gas usage: &lt;code&gt;21,474&lt;/code&gt; gas&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C9: Exchanging 2 variables by using a &lt;code&gt;tuple (a, b) = (b, a)&lt;/code&gt; (saves &lt;code&gt;5&lt;/code&gt; gas on every call):&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;          &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;not_optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
               &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
               &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
               &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
               &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
               &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
           &lt;span class="p"&gt;}&lt;/span&gt;

           &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
               &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
               &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
               &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
           &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;not_optimised()&lt;/code&gt; function gas usage: &lt;code&gt;21,241&lt;/code&gt; gas&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;optimised()&lt;/code&gt; function gas usage: &lt;code&gt;21,236&lt;/code&gt; gas&lt;/li&gt;
&lt;li&gt;We put it on the C tier because the notation is way more readable.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;C10: Use Calldata instead of Memory in function arguments in case of not changing the passed data:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Using calldata in a summing function that loops over an input array can save about 1829 gas (or 3.5%) on average.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;      &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;not_optimised&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;memory&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
         &lt;span class="c1"&gt;// code ...&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

     &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;optimised&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;calldata&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
       &lt;span class="c1"&gt;// code ...&lt;/span&gt;
         &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;not_optimised()&lt;/code&gt; function gas usage: &lt;code&gt;50,992&lt;/code&gt; gas&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;optimised()&lt;/code&gt; function gas usage: &lt;code&gt;49,163&lt;/code&gt; gas&lt;/li&gt;
&lt;/ul&gt;




&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Tier D: (Useless/harmful)&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These optimizing gas tips are useless, don’t lose your time for saving a low amount of gas it will be shadowed anyway by the &lt;code&gt;21,000&lt;/code&gt;gas transaction.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;D1: using &lt;code&gt;unchecked&lt;/code&gt; (gas saved: &lt;code&gt;150&lt;/code&gt;/operation):&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Since solidity 0.8.0, operations like + * / — are checked every time if there is an &lt;code&gt;overflow/underflow&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;But it cost a little bit of gas to verify if there is an overflow/underflow.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Unless you are doing a lot of operations in a single function call (like in for loops), you should NOT use &lt;code&gt;unchecked{}&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;         &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;not_optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
              &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
              &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
              &lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;

          &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
               &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
               &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
               &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
               &lt;span class="n"&gt;unchecked&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
           &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;not_optimised()&lt;/code&gt; function gas usage: &lt;code&gt;21,419&lt;/code&gt; gas&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;optimised()&lt;/code&gt; function gas usage: &lt;code&gt;21253&lt;/code&gt; gas&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;D2: changing and optimizing the &lt;code&gt;bytecode&lt;/code&gt; yourself:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Don’t do this unless you have a good reason to do so, some of the functions may end up with undefined behavior and security issues.&lt;/li&gt;
&lt;li&gt;Use the optimizer instead or implement a very strict testing policy.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;D3: Delete &lt;code&gt;Errors string&lt;/code&gt; messages (about &lt;code&gt;78.125&lt;/code&gt; gas saved on deployment cost per characters):&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Don’t do it, the code may be harder to debug for you and for users. Use the custom errpr instead.&lt;/li&gt;
&lt;li&gt;For example, someone can replace:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;         &lt;span class="k"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;account&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nf"&gt;address&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; 
           &lt;span class="s2"&gt;"ERC20: mint to the zero address"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nc"&gt;To&lt;/span&gt;&lt;span class="o"&gt;:-&lt;/span&gt;
         &lt;span class="k"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;account&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nf"&gt;address&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;D4: using &lt;code&gt;low level assembly&lt;/code&gt; (gas saved: variable):&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;If you don't know what you’re doing, &lt;code&gt;DON’T do solidity assembly&lt;/code&gt;. You will end up having a lot more chances to have a security hole on your contract for a marginal saved gas amount.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;D5: Use &lt;code&gt;external&lt;/code&gt; instead of &lt;code&gt;public&lt;/code&gt;:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Surprisingly they are not any difference between &lt;code&gt;external&lt;/code&gt; and &lt;code&gt;public&lt;/code&gt; functions,
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;       &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;not_optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
                 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;777&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
             &lt;span class="p"&gt;}&lt;/span&gt;
       &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt; &lt;span class="nf"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;777&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
           &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;not_optimised()&lt;/code&gt; function gas usage: &lt;code&gt;21,401&lt;/code&gt; gas&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;optimised()&lt;/code&gt; function gas usage: &lt;code&gt;21,379&lt;/code&gt; gas (22)&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;D6: Use &lt;code&gt;left shift&lt;/code&gt; instead of &lt;code&gt;multiplication&lt;/code&gt; &amp;gt;&amp;gt; (150 saved gas):&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Shifting binary data n times to the left results in multiplying the data by 2^n&lt;/code&gt;. Here is an example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;      &lt;span class="mo"&gt;00000001&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="n"&gt;dec&lt;/span&gt;
      &lt;span class="mo"&gt;00000010&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="n"&gt;dec&lt;/span&gt;
      &lt;span class="mo"&gt;00000100&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="n"&gt;dec&lt;/span&gt;
      &lt;span class="mo"&gt;00001000&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="n"&gt;dec&lt;/span&gt;

     &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;not_optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="kt"&gt;external&lt;/span&gt; &lt;span class="nf"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
                   &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                   &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
              &lt;span class="p"&gt;}&lt;/span&gt;

     &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt; &lt;span class="nf"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
                   &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                   &lt;span class="n"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
              &lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;not_optimised()&lt;/code&gt; function gas usage: &lt;code&gt;21,615&lt;/code&gt; gas&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;optimised()&lt;/code&gt; function gas usage: &lt;code&gt;21,436&lt;/code&gt; gas&lt;/li&gt;
&lt;li&gt;Gains are not bad, but the &lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt; operator doesn't check for an overflow.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;D7: Delete &lt;code&gt;metadata hash&lt;/code&gt; (&lt;code&gt;3000–5000&lt;/code&gt; gas on deployment):&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;On every smart contract byte code a “&lt;code&gt;hash&lt;/code&gt;” is appended in the end.&lt;/li&gt;
&lt;li&gt;This is the &lt;code&gt;hash of all the metadata of the smart contract&lt;/code&gt;. (Including &lt;code&gt;abi&lt;/code&gt;, &lt;code&gt;comments&lt;/code&gt;, &lt;code&gt;code&lt;/code&gt;…)&lt;/li&gt;
&lt;li&gt;As the metadata hash is &lt;code&gt;32 bytes&lt;/code&gt; in length, it can save you a bunch of gas.&lt;/li&gt;
&lt;li&gt;But this is quite hard to do, because you have to do it by hand.&lt;/li&gt;
&lt;li&gt;So it may be dangerous if executed badly.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;D8: Add &lt;code&gt;payable&lt;/code&gt; to every function (&lt;code&gt;24&lt;/code&gt; gas saved per call):&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Adding &lt;code&gt;payable&lt;/code&gt; to a function, removes the verification on &lt;code&gt;msg.value&lt;/code&gt;. Therefore, some gas is saved.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;          &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;not_optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="c1"&gt;// code ...&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;

          &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;optimized&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;external&lt;/span&gt; &lt;span class="n"&gt;payable&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
               &lt;span class="c1"&gt;// code ...&lt;/span&gt;
           &lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;not_optimised()&lt;/code&gt; function gas usage: &lt;code&gt;21,186&lt;/code&gt; gas&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;optimised()&lt;/code&gt; function gas usage: &lt;code&gt;21,184&lt;/code&gt; gas&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;D9: Doing the important work &lt;code&gt;offchain&lt;/code&gt;:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Be careful about the work you’re doing off-chain (Like in JS).&lt;/li&gt;
&lt;li&gt;For example, you &lt;code&gt;can’t do security verification off-chain&lt;/code&gt; in the website front end, this is very dangerous as anyone may be able to change the JS code in the browser and send bad input to the smart contract.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;ALWAYS DO THE SECURITY VERIFICATION ON-CHAIN&lt;/code&gt;&lt;/strong&gt;.!!!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Some other gas saving techniques: -
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;    &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nc"&gt;Loading&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="n"&gt;variable&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;memory&lt;/span&gt;
    &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nc"&gt;Replace&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;loop&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;
    &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nc"&gt;Caching&lt;/span&gt; &lt;span class="k"&gt;array&lt;/span&gt; &lt;span class="n"&gt;elements&lt;/span&gt;
    &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nc"&gt;Short&lt;/span&gt; &lt;span class="n"&gt;circuit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Some more details: (Credit - CodeEater)
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Internals of Solidity Compiler:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo6n7f05xmrxec59fo00n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo6n7f05xmrxec59fo00n.png" alt="Image description" width="800" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;About Nonce:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjmq599g2vc32hpqf260x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjmq599g2vc32hpqf260x.png" alt="Image description" width="800" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Why Gas Cost 21000:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Falmjiaixd306ql8iefyx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Falmjiaixd306ql8iefyx.png" alt="Image description" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;How's The Transaction Fee Calculated:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjfrele576ii1gi99kafs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjfrele576ii1gi99kafs.png" alt="Image description" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;How's The Transaction Fee Calculated with Input Data:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ceft3r5aqup0dr6cr1j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ceft3r5aqup0dr6cr1j.png" alt="Image description" width="800" height="438"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6c3yo90np1qrl1aowaso.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6c3yo90np1qrl1aowaso.png" alt="Image description" width="800" height="423"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;How's The Transaction Fee Calculated For Functions:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftnr14umllpp0gl2jf8mo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftnr14umllpp0gl2jf8mo.png" alt="Image description" width="800" height="441"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9kzw2ms65smaxlkb3v0v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9kzw2ms65smaxlkb3v0v.png" alt="Image description" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;How's The Transaction Fee Calculated For Arrays:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fszavh6fs98wb5dc3837z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fszavh6fs98wb5dc3837z.png" alt="Image description" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqz438nvh8alk4e02yt8j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqz438nvh8alk4e02yt8j.png" alt="Image description" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Other Useful links:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://medium.com/@sabir.sk/solidity-gas-optimisation-techniques-664b47eabb66" rel="noopener noreferrer"&gt;https://medium.com/@sabir.sk/solidity-gas-optimisation-techniques-664b47eabb66&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.cyfrin.io/blog/solidity-gas-optimization-tips" rel="noopener noreferrer"&gt;https://www.cyfrin.io/blog/solidity-gas-optimization-tips&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.rareskills.io/post/gas-optimization" rel="noopener noreferrer"&gt;https://www.rareskills.io/post/gas-optimization&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://medium.com/@solidity101/maximizing-gas-efficiency-expert-strategies-for-solidity-developers-%EF%B8%8F-a19f7a7df4e9" rel="noopener noreferrer"&gt;https://medium.com/@solidity101/maximizing-gas-efficiency-expert-strategies-for-solidity-developers-%EF%B8%8F-a19f7a7df4e9&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://0xmacro.com/blog/solidity-gas-optimizations-cheat-sheet/" rel="noopener noreferrer"&gt;https://0xmacro.com/blog/solidity-gas-optimizations-cheat-sheet/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://mudit.blog/solidity-gas-optimization-tips/?ref=0xmacro.com" rel="noopener noreferrer"&gt;https://mudit.blog/solidity-gas-optimization-tips/?ref=0xmacro.com&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/code-423n4/2022-08-olympus-findings/issues/140" rel="noopener noreferrer"&gt;https://github.com/code-423n4/2022-08-olympus-findings/issues/140&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.alchemy.com/overviews/solidity-gas-optimization" rel="noopener noreferrer"&gt;https://www.alchemy.com/overviews/solidity-gas-optimization&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://cryptoguide.dev/posts/solidity-gas-optimizations-guide/" rel="noopener noreferrer"&gt;https://cryptoguide.dev/posts/solidity-gas-optimizations-guide/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://dev.to/juanxavier/advanced-gas-optimizations-tips-for-solidity-1j2f"&gt;https://dev.to/juanxavier/advanced-gas-optimizations-tips-for-solidity-1j2f&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://medium.com/@0xkaden/how-to-write-smart-contracts-that-optimize-gas-spent-on-ethereum-30b5e9c5db85" rel="noopener noreferrer"&gt;https://medium.com/@0xkaden/how-to-write-smart-contracts-that-optimize-gas-spent-on-ethereum-30b5e9c5db85&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ethereum</category>
      <category>gas</category>
      <category>solidity</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>Mastering Ethereum Notes</title>
      <dc:creator>SK Sabiruddin</dc:creator>
      <pubDate>Tue, 06 Feb 2024 04:39:29 +0000</pubDate>
      <link>https://dev.to/sksabir/mastering-ethereum-notes-87g</link>
      <guid>https://dev.to/sksabir/mastering-ethereum-notes-87g</guid>
      <description>&lt;h2&gt;
  
  
  The components of an open, public blockchain are:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A peer-to-peer (P2P) network connecting participants and propagating transactions and blocks of verified transactions, based on a standardized &lt;code&gt;"gossip" protocol&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Messages, in the form of &lt;code&gt;transactions&lt;/code&gt;, representing &lt;code&gt;state transitions&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A set of &lt;code&gt;consensus rules&lt;/code&gt;, governing what constitutes a transaction and what makes for a &lt;code&gt;valid state transition&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;state machine&lt;/code&gt; those processes transactions according to the consensus rules.&lt;/li&gt;
&lt;li&gt;A chain of &lt;code&gt;cryptographically secured blocks&lt;/code&gt; that acts as a journal of all the &lt;code&gt;verified and accepted state transitions&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A consensus algorithm that &lt;code&gt;decentralizes control over the blockchain&lt;/code&gt;, by forcing participants to cooperate in the enforcement of the consensus rules.&lt;/li&gt;
&lt;li&gt;A game-theoretically sound incentivization scheme (e.g., &lt;code&gt;proof-of-work costs plus block rewards&lt;/code&gt;) to economically secure the state machine in an open environment.&lt;/li&gt;
&lt;li&gt;One or more open-source software implementations of the above ("clients").&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  About Ethereum:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Ethereum is often described as &lt;code&gt;the world computer&lt;/code&gt;. From a more practical perspective, Ethereum is an open source, globally decentralized computing infrastructure that &lt;code&gt;executes programs called smart contracts&lt;/code&gt;. It uses a blockchain to &lt;code&gt;synchronize and store the system’s state changes&lt;/code&gt;, along with a cryptocurrency called ether to meter and constrain execution resource costs.&lt;/li&gt;
&lt;li&gt;The Ethereum platform enables developers to build &lt;code&gt;powerful decentralized applications&lt;/code&gt; with built-in economic functions, while providing high &lt;code&gt;availability, auditability, transparency, and neutrality&lt;/code&gt;, it also reduces or eliminates censorship and reduces certain counterparty risks.&lt;/li&gt;
&lt;li&gt;Many people will come to Ethereum with some prior experience of cryptocurrencies, specifically Bitcoin.&lt;/li&gt;
&lt;li&gt;Ethereum shares many common elements with other open blockchains: a &lt;code&gt;peer-to-peer network&lt;/code&gt; connecting participants, a &lt;code&gt;Byzantine fault–tolerant&lt;/code&gt; consensus algorithm for &lt;code&gt;synchronization of state updates&lt;/code&gt; (a proof-of-work blockchain), the use of &lt;code&gt;cryptographic primitives&lt;/code&gt; such as &lt;code&gt;digital signatures&lt;/code&gt; and &lt;code&gt;hashes&lt;/code&gt;, and a &lt;code&gt;digital currency&lt;/code&gt; (ether).&lt;/li&gt;
&lt;li&gt;Ethereum’s development was planned over &lt;code&gt;four distinct stages&lt;/code&gt;, with major changes occurring at each stage. &lt;/li&gt;
&lt;li&gt;A stage may include sub releases, known as &lt;code&gt;"hard forks,"&lt;/code&gt; that change functionality in a way that is not backward compatible.&lt;/li&gt;
&lt;li&gt;The four main development stages are codenamed &lt;code&gt;Frontier&lt;/code&gt;, &lt;code&gt;Homestead&lt;/code&gt;, &lt;code&gt;Metropolis&lt;/code&gt;, and &lt;code&gt;Serenity&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;The intermediate &lt;code&gt;hard forks&lt;/code&gt; that have occurred to date are codenamed &lt;code&gt;Ice Age&lt;/code&gt;, &lt;code&gt;DAO&lt;/code&gt;, &lt;code&gt;Tangerine Whistle&lt;/code&gt;, &lt;code&gt;Spurious Dragon&lt;/code&gt;, &lt;code&gt;Byzantium&lt;/code&gt;, &lt;code&gt;Constantinople/St&lt;/code&gt;, &lt;code&gt;Petersburg&lt;/code&gt;, &lt;code&gt;Istanbul&lt;/code&gt; and &lt;code&gt;Muir Glacier&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;Both the &lt;code&gt;development stages&lt;/code&gt; and the &lt;code&gt;intermediate hard forks&lt;/code&gt; are shown on the following timeline, which is "dated" by block number:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Block #0&lt;/strong&gt;: &lt;code&gt;_Frontier_&lt;/code&gt; — The initial stage of Ethereum, lasting from &lt;code&gt;July 30, 2015, to March 2016&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Block #200,000:&lt;/strong&gt; &lt;em&gt;&lt;code&gt;Ice Age&lt;/code&gt;&lt;/em&gt; — A hard fork to introduce an exponential difficulty increase, to motivate a transition to PoS when ready.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Block #1,150,000&lt;/strong&gt;: &lt;em&gt;&lt;code&gt;Homestead&lt;/code&gt;&lt;/em&gt; — The second stage of Ethereum, launched in March 2016.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Block #1,192,000:&lt;/strong&gt; &lt;em&gt;&lt;code&gt;DAO&lt;/code&gt;&lt;/em&gt; — A hard fork that reimbursed victims of the hacked DAO contract and caused &lt;em&gt;Ethereum&lt;/em&gt; and &lt;em&gt;Ethereum Classic&lt;/em&gt; to split into two competing systems.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Block #2,463,000&lt;/strong&gt;: &lt;em&gt;&lt;code&gt;Tangerine Whistle&lt;/code&gt;&lt;/em&gt; — A hard fork to change the gas calculation for certain I/O-heavy operations and to clear the accumulated state from a denial-of-service (DoS) attack that exploited the low gas cost of those operations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Block #2,675,000&lt;/strong&gt;: &lt;em&gt;&lt;code&gt;Spurious Dragon&lt;/code&gt;&lt;/em&gt; — A hard fork to address more DoS attack vectors, and another state clearing. Also, a replay attack protection mechanism.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Block #4,370,000&lt;/strong&gt;: &lt;em&gt;&lt;code&gt;Metropolis Byzantium&lt;/code&gt;&lt;/em&gt; — Metropolis is the third stage of Ethereum. Launched in October 2017, Byzantium is the first part of Metropolis, adding low-level functionalities and adjusting the block reward and difficulty.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Block #7,280,000&lt;/strong&gt;: &lt;em&gt;&lt;code&gt;Constantinople / St. Petersburg&lt;/code&gt;&lt;/em&gt; — Constantinople was planned to be the second part of Metropolis with similar improvements. A few hours before its activation, a critical bug was discovered. The hard fork was therefore postponed and renamed St. Petersburg.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Block #9,069,000:&lt;/strong&gt; &lt;em&gt;&lt;code&gt;Istanbul&lt;/code&gt;&lt;/em&gt; — An additional hard fork with the same approach, and naming convention, as for the prior two.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Block #9,200,000:&lt;/strong&gt; &lt;em&gt;&lt;code&gt;Muir Glacier&lt;/code&gt;&lt;/em&gt; — A hard fork whose sole purpose was to adjust the difficulty again due to the exponential increase introduced by Ice Age.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Two hard forks, Berlin and London&lt;/code&gt;, have also been announced, and we are now in the final stage of Ethereum development, codenamed &lt;code&gt;Serenity&lt;/code&gt;. Serenity involves a profound reorganization of the infrastructure that will make &lt;code&gt;Ethereum more scalable, more secure, and more sustainable&lt;/code&gt;. It is presented as the second version of Ethereum, "Ethereum 2.0".&lt;/li&gt;
&lt;li&gt;The original blockchain, namely Bitcoin’s blockchain, &lt;code&gt;tracks the state of units of bitcoin and their ownership&lt;/code&gt;. You can think of Bitcoin as a distributed consensus state machine, where transactions cause a global state transition, altering the ownership of coins. &lt;/li&gt;
&lt;li&gt;The state transitions are constrained by the rules of consensus, allowing all participants to (eventually) converge on a common (consensus) state of the system, after several blocks are mined.&lt;/li&gt;
&lt;li&gt;Ethereum is also a distributed state machine. But &lt;code&gt;instead of tracking only the state of currency ownership, Ethereum tracks the state transitions of a general-purpose data store&lt;/code&gt;, i.e., a store that can hold any &lt;code&gt;data expressible as a key–value tuple&lt;/code&gt;. A key–value data store holds arbitrary values, each referenced by some key.&lt;/li&gt;
&lt;li&gt;In some ways, this serves the same purpose as the data storage model of Random Access Memory (RAM) used by most general-purpose computers. Ethereum has &lt;code&gt;memory that stores both code and data&lt;/code&gt;, and it uses the Ethereum blockchain to track how this memory changes over time. Like a general-purpose stored-program computer, Ethereum can load code into its state machine and run that code, storing the resulting state changes in its blockchain.&lt;/li&gt;
&lt;li&gt;Two of the critical differences from most general-purpose computers are that &lt;code&gt;Ethereum state changes are governed by the rules of consensus and the state is distributed globally&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;Ethereum answers the question: "What if we could track any arbitrary state and program the state machine to create a world-wide computer operating under consensus?"&lt;/li&gt;
&lt;li&gt;Ethereum has two different types of accounts: &lt;code&gt;externally owned accounts (EOAs)&lt;/code&gt; and &lt;code&gt;contracts&lt;/code&gt;. Ownership of ether by EOAs is established through &lt;code&gt;digital private keys, Ethereum addresses, and digital signatures&lt;/code&gt;. The private keys are at the heart of all user interaction with Ethereum. In fact, account addresses are derived directly from private keys: a &lt;code&gt;private key uniquely determines a single Ethereum address, also known as an account&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;That is to say that private keys should remain private and never appear in messages passed to the network, nor should they be stored on-chain; only &lt;code&gt;account addresses&lt;/code&gt; and &lt;code&gt;digital signatures&lt;/code&gt; are ever transmitted and stored on the Ethereum system.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Access and control of funds is achieved with digital signatures, which are also created using the private key&lt;/code&gt;. Ethereum transactions require a valid digital signature to be included in the blockchain. Anyone with a copy of a private key has control of the corresponding account and any ether it holds. Assuming a user keeps their private key safe, the digital signatures in Ethereum transactions prove the true owner of the funds, because they prove ownership of the private key.&lt;/li&gt;
&lt;li&gt;In the payment portion of an Ethereum transaction, the intended recipient is represented by an Ethereum address, which is used in the same way as the beneficiary account details of a bank transfer. As we will see in more detail shortly, an Ethereum address for an EOA is generated from the public key portion of a key pair. However, not all Ethereum addresses represent &lt;code&gt;public–private&lt;/code&gt; key pairs; they can also represent contracts, are not backed by private keys.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Public key cryptography&lt;/code&gt; (also called "asymmetric cryptography") is a core part of modern-day information security. Public key cryptography uses unique keys to secure information. These keys are based on mathematical functions that have a special property: it is easy to calculate them, but hard to calculate their inverse. Based on these functions, cryptography enables the creation of digital secrets and unforgeable digital signatures, which are secured by the laws of mathematics.&lt;/li&gt;
&lt;li&gt;A more advanced category of mathematical functions that is useful in cryptography is based on arithmetic operations on an &lt;code&gt;elliptic curve&lt;/code&gt;. &lt;code&gt;In elliptic curve arithmetic, multiplication modulo a prime is simple but division (the inverse) is practically impossible&lt;/code&gt;. This is called the &lt;code&gt;discrete logarithm problem&lt;/code&gt; and there are currently no known trapdoors. Elliptic curve cryptography is used extensively in modern computer systems and is the basis of Ethereum’s (and other cryptocurrencies') use of private keys and digital signatures.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;A digital signature can be created to sign any message&lt;/code&gt;. For Ethereum transactions, the details of the transaction itself are used as the message. The mathematics of cryptography—in this case, &lt;code&gt;elliptic curve cryptography—provides a way for the message (i.e., the transaction details) to be combined with the private key to create a code that can only be produced with knowledge of the private key&lt;/code&gt;. That code is called the digital signature.&lt;/li&gt;
&lt;li&gt;When a transaction is sent to the Ethereum network in order to move funds or interact with smart contracts, it needs to be sent with a digital signature created with the private key corresponding to the Ethereum address in question. &lt;code&gt;Elliptic curve mathematics means that anyone can verify that a transaction is valid, by checking that the digital signature matches the transaction details and the Ethereum address to which access is being requested&lt;/code&gt;. The verification doesn’t involve the private key at all; that remains private. However, &lt;code&gt;the verification process determines beyond doubt that the transaction could have only come from someone with the private key that corresponds to the public key behind the Ethereum address. This is the "magic" of public key cryptography&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;There is no encryption as part of the Ethereum protocol—all messages that are sent as part of the operation of the Ethereum network can (necessarily) be read by everyone. As such, private keys are only used to create digital signatures for transaction authentication.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Ethereum’s Components:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;P2P network&lt;/strong&gt;: Ethereum runs on the Ethereum main network, which is addressable on &lt;code&gt;TCP port 30303&lt;/code&gt;, and runs a protocol called &lt;code&gt;ÐΞVp2p&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Consensus rules&lt;/strong&gt;: Ethereum’s consensus rules are defined in the reference specification, the Yellow Paper.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Transactions&lt;/strong&gt;: Ethereum transactions are network messages that include (among other things) a &lt;code&gt;sender, recipient, value, and data payload&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;State machine&lt;/strong&gt;: Ethereum state transitions are processed by the &lt;code&gt;Ethereum Virtual Machine (EVM)&lt;/code&gt;, a &lt;code&gt;stack-based virtual machine that executes bytecode&lt;/code&gt; (machine-language instructions). EVM programs, called "smart contracts," are written in &lt;code&gt;high-level languages (e.g., Solidity)&lt;/code&gt; and compiled to bytecode for execution on the EVM.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data structures&lt;/strong&gt;: Ethereum’s state is stored locally on each node as a database (&lt;code&gt;usually Google’s LevelDB&lt;/code&gt;), which contains the &lt;code&gt;transactions&lt;/code&gt; and &lt;code&gt;system state&lt;/code&gt; in a serialized hashed data structure called a &lt;code&gt;Merkle Patricia Tree&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Consensus algorithm&lt;/strong&gt;: Ethereum uses Bitcoin’s consensus model, &lt;code&gt;Nakamoto Consensus&lt;/code&gt;, which uses &lt;code&gt;sequential single-signature blocks&lt;/code&gt;, weighted in importance by PoS to determine the longest chain and therefore the current state. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Economic security&lt;/strong&gt;: Ethereum currently uses a PoS algorithm.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Clients&lt;/strong&gt;: Ethereum has several interoperable implementations of the client software, the most prominent of which are Go-Ethereum (Geth) and Parity.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  EVM Architecture:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foxevi2sey3rm1rdct6lu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foxevi2sey3rm1rdct6lu.png" alt="EVM" width="800" height="452"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The EVM uses a &lt;code&gt;stack-based architecture&lt;/code&gt; and a word size of &lt;code&gt;256 bits&lt;/code&gt;. The 256-bit word size allows the EVM to facilitate &lt;code&gt;native hashing&lt;/code&gt; and &lt;code&gt;elliptic curve operations&lt;/code&gt; that ensure funds can be spent only by their rightful owners. The EVM supports various programming languages such as Vyper and Solidity, with Solidity being the most popular programming language for smart contract source code. These programming languages are used to write smart contracts, which are converted into the bytecode needed to be utilized by the EVM.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When an Ethereum transaction executes a &lt;code&gt;smart contract&lt;/code&gt;, an EVM is loaded with the information for the transaction being processed. For example, one &lt;em&gt;variable&lt;/em&gt; needed for a smart contract execution is the &lt;em&gt;gas&lt;/em&gt; supply, which is set to the amount of gas paid by the sender. &lt;code&gt;The gas supply is reduced as the transaction progresses&lt;/code&gt;, and if, at any point, the gas supply reaches zero, the transaction is abandoned. Although abandoned transactions don’t result in changes to the Ethereum state and are not considered valid transactions, the block’s beneficiary is paid for providing resources up to the halting point.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;bytecode stored on-chain&lt;/code&gt;, known as the runtime bytecode, is then converted into an opcode that the EVM computation engine interprets to carry out those actions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Smart contracts can initiate transactions and call other contracts on their own. In this case, each call results in another EVM being loaded with specific information for the new transaction. This new information is &lt;code&gt;initialized from the EVM one level above.&lt;/code&gt; If there isn’t enough gas to complete the execution, the state is discarded, and the transaction execution is reset to the EVM one level above.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Ethereum protocol uses two distinct data types – &lt;code&gt;permanent data&lt;/code&gt; and &lt;code&gt;ephemeral data&lt;/code&gt;. &lt;code&gt;_Permanent data_, such as a transaction, is recorded in Ethereum’s tree-like data structure and will never be altered&lt;/code&gt;. &lt;code&gt;_Ephemeral data_, such as a wallet’s balance, is recorded and changed in response to new transactions&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;EVM’s &lt;code&gt;opcodes&lt;/code&gt; use &lt;code&gt;contract memory to retrieve data&lt;/code&gt;. &lt;code&gt;Contract state memory is stored at the contract address and is not persistent&lt;/code&gt;. A variable’s position in a smart contract’s storage array is determined by its order in the code. &lt;code&gt;If a given variable is 256 bits or less, the EVM will try to fit multiple variables in the space&lt;/code&gt;. &lt;code&gt;When a contract inherits another contract, the storage variables of the base contract are stored in the first slots in the order of inheritance.&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Whereas contract memory is impermanent, contract storage is stored indefinitely&lt;/code&gt;. &lt;code&gt;Contract storage is like a public database where values can be read externally without the need to send a transaction&lt;/code&gt;. Still, contract storage is expensive compared to contract state memory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once the Smart contract is compiled, it'll generate &lt;code&gt;ABI&lt;/code&gt; and &lt;code&gt;Bytecode&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ethereum’s EVM has allowed the project to achieve its goal of “&lt;code&gt;decentralizing everything.&lt;/code&gt;”&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Alan Turing&lt;/em&gt; further defined a system to be Turing complete if it can be used to simulate any Turing machine. Such a system is called a Universal Turing machine (UTM).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ethereum’s ability to execute a stored program, in a state machine called the Ethereum Virtual Machine, while reading and writing data to memory makes it a Turing-complete system and therefore a UTM. Ethereum can compute any algorithm that can be computed by any Turing machine, given the limitations of finite memory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Turing proved that you cannot predict whether a program will terminate by simulating it on a computer&lt;/code&gt;. In simple terms, we cannot predict the path of a program without running it. Turing-complete systems can run in "&lt;code&gt;infinite loops&lt;/code&gt;," a term used (in oversimplification) to describe a program that does not terminate. It is trivial to create a program that runs a loop that never ends. But unintended never-ending loops can arise without warning, due to complex interactions between the starting conditions and the code. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To answer this challenge, Ethereum introduces a metering mechanism called &lt;code&gt;gas&lt;/code&gt;. As the EVM executes a smart contract, it carefully accounts for every instruction (computation, data access, etc.). Each instruction has a predetermined cost in units of gas. When a transaction triggers the execution of a smart contract, it must include an amount of gas that sets the upper limit of what can be consumed running the smart contract. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The EVM will terminate execution if the amount of gas consumed by computation exceeds the gas available in the transaction. Gas is the mechanism Ethereum uses to allow Turing-complete computation while limiting the resources that any program can consume.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The next question is, '&lt;em&gt;how does one get gas to pay for computation on the Ethereum world computer?&lt;/em&gt;' You won’t find gas on any exchanges.&lt;code&gt;It can only be purchased as part of a transaction and can only be bought with ether&lt;/code&gt;. Ether needs to be sent along with a transaction and it needs to be explicitly earmarked for the purchase of gas, along with an acceptable gas price. Just like at the pump, the price of gas is not fixed. &lt;code&gt;Gas is purchased for the transaction, the computation is executed, and any unused gas is refunded back to the sender of the transaction&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Ether denominations and unit names:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsp6zrfemstzyoupfukeq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsp6zrfemstzyoupfukeq.png" alt="Ether denomination" width="800" height="365"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Currently, there are &lt;code&gt;six&lt;/code&gt; main implementations of the Ethereum protocol, written in six different languages:

&lt;ul&gt;
&lt;li&gt;Parity, written in Rust&lt;/li&gt;
&lt;li&gt;Geth, written in Go&lt;/li&gt;
&lt;li&gt;cpp-ethereum, written in C++&lt;/li&gt;
&lt;li&gt;pyethereum, written in Python&lt;/li&gt;
&lt;li&gt;Mantis, written in Scala&lt;/li&gt;
&lt;li&gt;Harmony, written in Java&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Full Node Advantages and Disadvantages:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Choosing to run a full node helps with the operation of the networks you connect it to, but also incurs some mild to moderate costs for you. Let’s look at some of the advantages and disadvantages.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Advantages&lt;/strong&gt;:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Supports the resilience and censorship resistance of Ethereum-based networks&lt;/li&gt;
&lt;li&gt;Authoritatively validates all transactions&lt;/li&gt;
&lt;li&gt;Can interact with any contract on the public blockchain without an intermediary&lt;/li&gt;
&lt;li&gt;Can directly deploy contracts into the public blockchain without an intermediary&lt;/li&gt;
&lt;li&gt;Can query (read-only) the blockchain status (accounts, contracts, etc.) offline&lt;/li&gt;
&lt;li&gt;Can query the blockchain without letting a third party know the information you’re reading&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Disadvantages:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Requires significant and growing hardware and bandwidth resources&lt;/li&gt;
&lt;li&gt;May require several days to fully sync when first started&lt;/li&gt;
&lt;li&gt;Must be maintained, upgraded, and kept online to remain synced&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Types of Keys in Ethereum:
&lt;/h2&gt;

&lt;p&gt;There are 2 kind of keys present in Ethereum.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Private Key&lt;/li&gt;
&lt;li&gt;Public Key&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Private Keys:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;A private key is simply a number, picked at random. Ownership and control of the private key is the root of user control over all funds associated with the corresponding Ethereum address, as well as access to contracts that authorize that address. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;The private key is used to create signatures required to spend ether by proving ownership of funds used in a transaction&lt;/code&gt;. The private key must remain secret at all times, because revealing it to third parties is equivalent to giving them control over the ether and contracts secured by that private key. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The private key must also be backed up and protected from accidental loss. If it’s lost, it cannot be recovered, and the funds secured by it are lost forever too.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Ethereum private key is just a number. One way to pick your private keys randomly is to simply use a coin, pencil, and paper: &lt;code&gt;toss a coin 256 times and you have the binary digits of a random private key you can use in an Ethereum wallet (probably—see the next section)&lt;/code&gt;. &lt;code&gt;The public key and address can then be generated from the private key&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;Generating a Private Key from a Random Number:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;The first and most important step in generating keys is to find a secure source of entropy, or randomness&lt;/code&gt;. Creating an Ethereum private key essentially involves &lt;code&gt;picking a number between 1 and 2^256&lt;/code&gt;. The exact method you use to pick that number does not matter as long as it is not predictable or deterministic. Ethereum software uses the underlying operating system’s &lt;code&gt;random number generator to produce 256 random bits&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;More precisely, &lt;code&gt;a private key can be any nonzero number up to a very large number slightly less than 2^256—a huge 78-digit number, roughly 1.158 * 10^77&lt;/code&gt;. The exact number shares the &lt;code&gt;first 38 digits with 2256 and is defined as the order of the elliptic curve used in Ethereum&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;To create a private key, we randomly pick a &lt;code&gt;256-bit number&lt;/code&gt; and check that it is within the valid range. In programming terms, this is usually achieved by feeding an even&lt;code&gt;larger string of random bits&lt;/code&gt; (collected from a cryptographically secure source of randomness) &lt;code&gt;into a 256-bit hash algorithm such as Keccak-256 or SHA-256&lt;/code&gt;, both of which will conveniently produce a 256-bit number. If the result is within the valid range, we have a suitable private key. Otherwise, we simply try again with another random number.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;2^256—the size of Ethereum’s private key space—is an unfathomably large number&lt;/code&gt;. It is approximately &lt;code&gt;10^77 in decimal&lt;/code&gt;; that is, a number with &lt;code&gt;77 digits&lt;/code&gt;. For comparison, the visible universe is estimated to contain between 10^77 and 10^80 atoms. Therefore, at the lower range there are enough private keys to give every atom in the universe an Ethereum account. If you pick a private key randomly, there is no conceivable way anyone will ever guess it or pick it themselves.&lt;/li&gt;
&lt;li&gt;Note that &lt;code&gt;the private key generation process is an offline one; it does not require any communication with the Ethereum network, or indeed any communication with anyone at all&lt;/code&gt;. As such, in order to pick a number that no one else will ever pick, it needs to be truly random. If you choose the number yourself, the chance that someone else will try it (and then run off with your ether) is too high. &lt;/li&gt;
&lt;li&gt;The following is a randomly generated private key shown in hexadecimal format (&lt;em&gt;256 bits shown as 64 hexadecimal digits, each 4 bits&lt;/em&gt;): &lt;code&gt;f8f8a2f43c8376ccb0871305060d7b27b0554d2cc72bccf41b2705608452f315&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Public Keys:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;An &lt;code&gt;Ethereum public key is a point on an elliptic curve&lt;/code&gt;, meaning it is a set of x and y coordinates that satisfy the elliptic curve equation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In simpler terms, an Ethereum &lt;code&gt;public key is two numbers, joined together&lt;/code&gt;. These numbers are produced from the private key by a calculation that can only go one way. That means that it is trivial to calculate a public key if you have the private key, but you cannot calculate the private key from the public key.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;public key is calculated from the private key using elliptic curve multiplication, which is practically irreversible: K = k * G&lt;/code&gt;, where k is the private key, G is a constant point called the generator point, K is the resulting public key, and * is the special elliptic curve "multiplication" operator. Note that elliptic curve multiplication is not like normal multiplication. It shares functional attributes with normal multiplication, but that is about it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In simpler terms: arithmetic on the elliptic curve is different from "regular" integer arithmetic. A point (G) can be multiplied by an integer (k) to produce another point (K). But there is no such thing as division, so it is not possible to simply "divide" the public key K by the point G to calculate the private key k. This is the one-way mathematical function described in Public Key Cryptography and Cryptocurrency.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Elliptic curve multiplication is a type of function that cryptographers call a "one-way" function&lt;/code&gt;: it is easy to do in one direction (multiplication) and impossible to do in the reverse direction (division). The owner of the private key can easily create the public key and then share it with the world, knowing that no one can reverse the function and calculate the private key from the public key. This mathematical trick becomes the basis for unforgeable and secure digital signatures that prove ownership of Ethereum funds and control of contracts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Elliptic curve cryptography is a type of &lt;code&gt;asymmetric or public key cryptography&lt;/code&gt; based on the discrete logarithm problem as expressed by addition and multiplication on the points of an elliptic curve.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ethereum uses the exact same elliptic curve, called &lt;code&gt;secp256k1&lt;/code&gt;, as Bitcoin. That makes it possible to reuse many of the elliptic curve libraries and tools from Bitcoin.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Serialized EC public key prefixes:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmr3jgvdeawm3he6gjqn3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmr3jgvdeawm3he6gjqn3.png" alt="EC Public key" width="800" height="231"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ethereum only uses &lt;code&gt;uncompressed public keys&lt;/code&gt;; therefore, the only prefix that is relevant is &lt;code&gt;(hex) 04&lt;/code&gt;. The serialization concatenates the x and y coordinates of the public key:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;04 + x-coordinate (32 bytes/64 hex) + y-coordinate (32 bytes/64 hex)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Therefore, the public key we calculated earlier is serialized as:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;046e145ccef1033dea239875dd00dfb4fee6e3348b84985c92f103444683bae07b83b5c38e5e2b0 \ c8529d7fa3f64d46daa1ece2d9ac14cab9477d042c84c32ccd0&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A hash function is “&lt;code&gt;any function that can be used to map data of arbitrary size to data of fixed size&lt;/code&gt;.”&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A cryptographic hash function is a &lt;code&gt;one-way hash function&lt;/code&gt; that &lt;code&gt;maps data of arbitrary size to a fixed-size string&lt;/code&gt; of bits. The "one-way" nature means that it is computationally infeasible to recreate the input data if one only knows the output hash. The only way to determine a possible input is to conduct a brute-force search, checking each candidate for a matching output; given that the search space is virtually infinite, it is easy to understand the practical impossibility of the task. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Let’s take a closer look at the main properties of cryptographic hash functions. These include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Determinism&lt;/strong&gt;: A given input message always produces the same hash output.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Verifiability&lt;/strong&gt;: Computing the hash of a message is efficient (linear complexity).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Noncorrelation&lt;/strong&gt;: A small change to the message (e.g., a 1-bit change) should change the hash output so extensively that it cannot be correlated to the hash of the original message.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Irreversibility&lt;/strong&gt;: Computing the message from its hash is infeasible, equivalent to a brute-force search through all possible messages.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Collision protection&lt;/strong&gt;: It should be infeasible to calculate two different messages that produce the same hash output.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Resistance to hash collisions is particularly important for avoiding digital signature forgery in Ethereum.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;The combination of these properties makes cryptographic hash functions useful for a broad range of security applications, including:&lt;br&gt;
• Data fingerprinting&lt;/p&gt;

&lt;p&gt;• Message integrity (error detection)&lt;/p&gt;

&lt;p&gt;• Proof of work&lt;/p&gt;

&lt;p&gt;• Authentication (password hashing and key stretching)&lt;/p&gt;

&lt;p&gt;• Pseudorandom number generators&lt;/p&gt;

&lt;p&gt;• Message commitment (commit–reveal mechanisms)&lt;/p&gt;

&lt;p&gt;• Unique identifiers&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ethereum uses the &lt;code&gt;Keccak-256&lt;/code&gt; cryptographic hash function in many places. Keccak-256 was designed as a candidate for the &lt;code&gt;SHA-3&lt;/code&gt; Cryptographic Hash Function Competition held in 2007 by the National Institute of Standards and Technology. Keccak was the winning algorithm, which became standardized as Federal Information Processing Standard (FIPS) 202 in 2015.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Ethereum Addresses&lt;/em&gt;: Ethereum addresses are unique identifiers that are derived from public keys or contracts using the &lt;code&gt;Keccak-256 one-way hash function&lt;/code&gt;. Ethereum addresses are &lt;code&gt;hexadecimal numbers&lt;/code&gt;, identifiers derived from the &lt;code&gt;last 20 bytes of the Keccak-256&lt;/code&gt; hash of the public key.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Wallets:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The word "wallet" is used to describe a few different things in Ethereum.&lt;/li&gt;
&lt;li&gt;At a high level, a wallet is a software application that &lt;code&gt;serves as the primary user interface to Ethereum&lt;/code&gt;. The wallet controls access to a user’s money, managing keys and addresses, tracking the balance, and creating and signing transactions. In addition, some &lt;code&gt;Ethereum wallets can also interact with contracts, such as ERC20 tokens&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The word wallet refers to the system used to s&lt;code&gt;tore and manage a user’s keys&lt;/code&gt;. Every wallet has a key-management component. For some wallets, that’s all there is. Other wallets are part of a much broader category, that of browsers, which are interfaces to Ethereum-based decentralized applications, or DApps. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Ethereum wallets contain keys, not ether or tokens&lt;/code&gt;. Wallets are like keychains containing &lt;code&gt;pairs of private and public keys&lt;/code&gt;. Users sign transactions with the private keys, thereby proving they own the ether. The ether is stored on the blockchain.&lt;/li&gt;
&lt;li&gt;There are two primary types of wallets, distinguished by whether the keys they contain are related to each other or not.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;The first type&lt;/em&gt; is a &lt;code&gt;Nondeterministic wallet&lt;/code&gt;, where each key is independently generated from a &lt;code&gt;different random number&lt;/code&gt;. The keys are &lt;code&gt;not related to each other&lt;/code&gt;. This type of wallet is also known as a &lt;code&gt;JBOK wallet&lt;/code&gt;, from the phrase "&lt;code&gt;Just a Bunch of Keys&lt;/code&gt;.”&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;second type&lt;/em&gt; of wallet is a &lt;code&gt;Deterministic wallet&lt;/code&gt;, where all the keys are &lt;code&gt;derived from a single master key,&lt;/code&gt; known as the &lt;code&gt;seed&lt;/code&gt;. All the keys in this type of wallet are related to each other and can be generated again if one has the original seed. There are a number of different key derivation methods used in deterministic wallets. The most commonly used derivation method uses a tree-like structure, as described in &lt;code&gt;Hierarchical Deterministic Wallets (BIP-32/BIP-44).&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;To make deterministic wallets slightly more secure against data-loss accidents, such as having your phone stolen or dropping it in the toilet, the seeds are often encoded as a list of words (in English or another language) for you to write down and use in the event of an accident. &lt;code&gt;These are known as the wallet’s mnemonic code words.&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Deterministic or "seeded" wallets are wallets that contain private keys that are all derived from a single master key, or seed. The seed is a randomly generated number that is combined with other data, such as an index number or "chain code" (see Extended public and private keys), to derive any number of private keys. &lt;/li&gt;
&lt;li&gt;In a deterministic wallet, &lt;code&gt;the seed is sufficient to recover all the derived keys&lt;/code&gt;, and therefore a single backup, at creation time, is sufficient to &lt;code&gt;secure all the funds and smart contracts in the wallet&lt;/code&gt;. The seed is also sufficient for a wallet export or import, allowing for easy migration of all the keys between different wallet implementations.&lt;/li&gt;
&lt;li&gt;Deterministic wallets were developed to make it easy to derive many keys from a single seed. Currently, the most advanced form of deterministic wallet is the &lt;code&gt;hierarchical deterministic (HD) wallet defined by Bitcoin’s BIP-32 standard.&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;HD wallets contain keys derived in a tree structure, such that a parent key can derive a sequence of child keys, each of which can derive a sequence of grandchild keys, and so on. This tree structure is illustrated in HD wallet: a tree of keys generated from a single seed.&lt;/li&gt;
&lt;li&gt;HD wallets offer a few key advantages over simpler deterministic wallets. &lt;em&gt;First&lt;/em&gt;, the tree structure can be used to express additional organizational meaning, such as &lt;code&gt;when a specific branch of subkeys is used to receive incoming payments&lt;/code&gt; and a &lt;code&gt;different branch is used to receive change from outgoing payments&lt;/code&gt;. Branches of keys can also be used in corporate settings, allocating different branches to departments, subsidiaries, specific functions, or accounting categories.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;second advantage&lt;/em&gt; of HD wallets is that users can &lt;code&gt;create a sequence of public keys without having access to the corresponding private keys&lt;/code&gt;. This allows HD wallets to be used on an insecure server or &lt;code&gt;in a watch-only or receive-only capacity&lt;/code&gt;, where the wallet doesn’t have the private keys that can spend the funds.&lt;/li&gt;
&lt;li&gt;There are many ways to encode a &lt;code&gt;private key for secure backup and retrieval&lt;/code&gt;. The currently preferred method is using a &lt;code&gt;sequence of words&lt;/code&gt; that, when taken together in the correct order, can uniquely recreate the private key. This is sometimes known as a &lt;code&gt;mnemonic&lt;/code&gt;, and the approach has been standardized by &lt;code&gt;BIP-39&lt;/code&gt;. Today, many Ethereum wallets (as well as wallets for other cryptocurrencies) use this standard and can import and export seeds for backup and recovery using interoperable mnemonics.&lt;/li&gt;
&lt;li&gt;Some examples of software wallets supporting these standards include (listed alphabetically) &lt;code&gt;Jaxx, MetaMask, MyCrypto, and MyEtherWallet (MEW)&lt;/code&gt;. Examples of hardware wallets supporting these standards include &lt;code&gt;Keepkey, Ledger, and Trezor.&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Generating Mnemonic words 128 bit / 12 words:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk605raqbo8g8lt46p0gw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk605raqbo8g8lt46p0gw.png" alt="Mnemonic words" width="800" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Transactions are signed messages originated by an &lt;code&gt;externally owned account (EOA),&lt;/code&gt; transmitted by the Ethereum network, and recorded on the Ethereum blockchain. &lt;/li&gt;
&lt;li&gt;This basic definition conceals a lot of surprising and fascinating details. &lt;/li&gt;
&lt;li&gt;Another way to look at transactions is that they are the only things that can trigger a change of state or cause a contract to execute in the EVM. &lt;/li&gt;
&lt;li&gt;Ethereum is a global singleton state machine, and transactions are what make that state machine "tick," changing its state. Contracts don’t run on their own. Ethereum doesn’t run autonomously. Everything starts with a transaction.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  The Structure of a Transaction:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;First let’s take a look at the basic structure of a transaction, as it is serialized and transmitted on the Ethereum network. Each client and application that receives a serialized transaction will store it in-memory using its own internal data structure, perhaps embellished with metadata that doesn’t exist in the network serialized transaction itself. The network-serialization is the only standard form of a transaction.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;A transaction is a serialized binary message that contains the following data:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Nonce&lt;/strong&gt;: A sequence number, issued by the originating EOA, used to prevent message replay&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Gas price&lt;/strong&gt;: The amount of ether (in wei) that the originator is willing to pay for each unit of gas&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Gas limit&lt;/strong&gt;: The maximum amount of gas the originator is willing to buy for this transaction&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Recipient&lt;/strong&gt;: The destination Ethereum address&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Value&lt;/strong&gt;: The amount of ether (in wei) to send to the destination.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data&lt;/strong&gt;: The variable-length binary data payload&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;v,r,s&lt;/strong&gt;: The three components of an ECDSA digital signature of the originating EOA&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Chain identifiers:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4xr0a60fcsz1ag4rg79w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4xr0a60fcsz1ag4rg79w.png" alt="Chain identifiers" width="800" height="670"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Oracles:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Oracles are systems that can provide &lt;code&gt;external data sources to Ethereum smart contracts&lt;/code&gt;. The term "oracle" comes from Greek mythology, where it referred to &lt;code&gt;a person in communication with the gods who could see visions of the future&lt;/code&gt;. In the context of blockchains, an oracle is a system that can answer questions that are external to Ethereum. Ideally oracles are systems that are &lt;code&gt;trustless&lt;/code&gt;, meaning that they do not need to be trusted because they operate on decentralized principles.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Oracles, ideally, provide a trustless (or at least near-trustless) way of getting extrinsic (i.e., "real-world" or off-chain) information&lt;/code&gt;, such as the results of football games, the price of gold, or truly random numbers, onto the Ethereum platform for smart contracts to use. They can also be used to relay data securely to DApp frontends directly. Oracles can therefore be thought of as a mechanism for bridging the gap between the off-chain world and smart contracts. Allowing smart contracts to enforce contractual relationships based on real-world events and data broadens their scope dramatically.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Some more examples of data that might be provided by oracles include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Random numbers/entropy&lt;/code&gt; from physical sources such as quantum/thermal processes: e.g., to fairly select a winner in a lottery smart contract&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Parametric triggers indexed&lt;/code&gt; to natural hazards: e.g., triggering of catastrophe bond smart contracts, such as Richter scale measurements for an earthquake bond&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Exchange rate data&lt;/code&gt;: e.g., for accurate pegging of cryptocurrencies to fiat currency&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Capital markets data&lt;/code&gt;: e.g., pricing baskets of tokenized assets/securities&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Benchmark reference data&lt;/code&gt;: e.g., incorporating interest rates into smart financial derivatives&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Static/pseudostatic data&lt;/code&gt;: security identifiers, country codes, currency codes, etc.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Time and interval data:&lt;/code&gt; for event triggers grounded in precise time measurements&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Weather data&lt;/code&gt;: e.g., insurance premium calculations based on weather forecasts&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Political events&lt;/code&gt;: for prediction market resolution&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Sporting events&lt;/code&gt;: for prediction market resolution and fantasy sports contracts&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Geolocation data&lt;/code&gt;: e.g., as used in supply chain tracking&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Damage verification&lt;/code&gt;: for insurance contracts&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Events occurring on other blockchains&lt;/code&gt;: interoperability functions&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Ether market price&lt;/code&gt;: e.g., for fiat gas price oracles&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Flight statistics&lt;/code&gt;: e.g., as used by groups and clubs for flight ticket pooling&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;All oracles provide a few key functions, by definition. These include the ability to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Collect data from an off-chain source.&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Transfer the data on-chain with a signed message&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Make the data available by putting it in a smart contract’s storage&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once the data is available in a smart contract’s storage, it can be &lt;code&gt;accessed by other smart contracts via message calls that invoke a "retrieve" function of the oracle’s smart contract&lt;/code&gt;; it can also be accessed by Ethereum nodes or network-enabled clients directly by "looking into” the &lt;code&gt;oracle’s storage&lt;/code&gt;. The three main ways to set up an oracle can be categorized as &lt;code&gt;request–response, publish-subscribe, and immediate-read.&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The steps for a request–response oracle may be summarized as follows:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Receive a query from a DApp.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Parse the query.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Check that payment and data access permissions are provided.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Retrieve relevant data from an off-chain source (and encrypt it if necessary).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sign the transaction(s) with the data included.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Broadcast the transaction(s) to the network.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Schedule any further necessary transactions, such as notifications, etc.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A range of other schemes are also possible; for example, &lt;code&gt;data can be requested from and returned directly by an EOA&lt;/code&gt;, removing the need for an oracle smart contract. Similarly, the request and response could be made to and from an Internet of Things–enabled hardware sensor. Therefore, oracles can be human, software, or hardware.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;request–response pattern described here is commonly seen in client–server architectures&lt;/code&gt;. While this is a useful messaging pattern that allows applications to have a two-way conversation, it is perhaps inappropriate under certain conditions. For example, a smart bond requiring an interest rate from an oracle might have to request the data on a daily basis under a request–response pattern in order to ensure the rate is always correct. Given that interest rates change infrequently, a publish–subscribe pattern may be more appropriate here—especially when taking into consideration Ethereum’s limited bandwidth.&lt;/li&gt;
&lt;li&gt;Publish–subscribe is a pattern where publishers (in this context, oracles) do not send messages directly to receivers, but instead categorize published messages into distinct classes. Subscribers are able to express an interest in one or more classes and retrieve only those messages that are of interest. Under such a pattern, an oracle might write the interest rate to its own internal storage each time it changes. Multiple subscribed DApps can simply read it from the oracle contract, thereby reducing the impact on network bandwidth while minimizing storage costs.&lt;/li&gt;
&lt;li&gt;ChainLink has proposed a decentralized oracle network consisting of &lt;code&gt;three key smart contracts—a reputation contract&lt;/code&gt;, an order-matching contract, and an aggregation contract—and an off-chain registry of data providers. &lt;/li&gt;
&lt;li&gt;The reputation contract is used to keep track of data providers' performance. Scores in the reputation contract are used to populate the off-chain registry. The order-matching contract selects bids from oracles using the reputation contract. It then finalizes a service-level agreement, which includes query parameters and the number of oracles required.&lt;/li&gt;
&lt;li&gt; This means that the purchaser needn’t transact with the individual oracles directly. The aggregation contract collects responses (submitted using a commit–reveal scheme) from multiple oracles, calculates the final collective result of the query, and finally feeds the results back into the reputation contract.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; // query params are (delay in seconds, datasource type,
 // datasource argument)
 // specifies JSONPath, to fetch specific portion of JSON API result
   oraclize_query(60 * 10, "URL", 
"json(https://min-api.cryptocompare.com/data/price?\
      fsym=ETH&amp;amp;tsyms=USD,EUR,GBP).USD");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  What Is a DApp?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;A DApp is an application that is mostly or entirely decentralized. Consider all the possible aspects of an application that may be decentralized:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Backend software (application logic)&lt;/li&gt;
&lt;li&gt;Frontend software&lt;/li&gt;
&lt;li&gt;Data storage&lt;/li&gt;
&lt;li&gt;Message communications&lt;/li&gt;
&lt;li&gt;Name resolution&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Each of these can be somewhat centralized or somewhat decentralized. For example, a &lt;code&gt;frontend can be developed as a web app that runs on a centralized server&lt;/code&gt;, or as a &lt;code&gt;mobile app that runs on your device&lt;/code&gt;. The &lt;code&gt;backend and storage can be on private servers and proprietary databases&lt;/code&gt;, or you can use a smart contract and P2P storage.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;There are many advantages to creating a DApp that a typical centralized architecture cannot provide:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Resiliency&lt;/strong&gt;: Because the business logic is controlled by a smart contract, a DApp backend will be fully distributed and managed on a blockchain platform. Unlike an application deployed on a centralized server, a DApp will have no downtime and will continue to be available as long as the platform is still operating.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transparency&lt;/strong&gt;: The on-chain nature of a DApp allows everyone to inspect the code and be more sure about its function. Any interaction with the DApp will be stored forever in the blockchain.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Censorship resistance&lt;/strong&gt;: As long as a user has access to an Ethereum node (running one if necessary), the user will always be able to interact with a DApp without interference from any centralized control. No service provider, or even the owner of the smart contract, can alter the code once it is deployed on the network.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;




</description>
      <category>ethereum</category>
      <category>blockchain</category>
      <category>web3</category>
      <category>cryptocurrency</category>
    </item>
    <item>
      <title>Mastering ₿itcoin Notes</title>
      <dc:creator>SK Sabiruddin</dc:creator>
      <pubDate>Sat, 03 Feb 2024 17:59:22 +0000</pubDate>
      <link>https://dev.to/sksabir/mastering-bitcoin-book-notes-1bcp</link>
      <guid>https://dev.to/sksabir/mastering-bitcoin-book-notes-1bcp</guid>
      <description>&lt;h3&gt;
  
  
  &lt;strong&gt;Bitcoin&lt;/strong&gt;:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;The name of the currency unit (the coin), the network and the software. Bitcoin is a collection of concepts and technologies that form the basis of a digital money ecosystem. Units of currency called bitcoins are used to store and transmit value among participants in the bitcoin network. &lt;/p&gt;

&lt;p&gt;Bitcoin users communicate with each other using the bitcoin protocol primarily via the Internet, although other transport networks can also be used. The bitcoin protocol stack, available as open-source software, can be run on a wide range of computing devices, including laptops and smartphones, making the technology easily accessible.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Address (aka public key):
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;A bitcoin address looks like &lt;code&gt;1DSrfJdB2AnWaFNgSbv3MZC2m74996JafV&lt;/code&gt; - they consist of a string of letters and numbers starting with a &lt;code&gt;“1” (number one)&lt;/code&gt;. Just like you ask others to send an email to your email address, you would ask others to send you bitcoin to your bitcoin address.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  BIP:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Bitcoin Improvement Proposals. A set of proposals that members of the bitcoin community have submitted to improve bitcoin. For &lt;code&gt;example BIP0021&lt;/code&gt; is a proposal to improve the bitcoin URI scheme.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Block:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;A grouping of &lt;code&gt;transactions, marked with a timestamp, and a fingerprint of the previous block&lt;/code&gt;. The block header is hashed to find a proof-of-work, thereby validating the transactions. Valid blocks are added to the main blockchain by network consensus.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Blockchain:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;A list of validated blocks, each linking to its predecessor all the way to the &lt;code&gt;genesis block&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Confirmations:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Once a transaction is included in a block, it has &lt;code&gt;one confirmation&lt;/code&gt;. As soon as another block is mined on the same blockchain, the transaction has two confirmations etc. &lt;code&gt;Six or more confirmations&lt;/code&gt; is considered sufficient proof that a transaction cannot be reversed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Difficulty:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;A network-wide setting that controls how much computation is required to find a proof-of-work.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Difficulty target:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;A difficulty at which all the computation in the network will find blocks approximately &lt;code&gt;every 10 minutes&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Difficulty re-targeting:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;A network-wide re-calculation of the difficulty which occurs once &lt;code&gt;every 2106 blocks&lt;/code&gt; and considers the hashing power of the previous 2106 blocks.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Fees:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;The sender of a transaction often includes a fee to the network for processing their requested transaction. Most transactions require a minimum fee of &lt;code&gt;0.5mBTC&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Hash:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;A digital fingerprint of some binary input.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Genesis block:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;first block&lt;/code&gt; in the blockchain, used to initialize the crypto-currency.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Miner:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;A network node that finds &lt;code&gt;valid proof-of-work&lt;/code&gt; for new blocks, by repeated hashing.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Network:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;A peer-to-peer network that &lt;code&gt;propagates transactions and blocks&lt;/code&gt; to every bitcoin node on the network.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Proof-of-work:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;A piece of data that requires significant computation to find. In bitcoin, miners must find a numeric solution to the SHA256 algorithm that meets a network wide target, the difficulty target.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Reward:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;An amount included in each new block as a reward by the network to the miner who found the proof-of-work solution. It is currently &lt;code&gt;6.25BTC&lt;/code&gt; per block.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Secret key (aka private key):
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;The secret number that &lt;code&gt;unlocks bitcoins&lt;/code&gt; sent to the corresponding address. A secret key looks like &lt;code&gt;5J76sF8L5jTtzE96r66Sf8cka9y44wdpJjMwCxR3tzLh3ibVPxh&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Transaction:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;In simple terms, a &lt;code&gt;transfer of bitcoins from one address to another&lt;/code&gt;. More precisely, a transaction is a signed data structure expressing a transfer of value. &lt;code&gt;Transactions are transmitted over the bitcoin network, collected by miners and included into blocks, made permanent on the blockchain&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Wallet:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Software that holds all your bitcoin addresses and secret keys. Use it to send, receive and store your bitcoin.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Transaction Locktime:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Locktime defines the earliest time that a transaction can be added to the blockchain. It is set to &lt;code&gt;zero&lt;/code&gt; in most transactions to indicate &lt;code&gt;immediate execution&lt;/code&gt;. If locktime is &lt;code&gt;nonzero&lt;/code&gt; and &lt;code&gt;below 500 million&lt;/code&gt;, it is interpreted as a &lt;code&gt;block height&lt;/code&gt;, meaning the transaction is &lt;code&gt;not included in the blockchain prior to the specified block height&lt;/code&gt;. If it is above 500 million, it is interpreted as a &lt;code&gt;Unix Epoch timestamp (seconds since Jan-1-1970)&lt;/code&gt; and the&lt;br&gt;
&lt;code&gt;transaction is not included in the blockchain prior to the specified time&lt;/code&gt;. The use of &lt;code&gt;locktime is equivalent to post-dating a paper cheque.&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Transaction Outputs and Inputs : (UTXO)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The fundamental building block of a bitcoin transaction is an &lt;code&gt;unspent transaction output or UTXO&lt;/code&gt;. &lt;code&gt;UTXO are indivisible chunks of bitcoin currency locked to a specific owner&lt;/code&gt;, recorded on the blockchain, and recognized as currency units by the entire network. &lt;/li&gt;
&lt;li&gt;The bitcoin network tracks all available (unspent) UTXO currently numbering in the millions. &lt;/li&gt;
&lt;li&gt;Whenever a user receives bitcoin, that amount is recorded within the blockchain as a UTXO. Thus, a user’s bitcoin may be &lt;code&gt;scattered as UTXO amongst hundreds of transactions and hundreds of blocks&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In effect, there is no such thing as a &lt;code&gt;stored balance of a bitcoin address or account&lt;/code&gt;; there &lt;code&gt;are only scattered UTXO&lt;/code&gt;, locked to specific owners. &lt;/li&gt;
&lt;li&gt;The concept of a user’s bitcoin balance is a derived construct created by the wallet application. The wallet calculates the user’s balance. &lt;/li&gt;
&lt;li&gt;There are no accounts or balances in bitcoin, there are only unspent transaction outputs (UTXO) scattered in the blockchain.&lt;/li&gt;
&lt;li&gt;UTXO are &lt;code&gt;tracked by every full node bitcoin client&lt;/code&gt; in a database held in memory, &lt;code&gt;called the UTXO set or UTXO pool&lt;/code&gt;. New transactions consume (spend) one or more of these outputs from the UTXO set. Transaction outputs consist of two parts:

&lt;ul&gt;
&lt;li&gt;An amount of bitcoin, denominated in &lt;code&gt;Satoshi&lt;/code&gt;, the smallest bitcoin unit.&lt;/li&gt;
&lt;li&gt;A locking script, also known as an &lt;code&gt;encumbrance&lt;/code&gt; that &lt;code&gt;locks&lt;/code&gt; this amount by specifying the conditions that must be met to spend the output.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;if you consume a &lt;code&gt;20 bitcoin UTXO&lt;/code&gt; to make a &lt;code&gt;1 bitcoin&lt;/code&gt; payment, you must &lt;code&gt;include a 19 bitcoin change output back to your wallet&lt;/code&gt;. Otherwise, the &lt;code&gt;19 bitcoin “leftover” will be counted as a transaction fee&lt;/code&gt; and will be &lt;code&gt;collected by the miner&lt;/code&gt; who mines your transaction in a block. While you will receive priority processing and make a miner very happy, this is probably not what you intended.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Transaction Pools:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Almost every node on the bitcoin network maintains a &lt;code&gt;temporary list of unconfirmed transactions called the memory pool or transaction pool&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;Nodes use this pool to keep track of transactions that are known to the network but are not yet included in the blockchain. For example, a node that holds a user’s wallet will use the transaction pool to track incoming payments to the user’s wallet that have been received on the network but are not yet confirmed. &lt;/li&gt;
&lt;li&gt;As transactions are received and verified, they are added to the transaction pool and relayed to the neighboring nodes to propagate on the network.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  About Bitcoin:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Bitcoin is a fully distributed, peer-to-peer system. As such there is no “central” server or point of control. Bitcoins are created through a process called “mining”, which involves looking for a solution to a difficult problem. &lt;/li&gt;
&lt;li&gt;Any participant in the bitcoin network (i.e., any device running the full bitcoin protocol stack) may operate as a miner, 1 using their computer’s processing power to attempt to find solutions to this problem.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Every 10 minutes on average&lt;/code&gt;, a new solution is found by someone who then is able to validate the transactions of the past 10 minutes and is rewarded with brand new bitcoins.&lt;/li&gt;
&lt;li&gt;The bitcoin protocol includes built-in algorithms that regulate the mining function across the network. The difficulty of the problem that miners must solve is adjusted dynamically so that, on average, someone finds a correct answer every 10 minutes regardless of how many miners (and CPUs) are working on the problem at any moment. &lt;/li&gt;
&lt;li&gt;The protocol also halves the rate at which new bitcoins are created &lt;code&gt;every 4 years&lt;/code&gt; and limits the total number of bitcoins that will be created to a fixed total of &lt;code&gt;21 million&lt;/code&gt; coins. &lt;/li&gt;
&lt;li&gt;The result is that the number of bitcoins in circulation closely follows an easily predictable curve that reaches 21 million by the &lt;code&gt;year 2140&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Bitcoin represents the culmination of decades of research in cryptography and distributed systems and includes four key innovations brought together in a unique and powerful combination. Bitcoin consists of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;A de-centralized peer-to-peer network&lt;/code&gt; (the bitcoin protocol);&lt;/li&gt;
&lt;li&gt; &lt;code&gt;A public transaction ledger&lt;/code&gt; (the blockchain);&lt;/li&gt;
&lt;li&gt; &lt;code&gt;A de-centralized mathematical and deterministic currency issuance distributed mining&lt;/code&gt;, and.&lt;/li&gt;
&lt;li&gt; &lt;code&gt;A de-centralized transaction verification system&lt;/code&gt; (transaction script).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Byzantine Generals’ Problem&lt;/em&gt;&lt;/strong&gt;: Briefly, the problem consists of trying to agree on a course of action by exchanging information over an unreliable and potentially compromised network.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;The three primary forms of bitcoin clients are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Full Client&lt;/em&gt;&lt;/strong&gt;:  A full client, or “full node” is a client that stores the entire history of bitcoin transactions (every transaction by every user, ever), manages the user’s wallets and can initiate transactions directly on the bitcoin network. This is similar to a standalone email server; in that it handles all aspects of the protocol without relying on any other servers or third-party services.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Light Client&lt;/em&gt;&lt;/strong&gt;: A lightweight client stores the user’s wallet but relies on third-party owned servers for access to the bitcoin transactions and network. The light client does not store a full copy of all transactions and therefore must trust the third-party servers for transaction validation. This is similar to a standalone email client that connects to a mail server for access to a mailbox, in that it relies on a third party for interactions with the network.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Web Client&lt;/em&gt;&lt;/strong&gt;: Web-clients are accessed through a web browser and store the user’s wallet on a server owned by a third party. This is similar to webmail in that it relies entirely on a third-party server.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;The bitcoin system of trust is based on computation. Transactions are bundled into blocks, which require an enormous amount of computation to prove, but only a small amount of computation to verify as proven. This process is called mining and serves two purposes in bitcoin:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mining creates new bitcoins in each block, almost like a central bank printing new money. The amount of bitcoin created per block is fixed and diminishes with time.&lt;/li&gt;
&lt;li&gt;Mining creates trust by ensuring that transactions are only confirmed if enough computational power was devoted to the block that contains them. More blocks mean more computation which means more trust.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The algorithm for “Proof-of-Work” involves repeatedly &lt;code&gt;hashing the header of the block&lt;/code&gt; and a &lt;code&gt;random number with the SHA256 cryptographic algorithm&lt;/code&gt; until a &lt;code&gt;solution matching a pre-determined pattern emerges&lt;/code&gt;. The first miner to find such a solution wins the round of competition and publishes that block into the blockchain.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A transaction transmitted across the network is not verified until it becomes part of the global distributed ledger, the blockchain. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Every &lt;code&gt;10 minutes&lt;/code&gt; on average, &lt;code&gt;miners generate a new block&lt;/code&gt; that contains all the transactions since the last block. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;New transactions are constantly flowing into the network from user wallets and other applications. As these are seen by the bitcoin network nodes, they get added to a temporary &lt;code&gt;pool&lt;/code&gt; of unverified transactions maintained by each node. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;As miners build a new block, they &lt;code&gt;add unverified transactions from this pool to a new block&lt;/code&gt; and then &lt;code&gt;attempt to solve a very hard problem&lt;/code&gt; (aka Proof-of-Work) to prove the validity of that new block.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Each miner starts the process of mining a new block of transactions as soon as they receive the previous block from the network, knowing they have lost that previous round of competition. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;They immediately create a new block, fill it with &lt;code&gt;transactions and the fingerprint of the previous block&lt;/code&gt; and start calculating the Proof-of-Work for the new block. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Each miner includes a &lt;code&gt;special transaction in their block&lt;/code&gt;, one that &lt;code&gt;pays their own bitcoin address a reward of newly created bitcoins&lt;/code&gt; (currently 6.25 BTC per block). &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If they find a solution that makes that block valid, they &lt;code&gt;win&lt;/code&gt; this reward because their successful block is added to the global blockchain and the reward transaction they included becomes spendable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In most wallet implementations, the private and public keys are stored together as a key pair for convenience. However, the public key can be calculated from the private key, so storing only the private key is also possible.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In most wallet implementations, the &lt;code&gt;private and public keys&lt;/code&gt; are &lt;code&gt;stored together as a key pair&lt;/code&gt; for convenience. However, the &lt;code&gt;public key can be calculated from the private key&lt;/code&gt;, so storing only the private key is also possible.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A bitcoin wallet contains a &lt;code&gt;collection of key pairs&lt;/code&gt;, each consisting of a &lt;code&gt;private key and a public key&lt;/code&gt;. The &lt;code&gt;private key (k) is a number&lt;/code&gt;, usually &lt;code&gt;picked at random&lt;/code&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;From the private key, we use &lt;code&gt;elliptic curve multiplication&lt;/code&gt;, a &lt;code&gt;one-way cryptographic function&lt;/code&gt;, to &lt;code&gt;generate a public key (K)&lt;/code&gt;. From the &lt;code&gt;public key (K)&lt;/code&gt;, we use a &lt;code&gt;one-way cryptographic hash function to generate a bitcoin address (A)&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A private key is simply a number, picked at random. Ownership and control over the private key is the root of user control over all funds associated with the corresponding bitcoin address. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;private key is used to create signatures&lt;/code&gt; that are required to &lt;code&gt;spend bitcoins by proving ownership of funds&lt;/code&gt; used in a transaction.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Algorithm used: &lt;code&gt;Secure Hash Algorithm (SHA)&lt;/code&gt; and the &lt;code&gt;RACE Integrity Primitives Evaluation Message Digest (RIPEMD)&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Starting with the &lt;code&gt;public key K&lt;/code&gt;, we &lt;code&gt;compute the SHA256 hash&lt;/code&gt; and &lt;code&gt;then compute the RIPEMD160 hash of the result&lt;/code&gt;, &lt;code&gt;producing a 160-bit (20 byte) number&lt;/code&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;A = RIPEMD160(SHA256(K))&lt;/em&gt; // bitcoin address&lt;br&gt;
where&lt;code&gt;K is the public key and A is the resulting bitcoin address&lt;/code&gt;. eg : &lt;code&gt;1DSrfJdB2AnWaFNgSbv3MZC2m74996JafV&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Bitcoin addresses are almost always presented to users in an encoding called &lt;code&gt;Base58Check&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Generating Bitcoin address from private key:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fezujl9avdrpab3w095ga.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fezujl9avdrpab3w095ga.png" alt="How Bitcoin address is generated" width="800" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Even more compact, &lt;code&gt;Base-64&lt;/code&gt; representation uses &lt;code&gt;26 lower case letters, 26 capital letters, 10 numerals and 2 more characters such as “+” and “/”&lt;/code&gt;to transmit binary data over text-based media such as email. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Base-64&lt;/code&gt; is most commonly used to add binary attachments to email. &lt;code&gt;Base-58&lt;/code&gt; is a text-based binary encoding format developed for use in bitcoin and used in many other crypto-currencies. &lt;/li&gt;
&lt;li&gt;It offers a balance between compact representation, readability and error detection and prevention. &lt;code&gt;Base-58 is a subset of Base-64&lt;/code&gt;, using the &lt;code&gt;uppercase and lowercase letters and numbers but omitting some characters that are frequently mistaken&lt;/code&gt; for one another and can appear identical when displayed in certain fonts. Specifically, Base-58 is Base-64 without the 0 (number zero), O (capital o), l (lower L), I (capital i) and the symbols “\ +” and “/”. Or, more simply, it is a set of lower and capital letters and numbers without the &lt;code&gt;four (0, O, l, I)&lt;/code&gt;mentioned above. Bitcoin’s &lt;code&gt;Base-58&lt;/code&gt; Alphabet: &lt;code&gt;123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Base58Check Encoding:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm03c331xe1w5vqjlid3q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm03c331xe1w5vqjlid3q.png" alt="Base58Check Encoding to get bitcoin address" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Base58Check Version Prefix and Encoded Result Examples:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31styf704zo0vzr8gmto.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31styf704zo0vzr8gmto.png" alt="Base58Check example" width="800" height="244"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Wallets contain keys, not coins&lt;/code&gt;. &lt;code&gt;The coins are stored on the blockchain in the form of transaction-outputs&lt;/code&gt; (often noted as vout or txout). &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Each user has a wallet containing keys&lt;/code&gt;. &lt;code&gt;Wallets are really keychains containing pairs of private/public keys&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Users &lt;code&gt;sign transactions with the keys&lt;/code&gt;, thereby proving they own the transaction outputs (their coins).&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Hierarchical Deterministic Wallets (BIP0032/BIP0044):
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Deterministic wallets were developed to make it easy to derive many keys from a single &lt;code&gt;seed&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;The most advanced form of deterministic wallets is the &lt;code&gt;_Hierarchical Deterministic Wallet or HD Wallet_&lt;/code&gt; defined by the &lt;code&gt;BIP0032&lt;/code&gt; standard. &lt;/li&gt;
&lt;li&gt;Hierarchical deterministic wallets contain &lt;code&gt;keys derived in a tree structure&lt;/code&gt;, such that a &lt;code&gt;parent key can derive a sequence of children keys&lt;/code&gt;, each of which can &lt;code&gt;derive a sequence of grandchildren keys and so on to an infinite depth.&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;HD wallets offer two major advantages over random (non-deterministic) keys. 

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;First&lt;/em&gt;, the tree structure can be used to express additional organizational meaning, such as when a specific branch of &lt;code&gt;sub-keys is used to receive incoming payments&lt;/code&gt; and a &lt;code&gt;different branch is used to receive change from outgoing payments&lt;/code&gt;. Branches of keys can also be used in a corporate setting, allocating different branches to departments, subsidiaries, specific functions or accounting categories.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;The second&lt;/em&gt; advantage of HD wallets is that users can &lt;code&gt;create a sequence of public keys&lt;/code&gt; without having &lt;code&gt;access to the corresponding private keys&lt;/code&gt;. This allows HD wallets &lt;code&gt;to be used on an insecure server or in a receive-only capacity&lt;/code&gt;, issuing &lt;code&gt;a different public key for each transaction&lt;/code&gt;. The &lt;code&gt;public keys do not need to be pre-loaded or derived&lt;/code&gt; in advance, yet the server doesn’t have the private keys that can spend the funds.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;HD wallets are created from a &lt;code&gt;single root seed&lt;/code&gt;, which is a &lt;code&gt;128-, 256- or 512-bit random number&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;Everything else in the HD wallet is deterministically derived from this root seed, which makes it possible to re-create the entire HD wallet from that seed in any compatible HD wallet. &lt;/li&gt;
&lt;li&gt;This makes it easy to backup, restore, export and import HD wallets &lt;code&gt;containing thousands or even millions of keys by simply transferring only the root seed&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;The root seed is most often represented by a &lt;code&gt;mnemonic word sequence&lt;/code&gt;, as described in the previous section “Mnemonic Code Words”, to make it easier for people to transcribe and store it.&lt;/li&gt;
&lt;li&gt;Hierarchical Deterministic wallets use a &lt;code&gt;child key derivation (CKD)&lt;/code&gt; function to derive children keys from parent keys. The child key derivation functions are based on &lt;code&gt;one-way hash functions&lt;/code&gt; that combines:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;A parent private or public key&lt;/code&gt; (ECDSA uncompressed key)
-&lt;code&gt;A seed called a chain code&lt;/code&gt; (256 bits)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;An index number&lt;/code&gt; (32 bits)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;child private key&lt;/code&gt;, the &lt;code&gt;corresponding public key and the bitcoin address are all indistinguishable from keys&lt;/code&gt; and addresses created randomly. The fact that they are part of a sequence is not visible, outside of the HD wallet function that created them. Once created, they operate exactly as “normal” keys.&lt;/li&gt;
&lt;li&gt;When a transaction is added to the transaction pool, the &lt;code&gt;orphan pool&lt;/code&gt; is checked for any orphans that reference this transaction’s outputs (its children). Any matching orphans are then validated. If valid, they are removed from the orphan pool and added to the transaction pool, completing the chain that started with the parent transaction. &lt;/li&gt;
&lt;li&gt;In light of the newly added transaction, which is no longer an orphan, the process is repeated recursively looking for any further descendants, until no more descendants are found.&lt;/li&gt;
&lt;li&gt;Through this process, the arrival of a parent transaction triggers a cascade reconstruction of an entire chain of interdependent transactions by re-uniting the orphans with their parents all the way down the chain.&lt;/li&gt;
&lt;li&gt;Both the &lt;code&gt;transaction pool&lt;/code&gt; and &lt;code&gt;orphan pool&lt;/code&gt; (where implemented) are stored in &lt;code&gt;local memory&lt;/code&gt; and are not saved on persistent storage, rather they are dynamically populated &lt;code&gt;from incoming network messages&lt;/code&gt;. When a node starts, both pools are empty and are gradually populated with new transactions received on the network.&lt;/li&gt;
&lt;li&gt;The blockchain data structure is an &lt;code&gt;ordered back-linked&lt;/code&gt; list of blocks of transactions. &lt;code&gt;The blockchain can be stored as a flat file, or in a simple database&lt;/code&gt;. The bitcoin core client stores the blockchain metadata using Google’s &lt;code&gt;LevelDB database&lt;/code&gt;. Blocks are linked “back”, each referring to the previous block in the chain. The blockchain is often &lt;code&gt;visualized as a vertical stack&lt;/code&gt;, with blocks layered on top of each other and the &lt;code&gt;first block ever serving as the foundation of the stack&lt;/code&gt;. The visualization of blocks stacked on top of each other results in the use of terms like “height” to refer to the distance from the first block, and “top” or “tip” to refer to the most recently added block.&lt;/li&gt;
&lt;li&gt;One way to think about the blockchain is like layers in a geological formation, or glacier core sample. The surface layers may change with the seasons, or even be blown away before they have time to settle. But once you go a few inches deep, geological layers become more and more stable. &lt;/li&gt;
&lt;li&gt;By the time you look a few hundred feet down, you are looking at a snapshot of the past that has remained undisturbed for millennia or millions of years. &lt;/li&gt;
&lt;li&gt;In the blockchain, the most recent few blocks may be revised if there is a chain recalculation due to a fork. &lt;code&gt;The top six blocks are like a few inches of topsoil&lt;/code&gt;. But once you go deeper into the blockchain, &lt;code&gt;beyond 6 blocks, blocks are less and less likely to change.&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;After &lt;code&gt;100 blocks&lt;/code&gt; back there is so much stability that the &lt;code&gt;“coinbase” transaction, the transaction containing newly-mined bitcoins, can be spent&lt;/code&gt;. A few thousand blocks back (a month) and the blockchain is settled history. It will never change.&lt;/li&gt;
&lt;li&gt;A block is a container data structure that aggregates transactions for inclusion in the public ledger, the blockchain. &lt;/li&gt;
&lt;li&gt;The block is made of a &lt;code&gt;header&lt;/code&gt;, containing metadata, followed by a long list of transactions that make up the bulk of its size. The &lt;code&gt;block header is 80 bytes&lt;/code&gt;, whereas the average transaction is at least &lt;code&gt;250 bytes&lt;/code&gt; and the &lt;code&gt;average block contains more than 500 transactions&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;A complete block, with all transactions, is therefore &lt;code&gt;1000 times larger than the block header&lt;/code&gt;.

&lt;ul&gt;
&lt;li&gt;4 bytes: Block Size: The size of the block, in bytes, following this field.&lt;/li&gt;
&lt;li&gt;80 bytes: Block Header: Several fields form the block header.&lt;/li&gt;
&lt;li&gt;1-9 bytes (VarInt): Transaction Counter: How many transactions follow&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Block Header:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A Block Header contains:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Block Number&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Version&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Timestamp&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Previous Hash&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Merkle Root&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Nonce&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;The block header consists of three sets of &lt;code&gt;block metadata&lt;/code&gt;. First, there is a &lt;code&gt;reference to a previous block hash&lt;/code&gt;, which connects this block to the previous block in the blockchain.&lt;/li&gt;
&lt;li&gt;The second set of metadata, &lt;code&gt;namely the difficulty, timestamp and nonce&lt;/code&gt;, relate to the mining competition. &lt;/li&gt;
&lt;li&gt;The third piece of metadata is the &lt;code&gt;Merkle Tree root&lt;/code&gt;, a data structure used to efficiently summarize all the transactions in the block. `
&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ajlairkgyo69rs1uxy6.png" alt="Block Header" width="800" height="244"&gt;
&lt;/li&gt;
&lt;li&gt;Unlike the block hash, the block height is not a unique identifier. While a &lt;code&gt;single block will always have a specific and invariant block height&lt;/code&gt;, the &lt;code&gt;reverse is not true&lt;/code&gt; - the &lt;code&gt;block height does not always identify a single block&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Two or more blocks may have the same block height&lt;/code&gt;, competing for the same position in the blockchain. Each node dynamically identifies a block’s position (height) in the blockchain when it is received from the bitcoin network. &lt;code&gt;The block height may also be stored as metadata in an indexed&lt;br&gt;
database table for faster retrieval.&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sample block:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4c99lxvyjifrfsr9jxk7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4c99lxvyjifrfsr9jxk7.png" alt="Block sample" width="800" height="357"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Merkle Trees:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Each block in the bitcoin blockchain contains a summary of all the transactions in the block, using a Merkle Tree&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A Merkle Tree, also known as a &lt;code&gt;Binary Hash Tree&lt;/code&gt; is a data structure used for &lt;code&gt;efficiently summarizing and verifying&lt;/code&gt; the integrity of &lt;code&gt;large sets of data&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Merkle Trees are binary trees containing cryptographic hashes&lt;/code&gt;. The term “tree” is used in computer science to describe a branching data structure, but these trees are usually displayed upside down with the “root” at the top and the “leaves” at the bottom of a diagram.&lt;/li&gt;
&lt;li&gt;Merkle trees are used in bitcoin to &lt;code&gt;summarize all the transactions in a block&lt;/code&gt;, &lt;code&gt;producing an overall digital fingerprint&lt;/code&gt; of the &lt;code&gt;entire set of transactions&lt;/code&gt;, providing a very efficient process to verify if a transaction is included in a block. &lt;code&gt;A merkle tree is constructed by recursively hashing pairs of nodes until there is only one hash, called the root, or merkle root&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;The cryptographic hash algorithm used in bitcoin’s &lt;code&gt;merkle trees is SHA256 applied twice, also known as double-SHA256&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When&lt;code&gt; N data elements&lt;/code&gt; are hashed and summarized in a Merkle Tree, you can check to see if any one data element is included in the tree with &lt;code&gt;at most 2*log2(N) calculations&lt;/code&gt;, making this a very efficient data structure.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The merkle tree is constructed bottom-up. The transactions are not stored in the merkle tree, rather their data is hashed and the resulting hash is stored in each leaf node:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt; Eg: HA = SHA256(SHA256(Transaction A))&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Consecutive pairs of leaf nodes are then summarized in a parent node, by concatenating the &lt;code&gt;two hashes and hashing them together&lt;/code&gt;. For example, to construct the parent node HAB, the &lt;code&gt;two 32-byte hashe&lt;/code&gt;s of the children are concatenated to create a &lt;code&gt;64-byte string&lt;/code&gt;. That string is then &lt;code&gt;double-hashed &lt;/code&gt;to produce the parent node’s hash:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;Eg: HAB = SHA256(SHA256(HA + HB)&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The process continues until there is &lt;code&gt;only one node&lt;/code&gt; at the top, the node known as the Merkle Root. That &lt;code&gt;32-byte hash is stored in the block header&lt;/code&gt; and summarizes all the data in all four transactions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since the merkle tree is a binary tree, it needs &lt;code&gt;an even number of leaf nodes&lt;/code&gt;. If there is an &lt;code&gt;odd number of transactions to summarize, the last transaction hash will be duplicated to create an even number of leaf nodes, also known as a balanced tree.&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To prove that a specific transaction is included in a block, &lt;code&gt;a node only needs to produce log2(N) 32-byte hashes&lt;/code&gt;, constituting an authentication path or merkle path connecting the specific transaction to the root of the tree. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This is especially important as the number of transactions increases, because the base-2 logarithm of the number of transactions increases much more slowly. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This allows bitcoin nodes to efficiently produce paths of&lt;br&gt;
&lt;code&gt;ten or twelve hashes (320-384 bytes)&lt;/code&gt; which can provide proof of a &lt;code&gt;single transaction out of more than a thousand transactions in a megabyte sized block&lt;/code&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Cryptography:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Cryptography is a key technology that underpins the security of blockchain systems&lt;/code&gt;. It is used to secure the transactions and data on the blockchain, ensuring that they cannot be tampered with or modified without being detected.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In a blockchain, cryptography is used in several different ways, including the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Hashing&lt;/strong&gt;: Hashing is the process of &lt;code&gt;generating a fixed-size, unique output from an input of any size&lt;/code&gt;. In a blockchain, hashing is used to generate a unique "fingerprint" for each block of transactions, known as a block hash. This allows the blockchain to efficiently store and verify the integrity of the transactions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Digital signatures&lt;/strong&gt;: Digital signatures are a cryptographic technique that &lt;code&gt;allows the sender of a message to prove that they are the true owner of the message&lt;/code&gt;. In a blockchain, digital signatures are used to sign transactions, proving that they are authorized by the owner of the associated cryptocurrency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Public-key cryptography&lt;/strong&gt;: &lt;code&gt;Public-key cryptography is a cryptographic technique that uses a pair of keys, one public and one private, to secure communications&lt;/code&gt;. In a blockchain, &lt;code&gt;public-key cryptography is used to generate addresses for users and to enable them to securely receive and send transactions&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These are just a few examples of the ways that cryptography is used in blockchain technology. Cryptography is a complex and constantly evolving field, and it is essential for the security and functionality of blockchain systems.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h4&gt;
  
  
  Types of Cryptography:
&lt;/h4&gt;

&lt;h5&gt;
  
  
  &lt;strong&gt;Symmetric Cryptography&lt;/strong&gt;:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;With symmetric cryptography (or symmetric-key encryption), the same key is used for both encryption and decryption.&lt;/li&gt;
&lt;li&gt;Alex wants to send some confidential message to Blake. In symmetric cryptography both of them will have a common key, let's call it K1. Alex encrypts the message using the key (K1) and then Bob decrypts the message using the same key (K1). The problem with this method is, if the key is compromised anyone can read the message.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Asymmetric cryptography or Public Key Cryptography:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;In this method, &lt;code&gt;each node has two keys&lt;/code&gt;, one is a &lt;code&gt;public key&lt;/code&gt; and the other one is a &lt;code&gt;private key&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;The most commonly used implementations of public key cryptography (also known as public-key encryption and asymmetric encryption) are based on algorithms presented by &lt;code&gt;Rivest-Shamir-Adelman (RSA)&lt;/code&gt; Data Security.&lt;/li&gt;
&lt;li&gt;Public key cryptography involves a pair of keys known as a public key and a private key (a public key pair), which are associated with an entity that needs to authenticate its identity electronically or to sign or encrypt data. &lt;/li&gt;
&lt;li&gt;Each public key is published and the corresponding private key is kept secret. Data that is encrypted with the public key can be decrypted only with the corresponding private key.&lt;/li&gt;
&lt;li&gt;RSA public key pairs can be any size. Typical sizes today are &lt;code&gt;1024&lt;/code&gt; and &lt;code&gt;2048&lt;/code&gt; bits.&lt;/li&gt;
&lt;li&gt;In the above case, Alex and Blake both will have two keys each. Whenever there's a link between them, they'll share their public keys to each other. The encryption-decryption happens as follows.
&lt;code&gt;KA&lt;/code&gt; - Key of A, &lt;code&gt;KB&lt;/code&gt; -Key of B, &lt;code&gt;1&lt;/code&gt;- Private Key, &lt;code&gt;2&lt;/code&gt;- Public Key.

&lt;ol&gt;
&lt;li&gt;When Alex sends a message to Blake, It's first encrypted using his &lt;code&gt;private key (KA1)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Proceeded by Blake's &lt;code&gt;Public Key (KB2)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Then, Blake decrypts the first layer of encryption using his &lt;code&gt;private key (KB1)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Blake gets the original message when it's decrypted using A's &lt;code&gt;public key (KA2)&lt;/code&gt; in the last step.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;Properties of Asymmetric Cryptography:

&lt;ul&gt;
&lt;li&gt;It is not possible to &lt;code&gt;guess the Private key from the Public Key.&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;However, they are mathematically linked in such a way that anything &lt;code&gt;encrypted using either a Public or Private key can only be decrypted using only these two keys&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Two layers of encryption ensure authenticity of the message&lt;/code&gt;. In the last step Blake is sure that the message was from Alex as it could be decrypted using Alex public key only.&lt;/li&gt;
&lt;li&gt;End-to-End encryption in WhatsApp works the same way.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Mining:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Mining is all about finding a &lt;code&gt;Nounce&lt;/code&gt; value which will &lt;code&gt;generate a hash value&lt;/code&gt; below a certain target threshold set by bitcoin network.&lt;/li&gt;
&lt;li&gt;It is the process by which &lt;code&gt;new bitcoin is added&lt;/code&gt; to the money supply. &lt;/li&gt;
&lt;li&gt;Mining also serves to &lt;code&gt;secure the bitcoin system against fraudulent transactions&lt;/code&gt; or &lt;code&gt;transactions spending the same amount of bitcoin more than once, known as a double-spend&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;Miners provide processing power to the bitcoin network in exchange for the opportunity to be rewarded bitcoin.&lt;/li&gt;
&lt;li&gt;Miners validate new transactions and &lt;code&gt;record them on the global ledger&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;A new block, containing transactions that occurred since the last block, is &lt;code&gt;“mined” every 10 minutes&lt;/code&gt;, thereby adding those transactions to the blockchain. &lt;/li&gt;
&lt;li&gt;Transactions that become part of a block and added to the blockchain are considered “&lt;code&gt;confirmed&lt;/code&gt;”, which allows the new owners of bitcoin to spend the bitcoin they received in those transactions.&lt;/li&gt;
&lt;li&gt;Miners receive two types of reward for mining: &lt;code&gt;new coins created with each new block&lt;/code&gt; and &lt;code&gt;transaction fees&lt;/code&gt; from all the transactions included in the block. &lt;/li&gt;
&lt;li&gt;To earn this reward, the miners compete to solve a difficult mathematical problem based on a cryptographic hash algorithm. The solution to the problem, called the &lt;code&gt;Proof-of-Work&lt;/code&gt;, is included in the new block and acts as proof that the miner expended significant computing effort.&lt;/li&gt;
&lt;li&gt;The competition to solve the Proof-of-Work algorithm to earn reward and the right to record transactions on the blockchain is the basis for bitcoin’s security model.&lt;/li&gt;
&lt;li&gt;The process of new coin generation is called mining, because the reward is designed to simulate diminishing returns, just like mining for precious metals. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Bitcoin’s money supply is created through mining&lt;/code&gt;, similar to how a central bank issues new money by printing bank notes. &lt;/li&gt;
&lt;li&gt;The amount of newly created bitcoin a miner can a&lt;code&gt;dd to a block decreases approximately every four years&lt;/code&gt; (or &lt;code&gt;precisely every 210,000 blocks&lt;/code&gt;). It started at &lt;code&gt;50 bitcoin&lt;/code&gt; per block in &lt;code&gt;January of 2009&lt;/code&gt; and &lt;code&gt;halved to 25 bitcoin&lt;/code&gt; per block in &lt;code&gt;November of 2012&lt;/code&gt;. It&lt;code&gt; got halved again to 12.5 bitcoin&lt;/code&gt; per block in 2016. &lt;/li&gt;
&lt;li&gt;Based on this formula, bitcoin mining rewards decrease exponentially until approximately the year &lt;code&gt;2140&lt;/code&gt; when all bitcoin (&lt;code&gt;20.99999998 million&lt;/code&gt;) will have been issued. &lt;code&gt;After 2140, no new bitcoins will be issued.&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Bitcoin miners also earn fees from transactions&lt;/code&gt;. Every transaction may include a transaction fee, in the form of a surplus of bitcoin between the transaction’s inputs and outputs. &lt;/li&gt;
&lt;li&gt;The winning bitcoin miner gets to “keep the change” on the transactions included in the winning block. &lt;/li&gt;
&lt;li&gt;Today, the fees represent &lt;code&gt;0.5% or less&lt;/code&gt; of a bitcoin miner’s income, the vast majority coming from the newly minted bitcoins. &lt;/li&gt;
&lt;li&gt;However, as the reward decreases over time and the number of transactions per block increases, a &lt;code&gt;greater proportion of bitcoin mining earnings will come from fees&lt;/code&gt;. After &lt;code&gt;2140&lt;/code&gt;, all &lt;code&gt;bitcoin miner earnings will be in the form of transaction fees&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  How Node verifies transactions:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Each node verifies every transaction against a long checklist of criteria as follows:&lt;br&gt;
• The transaction’s &lt;code&gt;syntax&lt;/code&gt; and &lt;code&gt;data structure&lt;/code&gt; must be correct.&lt;/p&gt;

&lt;p&gt;• Neither &lt;code&gt;lists of inputs or outputs&lt;/code&gt; are empty.&lt;/p&gt;

&lt;p&gt;• The transaction size in &lt;code&gt;bytes&lt;/code&gt; is less than &lt;code&gt;MAX_BLOCK_SIZE&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;• Each &lt;code&gt;output value&lt;/code&gt;, as well as the &lt;code&gt;total&lt;/code&gt;, must be &lt;code&gt;within the allowed range of values&lt;/code&gt;(less than &lt;code&gt;21m&lt;/code&gt; coins, more than 0).&lt;/p&gt;

&lt;p&gt;• None of the &lt;code&gt;inputs have hash=0, N=-1&lt;/code&gt; (coinbase transactions should not be relayed).&lt;/p&gt;

&lt;p&gt;• &lt;code&gt;nLockTime&lt;/code&gt; is less than or equal to &lt;code&gt;INT_MAX&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;• The &lt;code&gt;transaction size in bytes&lt;/code&gt; is greater than or equal to &lt;code&gt;100&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;• The &lt;code&gt;number of signature operations&lt;/code&gt; contained in the transaction is less than the &lt;code&gt;signature operation limit&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;• The &lt;code&gt;unlocking script (scriptSig)&lt;/code&gt; can only push numbers on the stack, and the &lt;code&gt;locking script (scriptPubkey) &lt;/code&gt;must match &lt;code&gt;isStandard&lt;/code&gt; forms (this rejects “nonstandard”     transactions)&lt;/p&gt;

&lt;p&gt;• A &lt;code&gt;matching transaction in the pool&lt;/code&gt;, or in a block in the main branch, must exist.&lt;/p&gt;

&lt;p&gt;• For &lt;code&gt;each input&lt;/code&gt;, if the &lt;code&gt;referenced output exists&lt;/code&gt; in any &lt;code&gt;other transaction in the pool&lt;/code&gt;, reject this transaction.&lt;/p&gt;

&lt;p&gt;• For &lt;code&gt;each input, look in the main branch and the transaction pool&lt;/code&gt; to find the referenced output transaction. If the &lt;code&gt;output transaction is missing for any input&lt;/code&gt;, this will be an orphan transaction. &lt;code&gt;Add to the orphan transactions pool, if a matching transaction is not already in the pool&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;• For each input, if the referenced output transaction is a coinbase output, it must have at least &lt;code&gt;COINBASE_MATURITY&lt;/code&gt; &lt;code&gt;(100) confirmations&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;• For each input, the &lt;code&gt;referenced output must exist&lt;/code&gt; and cannot already be spent.&lt;/p&gt;

&lt;p&gt;• Using the referenced output transactions to get input values, check that each input value, as well as the sum, are in the allowed range of values &lt;code&gt;(less than 21m coins, more than 0)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;• Reject if &lt;code&gt;the sum of input values &amp;lt; sum of output values.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;• Reject if &lt;code&gt;transaction fee would be too low to get into an empty block.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;• The &lt;code&gt;unlocking scripts&lt;/code&gt; for each input must validate against the corresponding &lt;code&gt;output locking scripts&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;By independently verifying each transaction as it is received and before propagating it, every node &lt;code&gt;builds a pool of valid new transactions&lt;/code&gt; (the transaction pool), roughly in the same order.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After validating transactions, a bitcoin node will&lt;code&gt; add them to the memory pool&lt;/code&gt;, or &lt;code&gt;transaction pool&lt;/code&gt;, where transactions await until they can be included (mined) into a block.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To construct the candidate block Jing’s bitcoin node selects transactions from the memory pool, by applying a &lt;code&gt;priority metric to each transaction&lt;/code&gt; and adding the &lt;code&gt;highest priority transactions first&lt;/code&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Transactions are &lt;code&gt;prioritized based on the “age” of the UTXO&lt;/code&gt; that is being spent in their inputs, allowing for &lt;code&gt;old and high-value inputs &lt;/code&gt;to be prioritized over newer and smaller inputs. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Prioritized transactions can be sent without any fees, if there is enough space in the block. The priority of a transaction is calculated as &lt;code&gt;the sum of the value and age of the inputs divided by the total size of the transaction&lt;/code&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt; Priority = Sum (Value of input * Input Age) / Transaction Size&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Forking Of Blockchain:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A “fork” occurs whenever there are &lt;code&gt;two candidate blocks competing to form the longest blockchain. &lt;/code&gt; &lt;/li&gt;
&lt;li&gt;This occurs under normal conditions whenever &lt;code&gt;two miners solve the Proof-of-Work algorithm within a short period of time from each other&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;As both miners discover a solution for their respective candidate blocks, they immediately broadcast their own “winning” block to their immediate neighbors who begin propagating the block across the network. &lt;/li&gt;
&lt;li&gt;Each node that receives a valid block will incorporate it into their blockchain, extending the blockchain by one block.&lt;/li&gt;
&lt;li&gt; If that &lt;code&gt;node later sees another candidate block extending the same parent, they connect the second candidate on a secondary chain&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;As a result, some nodes will “see” one candidate block first, while other nodes will see the other candidate block and &lt;code&gt;two competing versions of the blockchain will emerge&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Forks are almost always &lt;code&gt;resolved within one block&lt;/code&gt;. As part of the network’s hashing power is dedicated to building on &lt;code&gt;top of “red” as the parent&lt;/code&gt;, another part of the hashing power is focused on building on &lt;code&gt;top of “green”&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;Even if the hashing power is almost evenly split, it is &lt;code&gt;likely that one set of miners will find a solution and propagate it before the other set of miners have found any solutions&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;Let’s say for example that the miners building on top of “green” find a new block “pink” that extends the chain (e.g. bluegreen-pink). They immediately propagate this new block and the entire network sees it as a valid solution.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>bitcoin</category>
      <category>blockchain</category>
      <category>cryptocurrency</category>
      <category>token</category>
    </item>
    <item>
      <title>How Polygon Blockchain works...</title>
      <dc:creator>SK Sabiruddin</dc:creator>
      <pubDate>Sat, 03 Feb 2024 08:17:58 +0000</pubDate>
      <link>https://dev.to/sksabir/polygon-blockchain-101-c5c</link>
      <guid>https://dev.to/sksabir/polygon-blockchain-101-c5c</guid>
      <description>&lt;h2&gt;
  
  
  About:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Polygon is a blockchain platform which aims to create a &lt;code&gt;multi-chain blockchain&lt;/code&gt; system compatible with Ethereum. As with Ethereum, it uses a &lt;code&gt;proof-of-stake consensus&lt;/code&gt; mechanism for processing transactions on-chain. Polygon's native token is named &lt;code&gt;MATIC&lt;/code&gt;. &lt;code&gt;Matic is an ERC-20 token&lt;/code&gt;, allowing for compatibility with other Ethereum cryptocurrencies.&lt;/li&gt;
&lt;li&gt;Polygon uses a &lt;code&gt;modified proof of stake consensus mechanism that enables a consensus to be achieved with every block&lt;/code&gt;. Achieving consensus using traditional proof of stake requires processing many blocks to achieve consensus. &lt;/li&gt;
&lt;li&gt;The proof of stake method requires &lt;code&gt;network participants to stake—agree to not trade or sell—their MATIC tokens&lt;/code&gt;, in exchange for the right to validate Polygon network transactions. &lt;code&gt;Successful validators in the Polygon network are rewarded with MATIC tokens&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The Polygon network aims to address problems within the Ethereum platform, namely &lt;code&gt;high transaction fees and slow processing speeds&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Layer 2 solution&lt;/code&gt;. Off Chian computation. It's Decentralised.&lt;/li&gt;
&lt;li&gt;Scalability issues solved. High throughput (High TPS).
Enhance the capability of Ethereum.&lt;/li&gt;
&lt;li&gt;Can achieve &lt;code&gt;10000 TPS&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Provide SDK. Mobile apps can build using this.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Matic js&lt;/code&gt; : to build Polygon apps using JS.&lt;/li&gt;
&lt;li&gt; It has &lt;code&gt;Polygon mainnet&lt;/code&gt; and &lt;code&gt;mumbai testnet.&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Architecture of Polygon:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxb9xzvgg74pa68o1b59n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxb9xzvgg74pa68o1b59n.png" alt="Polygon Architecture" width="800" height="476"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Polygon network is divided into 3 Parts/Layers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1) &lt;code&gt;Ethereum Mainnet. / BeaconChain&lt;/code&gt;. &lt;code&gt;It has: (All the SC that polygon is using i.e.&lt;/code&gt; &lt;code&gt;Staking Contract , checkpoint contract and Reward Contract&lt;/code&gt; etc. ⮃&lt;/li&gt;
&lt;li&gt;2)&lt;code&gt;Heimdall (POS for Polygon)&lt;/code&gt;: &amp;lt;- (&lt;code&gt;Polygon consensus layer).It contains POS mechanism&lt;/code&gt;. &lt;code&gt;(2/3 voting)&lt;/code&gt;⮃&lt;/li&gt;
&lt;li&gt;3) &lt;code&gt;BOR layer. For adding new Blocks. (MATIC Chain = Native token for Polygon.)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Heimdall node is responsible for aggregating or rolling up blocks produced by the Bor node into a Merkle tree and publishing the Merkle root onto the Ethereum blockchain in periodic intervals called checkpoints.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Checkpointing provides finality on the Ethereum network which is used in the withdrawal of assets from Polygon to Ethereum through Proof of Burn proofs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Bor node, a Geth fork, is the execution layer of Polygon. It’s responsible for processing transactions into blocks and passing those blocks to Heimdall for validation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Polygon network blocks are by default &lt;code&gt;proposed in the BOR layer&lt;/code&gt;, after which they are &lt;code&gt;forwarded to the Ethereum layer&lt;/code&gt; after &lt;code&gt;consensus is reached via the POS mechanism in the Heimdall layer.&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There are various checkpoints available in Polygon POS. &lt;code&gt;One block is minted in every 2 seconds in the BOR chain.&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Each block will have its own blockhash. &lt;code&gt;One block has between 100 and 200 TXN.&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For example, &lt;code&gt;five blocks with various TXNs connected to a POS polygon checkpoint&lt;/code&gt;. &lt;code&gt;The merkel root hash for all block hashes was then represented by combining the block hashes of the individual blocks&lt;/code&gt;. &lt;code&gt;The ethereum mainnet will receive that single merkle root hash&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Thus, &lt;code&gt;1 hash will be used to represent 600 transactions on the mainnet&lt;/code&gt;. &lt;code&gt;Block will be put to the Ethereum blockchain after the Merkel root and TXN have been verified&lt;/code&gt;. Only the &lt;code&gt;final polygon transaction&lt;/code&gt;, not the individual blocks, will be able to alter the Ethereum State.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Make sure a specific transaction is real or not by using the Merkel root.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;At checkpoints, the &lt;code&gt;BOR layer continuously generates new blocks and adds them to the Heimdall layer&lt;/code&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In every &lt;code&gt;10-20 minutes&lt;/code&gt;, &lt;code&gt;checkpointing and snapshots of every block (Merkel Root) are taken&lt;/code&gt; i.e., &lt;code&gt;all suggested blocks are added to the Heimdall layer on the checkpoint every 20 minutes.&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;These blocks from &lt;code&gt;BOR networks&lt;/code&gt;were added by randomly selected validators from the POS polygon.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In order to avoid doing transactions on the Ethereum blockchain, Polygon must execute the majority of its &lt;code&gt;Transactions or operations in the offchain&lt;/code&gt;. Just To confirm the transactions, it uses Ethereum.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Heimdall Chain: (POS network , acting like BeaconChain)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;It is the POS verification layer of polygon.it does the Checkpointing of &lt;code&gt;BOR blocks/ Plasma blocks&lt;/code&gt; representation.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Transferring the Merkel root to the Ethereum blockchain from a checkpoint&lt;/code&gt;. And all the logic to validate this three-layer architecture is contained in the &lt;code&gt;Polygon Smart Contract that was implemented on Ethereum.&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The Ethereum mainnet is where the &lt;code&gt;staking contract is deployed&lt;/code&gt;. It includes all the logic for &lt;code&gt;proposing new blocks&lt;/code&gt;, &lt;code&gt;allocating voting rights to various validators&lt;/code&gt;, &lt;code&gt;selecting a random validator&lt;/code&gt;, and &lt;code&gt;overseeing checkpoints&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;On the Ethereum blockchain, &lt;code&gt;Heimdall&lt;/code&gt; is able to perform &lt;code&gt;function calls to the staking smart contracts&lt;/code&gt;. Polygon uses the &lt;code&gt;Ethereum Mainnet for all it's security&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Aggregation of blocks produced by BOR&lt;/code&gt;at the checkpoints. Then, &lt;code&gt;verify the block signature and hashs of the Individual blocks&lt;/code&gt;. It &lt;code&gt;merged the blocks to create the root hash&lt;/code&gt; after it was validated. &lt;/li&gt;
&lt;li&gt;After successful validation, the &lt;code&gt;blocks are merged to create the root hash&lt;/code&gt; and then it &lt;code&gt;will be sent to Ethereum mainnet as final transaction&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Tendermint&lt;/code&gt; software need to install in order to become a validator.&lt;/li&gt;
&lt;li&gt;Every new block, one valiadtor will perform the following duties:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Validate all blocks since the last checkpoint&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Creates a merkel tree of block hashes&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Final hash can be checkpointed and send to the mainnet&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;polygon will do 1-1 mapping with the mainnet&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Checkpoint importance/Task:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Providing finality on the root chain&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Providing proof of burn in the withdrawal assets&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;How transfer of token happens in both chain (zkEVM)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;ERC20 polygon &amp;lt;-&amp;gt; Mainnet ETH&lt;/code&gt;. Tokens that are mapped to equivalent amounts, that should be added to the mainnet, will be &lt;code&gt;burned on polygon&lt;/code&gt; before being &lt;code&gt;transferred to the mainnet and vice versa&lt;/code&gt;.
&lt;/h4&gt;

</description>
      <category>polygon</category>
      <category>web3</category>
      <category>layer2</category>
      <category>ethereum</category>
    </item>
    <item>
      <title>Blockchain Consensus Mechanism</title>
      <dc:creator>SK Sabiruddin</dc:creator>
      <pubDate>Fri, 02 Feb 2024 16:50:25 +0000</pubDate>
      <link>https://dev.to/sksabir/blockchain-consensus-mechanism-4f2</link>
      <guid>https://dev.to/sksabir/blockchain-consensus-mechanism-4f2</guid>
      <description>&lt;h2&gt;
  
  
  Consensus mechanism:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A consensus mechanism is a algorithm or protocol used by nodes in a distributed system to reach agreement on the state of the system. &lt;/li&gt;
&lt;li&gt;In a blockchain, the consensus mechanism is used to determine which transactions will be included in the next block of the ledger and to prevent conflicting transactions from being included.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;consensus protocol&lt;/code&gt; is a process used to achieve agreement between participants of a distributed network.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Proof Of Work (PoW):&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Yes, it means what it sounds like, showing the proof that you have done the work. As we know mining is basically solving a complex mathematical problem that requires high computational power. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At a time when many miners compete to mine a block, the node that wins the race has a proof of work of spending energy and resources and hence it’s rewarded when the block is verified and added to the chain. &lt;/li&gt;
&lt;li&gt;If the node tries to perform malicious activity, the block will not be verified by other nodes in the network and the miner doesn’t get the reward, which is a total waste of its resources and time. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Bitcoin works on PoW&lt;/code&gt;, while &lt;code&gt;Ethereum is using Proof of Stake&lt;/code&gt;. Proof Of Work requires a lot of power as many miners try to compete to mine a block, Proof Of Stake is a better alternative.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Facts about PoW:&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Bitcoin has around &lt;code&gt;2700 transactions&lt;/code&gt; in a block after &lt;code&gt;10 minutes&lt;/code&gt;which is around &lt;code&gt;4/5 TPS&lt;/code&gt;. (7–19 minutes to create a block, i.e., an average of 10 minutes).&lt;/li&gt;
&lt;li&gt;There is a &lt;code&gt;14-day&lt;/code&gt; maximum hold period for transactions in the mempool. Higher transaction fees are used in the mempool to replace lower transaction fees. You'll need to resend the transaction again after that because it will be removed from the mempool.&lt;/li&gt;
&lt;li&gt;Once a transaction is added to the blockchain, it will wait for &lt;code&gt;6&lt;/code&gt; more blocks to be added to the same blockchain for confirmation and to become irreversible.&lt;/li&gt;
&lt;li&gt;In every &lt;code&gt;2 weeks&lt;/code&gt;, the bitcoin blockchain checks the average time for the last two weeks. Based on this, the difficulty level was adjusted to make the average mining time per block 10 minutes.&lt;/li&gt;
&lt;li&gt;The bitcoin blockchain looks up the average time spent in mining a block over from previous two weeks. To make the average mining time per block 10 minutes, the difficulty level was adjusted in light of this.&lt;/li&gt;
&lt;li&gt;In &lt;code&gt;10 minutes, 1 block mined&lt;/code&gt; =&amp;gt; In &lt;code&gt;2 weeks = 2016 mined blocks&lt;/code&gt;. So, the difficulty level for miners is adjusted after every &lt;code&gt;2016 blocks (after 2 weeks).&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;After &lt;code&gt;210000 blocks&lt;/code&gt;, or every &lt;code&gt;4 years&lt;/code&gt;, Coinbase awards will be divided in half. In 2013, 50 bitcoins were given to miners for successfully mining a block then after &lt;code&gt;4 years it was 25 then 12.5 and now it's 6.26&lt;/code&gt;. This is known as halving. 4-year halving cycle.&lt;/li&gt;
&lt;li&gt;Since miners get fewer rewards, the price of bitcoin will increase. Miners control the circulation of bitcoin price.They'll sell the bitcoin at a higher price to get the benefit.&lt;/li&gt;
&lt;li&gt;The price of bitcoin will rise since miners receive smaller rewards. The price of bitcoins is determined by miners. To earn profit, they will sell the bitcoin for more money. Miners control the circulation of bitcoin price.&lt;/li&gt;
&lt;li&gt;Reward: Coinbase/Mining Reward &lt;code&gt;(6.25 BTC) + Transaction Fee (1-2 lakh)&lt;/code&gt; Reward points will be distributed to miners; however, they won't be able to use it for another &lt;code&gt;16 or 17 hours&lt;/code&gt;, or until the next &lt;code&gt;100 blocks&lt;/code&gt; are created. (also known as Maturity of Coinbase)&lt;/li&gt;
&lt;li&gt;Original Bitcoin network, commonly known as &lt;code&gt;bitcoin cash&lt;/code&gt;, was forked in order to extend the block size from &lt;code&gt;1 MB to 4 MB&lt;/code&gt;, allowing for the addition of more transactions. Each Block size: &lt;code&gt;1 MB&lt;/code&gt;, after forking: &lt;code&gt;4 MB&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Bitcoin uses/follows the longest chain in operation.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;wallet = public key + private key.&lt;/code&gt; Hierarchical deterministic wallet: HD wallet (uses different keys for each transaction)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Bitcoin address = SHA256(public key)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Transaction hash: To determine the final transaction hash, find the hash of each transaction and apply the merle tree. The block hash will be determined as follows: &lt;code&gt;block no + nonce + transaction hash + timestamp.&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Drawbacks of PoW:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;There is only one blockchain i.e. no sharding concept available.&lt;/li&gt;
&lt;li&gt;Too much energy consumption: &lt;code&gt;Bitcoin annual mining = Argentina's electricity&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Network Congestion (NC): When the network consumption is high, the dynamic gas price kicks in and vice versa, resulting in the extended block confirmation time. Also, it takes longer time to add transactions from Dapps with significant network consumption in the blockchain.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Proof of Stake (PoS):&lt;/u&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Ethereum 2.0: POS: POS based Ethereum blockchain (Also known &lt;code&gt;Serenity&lt;/code&gt; or &lt;code&gt;Beacon Chain&lt;/code&gt;). Has PoS consensus &amp;amp; Shard chains.&lt;/li&gt;
&lt;li&gt;In POS, there are no miners. They are referred as &lt;code&gt;validators&lt;/code&gt; rather than miners. In order to be a validator on the Ethereum network, one must stake &lt;code&gt;32 ETH&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;Groups of people can invest money in liquidity pools, also known as staking pools, until they reach 32 ETH and become validators. &lt;/li&gt;
&lt;li&gt;32 ETH will be locked in for a brief period of time, and the blockchain software will serve those as a validator. Liquidity pools can be found in Dapps. A liquidity pool can be a Dapp.&lt;/li&gt;
&lt;li&gt;Geth: GO implementation of the ETH, store private/public key.&lt;/li&gt;
&lt;li&gt;These three types of software must be installed. (Part of GETH): (POS architecture)

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Execution client&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Consensus client&lt;/code&gt; (consensus: voting and reaching agreement)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Validator Client&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Once 32 ETH are deposited (blockchain software will be activated), a validator joins a queue where others are waiting to be validators.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Adding new blocks in PoS: (How blocks added in POS)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Beacon chains have &lt;code&gt;slots&lt;/code&gt; and &lt;code&gt;epochs&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;There is a data structure that has &lt;code&gt;32 partitions&lt;/code&gt;. Each partition is called a &lt;code&gt;Slots&lt;/code&gt;. The combination of &lt;code&gt;32 slots&lt;/code&gt; is called an &lt;code&gt;Epoch&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;All of the &lt;code&gt;slots are capable of taking transactions&lt;/code&gt;. They are representations of proposed blocks. Slots are the intermediate state of a block.&lt;/li&gt;
&lt;li&gt;A transaction can be proposed in any slot by the validator who wishes to add a block to the Ethereum blockchain. To add these blocks, individuals or validators are selected at random.&lt;/li&gt;
&lt;li&gt;In POW, the miner with the best hardware mines the transaction. On the other hand, POS uses random selection. When a block is inserted, only one validator will be selected at a time to validate that.&lt;/li&gt;
&lt;li&gt;The amount of ETH staked, was chosen as a validator, based on probability. The more, one stake the ETH, has greater chance of becoming a validator.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  PoS committees: (128 committee members/attester)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;There will be at least &lt;code&gt;128 committee members&lt;/code&gt; (who are also randomly selected as validators) and &lt;code&gt;1 proposer&lt;/code&gt;, or &lt;code&gt;validator&lt;/code&gt;, who will be picked at random to add blocks or transactions to the blockchain, for each slot. Each of them also staked at least 32 ethers.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Committee members' responsibility is to verify if a transaction or block added by the proposer or validator is malicious or legitimate&lt;/code&gt;. In this case, the majority needed to confirm a certain slot is &lt;code&gt;2/3 = 66%&lt;/code&gt;(committee members).&lt;/li&gt;
&lt;li&gt;Therefore, each time a validator is selected at random from that queue, &lt;code&gt;128 other validators are selected at random for every slot&lt;/code&gt; so that committee members can validate one transaction or add one block. For all &lt;code&gt;32 slots in a single epoch, the same event occurs for every slot&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;There will be &lt;code&gt;12 seconds for each slot&lt;/code&gt;. The duration of the entire &lt;code&gt;epoch is 6.4 minutes&lt;/code&gt;. No vacant slots are uploaded to the blockchain.&lt;/li&gt;
&lt;li&gt;If the proposer is unable to add a transaction to the slot, Beacon Chain will penalize them. A portion of the Ethereum staked will be burnt. The procedure will use up some of the 32 ethers.&lt;/li&gt;
&lt;li&gt;It will burn some of the stake if it commits fraud. (&lt;code&gt;1% Stake&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;The Ethereum blockchain has a list of active validators. If any malicious activity found by a validator, Ethereum will remove it from the blockchain network.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;checkpoint&lt;/code&gt; will reverse any newly added slots or blocks to the blockchain in the event that any fraudulent transactions are found.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;first slot in each epoch serves as a checkpoint&lt;/code&gt;. It will function as the approved or finished version of the blockchain. The last accepted state of the first slot is based on the voting of committee members, that will roll back to the last checkpoint in case of fraud transactions.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Genesis Block = Slot 0 + Epoch 0&lt;/code&gt;. Every epoch is meant to reach finality.  A maximum of&lt;code&gt;&amp;lt;=32 slots&lt;/code&gt; can be present in an epoch. Epoch's function is to strengthen the blockchain's security by introducing checkpointing.&lt;/li&gt;
&lt;li&gt;Batches of &lt;code&gt;32 blocks&lt;/code&gt; are uploaded to the blockchain. One slot equals one block, and every block contains a unique collection of transactions.&lt;/li&gt;
&lt;li&gt;Each epoch's validator task is as follows:-&amp;gt; (for 1 slot)

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Randomly selected to propose a new block&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;In order to approve the transaction, a committee member was arbitrarily selected to vote&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;To confirm the signature of the transaction, i.e., the public key and private key, committee members will run the transaction through the slots once more&lt;/code&gt;. They'll cast their vote when they've checked the signature. Verify the signature by comparing it with the proposer's signature.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;Notes: 1 Epoch = 32 Slots = 32 blocks uploaded to bockchain = slots can store transactions. 128 Committee members/attester for 1 proposer/validator. 128 other validators to propose transaction for each slot. 12 seconds for each slot. epoch is 6.4 minutes long.&lt;/code&gt;
&lt;/h4&gt;

&lt;h3&gt;
  
  
  Reward for Mining a block in PoS:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Base reward = validator effective balance * 2^6 / SQRT(Effective Bal. of all active validators)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Reward for POS: % of total staked Ethereum.&lt;/code&gt; Rewards will be given for each slot.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;One can be an attester or a proposer at any point in time in one epoch.&lt;/code&gt; 1 task per epoch. Interest on the entire amount of ether staked will be given to you. &lt;/li&gt;
&lt;li&gt;There is no mining in POS. The portion of ether that you get in return will be added to the overall amount of ether in circulation. &lt;/li&gt;
&lt;li&gt;You can withdraw the staked ether only after a period of &lt;code&gt;1.5 years&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Sharding in POS (also known as horizontal sharding): (Parallel blockchain: 15% faster transactions)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;There are &lt;code&gt;64 shard chains&lt;/code&gt; and &lt;code&gt;1 beacon chain&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;64 shard chains will each have a portion of the tasks. &lt;/li&gt;
&lt;li&gt;Every shard chain will make reference to the beacon chain.&lt;/li&gt;
&lt;li&gt;Every single shard chain's data can be read by the beacon chain. &lt;/li&gt;
&lt;li&gt;Hashing will be used in beacon chains to store the distinctive characteristics of shard chains. &lt;/li&gt;
&lt;li&gt;The beacon chain is assigning tasks to the shard chains.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Drawbacks of Ethereum:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Gas prices are high due to heavy network congestion.&lt;/li&gt;
&lt;li&gt;Scalability issues i.e. &lt;code&gt;13-15 TPS.&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Solution:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Layer 2 scaling solution. It uses POS Consensus. A certain gas price changes each time a state is altered. Has some network throughput as well. (TPS).&lt;/li&gt;
&lt;li&gt;Layer 2 blockchain: They are of ERC20 type token. EVM compatible. E.g.: Polygon&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>web3</category>
      <category>consensus</category>
      <category>blockchain</category>
      <category>ethereum</category>
    </item>
    <item>
      <title>Ethereum Blockchain 101</title>
      <dc:creator>SK Sabiruddin</dc:creator>
      <pubDate>Fri, 02 Feb 2024 10:25:34 +0000</pubDate>
      <link>https://dev.to/sksabir/ethereum-blockchain-101-32pl</link>
      <guid>https://dev.to/sksabir/ethereum-blockchain-101-32pl</guid>
      <description>&lt;h2&gt;
  
  
  About:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Ethereum&lt;/strong&gt; is a decentralized, open-source blockchain platform that runs &lt;em&gt;smart contracts&lt;/em&gt;, applications that run exactly as programmed without any possibility of downtime, censorship, fraud or third-party interference. &lt;/li&gt;
&lt;li&gt;These applications are run on a custom-built blockchain, which is a decentralized database that runs on a network of computers.&lt;/li&gt;
&lt;li&gt;Ethereum is the second-largest blockchain platform by market capitalization, after Bitcoin. It was developed by Vitalik Buterin in 2013 and has since become a popular platform for creating and running decentralized applications.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Smart Contracts:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Smart contracts are self-executing contracts with the terms of the agreement between buyer and seller being directly written into lines of code. &lt;/li&gt;
&lt;li&gt;The agreements and the code are spread via a decentralized, distributed blockchain network.&lt;/li&gt;
&lt;li&gt;By automating digital contracts, smart contracts have the power to completely transform the way that business is conducted and documented, opening the door to entirely new types of ventures while also enhancing the effectiveness and safety of current systems.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Blockchain Layers:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Layer 0&lt;/em&gt;&lt;/strong&gt;: It’s the underlying infrastructure layer that forms the basic blockchain network consisting of distributed nodes forming the peer-to-peer architecture. Layer 0 enables interoperability (cross-chain communication) blockchain protocols that are built on the same layer 0, can easily transfer tokens and data.&lt;/p&gt;

&lt;p&gt;Example: &lt;em&gt;PolkaDot, Avalanche, Cosmos etc&lt;/em&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Layer 1&lt;/em&gt;&lt;/strong&gt;: L1 blockchains, also known as "layer 1" blockchains, are the foundational layer of a blockchain platform. They are the underlying infrastructure that supports the platform and enables it to function.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;L1 blockchains typically consist of a network of nodes that maintain a shared, decentralized ledger of transactions and a consensus mechanism to ensure the integrity and security of the ledger. &lt;/li&gt;
&lt;li&gt;L1 blockchains are often the first layer of a multi-layer blockchain platform, with additional layers built on top to provide additional features and functionality. Examples of L1 blockchains include the &lt;strong&gt;&lt;em&gt;Bitcoin&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;Ethereum&lt;/em&gt;&lt;/strong&gt; networks. &lt;/li&gt;
&lt;li&gt;Prominent blockchains like &lt;em&gt;Bitcoin&lt;/em&gt;. &lt;em&gt;Ethereum&lt;/em&gt;, &lt;em&gt;Solana&lt;/em&gt;, &lt;em&gt;Near&lt;/em&gt;, etc are the layer 1 chains. These are the execution protocols that provide the environment for transactions, handling cryptographic algorithms, data, consensus and tokenomics. Native tokens of the chain are the medium to interact with smart contracts, access resources, and mint NFT.&lt;/li&gt;
&lt;li&gt;As I mentioned earlier, some examples of L1 blockchains include the Bitcoin and Ethereum networks. Both of these networks are decentralized, open-source platforms that run on a network of nodes and use a consensus mechanism to maintain a shared ledger of transactions. &lt;/li&gt;
&lt;li&gt;Other examples of L1 blockchains include Litecoin, which is based on the Bitcoin network and provides faster transaction processing times, and Ripple, which is a payment network for financial institutions. These are just a few examples of L1 blockchains; there are many others that are in use today.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example : &lt;em&gt;Solana, Ethereum, Bitcoin, Tezos, Near&lt;/em&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Layer 2&lt;/em&gt;&lt;/strong&gt;: An L2 blockchain, or "layer 2 blockchain," is a blockchain that is built on top of another blockchain, known as the "layer 1" or "base" blockchain. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;L2 blockchains are designed to improve the scalability, performance, and security of the underlying blockchain by offloading some of the workload to the L2 blockchain. &lt;/li&gt;
&lt;li&gt;This can help to reduce transaction fees, increase transaction throughput, and improve the user experience. &lt;/li&gt;
&lt;li&gt;Some examples of L2 blockchains include the Lightning Network on top of the Bitcoin blockchain, and the Plasma framework on top of the Ethereum blockchain.&lt;/li&gt;
&lt;li&gt;Built on top of layer 1 chains, L2 solves the scalability problem of the blockchain trilemma by improvising on transaction speed (faster finality) &amp;amp; throughputs (higher transactions per second). &lt;/li&gt;
&lt;li&gt;Layer 1 handles security, data &amp;amp; consensus while Layer 2 handles transactions and regularly communicates with layer1. &lt;/li&gt;
&lt;li&gt;It takes away the transaction burden from layer 1 while taking the advantages of layer1’s security and decentralization.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example : &lt;em&gt;Polygon Matic L2&lt;/em&gt; on Ethereum&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Layer 3&lt;/em&gt;&lt;/strong&gt;: This is the utility layer where all the decentralized applications are built like &lt;em&gt;DeFi, games &amp;amp; wallets&lt;/em&gt;. It gives abstraction where the user is only use the applications.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Sharding:
&lt;/h2&gt;

&lt;p&gt;Sharding is a technique used to improve the scalability and performance of a blockchain. In a sharded blockchain, the network is divided into multiple "shards," or partitions, where each shard processes a subset of the transactions and maintains its own portion of the ledger. This allows the network to process multiple transactions in parallel, which can increase the overall transaction throughput and reduce transaction fees.&lt;br&gt;
Here is an example of the steps to build a blockchain with sharding:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Determine the number of shards and the shard size. The number of shards should be determined based on the desired level of scalability and performance, while the shard size should be determined based on the desired level of decentralization and security.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Implement the sharding mechanism. This involves designing and implementing the algorithms and data structures for dividing the network into shards, assigning transactions to shards, and maintaining the consistency and integrity of the ledger across all shards.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Implement the consensus mechanism. In a sharded blockchain, the consensus mechanism must be designed to ensure that the transactions within each shard are processed and validated in a decentralized and secure manner, and that the ledgers across all shards remain consistent.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Implement the transaction processing and validation logic. This involves designing and implementing the algorithms and data structures for processing and validating transactions within each shard, as well as the mechanisms for communicating and coordinating with other shards.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Implement the APIs and interfaces for interacting with the blockchain. This includes designing and implementing the APIs and interfaces for submitting transactions, querying the ledger, and accessing the data and functionality of the blockchain.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Consensus mechanism:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A consensus mechanism is a algorithm or protocol used by nodes in a distributed system to reach agreement on the state of the system. In a blockchain, the consensus mechanism is used to determine which transactions will be included in the next block of the ledger and to prevent conflicting transactions from being included.&lt;/li&gt;
&lt;li&gt;A consensus protocol is a process used to achieve agreement between participants of a distributed network.&lt;/li&gt;
&lt;li&gt;Basically, that’s how a consensus protocol works in blockchain. Different blockchain’s have different consensus protocol. Types of Consensus Protocol:&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Proof Of Work (PoW):&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Yes, it means what it sounds like, showing the proof that you have done the work. As we know mining is basically solving a complex mathematical problem that requires high computational power. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At a time when many miners compete to mine a block, the node that wins the race has a proof of work of spending energy and resources and hence it’s rewarded when the block is verified and added to the chain. &lt;/li&gt;
&lt;li&gt;If the node tries to perform malicious activity, the block will not be verified by other nodes in the network and the miner doesn’t get the reward, which is a total waste of its resources and time. &lt;/li&gt;
&lt;li&gt;Bitcoin works on PoW, while Ethereum is using Proof of Stake. Proof Of Work requires a lot of power as many miners try to compete to mine a block, Proof Of Stake is a better alternative.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Proof of Stake (PoS):&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
In Proof of Stake, instead of miners competing in a race to mine a block, they are asked to stake (deposit) some minimum tokens (32 ETH) of that blockchain to become validators (Miners in PoS are called as Validators). &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The validators are chosen randomly taking into consideration how many tokens they have staked or for how long they have been staking. &lt;/li&gt;
&lt;li&gt;If a validator performs any malicious activity he’s penalized from the stake. &lt;/li&gt;
&lt;li&gt;It is a more energy efficient protocol as not all the validators compete against each other which lowers transaction cost and increases throughput. &lt;/li&gt;
&lt;li&gt;This is one of the primary reasons for Ethereum to migrate to PoS.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Proof of History (PoH):&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Proof of History is an extension of Proof of Stake, developed by &lt;em&gt;Solana&lt;/em&gt;. And it’s one of the primary reasons why Solana is very fast, having the block time of 400 millisecond compared to Ethereum (1Sec) &amp;amp; BTC (10Mins). &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PoH introduces a variable of time in the block, it’s showing the proof that a historical event had occurred. &lt;/li&gt;
&lt;li&gt;In other consensus protocols the validators need to talk to each other to agree on when a block was added and how much time has passed. Whereas in Solana it has its own clock coded in a SHA256 algorithm, the validators keep mining blocks and they are sequentially added to the chain.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>web3</category>
      <category>ethereum</category>
      <category>solidity</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Blockchain Basics</title>
      <dc:creator>SK Sabiruddin</dc:creator>
      <pubDate>Fri, 02 Feb 2024 06:49:02 +0000</pubDate>
      <link>https://dev.to/sksabir/blockchain-basics-5a62</link>
      <guid>https://dev.to/sksabir/blockchain-basics-5a62</guid>
      <description>&lt;h2&gt;
  
  
  Blockchain Overview:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Blockchain is a type of digital ledger technology that makes it possible to build decentralized, transparent, and safe networks. A blockchain is a distributed database that is not controlled by a single entity but rather by a network of computers called nodes. A copy of the database, which holds a history of every transaction made on the blockchain, is kept on every node in the network.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Numerous nodes inside the network use complex cryptographic methods to verify and confirm every transaction on the blockchain. A new block is added to the blockchain when a transaction is approved and verified, completing the chain. All nodes inside the network get this block and update their respective copies of the blockchain accordingly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The reason the blockchain is regarded as safe is that each block in the chain is connected to the one before it, forming an unbreakable transaction chain. A block is a tamper-proof record of every transaction that has ever taken place on the blockchain since once it is added, it cannot be removed or changed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Applications for blockchain technology are numerous and include voting systems, healthcare, supply chain management, finance, and more. Its potential is found in its capacity to foster security, trust, and openness across a range of industries, do away with the need for middlemen, and open the door to new business models.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why do we need blockchain?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Decentralization&lt;/em&gt;&lt;/strong&gt;: Establishing decentralized systems is one of the main goals of employing blockchain technology. A decentralized network is more transparent, safe, and resistant to censorship and corruption since it lacks a central authority or middleman to exert control over it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Security&lt;/em&gt;&lt;/strong&gt;: Cryptographic algorithms are used by blockchain technology to protect data and transactions. Multiple parties validate and verify each transaction on the blockchain, making it more difficult for an unauthorized person to change the data or steal information.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Transparency&lt;/em&gt;&lt;/strong&gt;: By generating an immutable ledger of every transaction, blockchain technology promotes transparency. As a result, trust and accountability are encouraged because every member of the network can view every transaction that has occurred and confirm its legitimacy.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Efficiency&lt;/em&gt;&lt;/strong&gt;: Through the removal of middlemen and the automation of procedures, blockchain technology can lower transaction costs and boost efficiency. Businesses can cut expenses and boost revenues by expediting and streamlining transactions by doing away with middlemen.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Types of Blockchain:
&lt;/h2&gt;

&lt;p&gt;There are generally three types of blockchain: public, private, and hybrid.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Public Blockchain&lt;/em&gt;&lt;/strong&gt;: Anyone can access and participate in a public blockchain, which is a decentralized blockchain network. Anyone can read and write data on the network and join it. The public blockchains of &lt;em&gt;Ethereum, Litecoin, and Bitcoin&lt;/em&gt; are examples.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Private Blockchain&lt;/em&gt;&lt;/strong&gt;: A permissioned blockchain network is one in which only individuals with permission are able to access and engage with the network. Private blockchains are usually utilized by businesses or by a select group of reliable and well-known individuals. The &lt;em&gt;Quorum, Corda, and Hyperledger Fabric&lt;/em&gt; private blockchains are a few examples.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;&lt;strong&gt;Hybrid Blockchain&lt;/strong&gt;&lt;/em&gt;: A hybrid blockchain is made up of elements from both private and public blockchains. Because it enables authorized users to access and engage with both the public blockchain network and the private network, it provides the best of both worlds. &lt;em&gt;Aion, Dragonchain&lt;/em&gt; and other hybrid blockchains are examples of it.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Apart from the aforementioned trio of blockchain styles, there exist additional varieties or adjustments to the three main types of blockchains. A type of private blockchain known as a &lt;em&gt;consortium blockchain&lt;/em&gt;, for instance, is one in which a number of businesses band together to cooperatively run and maintain the network.&lt;/p&gt;

&lt;h2&gt;
  
  
  Blockchain Applications:
&lt;/h2&gt;

&lt;p&gt;Blockchain technology has a wide range of applications across various industries. Here are four examples of how blockchain is being used:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;&lt;strong&gt;Cryptocurrencies&lt;/strong&gt;&lt;/em&gt;: The development and utilization of cryptocurrencies such as &lt;em&gt;Bitcoin, Ethereum, and Litecoin&lt;/em&gt; is the most well-known application of blockchain technology. These digital currencies employ blockchain technology to enable safe, peer-to-peer transactions. They are decentralized, which means they are not governed by any financial organization or government.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Supply Chain Management&lt;/em&gt;&lt;/strong&gt;: By generating a safe, transparent record of each transaction that takes place in the supply chain, blockchain technology is being utilized to enhance supply chain management. This promotes efficiency, lowers fraud, and strengthens confidence between all supply chain participants.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Identity Verification&lt;/em&gt;&lt;/strong&gt;: People can establish safe, immutable digital identities for themselves using blockchain technology. This can lessen fraud and identity theft and make it simpler for people to authenticate themselves in a range of situations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Smart Contracts&lt;/em&gt;&lt;/strong&gt;: Smart contracts are self-executing agreements that automatically carry out their terms by utilizing blockchain technology. This can improve contract execution efficiency and speed while also lessening the need for middlemen like banks and lawyers. Financial services, insurance, and real estate are just a few of the businesses using smart contracts.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Ethereum Vs Bitcoin:
&lt;/h2&gt;

&lt;p&gt;Ethereum and Bitcoin are both decentralized blockchain platforms that have some similarities but also some&lt;br&gt;
important differences. Here are some key differences between Ethereum and Bitcoin:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Ethereum&lt;/em&gt; was intended to serve as a platform for smart contracts and decentralized applications (Dapps), whereas &lt;em&gt;Bitcoin&lt;/em&gt; was intended to serve as a decentralized digital currency.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Bitcoin&lt;/em&gt; uses a Proof of Work (PoW) mining method, which, in order to validate transactions and add new blocks to the blockchain, consumes a significant amount of processing power and energy. Nowadays, &lt;em&gt;Ethereum&lt;/em&gt; employs the more energy-efficient PoS algorithm.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;While &lt;em&gt;Bitcoin&lt;/em&gt; has a limited scripting language for transactions, &lt;em&gt;Ethereum&lt;/em&gt; allows developers to create custom smart contracts and decentralized applications using a variety of programming languages, such as &lt;em&gt;Solidity, Vyper , and Rust.&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The processing speed of &lt;em&gt;Ethereum&lt;/em&gt; transactions (15-20 tps) is generally faster than that of &lt;em&gt;Bitcoin&lt;/em&gt; (7/8 tps), with an average block time of approximately 15 seconds, as opposed to 10 minutes for Bitcoin. This is achieved through scalability. The network congestion that causes high fees and delayed transaction times during peak demand is one of Ethereum's scalability problems.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Ethereum&lt;/em&gt; does not have a fixed supply; instead, new Ethereum tokens are created through mining and transaction fees. &lt;em&gt;Bitcoin&lt;/em&gt; has a finite quantity of 21 million coins.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In Bitcon, miners will get 6.25BTC(halving in every 4 years) and in Ethereum, they will get 2 ETH.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Cryptocurrency VS Token:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Cryptocurrency is native to a Blockchain network. A Blockchain can have only one cryptocurrency but a million tokens. For example, Ethereum has its native currency, ether, but regulates multiple tokens such as USDC, AVAX, UNI, USDT, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Tokens are backed by a smart contract and can contain various functionalities depending upon different use cases. For example, security tokens, utility tokens, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Tokens can represent any asset on a blockchain network. For example, art, equity, insurance, identity, tickets, etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Broadly, there are three basic token standards and more will be added over time. Let’s understand them one by one.&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;ERC20&lt;/strong&gt; :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This standard is used to implement the functionality behind Fungible tokens. Fungible Tokens of a type are all alike and can be exchanged for the same value. For example, USDC, UNI, AVAX, DAI, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Ex: When you ride a metro/subway train, the ticket you buy is the same as everyone else for the same destination. Even if you exchange the ticket with someone, it would still work for you as there’s no differentiator between those tickets. Such items are called ‘fungible’, meaning that they can be mutually exchanged. Other examples of fungible items are currency notes, shares of a company etc.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ERC721&lt;/strong&gt; :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This standard is for Non-Fungible Tokens (NFTs). Tokens of this type are meant to be unique and cannot be exchanged for the same value given that none of them are alike. For example, Bored Apes, CryptoKitties, etc.  we usually call the image itself as an NFT and ultimately this is the thing that we bought it for. But technically, an NFT is a serial ID on the blockchain i.e. 1, 2, 3 etc. Against this serial ID, there’s a wallet address, and this mapping of address against the serial ID proves that a particular serial ID is owned by a particular address. In IPFS/Pinata, you can upload the images and metadata for your NFT that can then be connected to your ERC-721 smart contract. With this, we’ll now have two mappings on the blockchain:&lt;/li&gt;
&lt;li&gt;Token ID –&amp;gt; User Address&lt;/li&gt;
&lt;li&gt;Token ID –&amp;gt; Token URI 
(This mapping is actually done through a function that returns a URI for each token ID as we’ll see later in this lesson) i.e. one mapping will contain which user address owns a particular token ID. The second mapping maps the token URI to each token ID. The token URI is the hash of metadata stored on IPFS that contains the details and properties of the image associated with the token ID.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thus, we establish the ownership of NFTs on the blockchain by storing the token IDs, wallet addresses and token URI function. Whereas the images and their details &amp;amp; properties are stored on the IPFS and connected to the smart contract via token URI.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Ex: When you take a flight, you can’t exchange your boarding pass with another person even if you both are going to the same destination. That’s because your boarding pass has your name and your seat number, both of which are unique. Hence each boarding pass is different from others. Such items are called ‘non-fungible’, meaning they can’t be exchanged with other items. Other examples of non-fungible items are your driving licence, your college degree, a piece of art etc.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;ERC1155&lt;/em&gt;&lt;/strong&gt; :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This standard is used to implement functionalities of both fungible and non-fungible tokens. It can also be called a standard for semi-fungible tokens. For example, multiple editions of a song or photographs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Ex: When you attend a concert, you can buy a General pass, VIP pass, Multi-day pass and so on. If you hold a general pass, you can exchange your pass with someone holding a general pass but not with someone holding a VIP pass. Hence, such items are a mixture of fungible and non-fungible. Different categories of passes are non-fungible, but the passes within the same category are fungible. Other examples being shares of different companies, metro tickets for different destinations etc.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>web3</category>
      <category>blockchain</category>
      <category>webdev</category>
      <category>smartcontract</category>
    </item>
  </channel>
</rss>
