Created
January 2, 2018 15:22
-
-
Save sergio-bobillier/19be3d9c8c7f5c81f590d2a1fcd52874 to your computer and use it in GitHub Desktop.
Stripped-down implementation of a blockchain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'date' | |
require 'digest' | |
class Block | |
attr_reader :data, :hash, :parent, :author, :created_at | |
def initialize(data:, parent:, author:) | |
raise ArgumentError.new '`data` should respond to `to_s`' unless data.respond_to?(:to_s) | |
raise ArgumentError.new '`author` should respond to `to_s`' unless author.respond_to?(:to_s) | |
unless parent.nil? || parent.is_a?(Block) | |
raise ArgumentError.new '`parent` should be an instance of `Block` or `nil`' | |
end | |
@data = data | |
@parent = parent | |
@author = author | |
@created_at = DateTime.now | |
@hash = [@data.to_s, @author.to_s, @created_at.to_s] | |
@hash << @parent.hash if @parent | |
@hash = Digest::SHA256.hexdigest(@hash.join) | |
end | |
def to_s | |
s = "#{@hash}\n" | |
s += "Created at: #{@created_at.to_s}\n" | |
s += "Parent: #{@parent.hash}\n" if @parent | |
s += "Author: #{@author}\n" if @author | |
s += "\n" | |
data_string = @data | |
while data_string.length > 76 do | |
s += " #{data_string.slice!(0, 76)}\n" | |
end | |
s | |
end | |
end | |
class BlockChain | |
attr_reader :head | |
def initialize(block = nil) | |
unless block.nil? || block.is_a?(Block) | |
raise ArgumentError '`block` should be an instance of `Block` or `nil`' | |
end | |
@head = block if block | |
end | |
def <<(block) | |
unless block.is_a?(Block) | |
raise ArgumentError.new '`block` should be an instance of `Block`' | |
end | |
unless @head == block.parent | |
raise ArgumentError.new "`block`'s parent doesn't match the chain's head" | |
end | |
@head = block | |
end | |
end | |
chain = BlockChain.new | |
b = Block.new( | |
data: "Lorem ipsum dolor amet vegan stumptown copper mug chicharrones scenester small batch plaid viral flannel. Microdosing meh vice kale chips kitsch. Edison bulb bushwick man braid, farm-to-table aesthetic cornhole whatever everyday carry sustainable four loko brooklyn cred. Yuccie typewriter raw denim leggings. 90's tote bag everyday carry before they sold out. Selvage knausgaard etsy, sartorial gastropub migas vinyl sustainable vegan cornhole seitan cred.", | |
parent: chain.head, | |
author: "Sergio") | |
chain << b | |
b2 = Block.new( | |
data: "Synth truffaut palo santo bicycle rights swag plaid pork belly mustache fingerstache selvage banjo asymmetrical brooklyn enamel pin biodiesel. Heirloom vape kale chips sustainable. Health goth polaroid edison bulb, succulents marfa squid you probably haven't heard of them. Crucifix franzen kitsch, lomo pok pok pug bicycle rights shaman. Chillwave letterpress twee, poke thundercats kombucha snackwave gentrify lyft blue bottle cold-pressed celiac offal. Portland messenger bag paleo semiotics art party flannel.", | |
parent: chain.head, | |
author: "Sergio" | |
) | |
chain << b2 | |
b3 = Block.new( | |
data: "Synth truffaut palo santo bicycle rights swag plaid pork belly mustache fingerstache selvage banjo asymmetrical brooklyn enamel pin biodiesel. Heirloom vape kale chips sustainable. Health goth polaroid edison bulb, succulents marfa squid you probably haven't heard of them. Crucifix franzen kitsch, lomo pok pok pug bicycle rights shaman. Chillwave letterpress twee, poke thundercats kombucha snackwave gentrify lyft blue bottle cold-pressed celiac offal. Portland messenger bag paleo semiotics art party flannel.", | |
parent: chain.head, | |
author: "Sergio" | |
) | |
chain << b3 | |
block = chain.head | |
while block do | |
puts block | |
puts | |
block = block.parent | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment