How to Make a Simple Blockchain | R Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello good afternoon everyone welcome to liquid brain so today i want to show you how you can do your own blockchain system in r so just a disclaimer this is not a full-fledged blockchain or cryptocurrency but rather it's a very simple minimal blockchain system that helps you to understand the concept of blockchain rather than actually deploy yourself if you really need a blockchain system you can actually download one of the internet and just substitute your own this is more on education on how it's supposed to work okay so what is a blockchain so blockchain is a secure transaction record system is that when you have a list of transactions how can you make sure that every single one is recorded properly so how blockchain achieve that is through actually called a hash function so so a hash function generate a very unique code for every details that you code in i'll come back to this in a while but what it does is that for every single transaction they'll include a little bit details from the previous block which is why it's called block and you chain them together that's why it's called blockchain so what what essentially means is that if you change the the details in the first block you will affect the second block and you affect the third block fourth flock six blur and so on so every time you wanna change the information in the middle of the chain you will affect every single thing in in in the rest of the chain okay so you can also detect the you can detect the error by just checking the hash code so before we go too much into it what is a hash function so one of the hash algorithm or hash function i'm using here is called sha256 so what hash function do is that you take a fixed not fixed you take any amount of string so any text it can be a paragraph it can be a word it can be three words five words it can be a book but if you feed this text into this hash function you'll produce a fixed number of output and it's supposed to be unique okay they're not there's some bad glory them that doesn't actually produce the good one but sha-256 is one of the really good uh what is that called um hash function yeah that actually create a very unique code for everything that you put in you also have something called the avalanche effect so let's say this is the two example of the hash function you have hello where you fit into hash sha256 you have this long string and then if you add just one information the back it completely changes the whole code okay so 1 5 8 f and 9 4 8 b so basically every single thing change so the same thing will happen if you change let's say e to a 3 or l to i and so on and so forth so you can't eat any single part of the string without affecting like the whole thing so yeah so the the best thing about this hash function is that it's always produce a fixed uh length of output so now no matter how long of a string that you try to slot into this thing you always produce 256 letters so 256 uh alphanumeric basically okay so you can calculate number of power so it's very rare that you feed it two different things and you will actually come up the same thing and that's called a hash collision very rare in sha-256 and which is actually why bitcoin uses sha256 as their main hashing function okay so come we've come back to the block system so how does um a hashing function help blockchain to have a secure transaction details and records so if you have a normal data structure so this is let's say you're done in visas or or basically any kind of data database that's stored in something like that row one is james rose two main and top cat and and so on so forth so what i do is that i can just change something in the row two and you won't be able to detect it down the road so this is maybe not that important for names but when you're a banking system and you're trying to keep track of a record of transaction of people owing you or you owing people money uh every single bit of information is very important because you know you some people might own one million and pay back one and if you just change one zero it's actually ninety now uh one million is nine hundred thousand dollars difference on on the overall value so this is very very important where the details is correct so how does a blockchain allow for this kind of security is that every single detail will be attached or every single transaction here will be attached with a hash function so this is a hash function produced by feeding james into the sha-256 and the second block here actually produced a hash function from the the details of the second transaction plus the hash of the first transaction and that's different um blockchain that does it differently but what essentially it needs to have is the details so the hashing function for block 2 have to incorporate every detail from block two plus the detail from block one so that you you mix one of the one above into the block two which is why if you change anything here you actually changes the hand function completely and then your changes changes the hash function here completely as well and when row two changes which is block two changes block two changes block four changes and so on so every single thing in the back is different so you're unable to change it without people knowing it that's basically what it is so in in many cryptocurrencies such as like bitcoin or ethereum and many other things um they're a lot bigger than what a blockchain system basically is so a blockchain system is just a core system of how data is being recorded in many cryptocurrency it actually have a proof of work or proposition system so that only a certain people are allowed to add the block so it might be [Music] yeah it's basically a truss system that i'm not going to explain here so adding of the block is very difficult and very needs to be very controlled very carefully and the cryptocurrency will also allow something called a decentralization and how to synchronize all the database from everyone else because what basically it happened in block in cryptocurrency is that every single person would have a copy of this long list that you can check so if anyone produced the wrong one you can actually emit wrong hashes for example you immediately know that um what what's wrong with your thing and in most of the cryptocurrency that i know that uses a majority system so if majority have this information it's usually correct and if you have something different you're the minority and you know they'll usually just ignore you and take the majority answer okay so cryptocurrency will also need much more interconnects how it actually apply how do you actually know the transaction you can't get transactions on a piece of paper and all that thing well while blockchain itself is just a record system it doesn't care that much okay so this is not a cryptocurrency tutorial this is just a blockchain okay so in cryptocurrency you might also need to name it up the dog break so that like shiva you know and dodge coin so you can actually get a better value for um your cryptocurrency once you have released them okay so now let's go to r and actually see the code so the code i have is very simple it's only like 56 different um different what i call different line so let me close the others and let me make it a little bit bigger so you all can see okay so what i do is that i basically use an s or s4 object as individual blocks and i just chain all the s4 object into a list basically so the only library that you need to use is the open ssl so this library is actually used to to do the sha256 function so the first thing i do is to set class so this is create a kind of structure for our transaction or block system so that every block will have a timestamp which is the time of the when the block is created it has an account from and the account two so if you're doing something like a money transfer your you need a two and from all the time and you have a transaction amount so how many uh think that your transfer how many coins how many money that your transfer for example we also need a block number so we know which one is it down the line like we said about it just now so we can always know the previous block and we also need the hash number so that would be the the output from our hash function so this set the class for the s4 object but in order to actually create a new object you use the new function a new function will just tell that uh what kind of object is this so the object here object name sorry not yeah the class name i would say is link okay so this is the link of to create a transaction and we'll have a time so this will create the first block in our chain okay which is why i call it s1 so the timestamp is the current system time so this will change based on who you are where you're from and what time you're running the code an account from i will just randomly generate a five digit number for example an account two another random five digit number the transaction amount 0.225 so meaning that this person is transferred 0.225 of something to the other person account and this is the block number one and last one is the hash number so for hash number remember when output from chart256 is actually a hash a data type so you just need to convert the hash data type into a character so it's a lot easier to to understand and stop later because when you do a hash function you add to another hash function so and so forth is a little bit confusing over there okay so just for simplicity's sake we're going to repeat the same exercise for our second block and you can see that the first hash is not that important because they're the first in the chain and if you change the first thing in the chain everything will change anyway so the first hash is not fundamentally very important and let's move on to our second second block in the chain okay so same thing the time stamp will be system time and we have another two random five digit number and a render amount for that transaction to happen and we have block number two and we have the hash function okay so uh there's something i i did a kind of a short cut here so in this case the hash number should be a joint character of the time account account transaction block number plus the hash number from the first block okay so you have essentially this whole thing joined together and then join again by the hash number of the first block to create this hash function you know but i'm kind of lazy so i'll show you how to do that in the function later okay so this will create our second block so let's just try now so set class create one create two so this is our first block as you can see all the information are here and you have the hash number here and this is our second object we have all the random number that we have and we have a hash function sorry hash output here as well okay so now is that we chain s1 and s2 together into a new object called actual chain so the chain is the actual table of all the different information which is why in chain you can see this is block one information block two information again okay now let's now it's come to the complicated site where i can't create individually all of them and it's kind of complicated so what i do is that i'll write i write actually a function here so what does this create transaction function do is that it will help me to create the rest of the chain so that i don't have to input so many information so first thing is that in writing a function there is a way to input default value so that in certain way i know i don't have to do let's say the timestamp of a system time in the future every time i need to add a transaction it will automatically add the current system time every time i run this function okay so account from two and true extension i'll put it equals to zero so that if the user doesn't input anything specifically it will not speed up an error but you just give me an empty block where there's nothing inside okay so it will also not mess up the chain and so on okay so there's a default value so the chain will equals to the chain which is the object that we created just now just to make things a little bit easier to understand okay actually this should be capital okay so so what inside this function is first thing is to is to take the hash hash number from the previous block so what this do is that it takes the chain number so let's say what is the current chain so i'm let's say i'm adding the third block i will actually have to calculate the length of the chain so i'm i'm counting the third block now means there's two blocks already in the chain so this will output a two so it'll be chain the second object and get the hash number and this will be the hash number from the previous block okay so i call it previous hash so the block number will be the current block number that i'm creating which is why i use the same thing but i create a block number plus one so i'm creating the third object now so this is where the complication come in it's called a hash number so the hash number like i say just now is actually to join together the current block timestamp the account the transaction value the block number and this is most important one the hash number from the previous block add all together and run a chart 256 on it so you create a unique id or unique fingerprint for this transaction and its location in the chain okay so once i'm done i've just uh create a new object called out so do this new function it's the same function that i use up there to create my blog object so you create an out which is actually the new object the new block and once i get a new block i just add it to the chain and i return the chain on from this function so i just create transaction and lastly if i want to add another object to the chain i just run the create transaction function and what i need to supply over here is only the account from account 2 and transaction so of course you can actually have nothing so it will just fit in account zero account form is zero account two is zero and transaction is zero but if you do have some information that you want to put in uh you only need to fill in these three information because the rest has been already handled with the the default value from here to here okay so we can just add one to the chain uh there's something wrong promise under evaluation and uh earlier problem well hello i'm back so apparently you just have to explicitly indicate that the parameters that you're not putting in even though that is uh that's already default parameter assign so i'm not too sure why because it actually tests everything works properly when i test this code yesterday and when the camera is running it's a bit of camera shine okay so um so basically the the change the create transaction function would just create a new object in a chain so what i do is that i'll just create a sample which is a random number between 1 and 99999 and account number 2 is again a random number between 1 to 99999 and the transaction is a random number between 0 and 1. okay so you can if you are curious you can actually just copy this number out and it will give you a random number between zero and one and the chain eagles chain and time stem equals to system time and you can just run this so you can see now our chain is on six different block now and you can actually just add again add again add again and you can see we have more blocks you're supposed to refresh this yep so we have night block so every time when you run that function you actually create a new transaction and every transaction will have a new hash and this hash is actually based on whatever you have on the previous block so that if i want to go in and do a chain number five and if i want to change something from the account from i wouldn't be able to because this would result in the change in the hash function output uh to affect throughout the thing so people if they come in and actually check on all these value they should be able to know where the error is is their number five block or number six block okay so uh you can find this code in github and feel free to use it on whatever purpose that you want to want to do it from i'm not sure what you can use it for and that's basically the the really basic idea of blockchain which is create a chain so that all the transaction can be recorded securely and if and if anyone want to check if there's any error or someone have manipulated some previous data set it's very easy to look at the hash function and realize where is the error do understand that sometime is not a human deliberately go in and change the value it could be a cosmic ray or it could just be a computational errors that actually result in the errors in the earlier of the code okay so that that's it for now i learned i hope you know a little bit about blockchain more today after the video and we'll see you in the next video goodbye
Info
Channel: Liquid Brain
Views: 246
Rating: 5 out of 5
Keywords: Rstudio, Rstudio Tutorial, cryto, blockchain, hash function, sha256
Id: 5W7VZCHZAQY
Channel Id: undefined
Length: 18min 5sec (1085 seconds)
Published: Sat Jun 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.