Skip to content

Instantly share code, notes, and snippets.

@TestSubjector
Last active August 12, 2024 06:27
Show Gist options
  • Save TestSubjector/efae35941009dd5dc33aa639ff439b3f to your computer and use it in GitHub Desktop.
Save TestSubjector/efae35941009dd5dc33aa639ff439b3f to your computer and use it in GitHub Desktop.
A very basic implementation of a blockchain in Julia.
using SHA
using Dates
"""
An individual block structure
"""
struct Block
index::Int
timestamp::DateTime
data::String
previous_hash::String
hash::String
"""
The constructor which will also create hash for the block.
It will use 256 bit encryption for the blockchain's security needs.
"""
function Block(index, timestamp, data, previous_hash)
hash = sha2_256(string(index, timestamp, data, previous_hash))
new(index, timestamp, data, previous_hash, bytes2hex(hash))
end
end
"""
Add another block to the chain.
You can't have a blockchain with just one block after all.
"""
function next_block(tail_block::Block)
new_index = tail_block.index + 1
return Block(new_index, Dates.now(), string("This is block ",new_index), tail_block.hash)
end
# Create the special first block or the head of the blockchain.
Blockchain = [Block(0, Dates.now(), "Genesis Block", "0")]
println("Genesis Block : 1")
println("Hash :", Blockchain[1].hash)
# Size of the blockchain
Blockchain_limit = 13
# To make addition of blocks to the blockchain non-arbitary (unlike here),
# a proof of work task is required.
for tail = 1:Blockchain_limit
# Link the new block to the chain
append!(Blockchain, [next_block(Blockchain[tail])])
# The details of the block
println("Block : $(tail+1)")
println("Hash :", Blockchain[tail+1].hash)
end
# Reference - https://medium.com/crypto-currently/lets-build-the-tiniest-blockchain-e70965a248b
@woulschneider
Copy link

woulschneider commented Jul 9, 2019 via email

@TestSubjector
Copy link
Author

@andremillet In almost all cases, the blockchain that you would have will (have to) be the same as everyone else's. This is so that everything is in sync. The usefulness of a blockchain is mostly apparent only when it is used by several groups together.

Essentially as you are a healtcare provider, how it will work from your side is that - You upload your patients healtcare information in your existing database. A hash (encryted output) will be generated and sent to the blockchain (authorised by you to receive that hash).

Please have a look at various use cases for blockchain in healthcare and in particular, Use Case #3 which seems to be the most similar to your needs.

If you have any more queries feel free to DM me at [email protected].

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