LoginSignup
11
10

More than 5 years have passed since last update.

Rubyでブロックチェーンを作る

Posted at

Rubyで簡易ブロックチェーンを作ってみた。
参考サイト→https://qiita.com/weedslayer/items/d1aabe7cf31d182481fb#_reference-3ae72ffd32307f577de2

Blockクラス

チェーン上におけるブロックのインデックス、生成時刻、データ、前のブロックのハッシュ値、自ブロックのハッシュを持つ。自ブロックのハッシュはブロックの保持する情報を結合した値をSHA-256に通した値である。

BlockChainクラス

当クラス生成時に最初のブロックを生成する。block_appendで次のブロックを生成する。

blockchain.rb
module ArrayHashExtension
  refine Array do
    def create_hash
      require 'digest/sha2'
      Digest::SHA256.hexdigest self.join
    end
  end
end

using ArrayHashExtension
class Block
  attr_reader :index, :hash

  def initialize i, timestamp, data, previous_hash
    @index = i
    @timestamp = timestamp
    @data = data
    @previous_hash = previous_hash
    @hash = [@index, @timestamp, @data, @previous_hash].create_hash

    puts "ブロックが生成されました => index: #{@index} timestamp: #{@timestamp} data: #{@data}\nhash: #{@hash}"
  end
end

class BlockChain
  attr_reader :chain

  def initialize
    @chain = []
    @chain << Block.new(0, Time.now, "origin", "0")
  end

  def append_block
    current_block = @chain.last
    next_index = current_block.index + 1
    next_timestamp = Time.now
    next_data = "114514"
    next_hash = current_block.hash
    @chain << Block.new(next_index, next_timestamp, next_data, next_hash)
  end
end

chain = BlockChain.new
50.times { |i| chain.append_block }

実行結果

ブロックが生成されました => index: 0 timestamp: 2017-10-19 17:41:39 +0900 data: origin
hash: 619ceb50349d32c31972fc5cbe2fef4542375d3e10cc34fea75d49b9f4acd3d2
ブロックが生成されました => index: 1 timestamp: 2017-10-19 17:41:39 +0900 data: 114514
hash: c2f88c03ca246ad76745d3198356b2e98798f95306919689d64fb412556d859e
ブロックが生成されました => index: 2 timestamp: 2017-10-19 17:41:39 +0900 data: 114514
hash: ac3d08bc6767c8ef70c5221a2dc02faa90f2d654be25ae13be4c67158da80f63
ブロックが生成されました => index: 3 timestamp: 2017-10-19 17:41:39 +0900 data: 114514
hash: e3ec5c1cffaef401c475a19c6c9d04bfbf355b9158b21044c0030d537314eb53
ブロックが生成されました => index: 4 timestamp: 2017-10-19 17:41:39 +0900 data: 114514
hash: 46d1071a9ebacad550259ea6d6aac11c5d7205e03999727311eeb642b9648fcf
ブロックが生成されました => index: 5 timestamp: 2017-10-19 17:41:39 +0900 data: 114514
hash: 663f1d12ce08fd9ddf6418757dec00f1dd913c1a902fc1105447a9aa7464b2c7
ブロックが生成されました => index: 6 timestamp: 2017-10-19 17:41:39 +0900 data: 114514
hash: d37560c83f0664f09862434b18ed4da3208de2b2ed96eb2053dbcb3fa9e8b295
ブロックが生成されました => index: 7 timestamp: 2017-10-19 17:41:39 +0900 data: 114514
hash: 5d731e49f998cac3267cad052a4b751e04958565ad0038460999642397c26fab
ブロックが生成されました => index: 8 timestamp: 2017-10-19 17:41:39 +0900 data: 114514
hash: eaa5b11a31c3667b1958f8adb15ee2ff5142d531d026ffd7114e8142397d9210
ブロックが生成されました => index: 9 timestamp: 2017-10-19 17:41:39 +0900 data: 114514
hash: b605f9809f2668a94ea7b4a4f292a3cf0d0a8c1e80d24bb65c9631fe48f41cc9
ブロックが生成されました => index: 10 timestamp: 2017-10-19 17:41:39 +0900 data: 114514
hash: 0248b3a40937fe83cee3644928b7e9df5c66b5a5d27e9a679ff6bf49b58d3205
ブロックが生成されました => index: 11 timestamp: 2017-10-19 17:41:39 +0900 data: 114514
hash: 222525062d26c483fe68d27a443823f25428bbf725bc7e87812c0cf88ca31183
ブロックが生成されました => index: 12 timestamp: 2017-10-19 17:41:39 +0900 data: 114514
hash: 5f9131f3a2d1d620ec59e4267bc336d9412ff0fe0ef3c0b14dcf07bdd9ebfb84
11
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
10