We've been talking about all these applications that's built on the blockchain, but what exactly is
the blockchain and how does the technology work?
Now, the easiest way to think about this is the blockchain is just a Ledger.
Now, what's a ledger?
Well, let's say that I went to lunch with my friend Jack and he paid for the bill.
Well, I could pull out a notepad and say, well, Jack gave Angela $10 for lunch, and now Angela owes
Jack $10.
So this is the beginning of my ledger.
Now, then I decide to buy Helena a coffee.
So Angela gives Helena $5.
And as I continue this ledger, it's basically just a record of who owes who, what amount, and just
a bunch of data.
Now, later on, when I want to figure out, well, how much do I owe Jack, then I can look at this
ledger and I could see, "Oh, I owe him $10."
But there are two problems,
first, Jack, Helena, and I all need to agree on which transactions should go into the ledger.
And second, we have to trust the person who's holding onto the ledger that they aren't just going to
modify the transactions that we already recorded.
What if we could just simply modify this ledger?
I could just go in there and add a zero, and now all of a sudden I'm receiving $100 instead of $10.
This kind of data tampering makes these kinds of paper ledgers worthless to use,
and if we had this on the Internet, it would have the same problems.
What we need is to make the history of the transactions in this ledger immutable, basically lock it
so it can no longer be changed ever again.
How do we do that?
Well, we could do that with encryption.
And the simplest version is the Caesar Cipher.
So when Julius Caesar wanted to communicate with his generals, and wanted to make sure that the messages
didn't get intercepted by a third party,
he turned his message into a cipher.
That means if somebody got hold of it, they wouldn't be able to work it out unless they knew how it
was encrypted.
And this was a very simple form of encryption.
All it did was it shifted the letters in the message by a pre-specified number of characters,
so maybe A becomes E and B becomes F, etc..
Now, if you wanted to try out the Caesar cipher for yourself, you can take a look at this Caesar cipher
demo, which I'll link to in the Course Resources.
Now here you can enter some plain text, and if you click on Encrypt, you can see that there's a shift
amount.
So what it's going to do is it's going to take each and every letter and shift it up by 4.
So an E becomes an I, an L becomes a P, et cetera.
But you can see from looking at the ciphertext that there is a pattern here, right?
Because with the double L's, it's two P's, and then with the both of these U's in Julius, they both
get encoded to Y.
And if you knew which cipher was being used, you could just go through all of the shifts, you know,
from 1 to 26, and you'll eventually figure out how to decrypt this message.
So if you click Decrypt, you can see it goes back to Hello, Julius.
Now, the only thing that you need to know is what that shift number is in order to figure out how to
decrypt a message.
So here they've got an example message and if you try Decrypt, you can see it's still not working.
And you change the shift number from 1 to 2 to 3 until eventually you'll get to the right shift number
and you'll figure out what the message is.
Try it out yourself.
The Caesar cipher is one means of encryption, but there are many, many different types of encryption,
but what they all do is they take a message and you encrypt it and you can turn it into a cipher, which
will be difficult to read for any normal human being
and you disguise the meaning of the message.
Now, the key thing about encryption, though, is that you can actually go forwards and backwards,
which means that you can take the same encryption
as long as you know the components of the encryption, you can decipher it and get the message back
out.
So this means on the other side, the Roman general will be able to read what Julius Caesar meant to
send him by knowing which form of encryption was used.
And this makes encryption a little bit less secure, because if there is a method to be able to back
out the original data by decrypting the cipher, then your cipher could be broken, such as in the case
of the famous encryption of the German enigma in World War II.
So if our ledger was secured using encryption, then there's always a way to
decrypt it, which means we can still alter the ledger illicitly.
So instead of using encryption, the blockchain actually uses something called Hashing, which is very
similar to encryption
with a couple of notable differences.
Now, what's hashing?
Well, it's basically a mathematical formula that allows you to turn a message into a Hash code by passing
the message through the Hash function.
But unlike encryption, a hash is a bunch of mumbo jumbo,
but if you use the same hash function, no matter what message you put in, you will always get an output
with the same number of characters.
So if you hashed 123, you might get this top result.
And if you hashed abc, you might get this bottom result.
But they both have the same number of characters.
So let's try out a hash algorithm ourselves.
Now, again, in the course resources, you'll see a link to this demo.
And here we've got the SHA-256 Hash Algorithm, which is one of the most popular hash algorithms out
there.
And you can see here that you can type any sort of message into this box, and it will formulate a hash,
and it changes every single time you type anything.
So you can type of space,
it'll change it.
You can type numbers, it'll change it.
You can put the entire War and Peace into this message box, and it will still generate a hash with
64 characters.
And if you're interested, you can actually see below here for the source code of how the SHA-256 is
implemented.
But how is it possible that we can turn an input into a hash and be completely unable to work out what
the original input is from the hash?
Well, a really simplified example of a hash function is working out the factors of a number.
For example, 2 times 4 is 8, so 2 and 4 are
the factors of 8, because I can divide 8 by 4 and it will cleanly divide and I can divide
8 by 2, and it will also cleanly divide with no remainder.
So going forwards is very easy.
2 times 4 is 8, super easy.
Now, if I asked you to work out what are the factors of 8, which would be 1, 2, 4 and 8, that's
a little bit more difficult.
But what if I asked to work out the factors of 57?
Unless you already know what they are, you have to go through all the numbers.
So it will be 57 divided by 2,
57 divided by 3.
Now, here you would find that 57's factors are 3 and 19 because it can be cleanly divided
into 3 and into 19.
But what if I asked you, are there any other ones?
Well, you can't be sure unless you actually continued.
So you could say 57 divided by 4,
that doesn't cleanly divide because there's a remainder.
Then you do 57 divided by 5 and so on and so forth.
So essentially when we're multiplying two numbers together, it's almost like creating the hash from
the message.
And then the final result that we get is the hash.
But if you want to go from the hash to go back to the original message, you would have to brute force
it.
You would have to divide it by every single number from 1 to 57 to find out all of its factors, which
for humans takes a lot of time and for computers, takes a lot of energy and computing power, especially
when you're dealing with very, very large numbers.
You already saw how finding the factors of 57 was a lot more difficult than finding the factors of 8.
Now, real world cryptography uses numbers that are hundreds of digits long.
For example, in 2019, it took researchers 900 years of computing power to find the factors of a number
with 240 digits.
So this is kind of the underlying basis of cryptography; going forwards is very quick and easy, very
easy to calculate.
But going backwards is almost impossible using the current computation power that most people have access
to.
So how does this apply to blockchain anyways?
Remember that our problem with the ledger was that we wanted to make sure that no one can change our history
of transactions.
Let me show you how the Bitcoin ledger solves this problem.
Using what we know about one way functions, we can now take all transaction data and create a hash.
Some very clever people have figured out that you can encode a whole bunch of things and hash everything
together.
So for example, I could write a message and I could write which date it was written on.
So the 1st of January of 2023,
and I could also say I am the person who wrote that my name is Angela.
I could put all of this information together and it will create a hash for me.
So this is essentially how a block in the blockchain is formed.
We have a piece of data, so let's say Angela owes Jack $10.
This is our piece of information.
This is our piece of data that we want to encode into a block.
Now, in order to add that into the block, we're going to have to generate a hash, and the hash becomes
the identifier for this block.
It's kind of like the fingerprint of this block.
How can you know that this block is truly this block?
Well, you could look at my fingerprint, and it's probably different from yours because we are unique
people.
And you can look at this block and it'll probably be different from another block because it'll have
a different hash.
Now, in order for a block to be added to the blockchain, it has to satisfy some predetermined requirement.
And this requirement could be, for example, that the hash has to start out with three zeros.
Now you can see that as I start typing, the hash keeps on changing
and sometimes you might see that we get just purely by chance, a zero to show up.
Or in this case, even better two
zeroes to show up.
But if we wanted a hash that has to begin with three zeros, then we're going to have to keep modifying
our data, which is not what we want.
We want our data to be clean, or we can modify this thing, which is known as the nonce.
So the nonce is basically a number.
So we can start from 1 and you can see the hash change because the nonce is being combined with the
data to produce the hash.
Now 1 produces a hash that starts with 4,
so that doesn't work.
Well, 2, we can try 3, we can try 4, we can try 5.
And we can keep going through these numbers until we find a nonce that creates a hash that starts with
a zero,
but that's not enough for our block to be added to the chain.
We need it to be three zeros to begin with.
So you can continue changing this nonce to 7 to 8 to 9 until quite a while.
Trust me, I've tried it, to find a hash that begins with three zeros.
Or you can simply use the computer, and do that for you, to brute force the nonce until it finds from
1 all the way up, increasing by 1 each time, until it finds the right number
that's going to create a hash with three zeros.
And you can do that by simply clicking on the mine button in this website.
It took 0.1 seconds for my browser to figure it out, and it figured out that 3885 is the nonce that
I need in order to encode this data to have a hash with three zeros to start with.
So this makes this block valid.
And you can see it turned green because it satisfies the requirements of the blockchain to have three
zeros to begin with.
Now, if I had a blockchain that had a little bit higher of a difficulty, well, I can change that
to four zero to start with or five zeros to start with.
So then it's going to have to iterate through all the nonces until it finds a number when combined
with this particular piece of data will produce a hash with five zeros to start with.
And you can see when I click Mine, it's going to take a little bit longer, 0.6 seconds before it finds
the correct nonce, which happens to be 53,792.
But the important thing is that nonce only works to produce this hash with five zeros for this piece
of data.
As soon as I alter this piece of data, let's say that Jack decided that I actually owed him a bit more
money and he goes in and sneakily alters this data,
well, it's now become invalid because you can see the hash no longer starts with five zeros,
and this tells me that this block has been modified and it is not valid data.
So then what we're doing with our ledger is we're taking the data that we want to add into the ledger,
and we're putting it into each of these blocks by hashing the data with a nonce.
So this is where the block in the blockchain comes from.
But what about the chain part?
So if we go to the next tab of this demo, you can see we've got a whole bunch of blocks here, five
in total and each of the blocks,
in order to create the hash, it takes into account the hash of the previous block.
This is how they're chained together.
So here you'll see that the hash of the first block, which is called the Genesis block, because it
doesn't really have any previous blocks attached to it.
So that's just 64 zeros in this case.
Well, this has a hash, though.
And let's say that we decided to put in the data that
Angela owes Jack $10.
Now, in order for this block to be valid, it needs to have a nonce that combines the data with the
previous hash value, which in this case is zero,
to create a hash that starts with five zeros so we can click Mine and we can get our computer to work
it out. There
it's found the nonce: 53,792.
And with this nonce and our data and the previous hash, all of these things combined creates a hash
with five zeros to begin with.
So this is now a valid block that's entered the chain.
Now, on the next block, let's say that I decide to...
I decide to give Jenny $5, and I want to record this into my blockchain ledger.
There's my data,
and now you can see this is going to be chained to the previous block because it's now got the previous
block's hash in this section.
And this plus this plus this is going to be combined to create this hash. When any of these things changes,
so, for example if I change the data, if I change the nonce, and I can't but if I did change the
previous hash, it will also change the hash here.
So these three things together create the hash.
Now, if I go ahead and mine it, you can see it'll figure out a nonce in order to satisfy my requirement,
which is five zeros to begin with.
Now, here's the interesting part.
If I decide to go back in time and alter something in the ledger.
So let's say that instead of me owing Jack $10, I decide to sneakily change it to Jack actually owes
me $10,
you can see what it's done is it's invalidated
not only this block, it's turned it red because it doesn't start with a zero,
it's also invalidated every subsequent block.
So because this block depends on this one and the previous hash function has changed, which means this
has changed
and because we said that these three things together create the final hash, it no longer starts with
five zeros.
So this means I can't change this data without people knowing about it.
And if I change the data back to the original form, you can see everything turns green because that
is the original state of the ledger.
And this is how the ledger stays immutable.
We can't change anything that goes into the blockchain, otherwise it's going to invalidate all of our
hashes and it's no longer going to satisfy the requirements.
So as you can imagine, if you had more data and you mine each piece of data
until you have this entire chain which is all linked to each other
because in order to create each block's hash, it takes into account the previous block's hash and
whenever the previous block's data changes,
it's going to change the hash and thus invalidate every subsequent block.
Then this is how we can have security in our blockchain.
We now have a ledger where every single time you change anything in a previous block, it's going to
invalidate every subsequent block.
And in order to make it valid, we have to re-mine every single one of these blocks which, depending
on the difficulty level, I haven't even set it that high, is going to take a lot of time because we're
going to have to go through each of them.
And in reality, in chains like Bitcoin or Ethereum, it would literally be impossible with the current
computation power to be able to go through each of the blocks and re-mine them.
And you can see, even in this case, which is very a simple one, my Chrome is already telling me that
it's hanging, and it's not working, and I should shut it down.
So this is how we ensure the integrity of the blockchain.
But blockchain actually goes even one step further in order to assure that the data that's stored is
absolutely correct and has not been tampered with,
and this is the distributed part of the blockchain.
This goes back to the first problem that we talked about,
how does Jack, Helena, and I all agree on what's in the ledger?
So I'm going to make this a little bit easier on my computer and drop the requirement to only need four
zeros.
So you can see here, I've got a distributed blockchain.
So I've got three people with three computers Peer A, Peer B and Peer C.
And what happens is every time a new miner joins the blockchain, they're going to get a full copy of
the blockchain.
So this person's blockchain has the same data as this person's blockchain.
And you can see because if you look at the hash, they're all completely identical.
So now what this means is that three people have the same blockchain that they hold.
Now, if this first person, Peer A, decides, "Oh, I'm going to be a little bit cheeky and I'm going
to change the data in here, I'm going to say that, you know, in this block, actually, something
else happened.
Then you can see that they've invalidated all of their subsequent blocks.
Now, they could re-mine each of these blocks in order to make it valid again.
Now because I've set the difficulty quite low.
That was relatively quick to do.
But now they no longer match up with the other blocks.
You can see this one is...starts with four zeros, but then it's 347e.
But for the other two, they've got a88f, a88f.
And this is how the network can tell that this Peer A has become a bad player.
They're trying to mess with the system because their hashes don't match with anybody else's.
So by distributing the blockchain on multiple computers, we now have another way of ensuring the integrity
of our data to make sure that our data is absolutely correct and nobody has tampered with it.
And this is where the power of blockchain really comes in, because we now have a distributed trustless
ledger.
We don't have to trust anybody.
We don't have to believe that, you know, PayPal is storing my money and they are making sure that
all the transactions are correct.
Well, actually, we've got blockchain, which encodes all of these transactions.
And because of the underlying technology, the distributed nature of it, the blockchain part, it means
that we don't have to care who is storing these copies of the blockchain.
We can trust in the technology that our ledger is always, always going to be accurate and nobody is
going to be able to tamper with it.
Now, this is great
and this technology up to this point that we've described already allows us to have cryptocurrencies
like Bitcoin,
but the real cherry on top happened when Ethereum was conceptualized.
Now, the real breakthrough here is they realized that not only can we store data inside each of these
blocks, who owes who money or who has how much money,
we can create what's called smart contracts by storing code into the blocks.
So back in our blockchain demo, we could have code, for example, if we were running a Kickstarter,
or a crowdfunding type of Web3 decentralized application, then we could have the code that says, Well,
if the total amount was greater than, I don't know, whatever goal they set, so maybe $5,000,
well, then in that case, this code is going to execute and we're going to transfer the money.
So let's say Angela has an account and is going to get these $5,000.
We're going to increase Angela's account by 5000.
So this code then gets put into the block, which is going to create a hash, which means that it can
never be changed again.
And then a user can come along and execute this code
and the resulting transaction where Angela gets $5,000 will get saved onto the Ethereum blockchain.
So now instead of having a crowdfunding company like Kickstarter, who's going to escrow the funds or
who's going to make sure that you know the money once the goal is hit, gets transferred to the project
owner,
well, we've actually got all of that encapsulated in a few lines of code.
So when somebody decides to raise some money and they happen to reach their goal, then our code from
the block gets activated automatically and transfers the money to the recipient.
And this is where the real mindblowing part of blockchain comes in.
And that is exactly what we're going to learn in this module, how to create these decentralized applications
and getting our apps to do things automatically based on the code that's stored on the blockchain.