<?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: amirreza sharifi</title>
    <description>The latest articles on DEV Community by amirreza sharifi (@amir_ashy).</description>
    <link>https://dev.to/amir_ashy</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%2F233796%2Fc037c03c-35e4-4f3a-961e-2406b1327ad4.jpeg</url>
      <title>DEV Community: amirreza sharifi</title>
      <link>https://dev.to/amir_ashy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amir_ashy"/>
    <language>en</language>
    <item>
      <title>Simplest Blockchain in C#</title>
      <dc:creator>amirreza sharifi</dc:creator>
      <pubDate>Fri, 10 Jan 2020 16:40:56 +0000</pubDate>
      <link>https://dev.to/amir_ashy/simplest-blockchain-in-c-1f70</link>
      <guid>https://dev.to/amir_ashy/simplest-blockchain-in-c-1f70</guid>
      <description>&lt;p&gt;I'm pretty sure that almost every developer in the world has heard of &lt;strong&gt;Blockchain&lt;/strong&gt;, some wrongly consider it as &lt;a href="https://bitcoin.org/en/" rel="noopener noreferrer"&gt;Bitcoin&lt;/a&gt; and some may not know how it exactly works. So this post aims to explain what exactly Blockchain is and how to create a simple blockchain in ".Net Core" using C#, and to figure out how Blockchain really works at the end.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/amir-ashy" rel="noopener noreferrer"&gt;
        amir-ashy
      &lt;/a&gt; / &lt;a href="https://github.com/amir-ashy/Blockchain" rel="noopener noreferrer"&gt;
        Blockchain
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Simple Blockchain in C#
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;h4&gt;
  
  
  What will be covered
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Defining models (&lt;a href="https://github.com/amir-ashy/Blockchain/blob/master/Model/Transaction.cs" rel="noopener noreferrer"&gt;Transaction&lt;/a&gt; + &lt;a href="https://github.com/amir-ashy/Blockchain/blob/master/Model/Block.cs" rel="noopener noreferrer"&gt;Block&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Using &lt;a href="https://github.com/amir-ashy/Blockchain/blob/master/TransactionPool.cs" rel="noopener noreferrer"&gt;TransactionPool&lt;/a&gt; for storing raw Transactions&lt;/li&gt;
&lt;li&gt;Using hash algorithm in order to create immutability&lt;/li&gt;
&lt;li&gt;Creating a block mining unit which mine blocks after a period of time&lt;/li&gt;
&lt;li&gt;Using &lt;a href="https://github.com/unosquare/embedio" rel="noopener noreferrer"&gt;embedio&lt;/a&gt; in order to view our blockchain in a web browser&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What is Blockchain
&lt;/h3&gt;

&lt;p&gt;Literally, Blockchain is a chain of blocks which could be simply assumed as an immutable data structure. Immutability is one of the most prominent features of a blockchain, which leads us to build trust in completely unreliable environments.&lt;br&gt;
This kind of immutability is achieved by using Cryptographic hashing algorithms, (in our case we use &lt;strong&gt;Double SHA256&lt;/strong&gt;). which prevents data from being changed or deleted after data is added to the Blockchain.&lt;br&gt;
As it's obvious in the following image; Two different hashes are stored in every block, one hash is calculated for the current block and the other, is for the previous one. so any change in a block invalidates every block after it.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ffd8k90k557nb516mnm1u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ffd8k90k557nb516mnm1u.png" alt="Blocks in Blockchain"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's Start Coding!
&lt;/h3&gt;

&lt;p&gt;First of all, as we are to use ".Net Core", it's necessary to install ".NET Core" 3.1 Framework (or any other preferred version).&lt;br&gt;
You could find more about installation &lt;a href="https://dotnet.microsoft.com/download/dotnet-core/3.1" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;br&gt;
After installation completes, we should create our project using dotnet CLI&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

dotnet new console Blockchain.


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

&lt;/div&gt;
&lt;p&gt;As we are going to use &lt;a href="https://github.com/unosquare/embedio" rel="noopener noreferrer"&gt;embedio&lt;/a&gt; and &lt;a href="https://github.com/JamesNK/Newtonsoft.Json" rel="noopener noreferrer"&gt;Newtonsoft.Json&lt;/a&gt;, we add their corresponding packages to our project, again with dotnet CLI&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

dotnet add package Newtonsoft.Json
dotnet add package EmbedIO



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

&lt;/div&gt;
&lt;p&gt;Now that our project is ready, let's define our data models. Our first step is to define a model for transaction.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
The &lt;em&gt;From&lt;/em&gt; property identifies the sender of the money, the &lt;em&gt;To&lt;/em&gt; property identifies the receiver of the money and the &lt;em&gt;Amount&lt;/em&gt; represents the amount of money sent. &lt;br&gt;&lt;br&gt;
Let's create our next model, which is a Block's model. As we know, Each block contains data that will be written to the blockchain. Block data model has a list of transactions which are processed in the current block and two strings for storing hashes of the current Block(&lt;em&gt;Hash&lt;/em&gt;) and the previous one(&lt;em&gt;PrevHash&lt;/em&gt;). There are two other properties in every Block, Nounce is used for the mining algorithm and TimeStamp goes for DateTime of Creation of Block.&lt;br&gt;&lt;/p&gt;

&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
It's time to define our Transaction Pool class, which is used for storing unprocessed transactions during the time that a transaction is received and added to our blockchain. there are two methods in Transaction pool; one for adding raw Transactions (&lt;em&gt;AddRaw&lt;/em&gt;), and the other one for getting all unprocessed transactions(&lt;em&gt;TakeAll&lt;/em&gt;).&lt;br&gt;&lt;/p&gt;

&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
This is the time to define our main class which is &lt;a href="https://github.com/amir-ashy/Blockchain/blob/master/BlockMiner.cs" rel="noopener noreferrer"&gt;BlockMiner&lt;/a&gt; class. First, we model out the blockchain itself which is simply a list of Blocks.&lt;br&gt;&lt;/p&gt;

&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
We should now define a function which is responsible for calculating hash of raw string data and returning the result as hashed string. As it's obvious we are using SHA256 algorithm since it has been used in other famous blockchains like Bitcoin.&lt;br&gt;&lt;/p&gt;

&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
So as we create our hashing function, now we have to find a way to calculate hash of our block. The primary problem we're facing here is to find a way to calculate a hash for the list of transactions existing in the ongoing block. As you may know already &lt;strong&gt;Merkle Tree&lt;/strong&gt; is what comes into use here. You can learn more about it &lt;a href="https://brilliant.org/wiki/merkle-tree/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2F9%2F95%2FHash_Tree.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2F9%2F95%2FHash_Tree.svg" alt="Merkle Tree"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;
Notice that by using Merkle tree any little change in a transaction will lead to change in Block's hash and will effect every other Block after corresponding block.&lt;br&gt;&lt;br&gt;
We simply use discussed algorithm to calculate hash of all transactions in a block.&lt;br&gt;&lt;/p&gt;

&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Now we need to write our mining function that tries to find appropriate &lt;strong&gt;Hash&lt;/strong&gt; for our ongoing block (starts with specific amount of '0' at the start of hash). The way that we extract the appropriate hash is by incrementing nounce property defined in block and calculating the hash again and again until we reach our goal. Originally this kind of mining algorithms are called &lt;em&gt;Proof of work&lt;/em&gt; which are very useful these days.&lt;br&gt;&lt;/p&gt;

&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
After our mining process is done we can simply add our mined block to the blockchain. &lt;br&gt;&lt;br&gt;
At this point, we need one extra function in BlockMiner class which is responsible for continuously mining Blocks and we need two final functions to &lt;em&gt;start&lt;/em&gt; and &lt;em&gt;stop&lt;/em&gt; the mining process.&lt;br&gt;&lt;/p&gt;

&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Let's create our HTTP server using &lt;a href="https://github.com/unosquare/embedio" rel="noopener noreferrer"&gt;embedio&lt;/a&gt; which is a tiny, cross-platform, module based web server.&lt;br&gt;&lt;/p&gt;

&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Our controller has four methods, three of them will be used for exposing blockchain data and the other one will be used for adding new transactions to blockchain.&lt;/p&gt;

&lt;h4&gt;
  
  
  We’re done!
&lt;/h4&gt;

&lt;p&gt;Let’s try it out now. &lt;br&gt;
Fire up your application from terminal using command below&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

dotnet run


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

&lt;/div&gt;

&lt;p&gt;you will see something like this in your terminal&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fenh9h9a8zlgijr30vwqy.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fenh9h9a8zlgijr30vwqy.JPG" alt="app run"&gt;&lt;/a&gt;&lt;br&gt;
Now, let’s send some POST requests to add blocks.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fdlnvmlo8kp7410majmcz.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fdlnvmlo8kp7410majmcz.JPG" alt="Post"&gt;&lt;/a&gt;&lt;br&gt;
Now if you navigate to &lt;a href="http://localhost:5449/api/blocks/latest" rel="noopener noreferrer"&gt;http://localhost:5449/api/blocks/latest&lt;/a&gt; or &lt;a href="http://localhost:5449/api/blocks" rel="noopener noreferrer"&gt;http://localhost:5449/api/blocks&lt;/a&gt; you will see mined blocks directly in your browser.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fpgjg7o3itv4xeien9cbt.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fpgjg7o3itv4xeien9cbt.JPG" alt="mined block"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>dotnet</category>
      <category>csharp</category>
    </item>
  </channel>
</rss>
