develop

Building a Blockchain in Under 15 Minutes - Programmer explains



Sharing buttons:

hey what up guys welcome back to my

channel and today I have a very special

video for you that I think many of you

will enjoy because we will namely be

building a block chain in under 15

minutes because I want to demonstrate to

you guys that block chain in itself is a

very simple concept it's a very simple

data structure at its core of course

cryptocurrencies are complicated but the

blockchain is not the reason they are

complicated the reason why they're

complicated is the consensus algorithm

it's the mining is the transactions is

everything that is around but blockchain

in itself is very easy to understand but

you need to know what you need to know

before you can actually understand what

blockchain is and how it works you need

to know what hashes are and you need to

know how they work and so a hash is

basically a digital signature of of some

kind of data so for example I can take a

movie and I can hash it and get a

digital signature I can take an email

and I can hash it and get a digital

signature I can take a word and they can

hash it I can take a whatever data you

just take it you hash it and you get the

digital signature for that data so

before we start with the actual

blockchain let me just demonstrate it to

you how this works so we will be

building our blockchain in Java it will

be very simple and you if you know

programming you can do that in another

language and if you don't know

programming I think you still will be

able to understand roughly how this

works so let's let's talk about hashes

as we can see us we have two lists we

have one list that contains ABC and we

have a second list that also contains a

B and C so this this is the data this is

basically the strings we're trying to

hash so let's see what kind of what kind

of digital signature we will get from

list 1 and what kind of so we will just

print the digital signature or the hash

whenever you hear hash guys you can just

think you can replace hash with digital

signature so let's see what kind of

output we get if we print the Dignitas

digital signature of a and

signature of at least one and digital

signature of least two so as you can see

as we get these two numbers and digital

signatures are identical for these two

lists if I now change anything in list

two for example and I run it again you

see that I get a completely different

digital signature so if I just make a

small amendment I just meddle with the

data I try to change anything in this

list I will get a completely different

digital signature and that is the

foundation of a blockchain because a

blockchain is a list of blocks basically

a chain of blocks and each block holds

the digital signature of the previous

block and the digital signature of the

next block is based upon the digital

signature of the current block so they

are always together the next signature

will be based on the current signature

and the current signature is based on

the previous signature so if you change

anything back back in the past you will

break all of the signatures they will

look completely different and I'm trying

to adjust the lighting I should get some

kind of artificial light so I don't need

to to reduce the window as the light

source okay so now that we understand

what digital signatures are hashes and

digital signatures is basically the same

world and how you use them let's

actually get into our blockchain coding

and let's see if I can make it under 15

in under 15 minutes so the first thing

we need to do is talk about our block

our block in this case will be very

simple it will only contain three things

it will contain the list of transactions

that happened in that block it will

contain the previous hash the hash or

the digital signature of the previous

block and it will also contain the hash

of the current of of itself and this

hash will be based on the transactions

and the previous hash so if anyone would

change anything in the previous block

the digital signature of the current

block will change

and when that changes the digital

signature of the next block will change

you will see how this works very soon

guys so let me just remove this this

class that I played around with earlier

let me see let me delete and let's start

from scratch and we will create a new

class and it will be the block so I'm

doing it in Java you can do it in

whatever language you want it's

basically a simple and simple stuff so

let's create the fields it will have the

previous hash it will have the let me

see it will have the previous hash and

it will have the transactions list of

transactions so I'm just making a a an

array of strings so each transaction

will be a string in this case of course

in in a real block chain the

transactions would be their own objects

and we will need to create a object for

that and define a class and all of that

but we're just playing around let's just

create let's just create them as strings

so and each block will also have a its

own hash and this hash will be based on

all the transactions let me just define

and the array on which we will base the

hash so we first of all need to

calculate the hash of the transactions

transactions let me add an S here and s

here and an S here and here and also

here

so we we will calculate the current hash

based on the hash of all of the

transactions and the previous hash like

this and so the block hash will be the

digital signature all the hash however

you want to call it of contents and

contents are the hash of the

transactions that are in this block and

the previous hash the hash of the

previous block so if I change anything

in the transactions or if I change

anything in the transactions in the

previous block or anything in these

transactions in the block before the

previous block

this hash will be completely different

and here you also need to change okay

very good guys we have finished our blog

let's create the blockchain an ArrayList

of blocks so it's basically an array of

blocks and ArrayList is just easier to

work with

