Blockchain: The Data Structure, Develop it yourself [DIY]

Blockchain seems like a buzzword today. Every other day, we hear about a new application developed on blockchain. So, you might be interested about it is, and what makes it so valuable? Brace yourself, because you’re gonna Develop-It-Yourself.

I admit it, Computer Science guys always try to keep compensating something. That’s why I think we keep super complicated names and ambiguous terms. Like Polymorphism, Encapsulation… I mean who named that? The spellings are harder than the concepts man! Whatever that might be, Blockchain isn’t like that. Blockchain is exactly what it reads — a chain of blocks.

Blockchain made its appearance first in Satoshi Nakamoto’s BitCoin. And it is on the hype ever since. Blockchain, in its essence is a data structure. It is iterable, immutable and appendable, again, the concepts are simpler than these words.

  • Iterable: It is a collection of some data that we can go through. Like a set or array.
  • Immutable: It basically means read-only. Write once, and we can’t change it ever in the damn future. Like writing on paper in ink.
  • Appendable: While you can write once, but you can keep writing. Exactly like writing in ink, you just can’t change/erase once you inked something, but you can write more.

What’s so special about blockchain, a data structure?

Nothing special. Except that, you can trust a blockchain more than your ex. It will never lie to you, and if it does, you can detect its lies. Good luck doing that with your ex.

And unlike how you had to “trust” your ex without knowing what she is really up to, you trust blockchain after knowing that it can’t lie. Like, when you run a JS interpreter, you declare a const and some malicious program, can, in theory change the value in memory and your program doesn’t know. Because you trusted the wrong mechanism.

In fact, this is widely exploited to “lock” the memory location in a game counter, then your resources never end because, the Game mechanics “trust” the value at that memory location and if it reports incorrect values, the game keeps on trusting them. Reminds me so much of my ex.

But, Blockchain is different. It is simply not possible to corrupt a blockchain without getting caught. Let us all dive in, and honestly, this is the last reference to exes! I swear!

Hashes to the rescue of data integrity

Hashes are simply put, life savers! So much, in fact, Leo DiCaprio would’ve been saved if there were hashes on board the Titanic! Wait! Are you too young for Titanic? Sorry Gen X. Coming to hashes, you might love them, you might hate them. If you do hate them, don’t bother pursuing computer. That’s so not-gonna-happen. But if you don’t, I understand them, I can help.

Let me explain this to you (I hope you don’t mind a li’l man-splaining, ay!). A hash is an algorithm, which takes some data, and spits out a “hashed value”. Every unique data has a corresponding unique hash. And it is impossible (or rather absurdly difficult) to reverse the input from the hash.

So, basically, if you hash the value “hi” using SHA256,  you’d get ‘8f434346648f6b96df89dda901c5176b10a6d83961dd3c1ac88b59b2dc327aa4’ but there is no other input which has this exact hash. And also, if you know the hash, there is no way for you to tell what was the message.

As we store both the hash and the data, we can check the data integrity anytime, Blockchain done! Problem solved! Except, it won’t work. yet.

Hashes are easy to calculate

Hashes are very easy to calculate (for computers), even most phones can calculate several thousand hashes every second. So, an attacker will also update the hash along with the data and it will match, unless the hash values come from a separate server. Which, again, demands that we “trust” this server. Not something we are looking for in a distributed computing environment.

Hashes are like dolphins, no, they aren’t. I mean, really? Why would I even say that?

The secret sauce: The “mining” of blocks, a Proof-of-work algorithm

So far, we understand that, hashes can be life-savers, but it can also be simple to recalculate. To solve this problem, we add process called mining, and due to our habit of complicated words, we call it a proof-of-work algorithm!

Basically, we take data, and append a number called nonce  at the end (it’s not called nonce, but rather referenced to as nonce). If the hash satisfies a condition (like number of zeroes in the beginning), we take the hash value, otherwise, we increment the nonce and hash it again.

For example, say, our condition is that the hash must start with “123”

Now, we take a nonce=0 and message="hi" and keep hashing until our hash starts with 123. We do this with 9 lines of python

from hashlib import sha256
has="" # hash is a reserved keyword in python
while has[0:3]!="123":
print nonce
print has

It takes around 0.03s to run. But if we increase the difficulty from 123 to 1234, it takes 0.2s to run, which is 600% more. And if we take it to 12345, it takes 3 seconds, which is 10000% more time. And the nonce increases from 350, to 41615 to 867895.

This mining algorithm, when adjusted for difficulty, can make it take longer to calculate a hash. This means, we can mine a block, and if someone modifies it, they have to mine the data all over again. Even if the data change is just 1 bit.

I wish I could do that with my girlfriend! I said no more ex jokes!

Put your hands together for the blockchain

While the concept of blocks is nice and all but the data is still not completely immutable. We can still change and mine a single block right?

Not so fast. If we have a stream of data, that is data that is coming continuously, then, we take the data for, say, 10 minutes, compile them into a block, and in the current block, we add the hash of the previous block.

The idea is, if someone modifies the current block, they will have to update all next blocks as well. And the more time passes by, the harder it will be to compute. This makes it highly resistive to changes.

Kinda like, how you have to make up more lies to hide a single lie and end up breaking up! I never lied, so I never feel how it feels to lie! Completely true! I swear!

On the next page we will build our first blockchain.

CODE ALERT: There will be code. And if you don’t like code, why have you even read so far!

Pages: 1 2

Recent Posts

Recent Comments




Be First to Comment

    Share your valuable feedback