MultiversX Tracker is Live!

So you're in it for the tech? Let's talk tech. A simple POW blockchain explanation with code PART 1

All Cryptocurrencies

by COINS NEWS 199 Views

It always amazes me how many people say they are in it for the tech (yes I know it's a meme), but they can't begin to explain the first thing about how a blockchain works. If that's you, let's change that! I am going to try to really ELI5 the concepts but reinforce them with some simple python code. Disclaimer: I'm not an expert and happy to have anyone correct me.

For this example, we'll be talking about proof of work (POW). This is just part 1. I don't want to overwhelm you, but if there is interest, I will make new posts until we've built out a full blockchain in python.

What is a blockchain?

It's nothing more than an immutable (not changeable) store of information (in blockchain, we call these blocks). What makes it special is that as there is new information to store, it gets saved on top of the existing data. Each new piece of information (block) stores a reference to the data below it. These references are in the form of hashes.

What is a hash?

The topic are hashes are really complex, but all you need to know about them is that they are a computer algorithm's that take data of an arbitrary size and converts it to a fixed size. If the input data is the same, the hash will be the same every time you generate it. If you only have the hash, you cannot reverse it to get back to the original data. You should also know that a tiny change to the input data, results in a completely different hash. It won't look similar at all.

Let's try it out:

This assumes you are using Python 3 and have a basic understanding of Python and the command line. If not, there are tons of resources online, but happy to try to answer any questions.

in this example, we are going to use the standard programming string "hello world" as our input data for simplicity. But in a real blockchain the input data is most likely a group of many transactions.

Open a terminal and enter the following. This opens a python interpreter in which you can run code.

 python3 

Now enter each line 1 by 1, pressing enter between each line.

import hashlib # imports the hashing library input_data = "hello world".encode('utf-8') # set some input data first_hash = hashlib.sha256(input_data).hexdigest() # saves the hash of our input data to a variable print(first_hash) # prints out the hash 

Notice that after the entering the last line you get this out:

b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9 

Repeat the steps above and you'll see the hash doesn't change.

Now let's make one tiny change to our input data (making the word world plural) and prove that the hash stays the same length but completely changes. In the same terminal enter the following lines:

input_data_updated = "hello worlds".encode('utf-8') # create some new input data second_hash = hashlib.sha256(input_data_updated).hexdigest() print(second_hash) 

Notice this time you the following output:

8067f1ae16f20dea0b65bfcbd50d59014d143c8ecebab179d923f6ef244b40f8 

You should know that we can just treat the hash like any other text and hash it again. We can even combine it with other text before we hash it. Let's try it out:

combined_hash = hashlib.sha256(str(first_hash).encode('utf-8') + str(second_hash).encode('utf-8')).hexdigest() print(combined_hash) 

Now you will get the following:

3ef229cf1df86a5a0a0fed22d95484585995bc9d851e4b46ac57b7287fa0f9ea 

How do hashes make a blockchain immutable?

Because we now know the properties of hashes, we know that changing the input data changes our hash. We also know the each block refers to the block below it by referencing it's hash. The way it does this is by combining its own input data with the hash of the previous block, just as we did in the above example. In POW, nodes run special software the keep track of the existing blocks and validate new ones. Just like we did in the above example, they can calculate the hash for themselves. If things don't check out they will reject the block.

So imagine an attacker wants to change data in an older block so instead of saying you sent crypto to your friend, they try to rewrite history to say you sent crypto to them. But we know if they try to propose a block with this change, it will change the hash of the block they modified and thus will change the hash of every block after it.

The only way they could work around this is by performing a 51% attack. in POW, nodes follow the longest chain of valid blocks. But because the attacker has gone and changed an older block all the blocks after that one are invalid because the hashes are wrong. They would need to regenerate all the blocks after the one they modified to make them valid. But doing so is expensive because meanwhile, all other miners are generating real blocks. This is where 51% attack comes in. If the attacker controlled > 50% of the hashing power (computer's generating blocks), their modified chain will eventually become longer than the real one and nodes will begin treating that as the real chain.

This scenario is difficult to do because gaining hashing power requires a lot of computer resources that are expensive. In the next part, we'll look at what these computers are doing that makes it a long and costly attack.

submitted by /u/Routine_Elk_7421
[link] [comments]
Get BONUS $200 for FREE!

You can get bonuses upto $100 FREE BONUS when you:
πŸ’° Install these recommended apps:
πŸ’² SocialGood - 100% Crypto Back on Everyday Shopping
πŸ’² xPortal - The DeFi For The Next Billion
πŸ’² CryptoTab Browser - Lightweight, fast, and ready to mine!
πŸ’° Register on these recommended exchanges:
🟑 Binance🟑 Bitfinex🟑 Bitmart🟑 Bittrex🟑 Bitget
🟑 CoinEx🟑 Crypto.com🟑 Gate.io🟑 Huobi🟑 Kucoin.



Comments