Week 1: Blockchain Basics (Hash Functions And Blocks)
March 31, 2023
During the first week of this project, I decided it was important to code a simple blockchain implementation using Python. You can observe my current project and view it as it evolves here on GitHub. The point of this is to explore the ins and outs of blockchain technology on a mechanical level. I will delve into the decisions I made when coding this and how it works next week but I think this first blog should be dedicated to providing a foundational understanding of blockchain conceptually.
Firstly we must define a block. A block’s most important feature is that it contains the transaction data of the blockchain. So bitcoin’s blockchain network these blocks contain information like “Morgan pays Matt 0.1 bitcoin.” By approving (a process which I will further explain throughout this project) and adding these blocks to the chain these transactions are agreed upon and are then unable to be modified. On the coding side, these blocks have a few additional attributes: the index of which number block it is, the timestamp of the block when it was added to chain, the hash of the block (which is very important and will be explained in the next paragraph), the hash of the previous block in the chain, and a nonce value (which is a computer science term for a nonsense value that is important for hashing of the block).
A hash is a digital signature. Hashes are the returned value of a hash function. Hash functions are mathematical one-directional functions that can crunch bits of information and output a unique hexadecimal string of characters of set length. These functions are deterministic meaning the same information will always return the same hash output. If you change the information even slightly the returned hash will alter completely. An important component of hash functions is that it is nearly impossible to decipher an input when you only know the output making it a one-directional function. In blockchains, these functions are used to hash all the information within a block as a simple way to verify that the information within the block has not been changed.
You can see in this image that when the inputted “n” in “Morgan” is removed from the top the resulting hash is a completely different and unique output. This function is the SHA-256 hash function which is the same one used in bitcoin hashing, however, there are plenty of alternatives one can use. Feel free to play around with this hashing function here.
These hash functions are critical to a bitcoins proof of work consensus algorithm. As I mentioned earlier, this is the process of approving and adding blocks to the chain. They are called consensus algorithms because it is the method these chains use to determine which node (or computer on the network) will validate the block information and add it to the chain. The proof of work algorithm works by computers adjusting the nonce value of the block until the resulting hash begins with a certain amount of leading zeros. Once a node finds a proper hash, the node then validates all the transactions, appends the block to the chain, and shares it across the network.