Blockchain Explained in 7 Python Functions
It wasn’t until I wrote my own simple Blockchain, that I truly understood what it is and the potential applications for it. So without further ado, lets set up our 7 functions!
By Tom Cusack, Data Scientist in the Banking Sector
I think for many people out there, Blockchain is this phenomenon, which is hard to get your head around. I started watching videos and reading articles, but for me personally, it wasn’t until I wrote my own simple Blockchain, that I truly understood what it is and the potential applications for it.
The way I think about blockchain is it is an encrypted database that is public. If you were Amazon and you wanted to use the technology to track your stock levels, would using Blockchain make sense? Probably not, since your customers won’t want to expend their resources verifying your blockchain, since they state on their website, ‘Only 1 left!’, anyway.
I’ll leave you to think about future applications. So without further ado, lets set up our 7 functions!
At the heart of the blockchain is the hashing function. Without encryption, the blockchain will be easily manipulable and transactions will be able to be fraudulently inserted.
The ‘state’ is the record of who owns want. For example, I have 10 coins and I give 1 to Medium, then the state will be the value of the dictionary below.
The important thing to note is that overdrafts cannot exist. If there are only 10 coins in existence, then I cannot give 11 coins to someone. The below function verifies that the transaction we attempt to make is indeed valid. Also, a transaction must balance. I cannot give 5 coins and have the recipient receive 4 coins, since that would allow the destruction and creation of coins.
Now, we can make our block. The information from the previous block is read, and used to link it to the new block. This, too, is central to the idea of blockchain. Seemingly valid transactions can be attempted to fraudulently be inserted into the blockchain, but decrypting all the previous blocks is computationally (nearly) impossible, which preserves the integrity of the blockchain.
Below is a small helper function to check the hash of the previous block:
Once we have assembled everything together, its time to create our block. We will now update the blockchain.
Before we are finished, the chain must be verified:
Finally, need a transaction function, which hangs all of the above together:
So, now we have our 7 functions. How do we interact with it? Well, first we need to start our chain with a Genesis Block. This is the inception of our new coin (or stock inventory, etc). For the purposes of this article, I will say that I, Tom, will start off with 10 coins.
Now, look what happens when I give some coin to Medium:
The state gets updated to show who has what:
And the blockchain looks like this:
Our first new transaction has been created and inserted to the top of the stack. Now, I hope I have piqued your curiosity, and are interested in copying the code down and playing with it. In my opinion, this is the best way to learn a new technology — Get inside it.
Play with the code and make your own coin. What happens if you try and give more coins than exist? What happens to the state if you keep creating new payees?
Can you think of future applications for blockchain? Feel free to ask me anything in the comments, and I’ll try and help.
Bio: Tom Cusack is a Data Scientist in the Banking Sector.
Original. Reposted with permission.
- Blockchains and APIs
- The Convergence of AI and Blockchain: What’s the deal?
- Swiftapply – Automatically efficient pandas apply operations
Top Stories Past 30 Days