Quick and dirty version of how a blockchain works

Published: 2018-12-05

 

This is a blockchain demo. We're gonna do this in a very visual way though we're gonna make it very easy to understand by stepping through the key pieces of what a blockchain is in a visual way. But before we get started we need to take a look at this thing that we call a sha 256 hash.

A hash looks like a bunch of random numbers and essentially what it is it's a fingerprint of some digital data and it just so happens it's a fingerprint of whatever I type in this box. So if I type my name "Timothy" into this box you see that the hash has changed matter of fact it changed every time I typed a letter right. I'm going to go back to so it says Timothy. So this is a the hash of the name Timothy all lower case it starts with 19ea right. So if I delete that and I go again type Timothy again you can see it starts with 19ea, the same exact hash in that sense it's a digital fingerprint of this data whatever data is here every time you type exactly the same data you get exactly the same hash. I can type anything I want so I can you can have nothing like this you know e3b0 that's that's the hash of nothing or you could type tons and tons of stuff a matter of fact you could put like the library of congress in here and you would get a hash and the interesting thing about it is regardless of their if there's a tiny amount of information no information or the entire library of congress you're always going to get a hash that is this long this many characters are you not going to be able to pre guess what this is you kind of have to put the library of congress in here to figure out what the hash is but you'll always get exactly the same hash regardless of how many times you put exactly the same information in.

So what I'm going to do is extend this idea of a hash into something that we're going to call a block alright. Let's take a look at a block so this is a block and it's exactly like the hash it's just that data section. I've broken out now into three sections one called block this is just some kind of a number this block number 1 a nonce which is just yet another number will go into what that is in a second and then just some more data just very similarly to the way that we had it before. However the hash of this which includes all of this information up here is down here and it begins with four zeros you see that it's a relatively unusual hash you know most of them are not really going to start with four zeros like that but this one happens to and because it does totally arbitrarily i'm going to say that this block is signed.

So what would happen if I were to change any one piece of this information. Let's say if I were to type something here right the hash is going to change and what's the chance of that if I type letters this hash is going to start with four zeros pretty low it's probably not right. Let's see what happens when I do that i'm just going to say hi look at that right this hash does not start with four zeros and so the big background here has turned red so now you know that this this block with this information in it is not a valid or a signed block ok and that's where this nonce comes in this nonce is just a number that you can set to try to find a number that fits so that this hash starts with four zeros again.

How do we do that well let's start with one that start with now it's 32 so that's not one let's try two FF now 3 4 5 6 so you get the idea like I could sit here all there's one that starts with 0 I can sit here all day typing these numbers and trying to figure out one that actually is going to hash out to something that starts with four zeros that wold take a long time so here I have my little mine button i'm sure you've been wondering what happens if I press that so what's going to happen when I press this mine button is it's gonna run through all the numbers from 1 all the way up to try to find one where the hash starts with four zeros and this process is called mining.

Let's do it right now now it's checking all of the numbers from 1 all the way up with their now it's stopped at 59,396 and that one just happens to hash out to something that starts with four zeros and it satisfies my little definition of what assigned block is ok so that's that's a block now can you tell me what a blockchain is it's probably just a chain of these blocks well how do you how do you put them together. Let's do that alright so here's my blockchain I've blocked number one has some kind of a nonce just like before there's some data area too but then it has this previous here is a bunch of zeros let's let's roll forward so this is block two and block 3 and 4 this block chain has five blocks on it right the previous here starts with 0000ae8 right is this number ae8 and then this previous you know b90 is this one over here b90 so you can see that each block points backwards to the one before it you remember that that first block over here there actually is no previous so it's just a bunch of zeros it's actually just a fake number ok so just like we did before what happens if I change some information here right it's going to change the hash of this block and it's going to invalidate it right. Let's try that so i'm going to type pie again sure enough that block is invalid alright just as we assume but what would happen i'm going to fix that now we'll go back to something that work what would happen if I changed something in this block, it's going to change this hash but this hash gets copied up to this previous so it's going to it's going to change this one to right so it should break both blocks.

Let me try typing hi in there and sure enough all right so we can go back as far as we want you know to some point in the past and break that block and it will break all the blocks since then everything before still green but this one is is red it's. If I wanted to you know change something in this this block chain I could just go over to block number five right here we could change it. I'll put hi and then we could remine it you know and pick a different nonce will do that right now and we could essentially alter the chain so we've done it so that we should be good down right all right well what happens if I go back in time to hear and I break it here now I have to mine this block which will pick a nonce that makes this block hash out to four zeros if we can find one sometimes it takes a while because it's gotta run through a lot of there it is it found one at a hundred and thirty-eight thousand alright but this one is still broken because although this one starts with four zeros adding the four zeros with different stuff up here still makes this block hash out incorrectly. So I also have to mine this block alright and that takes some amount of time that one was a little bit quicker and then I have to mine this block to fix it alright so what we're showing here is that if I go and change this last block. All I have to do is remine this block if I go way back in time to back here and I make a change like that i'm going to have to mind this one this one this one and this one so the more blocks that go by the more blocks.

