Skip to content

Instantly share code, notes, and snippets.

@chrisallick
Created July 18, 2017 14:31
Show Gist options
  • Save chrisallick/cb196b13555c86f9193f3ec4f7f15ecc to your computer and use it in GitHub Desktop.
Save chrisallick/cb196b13555c86f9193f3ec4f7f15ecc to your computer and use it in GitHub Desktop.
tiny blockchain in ruby
#https://medium.com/crypto-currently/lets-build-the-tiniest-blockchain-e70965a248b
#http://ruby-for-beginners.rubymonstas.org/writing_classes/self.html
#https://stackoverflow.com/questions/33768598/ruby-sha-256-hexidigest-values-are-different-from-what-python-generates
#import hashlib as hasher
require "rubygems"
require "digest"
# class Block:
# def __init__(self, index, timestamp, data, previous_hash):
# self.index = index
# self.timestamp = timestamp
# self.data = data
# self.previous_hash = previous_hash
# self.hash = self.hash_block()
# def hash_block(self):
# sha = hasher.sha256()
# sha.update(str(self.index) +
# str(self.timestamp) +
# str(self.data) +
# str(self.previous_hash))
# return sha.hexdigest()
class Block
def initialize(_index, _timestamp, _data, _previous_hash)
@index = _index
@timestamp = _timestamp
@data = _data
@previous_hash = _previous_hash
@hash = self.hash_block
end
def index
@index
end
def timestamp
@timestamp
end
def data
@data
end
def previous_hash
@previous_hash
end
def hash
@hash
end
def hash_block
sha = Digest::SHA256.new
sha.update @index.to_s + @timestamp.to_s + @data.to_s + previous_hash.to_s
sha.hexdigest
end
end
# def create_genesis_block():
# # Manually construct a block with
# # index zero and arbitrary previous hash
# return Block(0, date.datetime.now(), "Genesis Block", "0")
def create_genesis_block
Block.new(0, Time.now.strftime("%Y-%m-%d %H:%M:%S.%6N"), "Genesis Block", "0")
end
# def next_block(last_block):
# this_index = last_block.index + 1
# this_timestamp = date.datetime.now()
# this_data = "Hey! I'm block " + str(this_index)
# this_hash = last_block.hash
# return Block(this_index, this_timestamp, this_data, this_hash)
def next_block(last_block)
this_index = last_block.index + 1
this_timestamp = Time.now.strftime("%Y-%m-%d %H:%M:%S.%6N")
this_data = "Hey! I'm a block" + this_index.to_s
this_hash = last_block.hash
Block.new(this_index, this_timestamp, this_data, this_hash)
end
# # Create the blockchain and add the genesis block
# blockchain = [create_genesis_block()]
# previous_block = blockchain[0]
blockchain = [create_genesis_block]
previous_block = blockchain[0]
# # How many blocks should we add to the chain
# # after the genesis block
# num_of_blocks_to_add = 20
num_of_blocks_to_add = 20
# # Add blocks to the chain
# for i in range(0, num_of_blocks_to_add):
# block_to_add = next_block(previous_block)
# blockchain.append(block_to_add)
# previous_block = block_to_add
# # Tell everyone about it!
# print "Block #{} has been added to the blockchain!".format(block_to_add.index)
# print "Hash: {}\n".format(block_to_add.hash)
for i in 0..num_of_blocks_to_add
block_to_add = next_block(previous_block)
blockchain.push block_to_add
previous_block = block_to_add
puts "Block #{block_to_add.index} has been added to blockchain!"
puts "Hash: #{block_to_add.hash}\n"
end
# chrisallick@DRO-1367 : ~/Desktop
# $ ruby blockchain.rb
# Block 1 has been added to blockchain!
# Hash: 33e4326c77b75f177b9a69150df66f9a830ba65875b046ff50f29f2982bbba77
# Block 2 has been added to blockchain!
# Hash: 823435427ebf4d44bd10b89c2ea49079a9930d065955f36dab26b38d1bc3bc2b
# Block 3 has been added to blockchain!
# Hash: 59c88b788637d4a1695b8c2c2150e5199ddfcb75504474312b584ec2d57d3fbb
# Block 4 has been added to blockchain!
# Hash: 49db4fd4a6a19ee6de952d62afff646f4a41fc17442e0b1144d7543dcc6b8180
# Block 5 has been added to blockchain!
# Hash: 7b1975a2012a67ba115c424e4d2967b6ab241c32ac3ab9fe30909537d5e4865b
# Block 6 has been added to blockchain!
# Hash: 815e182d53d344ecd964d2ba65dc3e5bb6be8bec8e0eb5f51215756109f4b777
# Block 7 has been added to blockchain!
# Hash: 41e5de47ee57d038197d7ecddba9e7a56233e74ac0b0a87d3e7605142b81e941
# Block 8 has been added to blockchain!
# Hash: 55a6242e12cfec2a9f61962144a1801e162172f21400f8324e22f43143b9fd49
# Block 9 has been added to blockchain!
# Hash: 33b5722748b860c4a26ce39de80885b26135f16497bdf438da5ffc004bc05a48
# Block 10 has been added to blockchain!
# Hash: 1d28c921990cec1f4ade76e0385d7ab58d51561394d033ab07582793af1081b1
# Block 11 has been added to blockchain!
# Hash: dbff57b6c573595db7a8021e647b14af93d933e50a8f81ee8cb00c2d97bc0ffd
# Block 12 has been added to blockchain!
# Hash: c26a2d6c896c694e47f14f17c41ff9225bdd38bca55eb501513208c3ddef0a20
# Block 13 has been added to blockchain!
# Hash: 512a62acf2a3afc02b347b121ed386826fd991023b8e8d8aa53abbe2e56d16ea
# Block 14 has been added to blockchain!
# Hash: aeedc27ed62d9049a663dbdd8fd4b60a35063d24a07d2e46018f3188c031aaab
# Block 15 has been added to blockchain!
# Hash: 02e731c32484443e6a202b319af618614241c2dccd4016f4956a6b7664e4ce16
# Block 16 has been added to blockchain!
# Hash: 378be7c5f55884db86d2ad1bed51101ae17b64781c87fb46c8812a3884735e39
# Block 17 has been added to blockchain!
# Hash: 378cfae4fdf2e7ee0b8d273425cedbcdd7c0fa726a8be413ddc69a125e870ff1
# Block 18 has been added to blockchain!
# Hash: 4546e7a36a2927d67cc22c827c835a261042d600e658e7dba55fb48726f3dd6a
# Block 19 has been added to blockchain!
# Hash: 1d6f1962dba3d95f60f8ebbbc434eb5d1bbd455f41c4a780fcfb675fbd60b2b7
# Block 20 has been added to blockchain!
# Hash: 133266bac4bccd76a9f8d0f711f5f67710ce89cd3ab2c8b3739d51c44a6d1691
# Block 21 has been added to blockchain!
# Hash: 2e23d602ab68d4e6105d7c2103ddd9e3d3fc9a74a993fff0028c04f1129a6814
@nothinghope
Copy link

you do good thing in ruby.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment