Everything You Wanted To Know About Blockchains

but were too afraid to ask

Part One

Ah, blockchains.

Kind of a big deal. You'd have to be a total square not to have heard about them. Me? I've got eight.

Often over-complicated, over-mysticised, over-singularised (I don't even know what the right word for it is, but people say The Blockchain a lot). What are they? Join me for a rough tour from the ground up and I'll try to make sure you leave here knowing the answer to one question:

What are people talking about when they talk about blockchains?

There's a lot to cover, so it's actually going to come in two parts. This, the first, will look at the data structures known as blockchains and their properties, along with any other bits and pieces you need to make sense of them.

The second part will apply what you've learnt to the practical and widespread applications of blockchains to power distributed ledgers, cryptocurrencies such as Bitcoin and Litecoin and smart-contract based chains like Ethereum.

Toggle pointless gifs off

Hashes

Before we jump into defining blockchains themselves, we need to cover one concept which is so core to their working it makes absolutely no sense not to bring everyone up to speed.

That concept is what's known as a hash. Related is hashing, the action by which we get our hands on a hash, and a hash function, the specific algorithm you run.

I'll not go into how different hashing algorithms work, since there are many and that's not the important thing to know about them at this point. What's important is how they behave.

A hashing algorithm is a transformation that you can apply to any piece of data, long or short, and get an output which will always be the same length. What that length actually is depends on the specific algorithm, but any given algorithm will always produce hashes of the same length.

For a concrete example, let's look at SHA256, a popular and common hashing algorithm. This the hash of the string unwttng:

unwttng
SHA256
1
4
0
c
9
e
e
6
6
6
3
a
d
4
b
4
c
6
2
e
8
f
c
1
b
8
c
a
1
b
f
9
c
b
c
6
0
0
a
6
1
5
e
5
3
b
f
b
d
3
4
9
2
8
1
1
b
e
7
2
9
c
d
8
1
4
0
c
9
e
e
6
6
6
3
a
d
4
b
4
c
6
2
e
8
f
c
1
b
8
c
a
1
b
f
9
c
b
c
6
0
0
a
6
1
5
e
5
3
b
f
b
d
3
4
9
2
8
1
1
b
e
7
2
9
c
d
8

A small note: whilst the "256" in SHA256 refers to the fact that the output is exactly 256 bits long, it's quite customary to represent hashes in hexadecimal notation (that is, using 0-9 and a-f) - that's what we'll use in this article.

As long as we always use SHA256 to perform the hash, "unwttng" will always produce this exact output, and any string will produce a hash of exactly 64 characters in length. Take a look at the hash of a totally different string:

This is a string that's longer than 64 characters. The hash is still exactly 64.
SHA256
8
d
e
e
9
e
3
9
a
5
9
c
f
0
b
7
0
f
1
0
e
8
7
2
5
7
9
2
8
3
7
a
a
c
7
d
c
f
8
c
8
7
2
9
e
4
a
6
b
f
3
8
a
f
8
c
b
9
7
2
5
b
0
9
8
d
e
e
9
e
3
9
a
5
9
c
f
0
b
7
0
f
1
0
e
8
7
2
5
7
9
2
8
3
7
a
a
c
7
d
c
f
8
c
8
7
2
9
e
4
a
6
b
f
3
8
a
f
8
c
b
9
7
2
5
b
0
9

The above is true of any hashing algorithm. However, there are a few more properties that are also commonly expected of a good hash function.

Uniformity of the hash function says that we want any given hash output to be in some sense equally likely (or close to it). That is, if we compute millions and millions of hashes using the function, there shouldn't be any real pattern to where the outputs fall in the possible space of outputs. There shouldn't be loads of them that start with an "A", or loads that end with ten "X"s, or anything like that.

Uniformity is a very important feature because it minimises the risk that hashing any two inputs will produce the same output to vanishingly small probabilities. Most practical usage of hashing across all kinds of software is done under the assumption that accidental "collisions" of this kind are effectively impossible.

Non-invertibility is another feature that's often desirable, depending on the intended usage of the algorithm. This says that it should be impossible, or at least prohibitively difficult, to work out the input that led to any given hash. Ideally, it should be easy to transform data into a hash, and practically impossible to go the other way.

Finally, discontinuity tells us to expect that similar inputs will produce drastically different hashes. There are algorithms with very specific (usually search-related) applications that aim for the opposite of this, but most hash functions that you come across will be as non-continuous as possible. The difference between the hash of 'abcdefg' and 'abcdefh' should be almost total, rather than them differing by only one or a few characters.

Try it out for yourself! The below will let you explore SHA-256 - put anything you like in the input and watch the hash change. Try and observe the properties I've listed above if you can.

SHA256
3
d
6
c
1
0
0
8
2
b
6
b
e
e
1
5
b
a
2
e
f
2
4
4
7
d
2
e
7
c
e
1
0
d
4
d
4
e
b
f
a
2
b
3
8
c
7
e
d
e
e
d
f
3
c
1
8
0
a
1
c
4
e
e
3
d
6
c
1
0
0
8
2
b
6
b
e
e
1
5
b
a
2
e
f
2
4
4
7
d
2
e
7
c
e
1
0
d
4
d
4
e
b
f
a
2
b
3
8
c
7
e
d
e
e
d
f
3
c
1
8
0
a
1
c
4
e
e

Share this visualisation, earn Junie internet points

Share this on Twitter

My first blockchain

A blockchain is a data structure.

More specifically, it's a data structure for storing a carefully ordered sequence of data. Even more specifically, it's a data structure for storing a carefully ordered sequence of data in a way that makes it very hard to tamper with.

That last part's important - lots of structures have been invented to store things in order - plain arrays, linked lists, doubly-linked lists, yada yada. Blockchains aim to add a feature to these by making sure that if any member of the list is modified in any way, to any extent, the chain is verifiably invalid by anyone who cares to check. They're basically just robust lists - no magic.

A blockchain, like pretty much any other list-based data structure, is made up of a unit (a block) capable of storing a package of data, and some mechanism for joining the blocks together in order. Hence the name. We're packaging up our data in neat blocks, and then making a chain of blocks.

Let's make one! First, we'll need our data.

"I'm a block!"
"I'm another block!"
"Me three!"
...

What you actually decide to put inside your blocks is very much a usage-dependent thing. It could be a bunch of single strings like our example blocks here, or it could be entire bundled-up packs of thousands of transactions (as is the case with your average cryptocurrency). For now, keep in mind that it basically doesn't matter what kind of data we have, we just want to store generic blobs of something.

We'll also want some metadata about the block. Again, what form this takes is dependent on the purpose of the specific blockchain, but as an example we're going to include a block number inside each block. The first block is called number 1, the second one number 2, and so on.

1
"I'm a block!"
2
"I'm another block!"
3
"Me three!"
...

A typical dynamic list structure at this point would add a few bits of information to logically link lots of blocks together into an ordered sequence. These commonly take the form of pointers to the location of the previous and next items in memory. We'll go ahead and steal that idea.

1
"I'm a block!"
2
"I'm another block!"
3
"Me three!"
...

We now have a fully-functioning doubly linked list, which will serve many applications just fine.

However, a blockchain one-ups all of this: each block also stores a hash of the previous block. The discontinuity and uniformity of hashes (discussed above) lend them very nicely to checking the integrity of data, since if even a single bit of the input data changes, the hash it produces will be obviously and drastically different. Let's add in our hashes to complete a basic but functioning blockchain.

1
previous block hash
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
"I'm a block!"
2
previous block hash
4
8
b
4
8
a
f
9
a
2
6
c
b
f
3
d
b
4
8
6
3
3
1
c
c
2
2
8
8
5
4
a
9
4
c
5
9
d
4
d
5
0
0
c
b
1
1
f
c
5
5
f
1
d
c
a
2
1
8
e
7
6
c
b
"I'm another block!"
3
previous block hash
f
6
6
a
2
a
9
1
2
f
0
d
5
6
b
f
2
6
5
0
b
3
7
8
b
3
9
0
e
2
9
9
3
5
c
2
5
2
c
e
b
e
4
7
a
b
2
9
f
3
4
e
4
5
4
7
c
4
1
c
2
e
9
e
"Me three!"
...

There are a few things to point out here. Firstly, that the first block is special. It's "previous block hash" is all zeroes. That's because there is no previous block to hash, and so we have nothing to validate. This first block is often called the Genesis Block - you can even have a look at Bitcoin's genesis block here if you're interested. There's a lot in there that probably won't make much sense to you just now, but you can see the "Previous Block" hash is all zeroes, just like ours!

Secondly, it's important to get your head around exactly what data we're hashing. The "previous block hash" for any given block is not just the hash of the previous block's string data. It's actually the hash of the entire previous block - metadata, previous hash and all.

To labour that point (I said it was important), look at block number 2. It's previous hash is 48b48...e76cb. Take a moment to find it in the blockchain above.

However, SHA256("I'm a block!") is not equal to 48b48...e76cb:

I'm a block!
SHA256
5
2
8
a
b
e
6
6
3
6
9
d
9
7
c
1
e
e
1
a
b
f
f
c
0
2
8
9
6
4
f
d
3
4
4
b
3
8
c
c
6
9
e
a
6
5
e
5
8
2
e
d
1
0
8
a
f
4
5
f
7
0
5
e

We reached the 48b48...e76cb hash not by using the string data by itself, but by combining all of: the block number, the previous hash and the string data, in that order. That is, we hashed the string "1" + "00000000...00000000" + "I'm a block!", and in doing so we got the result you can see in the chain:

10000000000000000000000000000000000000000000000000000000000000000I'm a block!
SHA256
4
8
b
4
8
a
f
9
a
2
6
c
b
f
3
d
b
4
8
6
3
3
1
c
c
2
2
8
8
5
4
a
9
4
c
5
9
d
4
d
5
0
0
c
b
1
1
f
c
5
5
f
1
d
c
a
2
1
8
e
7
6
c
b

As far as their operation as a simple data structure go, that's literally all there is to a blockchain! Some connected blocks, with data, with each one holding onto a hash of the previous one.

Different implementations of blockchains use different amounts of extra metadata, and they bundle up all kinds of data in their blocks, but this is the principle. In the next article we'll go further and look at how we can unlock a huge amount of power in these simple structures by sharing them across large distributed networks.

For now though that's a lot to take in, so let's consolidate things by thinking a bit more about this simple blockchain we've put together. Specifically I want to take you through exactly how it is that including hashes in the chain makes it secure against tampering, as I mentioned above.

Support Me

Find out how to support my writing

Blockchain security

Let's roleplay. We work at a bank which has decided to store the record of all of its customers' transactions in a blockchain. They've made this choice because somebody told them that a blockchain allows them to verify that nobody has been naughty and tampered with any individual records. People might want to do this for all sorts of reasons - to send money to themselves, frame somebody else for a crime, whatever. It'd be bad if it was easy.

Toggle pointless gifs off

I want to make clear how the structure of a blockchain that we introduced above helps protect against this kind of mischief. Here's the blockchain that we'll use as an example (from now on I'll leave out the memory pointer arrows in a futile attempt at screen-space efficiency):

1
previous block hash
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
"John paid £10 to Mary"
2
previous block hash
4
f
c
d
b
4
4
b
e
b
5
6
7
8
1
6
9
a
8
0
1
f
8
6
0
0
0
a
a
f
e
8
a
a
0
e
e
3
e
5
8
e
0
9
7
1
e
c
3
6
f
0
0
6
b
8
e
7
8
4
a
a
9
3
"Ingrid paid £7.50 to Frieda"
3
previous block hash
b
9
a
b
7
f
b
6
7
9
9
6
0
f
f
c
d
6
d
f
5
6
2
6
f
6
f
4
3
2
d
0
a
7
8
4
0
c
d
6
a
8
4
0
1
d
4
5
6
d
f
1
0
8
c
f
9
4
0
d
5
a
e
5
"Marie paid £200 to Enrico"
4
previous block hash
e
2
3
2
7
f
e
d
5
6
e
f
7
9
a
2
2
4
b
2
1
c
c
5
b
8
0
3
d
e
c
1
0
8
0
d
7
5
2
1
c
4
b
6
a
a
4
8
e
b
6
5
d
2
7
6
9
0
1
b
9
2
f
e
"Tom paid £110 to Alice"
...

Perhaps these blocks are stored in some distributed way - one file per block, say, such that you can have reasonable confidence that a wannabe attacker might only be able to tamper with one or a few blocks of your chain. The detail isn't that important (although, if that confidence seems a little miguided to you, hold that thought - you're going to fit right into the cryptocurrency crowd).

Your job at the bank is to occasionally validate this transaction log to make sure it hasn't been messed with (or to write a program to do it, if you're not up for calculating many hashes with a pen and paper). What's your process?