In the past its the harder and harder and harder it is to make a change and so that it's how a blockchain is going to resist mutation resist change ok so now you like like you know identify if if I do this in this block here you can see that I've changed it to hi and I remine it. How would I know that my block chain has been remined. All right let's take a lookat that I'm gonna hit this little distributed thing so now we have a just distributed blockchain it looks exactly like the last blockchain ok up to five that but this is peer a the first peer. If you go down here you can see here is peer b and it happens to have an exact copy of the blockchain there's actually also a peer c down here right this could go on forever there's many many peers out on the internet and they all have a complete copy of the blockchain so in this case if I look at this hash it's 0000e4b all right if I go down to this one I notice it also has e4b. If I go down to this last one it has e4b so they must be identical and i'm going to demonstrate that by going here and typing something i'll type hi again and then I will remine this block I've got some other number now let's put some other number up here so I should be able to mine this block okay now all the chains are green right they're all green however this chain says the last hash is e4b the bottom one says that too e4b and this middle one here says 4cae so I know just by glancing at this one little hash that something is wrong in this blockchain even though all of the hashes start with four zeros I know that this one is different and it's different because I have two it's essentially two against one we are a little democracy here right this guy argues that it's e4b this guy argues that it's 4ca and this one is e4b so e4b wins.

So that's how a completely distributed copy having a copy on many different computers they can all very quickly see if all of the blocks are identical remember blockchains can have you know 4 500,000 blocks very easily. So rather than checking through all of them all you really have to do is look at the hash of the most recent one and you can see that if anything in the past anything way back here was altered you can tell by looking at this at the last block in this in the chain you know it's going to hash out to something that doesn't start with four zeros and looks very different from what the hashes on the good chains are. So that's a blocking that's the entire thing there is no more to it than that but it's kind of not really useful because we don't have some something in this data area that means anything I keep typing my name or hi and that's kind of that sort of irrelevant information so what we really want is a token so let's do a token on our blockchain all right now look at this.

I have this token just totally arbitrarily I'm calling these dollars right. We have twenty-five dollars from Darcy to Bingley four dollars and twenty-seven cents go from Elizabeth to Jane you get the idea it's basically there's all these transactions that are happening and I've just replaced the data with these transactions and just like we saw before you know so there's multiple blocks here this one has more transact... it doesn't matter how many transactions there are there can be many or there can be few or none.

If we keep going forward here just like we saw before if we go down and we notice we have all these other copies of the same block chain right, so now here's where the immutability is important if I change something back here you'll notice that this is you know a7fc blah blah blah something it's something else so it's something different than what's down here so in in this way I mean it's very important that if I were to go back in time and change some value that we would notice it's it's very important with money that you don't lose track and that's the whole point of using a blockchain here. This is a whole point of resisting any kind of modifications you know of things that have happened in the past so that's the that's the reasoning behind having using a blockchain to remember tokens.

Now I one thing I would mention here is that we're not listing you know Darcy has a hundred dollars and he's giving 25 of it to Bingley the only thing we're saying is Darcy gives 25 to Bingley. We're not remembering a bank account balance we're only remembering money movements. So this begs the question "does Darcy have $25?".

Well we have a problem here in this version of the blockchain: we don't actually know if Darcy has $25. So let's look at a coinbase transaction. So if we look back here a coin base we're going to add a coinbase transaction to our blocks and this is this is very similar to what we've seen before but we're just adding a coinbase at the top and what's that that what that's saying is we're going to invent a hundred dollars out of thin air and give it to Timothy and there's no transactions in this block because nobody had any money previous to this. In the next block another hundred dollars comes out of nowhere and goes to Timothy i'm a fan I love it right. I'll take a hundred bucks now we have some transactions you can see that they're all from Timothy, they're all from me because I'm the only one who has any money at this point. So I'm sending 10 of my dollars to Sophie. Do I have ten dollars? Yeah, I do, I look back and I see that this coinbase transaction has given me a hundred so I have at least 10 and I can send it on and you add all these up and they don't go over a hundred and it follows sort of a basic rule of a currency that you can't invent it out of thin air you can't create money out of thin air.

Thus its dispersion is controlled so now if we look at this block chain that we've created and we zip forward in time and we notice that we see that Jackson is giving Alexa two dollars and so does Jackson actually have two dollars to give Alexa well we go back a block before we see that Emily had gotten ten dollars from Timothy gave 10 to Jackson and so Jackson does have the money so we can just go backwards and and find that out that's actually one of the benefits of having a previous here it's easy to go backwards we just look for the block that looks like that that has that hash and here it is right.

So you points two blocks back in time and allows us to trace the provenance of any coin that we want so that's a basic block chain and we're running a currency on top of it and as you know blockchains are there many copies everybody has a copy of it so if we mutate you know this and make it six dollars these go invalid. It does not agree with with these block chains down here these copies of the same block chain down here so this resist tampering which is what you want for a currency it works very well for things that are small and transactional like this go ahead and fix that and they're just a very efficient way to handle agreement on what has happened in the past as kind of this immutable history that that goes down with time so that's a basic block chain and a token on it there were glossing over some main points but if you dig into the demo and and click through these things and play around with it you get a better and better idea of how this works.

 

Filed Under: Blockchain