Right… but how does blockchain REALLY work?
A follow-up on our Flatiron Presents: Blockchain presentation.
Last week, Hannah Chang and I gave a presentation (you can view it here) for Flatiron Presents, a series in which Flatiron students give a talk on a technical subject of their choosing. Hey, blockchain is pretty cool, we thought. And no one REALLY understands how it works. Surely WE can explain it to everyone…
Turns out, blockchain is really confusing. After every article/blog post I read, YouTube video I watched, everything seemed to make sense. But when I would come back to the material the following day, I would find my head all scrambled again. This stuff is difficult to grasp. After our presentation, we had difficulty
Luckily, I stumbled upon a super cool website (there’s always a website) that demos blockchain technology visually. Shoutout to Sean Han for building both https://blockchaindemo.io/ and this awesome Blockchain CLI app. As you read through this post, click through the tutorial on the site to see how this stuff works in action. This post is largely meant as a walk-through for my cohort as I demo the web app and CLI app to the group.
The Genesis Block
The “Genesis Block” on a blockchain is simply the first block in the chain. It has an index of 0. Because there are no preceding blocks, the previous hash value on the genesis block is simply zero. The genesis block contains data pertaining to the first group of transactions that we are trying to encode into the blockchain, and that data is used to generate a valid hash value for that data. Hmm… what the heck does that mean?
Valid Hash Values
You’ll need to have a cursory understanding of the concept of hashing in order to proceed. Per Lisk Academy, hashing is the process of converting an input of any length into a fixed size string of text, using a mathematical function. This means that any data, no matter how long, can be converted into an array of numbers and letters (a “hash value”) using an algorithm. Each hash value is unique, meaning it should be impossible to produce the same hash value with different inputs. Small changes to hash input data produce hugely different hash values. As it pertains to blockchain, all you really need to know is that hash values are used to represent the current state of a blockchain (aka all the data in the blockchain). This guarantees its immutability because any minor changes by any one member of the blockchain will produce a wildly different hash value on their local copy, making them easy to identify and discard.
Here is Lisk’s video on hash functions, have fun:
Mining a Block
When users “mine” a bitcoin block, essentially what they are doing is changing the nonce, which is a sort of key that is applied to the block’s data (the input into the hash function) to try to produce a valid hash. Miners are simply iterating over possible nonce values until they produce a valid hash value. In blockchain context, a valid hash is just a hash value that has a certain number of leading zeroes, as specified by that blockchain. The number of leading zeroes is referred to as difficulty. Per https://blockchaindemo.io/, as difficulty increases (i.e. the more zeroes that are required at the beginning of a valid hash), the number of possible valid hashes decreases. With fewer possible valid hashes, it takes more processing power to find a valid hash.
Each subsequent block following the genesis block must incorporate the hash value of the preceding block. That hash value is incorporated into the data of the new block, which is used to compute the hash value of the new block. Thus, any change to data on an “earlier” block will fundamentally change the data on subsequent blocks. We know that even minor changes to hash inputs will produce different hash values. The new hash will not have the requisite number of leading zeros, and therefore becomes invalid.
So, in order to mutate the data in any given block, the block must be “re-mined” for a new valid hash value. Then, the subsequent blocks must be re-mined as well (remember that subsequent blocks hash values are dependent upon preceding ones). Because new blocks are being mined, added, and verified to the chain by other nodes all the time, one user would have to “outmine” the entire network to mutate a block.
But, this immutability depends upon a robust network of users collaborating to keep the blockchain updated and secure. Each user that contributes to the blockchain is called a node. In a “proof-of-work” system (another can of worms), nodes compete to mine blocks to add to the chain. Essentially, nodes are constantly:
- Broadcasting their own local copy of the blockchain to the other nodes. And…
- Requesting updated copies from other nodes’ blockchains.
- Checking received copies against their own local copy, updating the local copy if needed. When comparing copies, longer valid chains take precedent over shorter ones. Eventually, all nodes have a copy of the longest valid chain, which becomes the “official” blockchain.
Trust in the system will be key to mainstream adoption of blockchain technology in the long run. Resources such as Sean Han’s can go a long way toward educating the general public about the underlying tech and thus help to build trust and interest in these platforms. Great job on a killer app!