Start at the beginning of the chain. For each block you come to, you're going to calculate the hash of all the data wrapped up in the previous block and compare it to the "previous hash" stored in the current block. If they match, great! Hashes are very fast to calculate so it's no skin off your back to make this check.

Now let's imagine I enter the scene. My job at the bank is to be a criminal stooge and try to steal money. I happen to know that Ingrid's filthy rich, so I update block number 2 for my own gain. However, I'm unable to get at any other blocks, so I leave them as they are. Now the chain looks like:

1
previous block hash
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
"John paid £10 to Mary"
2
previous block hash
4
f
c
d
b
4
4
b
e
b
5
6
7
8
1
6
9
a
8
0
1
f
8
6
0
0
0
a
a
f
e
8
a
a
0
e
e
3
e
5
8
e
0
9
7
1
e
c
3
6
f
0
0
6
b
8
e
7
8
4
a
a
9
3
"Ingrid paid £1000 to JACK"
3
previous block hash
b
9
a
b
7
f
b
6
7
9
9
6
0
f
f
c
d
6
d
f
5
6
2
6
f
6
f
4
3
2
d
0
a
7
8
4
0
c
d
6
a
8
4
0
1
d
4
5
6
d
f
1
0
8
c
f
9
4
0
d
5
a
e
5
"Marie paid £200 to Enrico"
4
previous block hash
e
2
3
2
7
f
e
d
5
6
e
f
7
9
a
2
2
4
b
2
1
c
c
5
b
8
0
3
d
e
c
1
0
8
0
d
7
5
2
1
c
4
b
6
a
a
4
8
e
b
6
5
d
2
7
6
9
0
1
b
9
2
f
e
"Tom paid £110 to Alice"
...

When you next come to validate the chain, what do you find? Block 1 checks out, as it always does. Block 2 checks out - it has the correct hash for block 1. That's weird, right? The bad block itself validates just fine. Block 3, however, very much does not check out. You work out the hash of block 2:

24fcdb44beb5678169a801f86000aafe8aa0ee3e58e0971ec36f006b8e784aa93Ingrid paid £1000 to JACK
SHA256
0
d
6
b
0
8
d
e
2
e
d
d
7
f
c
f
d
4
8
b
e
e
f
c
9
5
5
6
7
0
4
b
f
a
3
b
2
f
b
0
5
0
2
8
4
f
8
2
7
1
f
f
c
3
4
d
8
4
2
a
0
1
6
2

Woah! That's not the previous hash that block 3 claims it knows about! In fact, it's nothing like it at all - this is discontinuity of the hashing function at work. You now know that the chain has been tampered with. You'll need to... I don't know, do whatever a bank would do in this situation - restore the chain from last backups?

Panic, I guess.

How come it was so important to me earlier on that you understand that each block's "previous hash" is the hash of the entire previous block and not just its data string? To see, just go one step further into your validation process and look at block 4. If you've been good and recalculated block 3's "previous hash" to take account of block 2's changes, then you're going to find that block 4's "previous hash" now no longer matches the hash of block 3! This is because part of block 3 has changed - namely, the "previous hash" referring to block 2.

See how the hash-chaining mechanism of a blockchain means that even a single corrupt or tampered-with block will invalidate the entire chain after it. Cool, right? If each block only had the hash of the previous block's content, but not its metadata too, then in our example block 3 would have told us something was amiss, but block 4 would have checked out as good again. A much weaker state of affairs, I hope you agree.

Now...

Go take a break

In this part, we've learned about hashes, and the structure of a basic blockchain. We've also taken a look at an only-a-bit-contrived example of how a blockchain can provide security that a more basic list structure can't, using the power of hashes for data integrity checks.

Next time, we'll see how blockchains are being applied to great effect (and huge financial value) in the real world. Starting with the grand-daddy of cryptocurrencies, Bitcoin, we'll learn about proof-of-work and distributed ledgers to find out how you can make a functioning decentralised currency using a blockchain. Then, finally, I'll cover the basics of the even-more futuristic smart-contract blockchains emerging to generalise the concept to more than just currency, like Ethereum.

Click the link below for part 2.

Everything You Wanted To Know About Blockchains - Part 2

What is Bitcoin? What is Ethereum and how do smart contracts work? The second of a two-part series.

Thanks for reading!

Support Me

I made a Patreon... So.

Home

What else is going on?

Blog

Read from the archive

Follow Junie @unwttng

A god damn thrill ride or your money back