<?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: Takayoshi Nishida</title>
    <description>The latest articles on DEV Community by Takayoshi Nishida (@takp).</description>
    <link>https://dev.to/takp</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%2F43908%2Fbd68d38d-9508-4bfe-b516-c8abc4df96f4.jpeg</url>
      <title>DEV Community: Takayoshi Nishida</title>
      <link>https://dev.to/takp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/takp"/>
    <language>en</language>
    <item>
      <title>Very Simple Blockchain Implementation in Ruby</title>
      <dc:creator>Takayoshi Nishida</dc:creator>
      <pubDate>Tue, 23 Jan 2018 10:00:00 +0000</pubDate>
      <link>https://dev.to/takp/very-simple-blockchain-implementation-in-ruby-2129</link>
      <guid>https://dev.to/takp/very-simple-blockchain-implementation-in-ruby-2129</guid>
      <description>&lt;h2&gt;
  
  
  1. Why I implemented it in Ruby
&lt;/h2&gt;

&lt;p&gt;I created &lt;strong&gt;simple version of blockchain in Ruby&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is because I read this article &lt;a href="https://hackernoon.com/learn-blockchains-by-building-one-117428612f46"&gt;“Learn Blockchains by Building One”&lt;/a&gt;and I’m impressed with this. It creates simple blockchain in Python less than 200 lines of code. Then I thought why not in Ruby?&lt;/p&gt;

&lt;p&gt;For the engineers, it is easier to figure out something by creating simple one by coding, isn’t it?&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Implement blockchain in Ruby
&lt;/h2&gt;

&lt;p&gt;Just implement it in Ruby one by one.&lt;/p&gt;

&lt;p&gt;Complete version -&amp;gt; &lt;a href="https://github.com/takp/blockchain-ruby"&gt;https://github.com/takp/blockchain-ruby&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It looks long because it’s all inside one fiile, but it is simple less than 200 lines of code.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Run
&lt;/h2&gt;

&lt;h3&gt;
  
  
  3.1 Preparation for run
&lt;/h3&gt;

&lt;p&gt;After git clone it to local, run &lt;code&gt;bundle install&lt;/code&gt; and install necessary gems.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git clone git@github.com:takp/blockchain-ruby.git
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;blockchain-ruby/
&lt;span class="nv"&gt;$ &lt;/span&gt;bundle &lt;span class="nb"&gt;install&lt;/span&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;bundle &lt;span class="nb"&gt;exec &lt;/span&gt;ruby blockchain.rb
&lt;span class="o"&gt;[&lt;/span&gt;2018-01-09 21:49:25] INFO WEBrick 1.4.2
&lt;span class="o"&gt;[&lt;/span&gt;2018-01-09 21:49:25] INFO ruby 2.5.0 &lt;span class="o"&gt;(&lt;/span&gt;2017-12-25&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;x86_64-darwin16]
&lt;span class="o"&gt;==&lt;/span&gt; Sinatra &lt;span class="o"&gt;(&lt;/span&gt;v2.0.0&lt;span class="o"&gt;)&lt;/span&gt; has taken the stage on 4567 &lt;span class="k"&gt;for &lt;/span&gt;development with backup from WEBrick
&lt;span class="o"&gt;[&lt;/span&gt;2018-01-09 21:49:25] INFO WEBrick::HTTPServer#start: &lt;span class="nv"&gt;pid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;12067 &lt;span class="nv"&gt;port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;4567
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will start Sinatra and become ready to accepet the requeset.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.2 BlockChain’s State
&lt;/h3&gt;

&lt;p&gt;You can know the blockchain’s state by requesting &lt;code&gt;GET /chain&lt;/code&gt;. At this point, nothings are done so it returns only genesis block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;curl http://localhost:4567/chain

&lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="s2"&gt;"chain"&lt;/span&gt;:[
    &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="s2"&gt;"index"&lt;/span&gt;:1,
      &lt;span class="s2"&gt;"timestamp"&lt;/span&gt;:&lt;span class="s2"&gt;"2018-01-09 21:59:51 +0700"&lt;/span&gt;,
      &lt;span class="s2"&gt;"transactions"&lt;/span&gt;:[],
      &lt;span class="s2"&gt;"proof"&lt;/span&gt;:1,
      &lt;span class="s2"&gt;"previous_hash"&lt;/span&gt;:100
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;]&lt;/span&gt;,
  &lt;span class="s2"&gt;"length"&lt;/span&gt;:1
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3.3 Mining!
&lt;/h3&gt;

