Skip to content

Instantly share code, notes, and snippets.

@artemgurzhii
Created September 19, 2017 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artemgurzhii/2bdb8a0f1e318308bd807777ee80b783 to your computer and use it in GitHub Desktop.
Save artemgurzhii/2bdb8a0f1e318308bd807777ee80b783 to your computer and use it in GitHub Desktop.
require 'digest'
class Block
attr_reader :index, :hash
def initialize(index:, timestamp: Time.now, data:, previous_hash:)
@data = data
@index = index
@timestamp = timestamp
@previous_hash = previous_hash
str = [index, timestamp, data, previous_hash].map(&:to_s).to_s
@hash = Digest::SHA256.hexdigest(str)
end
end
b0 = Block.new(index: 0, data: 'Zero', previous_hash: '0')
b1 = Block.new(index: b0.index + 1, data: 'First', previous_hash: b0.hash)
b2 = Block.new(index: b1.index + 1, data: 'Second', previous_hash: b1.hash)
[b0, b1, b2].each do |block|
p block
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment