Explain By Example: Blockchain
Disclaimer: The following content is not affiliated with Microsoft.
Sometime back around July, I got challenged to 'build a blockchain'. I remember thinking, "Yeah alright, how hard can it be? It's just Elon buying dog-themed memes, right?".
Three months down this massive rabbit hole that I never intended to deep dive into...you know what, let's cut the complaining and get straight into it.
What is blockchain??
Everyone is obsessed with it, especially with the extraordinary monetary value inflation of cryptocurrencies. You might have heard of it…bitcoin.
The whole idea of bitcoin came about around the time of the 2007-2008 financial crisis, some anonymous person by the name of Satoshi Nakamoto published a paper called “Bitcoin: A Peer-to-Peer Electronic Cash System ” to bitcoin.org. Inside that paper, they proposed the idea of getting rid of the middleman through the concept of blockchain.
Getting rid of the middleman??
You know, the banks, the retailers, the middleman who are usually some trusted party that two parties engaged in a transaction go through because the two parties don’t inherently trust one another and need a third party as a “witness” to the transaction taking place.?
Think about it like wedding vows. You have one person exchange their wedding vows with another person. They’re meant to spend the rest of their lives with each other so inherently they should be able to trust one another yet the marriage is not official without a witness which is usually a pastor if you’re religious or some marriage officiant. In other words, the middleman.
So in other words, Mr. or Mrs. Satoshi Nakamoto proposed the idea of being legally married without a marriage officiant through the concept of blockchain. Wait, what?
How does that work??
Imagine if there was an official marriage registry book that is not owned by anybody but everyone can access it and everyone can take a copy of it. Every time a couple gets married (or divorced), it is recorded in this book. That means, we can track all the previous relationships a person may have had in the past. I know it sounds a little creepy but bear with me.?
Now, you might be wondering, “Well, what if someone wants to cheat?”?
How do we guarantee that a married person doesn’t try and marry someone else without first getting a divorce? In Nakamoto’s paper, they describe this as the double spending problem .
How do we prevent someone from cheating the system??
The trick is to make sure everyone knows about each marriage and divorce that takes place and timestamp it so that it is recorded in history and cannot be changed. So imagine if the timestamp gets a fingerprint signature over it. Now, that timestamp cannot be changed without modifying the fingerprint signature and if the fingerprint signature gets modified then we know someone is trying to cheat the system.?
Also, this fingerprint signature is uniquely tied to someone’s identity so now we have a record in history that proves the legitimacy of the marriage or divorce.?
In blockchain, we use asymmetric cryptography to provide this fingerprint signature.
With asymmetric cryptography , we have the concept of a public key and a private key. For simplicity sake, let’s say a public key is able to unlock a private key and vice versa. So if I wanted to send you some secret stuff, I might use my private key to lock up the secret stuff and then use your public key to lock up the secret stuff.?
When you get this secret stuff, you can then use my public key (which is publicly accessible) to do an unlock. It unlocks so you can verify that I did indeed send the secret stuff to you.?
Now, the secret stuff is still locked, it is locked with your public key and the only person who can unlock it is you who owns the private key so you use your private key to unlock it and viola!?We have just exchanged some secret stuff using asymmetric cryptography.?
Now, what is this secret stuff we are trying to lock and unlock? The transactions inside the blocks in the blockchain (of course!).
The blocks in the blockchain?
Blockchain is simply a chain of blocks.?
These blocks are made up of two things:?
Inside the block header, we can find:?
And other things like:?
Nakamoto’s paper first introduces us to the concept of Transactions. You may have heard of blockchain being described as a ‘distributed database’ or a ‘distributed general ledger that records transactions that everybody has a copy of’ and that is true because of the way these blocks work in the blockchain.
Just like the official marriage registry book example I gave above, these blocks which contains transactions like wedding vows and divorces are recorded in such a way that if someone tries to cheat, everyone would know because it would break the blockchain.?
How does someone break the blockchain?
Blocks in the blockchain are like bricks in a brick wall. If you decided to be a cheap (or a cheat in this case) by using a cardboard brick instead of a proper brick to lay down the next foundation, you’ll cause all of the rest of the bricks built on top of the cardboard brick to crumble and crash so the rest of the brick wall will no longer be valid.?
Blocks in the blockchain operate in the same manner. If you want to cheat the blockchain by modifying a block i.e. creating a fake brick and replacing a genuine brick that has already been verified then everyone will know because all other bricks after it will also become invalid. To quote the original paper, “To modify a past block, an attacker would have to redo the proof-of-work of the block and all blocks after it and then catch up with and surpass the work of the honest nodes”.?
That’s such a powerful statement in my opinion because it is saying, ‘You can try and cheat the system but that’s like trying to count to infinity and we got a head start.’?
Ok, so we can’t really modify a block which means we can’t really modify the transactions that have been committed to a block since the blocks themselves contain transactions inside it. And that’s why blockchain or the bitcoin blockchain became so famous. This whole idea that we can have a record of transactions between multiple parties that can’t be modified without having a trusted party to mediate all the transactions means we can add the ‘bank’s to bankruptcy.?
Wait hold on, what’s this ‘proof-of-work’ that you mentioned earlier??
You might have heard of the concept of mining blocks in the blockchain. That means each block needs to be discovered so we can add it to the blockchain.?
And how do we ‘discover’ these blocks? We mine them, of course! ?
To mine a block, we need to do some work. Or, to be more precise, we need to solve a computationally expensive problem. And find the right answer.?Now, it’s not a computationally complex problem, it is just expensive or difficult to find the right solution.?
Think about it this way. Let’s say I’m playing a guessing game with you. And the guessing game goes like this:?
Basically, there are three rules to this game.?
Rule number 1 says I’m a function that takes in a random number and adds it to a previous result.?
Rule 2 says this number can range between negative infinity and positive infinity.?
Rule 3 says the number I am thinking of is 456 .?
So, what’s ‘x’??
‘x’ could be anything.?
x could be 1 if previous was 455.?
x could be 218 if previous was 238.?
x could be 67 if previous was 389.?
And so on.?
But, I’ll start off the game and say the very first number which I’ll call the genesis block , is the number 199 .?
That’s our ‘x’.?
Basic arithmetic tells us that ‘x’ is 257 but let’s assume you don’t know how to do basic arithmetic. Instead, I give you a magical calculator called the SHA-256 calculator which will spit out random numbers and generate a result for you. However, because the numbers it spits out ranges between negative infinity (-∞) and positive infinity (+∞), it might take you a while to land on the number ‘257’.?
That’s a bit like throwing a dart at a dartboard however you are blind folded and you are throwing that dart backwards. It might take a while to land on the target.
What a stupid game, you might think. I can easily solve this problem using my highly advanced arithmetic skills.?
Ok, genius.?
But what if I didn’t give you rule 1 which states the function of this game? How will you reverse engineer the number ‘456’ to find out what ‘x’ is??
It’ll practically be impossible to do so. And that is what Blockchain relies on. A hash function called the SHA-256 hash function that is non-reversible. So you can’t reverse engineer it which makes finding the right answer to the…let’s call it the ‘Blockchain problem’ pretty much a guessing game.?
Hence it is computationally expensive to mine blocks in a blockchain.
The Blockchain problem
Once again I’m going to quote the paper, “The proof-of-work involves scanning for a value that when hashed, such as with SHA-256, the hash begins with a number of zero bits.” and “…we implement the proof-of-work by incrementing a nonce in the block until a value is found that gives the block’s hash the required zero bits”.?
So to break it down, the number ‘456’ that we want to reach is representative of the number of zero bits we want to find in the hash value.?
Basically that means the solution to the blockchain problem is to find an answer (a hash value) that starts with a certain number of leading zeros.?
Now we already know that to randomly find an ‘x’ value to the solution ‘456’ using our SHA-256 calculator would take some time so imagine trying to find an ‘x’ to a solution that needs to start with a certain amount of leading zeros.?
This ‘x’ is called a nonce by the way.?
These nonce also gets chained together through the blockchain kind of like the function we had in rule number 1 of our guessing game. The next solution is dependent on the previous solution which brings me back to my earlier point, you can’t change a block in the blockchain without screwing up the rest of the chain.?
Wait, how do we find these nonces?
Remember in our number guessing game, we had to guess what ‘x’ is by trying out random values for ‘x’ and putting it through our function until we get ‘456’. The same goes for our nonce, we need to guess what the nonce is by trying out random values for the ‘nonce’ and putting it through the SHA-256 hash function until we get a hashed value that starts with a certain amount of leading zeros.?
How many zeros are we talking?
Rather than fixing the number of leading zeros, bitcoin’s blockchain actually uses a target number to control the difficulty of the ‘Blockchain problem’.
A target number??
Let’s go back to our guessing game and change rule number 2.?
I’m still thinking of the number 456 however instead of telling you this number lies in the range of negative infinity (-∞) to positive infinity (+∞), I tell you its a number that lies in the range of 0 to 5000.?
领英推荐
If we had 10 players in the game and each player can make one random guess every second, it might take a couple of minutes to get to the number ‘456’. If we have 100 players in the game, it might only take a couple of seconds.?
In this case, I would probably increase the difficulty of our guessing game and tell everyone that the number I am thinking of lies in the range of -10,000 to 10,000 or if I’m feeling really mean, I could tell them its a number in the range of -∞ to ∞.?
Think of this range as the target that is used in bitcoin’s blockchain to control the difficulty of mining a new block. As more players (computer nodes) enter to play the game, the target difficulty is raised. When players drop out of the network, the target difficulty is decreased.?
Basically, “To compensate for increasing hardware speed and varying interest in running nodes over time, the proof-of-work difficulty is determined by a moving average targeting an average number of blocks per hour.”?
If you look up bitcoin’s difficulty , you can see that it has been increasingly going up but dipped when players left the game . This difficulty is set to a number to maintain an average of 1 block being mined every 10 minutes (roughly every 2 weeks) which means after roughly 2016 blocks are mined, the difficulty number will be re-evaluated and either increase in difficulty or decrease in difficulty to maintain this moving average target of 1 block every 10 minutes.?
In other words, we want to change the difficulty of the game after it runs 2016 times to make sure that on the 2017th run, all players in the game still find the game just as difficult as the 1st — 2016th time.?
What happens if more than one player land on ‘456’ at the same time?
That is to say, what happens, “if two nodes broadcast different versions of the next block simultaneously”? So think two random computers computed two separate nonce that generated two separate hashes and both hashes meet the conditions of the target.?
In this case, the two players (two computers) will get an extra challenge in the game. That extra challenge is to be the first to find the next proof-of-work.?
However in the case of Blockchain, imagine two separate teams rather than two separate players so all the players in the game (aka all the nodes in the network) will work together to try and find the next block (proof-of-work) and “the tie will be broken when the next proof-of-work is found and one branch becomes longer”.?
Alright, enough tech stuff. Tell me how I can become a bitcoin billionaire.?
In bitcoin’s blockchain, Satoshi Nakamoto created the first block with an incentive. That incentive was a form of crypto currency called a ‘bitcoin ’. These bitcoins were awarded to miners who mined a new block and added it to the bitcoin’s blockchain.?
Why have an incentive??
I could ask you the same question, ‘Why do you work?’.?
Most people would say, ‘money’. Some might say, ‘to keep me busy’ or, ‘because my team is the best’ or because, ‘I love what I do’.?
Whatever your reason might be, that is your incentive for doing work.?
In the same way, we need to convince miners to do the proof-of-work work to mine these blocks onto the blockchain. Just like your reason, we need to give them an incentive.?
The good thing about money is that most people are incentivized by money so creating a digital coin (i.e. bitcoin) would incentivize our miners to do the proof-of-work work or at least attempt to.?
When a miner successfully mines a block and (emphasize on the and) it gets added to the blockchain, they are awarded with some bitcoin. If we go back to our guessing game for example, this is like a player guessing the right ‘x’ value and getting rewarded with some treats but only if all other players agree that the ‘x’ value is correct.?
The other way we can incentivize our miners is with transaction fees.
Remember transactions is a way to transfer coins from one person to another to record that a payment has taken place. And these transactions are recorded onto each block as we build our blockchain. Think of the chain like our family tree, the transactions as wedding vows and miners as marriage services. The more wedding vows take place, the number of requests for marriage services grows and eventually our family tree becomes longer.?
How do transaction fees work?
Transaction fees are like service fees. You typically pay someone to do a service for you. In this case your service fee is paying someone to record your transaction onto the block they mine. According to the paper , this is how transactions are determined:
Input: 100 bitcoins?
Output: 99 bitcoins
Transaction fee: 1 bitcoin
I mean, given the current value of bitcoin , that’s a fairly significant transaction fee but you get the idea. Basically, whatever amount of coins you want to send someone as part of a transaction, the miner will take a certain amount of ‘service fee’ (transaction fee) from the transaction and keep it for themselves. I guess its true what they say, ‘there are no free lunches in this world’.?
How much are these transaction fees?
Well that depends . The thing is, your transactions are only recorded onto the blockchain once a miner successfully mines a block and we already know that the average time we are aiming for a bitcoin block to be mined is roughly every 10 minutes so if you want to pay someone in bitcoins, the transaction won’t go through immediately. In that time, lots and lots of people probably had the same idea which is, ‘I want to pay someone in bitcoins’ so by the time the next block is mined, there may be more transactions that needs recording than a single block can record.?
Think of it like the amount of marriages and divorces that needs to take place after lockdowns. Now our marriage services (miners) have an entire backlog of wedding requests (transactions) to process so they’ll probably prioritize the ones that have a high service fee (transaction fee) associated because the higher the fees, the greater the incentive.?
Why can’t a block record every transaction?
Everything has a limit. Unless you’re infinity or space which in the case of blockchain is neither one of those. For the bitcoin blockchain, that limit is set at 1MB per block. Think back to our wedding vows and divorces log book. That page size of that book could be A1 , A2 , A3 , A4 , A5 , and so on. Let’s say Nakamoto decided to set the page size of bitcoin blockchain to be A5. That means with each page (block) mined, we can only record as much transactions onto the page (block) that fits within the scope of an A5 page (1 megabytes of data).?
You can imagine how frustrating this can be if you want your wedding or divorce to be recorded but due to this A5 limit, you either have to pay higher transaction fees or wait until a new block is mined so there were debates on trying to increase the block limit which resulted in hard forks of the original bitcoin blockchain so that we can essentially have wedding and divorce log books with bigger page sizes to record more transactions.
What’s this whole ‘wallet’ business??
Remember when we talked about public and private keys earlier and how I mentioned that transactions are verified through a series of digital signatures??
Your wallets in blockchain is typically anything that holds your private keys.?
Private keys? I thought I just need one.?
According to the privacy section of Nakamoto’s paper, “a new key pair should be used for each transaction to keep them from being linked to a common owner”. Because the bitcoin blockchain is public ledger whereby all the transactions that take place are publicly announced. That means, to maintain some level of privacy, we need to keep public keys anonymous and private keys a secret. That’s similar to online games and using usernames instead of actual names to identify players.?
For example, we can see that ‘foxylady’ and ‘green_man’ are getting divorced on the 20th of July due to irreconcilable differences and they are splitting their assets on a 50:50 basis but we have no idea who ‘foxylady’ or ‘green_man’ is. But let’s say ‘foxylady’ then gets married to ‘mrpurpleturtle’ on the 16th of August and we know that our Aunt Judy had recently divorced from her husband to then marry her new fiancé on those exact dates then it will be pretty obvious who ‘foxylady’ is.?
Instead, imagine one transaction recording ‘foxylady’ and ‘green_man’ divorced and another transaction recording ‘warrior02’ and ‘mrpurpleturtle’ getting married. Now our Aunt Judy owns both ‘foxylady’ and ‘warrior02’ because she has the private keys for both of them but we would have no idea that the ‘foxylady’ and ‘warrior02’ transactions were connected to the same person.?
So if my wallets contains private keys which I shouldn’t share with anyone, how do I get paid in bitcoins? Asking for a friend (of course).
Let me introduce you to the concept of a bitcoin address . From here on out, we’ll specifically refer to the bitcoin blockchain but similar concept applies to other cryptocurrency blockchains too.?
A bitcoin address is basically a hash.?
A hash of what??
Well, there is two ways to generate a bitcoin address. The first is known as a hash of your public key or P2PKH which is essentially paying to your public key but it has been hashed. The second is known as a pay to script hash or P2SH which is even more advanced than the first one so I won’t even attempt to go into details.?
If you’re over the technical details on how bitcoin addresses are generated, just know that they are simply an anonymous way to represent someone like a ‘foxylady’ or ‘green_man’ or ‘mrpurpleturtle’ that you can send bitcoins to.?
So in a transaction, you would specify who to send certain amount of bitcoins to and to specify the who, you need to provide a bitcoin address to address it to that person. However, in the world of crypto, think of it like the physical cash system. You can’t really recover cash you have lost. So if you mistakenly send it to the wrong bitcoin address then you’ve lost your bitcoins even though the transaction is fully recorded. That’s because the public keys are completely anonymous. Think of it like you dropped a $50 note somewhere. That $50 note could be picked up by someone and brought back into circulation (i.e. that bitcoin address is owned by somebody) or it could never be picked up by anyone so even though it exists, it may never be used in circulation (i.e. no one owns that bitcoin address).
This is one of the reason why these bitcoin address are hashed. Rather than using just the public key, bitcoin address are generated through a series of crypto-magic algorithms that has a few safety checks in place.
Crypto-magic algorithms?
Nothing is really magical once you figure out the sleight-of-hand trick so here’s a run down on how these bitcoin addresses are created and used.
Welcome to the P2PKH magic show. My name is ‘M’ which is short for magician. We’re going to start off with an empty hand so as you can see, our hand is currently empty. Now, we’re going to use two decks of cards to make this magic trick work. One deck we’ll call it the ‘scriptSig ’ deck and another deck we’ll call it the ‘scriptPubKey ’ deck. Actually, to make things less complicated we’ll call the scriptSig the ‘unlock’ deck and the scriptPubKey the ‘lock’ deck. Got it??
Good. Now let’s add the unlock (scriptSig) deck to our hand. There are two cards in the unlock deck. One card we’ll get you to sign with your unique digital signature (your private key) so can you sign this card for me? Cool. And another card will contain your public key which can only be derived from your private key.
Think about it this way. Let’s say this suite of cards represents your public and private key pairs so you signed the 4 of hearts, right? That means the only other cards in the suite that hasn’t been signed is the 4 of spade, 4 of diamonds and 4 of clubs. Think of these 3 cards as your public key and the 4 of hearts as your private key.?
Ok, watch the magic happen here using the lock (scriptPubKey) deck.
Our first trick is to duplicate the public key, so these 3 cards here. We call this trick the OP_DUP trick. As magicians, we want to modify the cards as part of our tricks to entertain the audience but then at the end of the show, we want to reveal that we still have the original cards. See??
The crowd goes wild.?We just duplicated some cards out of thin air!
Ok, pipe down now. We’re got a few more tricks up our sleeves. Our next trick is to hash the public key which means we want to take these duplicates and… “Hey, anyone want to give me some ideas as to what I should turn these cards into?”.?
Someone in the audience suddenly shouts, “I want to see Jack.”?
Now, before our show started, we had our assistant prepare a suite of cards and sneak into the audience so they can shout out the right answer (the suite they had prepared beforehand). This trick is called the OP_HASH160 trick because we take the public key and generate a hash with it so that we have a public key hash we can compare it to. This is like the assistant sneaking into the crowd knowing that the suite of 4s will turn into a suite of Jacks. Think of the Jacks as a hash of the 4s.?
Why? Well, think of the suite of Jacks (the public key hash) as less easier to tamper with than with just the 3 cards (the public key). If, for example, we got hold of a Quantum Computer to uncover the private key from the public key then we might actually go from “Impossible” to “I’m Possible ”. Using the public key hash instead of the public key means we need to reverse the hash and then reverse the public key. Much harder to break.?
Now we pull out the suite of cards that our assistant had prepared beforehand that was hidden in our costume and quickly check to verify the prepared suite is indeed Jacks . This trick is called OP_EQUALVERIFY.
First, we check that it is a suite of Jack cards (the hash we are expecting). Then we turn all of our cards into either an Ace (1) or a Joker (0).?
This gives a signal to our assistant that we have verified the suite of cards and checked it for its accuracy. If it was accurate, we show the Ace (1) card and continue with the show. If it wasn’t accurate, we show the Joker (0) card which tells our assistant that we have to abort the show immediately. This step allows us to validate that the public key hash we generated matches the public key hash provided and reduce our hand to one card that can tell us the outcome of the results.?
Say we got an Ace (1). That means we can continue with the final trick. The final trick is called the OP_CHECKSIG trick. So basically, we bring back our original suite of cards and say to the crowd, “See, here is the original suite that turned into the suite of Jack cards.”?
If for some reason, we reached into the wrong pocket and retrieved a different suite of cards that hadn’t been signed, we would have to awkwardly terminate the show and refund the crowd because our magic trick failed.?
This final step allows us to unlock the funds which is why the scriptSig is called the unlock deck because we take the digital signature (private key) and generate a public key. If the hash of that public key matches the one we are expecting from the scriptPubKey public key hash then we know the provider of this scriptSig is the true owner of the funds and therefore they can unlock the funds for whatever use they want to use it for.?
The whole idea is that there are these public key hash that we call ‘Bitcoin Address’ which are used during a transaction to send funds from one person to another. To do so, we verify these public key hashes using a scriptSig (unlock) and a scriptPubKey (lock). This allows us to lock and unlock unspent transaction outputs (UTXO ) or in other words, whatever coins was passed onto your Bitcoin Address from the previous transaction.?
Speaking of, if you ever feel like donating some cryptocurrencies ??
BTC: 14CT3VYcwhNWtnChRzu6aVyXcdVLaDR1GL
ETH: 0x99bC11BEb457804a622556b03e700D8d3509A524?
Shameless plug: Remember to follow me on Twitter (@mishxie) and my actual blog for blog updates and other random stuff. I’ve also started a YouTube channel and the amount of attention its getting is looking pretty sad so please check it out. It might encourage me to post more content ???
Thanks for reading!
Customer Partner @ Versent
2 年Well written Michelle Xie!
Founder & CEO at SewaYou | Ex-Microsoft
2 年Awesome writing Michelle, like always!
AI Leader aspiring for CDO, CAIO, CTO | GenAI Product Engineering | Data & Analytics | Technology Consulting & Advisory | Presales | Enterprise Architecture | Azure, AWS, GCP, Oracle, Databricks, Snowflake, Salesforce...
3 年Great article Michelle....especially the examples are excellent to understand the concept of Blockchain and it's underlying architecture