Coding A Blockchain in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what is going on guys welcome back in today's video we're going to build a simple blockchain in python so let us get right into it now before we get into the actual coding i want to talk a little bit about the theory behind blockchain so how they're basically structured no worries i'm not going to make it mathematical here or go into too many details i'm just going to give you a basic uh simplified explanation of how blockchains work so that we are able to implement them so that we understand what we're implementing um and what we're going to do here is we're going to just use the example of some uh basic cryptocurrency that we're going to call neuralcoin and c and neuralcoin has transactions so transaction one for example could be that anna uh sends bob to neural coins and transaction actually nc like that uh transaction two can be bob sands daniel 4.3 nc and transaction 3 can be something like mark sense charlie 3.2 neural coins so those are three basic transactions and these transactions can be stored in a block so let's say we have the first block b1 this is the initial block and this initial block has the information about those three transactions and we can say okay it's the initial block so it doesn't have any additional hash information any additional basis or we can just say it has the basis start or anything like that or aaa just because it's the first block and then it has transaction one transaction two transaction three as the data now we can hash this block and this block results in a certain hash output um now usually we're going to to use for blockchains the shar 256 function and we're going to do that in the coding as well but for now let's just pretend that we get some random hash function that produces the output 76fd89 for example just a six letter a six digit hash hexadecimal number here and this is the hash result of the first block now then i open up the second block and this block is also going to have information about some additional transactions now i'm not going to write more transactions down but you can imagine you will have transaction 4 5 6 with other transaction transactions like those three here and we're just going to say okay we're going to have transaction 4 transaction 5 and transaction 6 in this block but also we're going to have the basis of this block so we're going to refer to the previous block by using its hash so we're going to enter 76fd89 as a basis for this hash so you can imagine it like that let's say we have some function called neural hash and this is just a random hash function we're going to use sha-256 after so afterwards but neural hash is just going to take the input of the transaction string so this is the transaction of course in real cryptocurrencies uh these are not just strings where it says anna sends bob to something but it's actually more uh like a number or a hash but let's just say we take this string here so we take the string of t1 the string of t2 the string of t3 and then we we take the string aaa we append all of them or we we concatenate all of them and then we calculate the hash and this is the result of the neural hash function so the same thing we do now for block two we take transaction four five and six we concatenate them and we also add the hash of the previous functions so we can add the string version of that hash so we just put some uh quotation marks around it and then we just concatenate this as well and then we produce the next hash and let's say the next hash is for example 8923 ff this is the hash of this uh block here and we can do this uh with more and more blocks but the basic idea is now you can maybe see why it's called a blockchain it's a little bit like like a reverse linked list uh with the exception that you're not actually pointing to the block so i cannot just use this hash to navigate to block number one because a hash is usually done in a one way or in a one way fashion so when i hash something i can get the output but it's very difficult to to to get back to the origin it's not like encryption where you have encryption and decryption uh but you're just hashing and then it's very hard to find out where this hash is coming from because hashing is trying to look kind of random and because of that we cannot really navigate back to block one so it's not actually a linked list but it's a blockchain because we have the information of the previous block in in the form of a hash in the next block and then we have the information of this hash here which contains this hash here as well and the information about those transactions and also in indirectly it contains the information about those three transactions we take this as the basis of block three so eight nine two three ff and then we have some more transactions t7 t8 and so on and this produces produces another hash and so on and why is this important it's important because you cannot just go ahead and change something because the whole thing is going to be messed up the whole integrity is lost if you change a single bit so for example since hash functions look kind of random a small change in the input it's going to uh to produce a radical change in the output so basically if i change this here to not 2 but 2.1 or to just one or anything like that this hash is not going to change slightly so it's not going to change like that it's going to look completely different so if i change 2 to 2.1 this is going to be something like fa56bb for example which is a completely different hash and because this changes this is going to change as well so this is now going to be fa 56bb and because of that this is going to be completely different as well so this is going to be 7 7 a b 0 0 for example and because of that this is going to change and so on so the whole integrity of the blockchain is now disrupted just because we changed a tiny little bit and because of that we can we can verify the integrity by just going through all the transactions and and actually we don't need to go through all the transactions because we only need to to look at the last hash and if anyone tries to to pretend like something else happened the whole blockchain is going to end up in a completely different hash and it's not going to work out so that is the basic idea of a blockchain all right so now we're done with the theory and we can start with the coding we're going to start by importing the hash lib module which is part of the core python stack and we're going to use it for hashing in particular we're going to use the sha-256 function so we're going to say hash lib.sha 256 but we're not going to do this yet we're going to use this later on uh first of all we're going to define a class which we're going to call neural coin block so this is going to be one block of the neural coin blockchain and in its constructor in its initializer we're going to pass the self keyword and then the hash as we already talked about this we're going to pass the hash of the previous block so we're going to say previous block hash and we're going to say transaction list transaction list because we need a list of all the transactions the hash of the previous blog and then we're going to calculate a calculate a new hash for this particular block um and now we're just going to say self dot previous block hash equals previous block hash self.transaction list equals transaction list and what we're going to do now is we're going to just construct a data string and you can do this in any way you want you can just append all of them or concatenate all the strings you can combine them with a separator which is what i'm going to do you can do whatever you want i'm just going to say self dot block data equals and then i'm going to use uh the simple dash as a as a separator and i'm going to say join all the transactions so join everything from the transaction list into one string and then i'm also going to just say plus and again the same separator and the previous block hash in the end so this is the basic uh construction method that i use here for the block data and then we're going to say self dot block hash this is going to be the resulting cache is going to be hash lib dot sha 256 and here we're going to pass this string so self dot block data we're going to have to encode it otherwise it's not going to work and then in order to get a string out of this or a hexadecimal string we're going to have to use the hex digest so we're going to say hex digest and this is how we calculate the hash so this is a very simple class it's actually done we don't need anything but the constructor and now we can go ahead and write some transactions so i'm going to try to keep it simple here so we're going to say t1 anna sends 2 nc to mike and we're going to copy that now and we're going to change this to 2 3 4 5 let's let's do a 6 as well and we're going to have just two transactions per block um and first of all anna sends two neuralcoin to mike then bob sends then mike sense then daniel sends then mike sends again and then one more time mike sends something here um and first of all or the first time we send two neural coins then we send four point come on 4.1 then we have 3.2 0.3 1 and 5.4 and in the first transaction mike is the receiver in the second transaction mike is also the receiver then mike is going to be sending to bob then uh daniel is going to be sending to anna mike is going to be sending to charlie and then mike is going to be sending to daniel like that those are the transactions and now we can just go ahead and create an initial block uh so we're going to say initial block is going to be neural coin block and here we're just going to either pass nothing but i think if we pass none this is not going to work so we can pass an anti-empty string or we can uh just choose a basic initializing message so like initial string like that doesn't really matter um and what we can now do is we can pass a list of transactions so here we're just going to say t1 t2 and by doing that we're going to take all the individual strings of that list we're going to join them with the separator we're going to combine this uh with the previous block cache which is in this case just an initial string and then we're going to calculate a hash so we can actually try and do that right now by just saying print initial block dot block data so to see uh so that we can see what this looks like and then block hash as well and we're going to open up a terminal and we're going to navigate here and we're going to say python3main.py uh i hope i'm not blocking again let me just check here no seems good um let me just see if i didn't block any code no i shouldn't be blocking any code but as you can see we get anna sends two nc to mike bob sends 4.1 and c to mike initial string and then we get this hash as a result now let me just show you really quick what happens if i change one tiny little bit so let's say bob didn't send 4.1 neuralcoin to mike but 4.2 neural coins to mike so if we do that and we rerun this it's going to be a completely different hash as you can see it's not just slightly different it's completely different and this this is not uh different every time we do it so this is always the same hash but if we change something in the input we're going to get a radical difference in the output so this is very important uh and now we can create a second block so we can say second block equals neural coin block and here we're going to use the hash of the previous block as as a basis here so we're going to say initial block dot block hash and then we're going to have transaction three and transaction four and we're going to print this data as well so we're going to copy that and i'm going to oh sorry i'm going to change this here to second block like that come on ah second block there you go and we can now run this and you can see that we got mike sense uh 3.22 bob daniel sends 0.3 to anna and then we get the hash of the previous one and we get a hash as you can see they're all fixed size no matter what the input string is they're all going to have the same size um and we can now also go ahead and create a third block and then we're going to see how these blocks are interconnected so we're going to actually just copy this here and we're going to um print this and we're going to change this to third block i'm going to change this to third block come on the mic is blocking my sights so i'm pressing the wrong keys here there you go and here we're not going to use the initial block as a basis but we're going to use the second block as a basis which indirectly means that we're actually using the first block as a basis as well and here we're going to pass transaction 5 transaction six so as you can see we now have the initial block with the first two transactions then the second block with three and four and the third block with five and six and the third block is based on the second block which is based on the initial block which is just based on a random string here or an initial string it's not random actually and i can now run this and we can see a bunch of different hashes but the interesting part is right now that if i change anything in the first hash we're going to get a completely different result in all of these hashes so let's just go ahead and change this again to not 4.2 but 4.3 for example if i now look at this we can see that not only the first hash is different but as a result of that since this is the basis for the second hash we can see that the second hash is also completely different and so is the third one so it's a completely different result just because we changed one thing so you cannot disrupt the integrity of the blockchain by changing some tiny thing in a previous uh transaction in the past because the whole blockchain is based on that so every future transaction has to be based on this block here and if it isn't based on this block we're going to see that this that it is not based on this block because we're going to see that there is something wrong uh with the hash it's not the right hash so uh the people are going to see that the hash is different from their hash and they're not going to accept your manipulation of the transactions so that's it for today's video hope you enjoyed and hope you'll learn something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this channel in order to see more future videos for free other than that thank you very much for watching see you in the next video and bye [Music] you
Info
Channel: NeuralNine
Views: 35,291
Rating: 4.9698009 out of 5
Keywords: python, finance, investing, chart, charts, data science, prices, price, stock, crypto, bitcoin, ethereum, crypto currency, cryptocurrency, cryptocurrencies, visualize, candlestick, ripple, litecoin, correlation, heatmap, blockchain, blockchains, hashing, cryptography, hash, sha256
Id: pYasYyjByKI
Channel Id: undefined
Length: 17min 15sec (1035 seconds)
Published: Sun Mar 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.