&lt;p&gt;You can execute mining by requesting &lt;code&gt;GET /mine&lt;/code&gt;. From the response, you can see the hash value is added to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;curl http://localhost:4567/mine

&lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="s2"&gt;"message"&lt;/span&gt;:&lt;span class="s2"&gt;"New Block Forged"&lt;/span&gt;,
  &lt;span class="s2"&gt;"index"&lt;/span&gt;:2,
  &lt;span class="s2"&gt;"transactions"&lt;/span&gt;:[
    &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="s2"&gt;"sender"&lt;/span&gt;:&lt;span class="s2"&gt;"0"&lt;/span&gt;,
      &lt;span class="s2"&gt;"recipient"&lt;/span&gt;:&lt;span class="s2"&gt;"caedf4a87f7841838061dd7dffca2916"&lt;/span&gt;,
      &lt;span class="s2"&gt;"amount"&lt;/span&gt;:1
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;]&lt;/span&gt;,
  &lt;span class="s2"&gt;"proof"&lt;/span&gt;:94813,
  &lt;span class="s2"&gt;"previous_hash"&lt;/span&gt;:&lt;span class="s2"&gt;"84e609b88e68764ac4546cb807d7cf0e"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3.4 Add new transaction
&lt;/h3&gt;

&lt;p&gt;You can add new transaction by requesting &lt;code&gt;POST /transactions/new&lt;/code&gt;. The content of the transaction is sent by &lt;code&gt;curl&lt;/code&gt;’s &lt;code&gt;-d&lt;/code&gt; options.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;curl &lt;span class="nt"&gt;-X&lt;/span&gt; POST &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"Content-Type: application/json"&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;'{
 "sender": "d4ee26eee15148ee92c6cd394edd974e",
 "recipient": "someone-other-address",
 "amount": 5
}'&lt;/span&gt; &lt;span class="s2"&gt;"http://localhost:4567/transactions/new"&lt;/span&gt;

&lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="s2"&gt;"message"&lt;/span&gt;:&lt;span class="s2"&gt;"Transaction will be added to Block 3"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, do mining again and add more transaction to the blockchain.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;curl http://localhost:4567/mine
&lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="s2"&gt;"message"&lt;/span&gt;:&lt;span class="s2"&gt;"New Block Forged"&lt;/span&gt;,
  &lt;span class="s2"&gt;"index"&lt;/span&gt;:3,
  &lt;span class="s2"&gt;"transactions"&lt;/span&gt;:[
    &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="s2"&gt;"sender"&lt;/span&gt;:&lt;span class="s2"&gt;"d4ee26eee15148ee92c6cd394edd974e"&lt;/span&gt;,
      &lt;span class="s2"&gt;"recipient"&lt;/span&gt;:&lt;span class="s2"&gt;"someone-other-address"&lt;/span&gt;,
      &lt;span class="s2"&gt;"amount"&lt;/span&gt;:5
    &lt;span class="o"&gt;}&lt;/span&gt;,
    &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="s2"&gt;"sender"&lt;/span&gt;:&lt;span class="s2"&gt;"0"&lt;/span&gt;,
      &lt;span class="s2"&gt;"recipient"&lt;/span&gt;:&lt;span class="s2"&gt;"caedf4a87f7841838061dd7dffca2916"&lt;/span&gt;,
      &lt;span class="s2"&gt;"amount"&lt;/span&gt;:1
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;]&lt;/span&gt;,
  &lt;span class="s2"&gt;"proof"&lt;/span&gt;:183390,
  &lt;span class="s2"&gt;"previous_hash"&lt;/span&gt;:&lt;span class="s2"&gt;"91d2d1296158c7faf58a37ddc152593d"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3.5 Check the Blockchain’s State
&lt;/h3&gt;

