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
@scheung38
Copy link

How does it run? Could you expand on it thanks.

@TestSubjector
Copy link
Author

@scheung38 Apologies for the delay in replying; for some reason Github didn't notify me about your comment on this Gist.
All you need to do is download and extract the Julia binary for your system.
Then run the Julia executable (in the bin folder) and download the SHA package. You can find information on how to install packages for Julia here.

Finally, download this code and run this command on your terminal "path/to/julia/executable Blockchain.jl'
I've made a minor edit so that this code works for Julia v1.1.

@woulschneider
Copy link

@TestSubjector do you think it could be implemented in a Eletronic Medical Record ?

@TestSubjector
Copy link
Author

TestSubjector commented Jul 9, 2019

@andremillet This is just a quick proof-of-concept. But yes, if developed into a proper blockchain tool, it can be used for EMRs. In fact countries like Estonia and Australia apparently already use blockchain technology to maintain their EMRs.
Of course getting organisations to agree to store their patients' medical records on such a system, may well be a bigger challenge than implementing the code itself.

@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