Skip to content

Instantly share code, notes, and snippets.

View bradford-hamilton's full-sized avatar

Bradford Lamson-Scribner bradford-hamilton

View GitHub Profile
module Crystal::Blockchain
blockchain = [] of NamedTuple(
index: Int32,
timestamp: String,
data: String,
hash: String,
prev_hash: String
)
end
module Block
extend self
end
def create(index, timestamp, data, prev_hash)
block = {
index: index,
timestamp: timestamp,
data: data,
prev_hash: prev_hash
}
block.merge({ hash: self.calculate_hash(block) })
end
def calculate_hash(block)
plain_text = "
#{block[:index]}
#{block[:timestamp]}
#{block[:data]}
#{block[:prev_hash]}
"
sha256 = OpenSSL::Digest.new("SHA256")
sha256.update(plain_text)
sha256.to_s
blockchain << Block.create(0, Time.now.to_s, "Genesis block's data!", "")
blockchain << Block.create(0, Time.now.to_s, "Genesis block's data!", "")
get "/" do
blockchain.to_json
end
Kemal.run
blockchain = [] of NamedTuple(
index: Int32,
timestamp: String,
data: String,
hash: String,
prev_hash: String,
difficulty: Int32,
nonce: String
)
def create(index, timestamp, data, prev_hash)
block = {
index: index,
timestamp: timestamp,
data: data,
prev_hash: prev_hash
difficulty: self.difficulty,
nonce: ""
}
end
def difficulty
3
end
def generate(last_block, data)
new_block = self.create(
last_block[:index] + 1,
Time.now.to_s,
data,
last_block[:hash]
)
i = 0