&lt;p&gt;Though the response looks longer, let’s investigate it again. You can find the 1st block to the 3rd block were chained.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;curl http://localhost:4567/chain
&lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="s2"&gt;"chain"&lt;/span&gt;:[
    &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="s2"&gt;"index"&lt;/span&gt;:1,
      &lt;span class="s2"&gt;"timestamp"&lt;/span&gt;:&lt;span class="s2"&gt;"2018-01-09 22:04:06 +0700"&lt;/span&gt;,
      &lt;span class="s2"&gt;"transactions"&lt;/span&gt;:[],
      &lt;span class="s2"&gt;"proof"&lt;/span&gt;:1,
      &lt;span class="s2"&gt;"previous_hash"&lt;/span&gt;:100
    &lt;span class="o"&gt;}&lt;/span&gt;,
    &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="s2"&gt;"index"&lt;/span&gt;:2,
      &lt;span class="s2"&gt;"timestamp"&lt;/span&gt;:&lt;span class="s2"&gt;"2018-01-09 22:04:11 +0700"&lt;/span&gt;,
      &lt;span class="s2"&gt;"transactions"&lt;/span&gt;:[
        &lt;span class="o"&gt;{&lt;/span&gt;
          &lt;span class="s2"&gt;"sender"&lt;/span&gt;:&lt;span class="s2"&gt;"0"&lt;/span&gt;,
          &lt;span class="s2"&gt;"recipient"&lt;/span&gt;:&lt;span class="s2"&gt;"caedf4a87f7841838061dd7dffca2916"&lt;/span&gt;,
          &lt;span class="s2"&gt;"amount"&lt;/span&gt;:1
        &lt;span class="o"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;]&lt;/span&gt;,
      &lt;span class="s2"&gt;"proof"&lt;/span&gt;:94813,
      &lt;span class="s2"&gt;"previous_hash"&lt;/span&gt;:&lt;span class="s2"&gt;"84e609b88e68764ac4546cb807d7cf0e"&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;,
    &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="s2"&gt;"index"&lt;/span&gt;:3,
      &lt;span class="s2"&gt;"timestamp"&lt;/span&gt;:&lt;span class="s2"&gt;"2018-01-09 22:06:40 +0700"&lt;/span&gt;,
      &lt;span class="s2"&gt;"transactions"&lt;/span&gt;:[
        &lt;span class="o"&gt;{&lt;/span&gt;
          &lt;span class="s2"&gt;"sender"&lt;/span&gt;:&lt;span class="s2"&gt;"d4ee26eee15148ee92c6cd394edd974e"&lt;/span&gt;,
          &lt;span class="s2"&gt;"recipient"&lt;/span&gt;:&lt;span class="s2"&gt;"someone-other-address"&lt;/span&gt;,
          &lt;span class="s2"&gt;"amount"&lt;/span&gt;:5
        &lt;span class="o"&gt;}&lt;/span&gt;,
        &lt;span class="o"&gt;{&lt;/span&gt;
          &lt;span class="s2"&gt;"sender"&lt;/span&gt;:&lt;span class="s2"&gt;"0"&lt;/span&gt;,
          &lt;span class="s2"&gt;"recipient"&lt;/span&gt;:&lt;span class="s2"&gt;"caedf4a87f7841838061dd7dffca2916"&lt;/span&gt;,
          &lt;span class="s2"&gt;"amount"&lt;/span&gt;:1
        &lt;span class="o"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;]&lt;/span&gt;,
      &lt;span class="s2"&gt;"proof"&lt;/span&gt;:183390,
      &lt;span class="s2"&gt;"previous_hash"&lt;/span&gt;:&lt;span class="s2"&gt;"91d2d1296158c7faf58a37ddc152593d"&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;]&lt;/span&gt;,
  &lt;span class="s2"&gt;"length"&lt;/span&gt;:3
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Reflection
&lt;/h2&gt;

&lt;p&gt;It is &lt;strong&gt;very intuitive&lt;/strong&gt; to write it down with the simple code. We can know how it adds a transaction and it confirms the block by calculating the hash value.&lt;/p&gt;

&lt;p&gt;In the original version, there’s the consensus implementation, but I skipped it. I’ll implement it when I can!&lt;/p&gt;

&lt;p&gt;Repository in Github: &lt;a href="https://github.com/takp/blockchain-ruby"&gt;https://github.com/takp/blockchain-ruby&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://takp.me/about"&gt;@takp&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>blockchain</category>
    </item>
  </channel>
</rss>
