This file contains 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
module Crystal::Blockchain | |
blockchain = [] of NamedTuple( | |
index: Int32, | |
timestamp: String, | |
data: String, | |
hash: String, | |
prev_hash: String | |
) | |
end |
This file contains 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
module Block | |
extend self | |
end |
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
blockchain << Block.create(0, Time.now.to_s, "Genesis block's data!", "") |
This file contains 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
blockchain << Block.create(0, Time.now.to_s, "Genesis block's data!", "") | |
get "/" do | |
blockchain.to_json | |
end | |
Kemal.run |
This file contains 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
blockchain = [] of NamedTuple( | |
index: Int32, | |
timestamp: String, | |
data: String, | |
hash: String, | |
prev_hash: String, | |
difficulty: Int32, | |
nonce: String | |
) |
This file contains 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
def create(index, timestamp, data, prev_hash) | |
block = { | |
index: index, | |
timestamp: timestamp, | |
data: data, | |
prev_hash: prev_hash | |
difficulty: self.difficulty, | |
nonce: "" | |
} | |
end |
This file contains 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
def difficulty | |
3 | |
end |
This file contains 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
def generate(last_block, data) | |
new_block = self.create( | |
last_block[:index] + 1, | |
Time.now.to_s, | |
data, | |
last_block[:hash] | |
) | |
i = 0 |
OlderNewer