let's see blockchain and it will be

interesting new ArrayList like this so

we need to create orogenesis block

because each block chain needs a Genesis

block that is hard-coded

the reason is because as you can see as

each block has the hash of the previous

block however the Genesis block is the

first block so it has no previous block

so we need to hard code how it looks

like so Genesis let's call it Genesis

block it will be a new block and let's

see what kind of things it needs a

previous hash which we don't have and it

needs transactions so let's just let's

just make fake transactions here Tran

Genesis transactions transactions and

they are equal to for example let's say

Satoshi cent Ivan 10 Bitcoin so I'm just

writing the transactions as strings of

course in a real blockchain this would

be some other data structure probably

some kind of transaction class but for

simplicity let's just let's just write

them as strings send ten bitcoins to

Ivan so everyone is sending me Bitcoin

in this Genesis block so I add it like

this

so guys now we can actually output the

get block hash let's see I need we need

to create a getter for the block hash so

now we can actually output now come on

yes now we can actually output the

digital signature of the block we just

created block hash let's see what it

looks like so as we can see I get this

number

there is a negative mind

something so this is a digital signature

of this block that we just created now

if I change anything at all in this

block let's say that I want I want to

cheat the system and I suddenly say that

Satoshi sent me a million Bitcoin or

like 100,000 Bitcoin and if I run this

now we will see that the digital

signature is completely different and

this is very important because when we

create our second block let's say block

let's create our second block it will

need the first of all it will need the

previous hash it will need the hash of

the previous block and that is the

Genesis block blockchain hash and it

will need some kind of transactions I

see let's say that block two

transactions let's say that in the

second block

Ivan sent ten Bitcoin to Satoshi I sent

him back his bitcoins and Satoshi sent

ten Bitcoin to Starbucks like this so

these are the transactions in the second

block so yes as you can see if I put the

transactions and we output now the

digital signature of the second block

let's just do like this hash of block

two hash of Genesis block so now we can

actually output the hash of block 2 and

the Genesis block something is ok I

forgotten you

let's see let me shrink myself here so

as we can see hash of the Genesis block

is this number

Hacha block - is this number now yes

here is the interesting part that if I

change anything in block 1 for example

let's say I suddenly say that Satoshi

send me this many bitcoins and they run

it you will see that these numbers would

be completely different as you can see

the hash of Genesis block is completely

different and the hash of the second

lock is completely different and so this

is the basic foundation of the

blockchain that if I change anything

back here the next block will get a

completely different signature and that

is because we're passing the previous

hash and group and we are calculating

the current hash based on the previous

hash and these transactions so if I

change anything in the past the

transactions will be different the hash

of the transactions will be different

and this change will propagate through

the blockchain and so let's just create

a third block like this let's say block

3 transactions and block 3 and now we

have the previous hash will be block 2

block 2 and the transactions will be

block three transactions and let's say

that in this block I will I sent one

Bitcoin to my mom like this or let's

give her this many bitcoins and if I get

the digital signature of that let's see

block 3 like this and we'll run it I get

the following I get the following

signatures so I just want to demonstrate

not to you guys now that if I change

anything in the first block let's say

that I add this a I just change it

change anything in these strings in

these strings as you can see it's

completely different the signatures in

all of these steps and this this hashing

algorithm is

is very hard to crack and the more

blocks are created on top of of the of

one block the transactions in that block

are more and more confirmed because it's

harder and harder to crack all of these

hashes because the previous hash will be

based on the hash before that and the

current hash will be based on the

previous hash and so as you can see a

change in any of these transactions in

any of the data in the one block will

propagate and change all of the digital

signatures in the future and that is how

abduction works because if I change

something everyone else will see that my

digital signatures are completely

different from everyone else's so that

means that I have cheated so if I just

tell everyone that hey Satoshi gave me

this many bitcoins back back then and

here is the blockchain that that

confirms that everyone will take a look

at it and say well I will know this

blockchain doesn't confirm that because

we have our own versions of the

blockchain and the digital signatures

are completely different so guys I hope

you enjoyed I hope you understood the

basic concepts of hashing of of linking

blocks together via their digital

signatures or the hashes and if you are

new viewer and you like blockchain

technology theorum programming software

development you should definitely

subscribe to this channel guys because

you will find it interesting I myself am

a software developer and I'll see you

guys tomorrow I hope you really know how

blockchain works and how the concepts

behind it have a good day guys goodbye