Blockchain Explained: Supply Chain Example with Python Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
there is no doubt that 2020 has really challenged our digital journey as a society and has really spawned a lot of innovation in different industries blockchain is one of those capabilities and technologies that's still in its infancy but has a lot of potential in the future there's several use cases that people can use with blockchain such as banking supply chains digital IDs and a whole bunch of other things so there's a lot of different use cases for it today I want to help introduce the concept of blockchain but at the same time I want to use an example and that is more of a retail example so for this example we're basically going to look at how goods move from a manufacturer to a transportation company filing to a retailer so we're gonna keep this example fairly simple though in reality it's generally a lot more complicated than just having these three different entities or different parties so in today's example what we're gonna try to look at is we're gonna see how manufacture X produces a pair of pants a shirt and another identical shirt and we want to see how this actually moves through the supply chain and how we can use things like blockchain to actually track the movement of these items but as well as the condition of these items so that by the time I get to the retailer or finally in the hands of a customer if there is an issue with any of these we can actually trace back to see where that issue started so let's go over some blockchain basics now to sort of articulate what this looks like alright so let's go back and look at exactly what a blockchain is so really in its simplicity form a blockchain is literally a series of blocks that are chained together hence the term blockchain now there's something unique about each and every one of these blocks and that is each one of these boxes house some kind of information and the information that's within this box is actually very unique let's take a look at an example so let's start with the most basic example and that is people now there's something uniquely identifiable about all these different people that enables us to separate them one from another for example each of these individuals have a name they have a birthdate they have a country they were born in they have an eye color they have a hair color and they have a gender and for the most part most of these attributes are fairly permanent and very difficult to change and that's what make these individuals very very unique very similarly in a block Chane when you have a block there are certain attributes about the blockchain that are very unique and very individualized to that specific block for example there's a specific timestamp so if a transaction happens it'll lock in that timestamp and it's not mutable it'll look at something called a previous hash and now the previous hash is a very important component as a part of the blockchain and really what ties one block to the next so when you think about a previous hash think about it as an identifier that enables me to link this block to the preceding block or the block just before this it carries that unique identifier so that if this block were ever to get changed that any proceeding blocks thereafter would also be mutated and therefore break the chain so let's take a look at some of these nodes that are coming in and we're gonna focus only on the previous hash and the hash although there are other data points as well as a part of this specific node or block here you can see that the hash of the current voxel in this case d7 0 1 in the first box becomes a previous hash in the next box and that previous hash in d7 0 1 is actually part of the data that's used to derive the hash for that box which is JQ LP now when that data gets tampered with so for example if we go ahead and remove the previous hash and we change it notice that the previous hash for this is now d7 a 3 which doesn't line up with d7 0 1 which is the current hash in the first box but because we change the previous hash in the second box that also changes a hash value in the second box as well and as a result we get a new hash now you can see that there's a misalignment in the second box and third box as well between the hash and the previous hash so for example you can see d7 a 1 no longer lines up with d7 a 3 and PLL P no longer lines up with JQ LP as a result this chain is broken we also have something called a nonce and a nonce is actually used in something called a proof-of-work so Bitcoin really brought this to light when they introduced a concept of proof-of-work and improve will work you have to solve a mathematical puzzle in order to be able to add a block to the end of the chain and the first person to solve that mathematical problem is the one that gets rewarded the Bitcoin in other words this process is called mining the nonce is a dummy variable we introduced to the data set that allows us to increment it every single time in order for us to meet certain types of rules so let's go ahead and see how this nonce works so for the hash that actually meet a specific rule in this case the hash has to start with X values of zero so we can say that has to start with one two or three zeros whatever that value that we pick that is actually known as the difficulty so if I say difficulty X is equal to two in this case it has to start with two hashes and that's what we're gonna go after now the hash function itself is composed of different things so we'll look at the previous hash we'll look at the transactions we'll look at the index we'll look at the nonce and there's a whole bunch of different things that people will go ahead and add to this and so we do the first iteration and we come up with this hash which is actually invalid because it does not start with two zeros then we go ahead and do it eration number two and we do the same thing here except you'll notice that an iteration number one nonce was equal to zero in iteration two we make nonce equal to one it goes ahead and does its work and again the result is invalid then we go ahead and do it eration number three now we set nadh's equal to two and finally we have a valid result because it starts with two zeros which is actually met the rule that we wanted and this is how bitcoin and some other big cryptocurrencies control tampering with some of these Bitcoin because what they do is they put these really difficult challenges that people have to go ahead and figure out and if somebody were to tamper with it it takes a really long time to recalculate the downstream nodes or the downstream blocks but by putting something like this in place it takes a lot longer for you to calculate the downstream blocks and by the time you're able to calculate that a new block has already entered the blockchain which makes it almost infinitely harder to go ahead and do this now we just did a very basic example where nonce was equal to two but in reality this number is sometimes in the millions or even billions now I'm not going to talk about things like the 51% Huck that's for a different video but at the end of the day now you know exactly what the nonce does next we also have current transactions so these are going to be the transactions we're going to look through within our supply chain that is the movement from manufacturer to transportation to supplier and then finally after all of that data gets encrypted we have something called a generated hash and that hash is specific to this block and will become the previous hash value for the next block and we're also gonna give this an index as well so that we can retrieve this one specific block if we ever need to all right so now that we got the concept down let's go ahead and explore what the code would look like to build this and we're gonna start with some of our dependencies so from hashlib we're gonna bring in sha-256 we're also going to use JSON and to derive a timestamp we're also gonna use date/time now remember shot 256 is just one of the different encryption algorithms that you can use with an hashlib there's many other ones there's shot 256 3 then there's sha-512 and a whole bunch of different ones but for the purpose of this we're gonna use 256 a lot of organizations for example bitcoin uses 256 as well so we're gonna stick with this algorithm for now alright first we're going to start off with creating the block now the block itself it needs to be an issue ice with a constructor and for that we're gonna have self we're gonna use an index to identify the block we're gonna have the previous hash because that's how blockchain works which we'll talk about in a second we're gonna look at all the current transactions so that includes everything moving through the supply chain a timestamp and a nonce as well then we're gonna go ahead and compute something called the hash function and the hash function is gonna allow us to take all of that data above and encode it into a hexadecimal or hexa digest in this case and that will act as our new hash value and finally when I go ahead and call this block I want to be able to return the actual dictionary values and not some random stuff next I want to go ahead and actually create the class for the blockchain and this we're gonna again have a constructor with the actual chain and what's in there the transactions and the Genesis block and again go back and return the dictionary so that I can pull something that's English and meaningful now the Genesis block is a very important block within the blockchain space it basically is the first block within the blockchain it doesn't necessarily have to be part of the transactions but it's going to be the first block that's going to initiate the blockchain and so in this case we're just putting in dummy data as a part of the block class and designating that to Genesis block and then going ahead and creating an initial hash value so that we have something to use in the next block to reference back to the Genesis block and use that as the previous hash we're also going to create a method that gets the last block which is essentially the last element in the actual chain and now we're going to talk about proof-of-work again this is the example we went through it we're looking at the difficulty initially setting the nonce is zero and we're gonna basically iterate through the nonce in this case I'm saying it has to end with one zero and it has to have five five in there somewhere we're gonna go ahead and compute the hash and then return the hash back has that specific blocks new hash value and now for us to add a node I'm gonna go ahead and create an instance of the class block where I'm going to add in the index the previous hash the data or transactions in this case the date time stamp and start off with the nonce of zero our hash is gonna be equal to what we found in the proof of work and then I'm gonna go ahead and append that hash to the chain and append the transactions to the transaction chain and you may be wondering why my date timestamp is in a string in practice you wouldn't do that I just did it so that my hash value then it bounced around all over the place and last we're gonna go ahead and add in our transactions function this is going to allow me to look at all the different blocks from the manufacturer of transportation and retailers perspective and we're just putting in command that when I call this function and if I use the word all as my ID it's gonna pull all the specific transactions related to this blockchain otherwise if I put in a specific ID then it's gonna show me just that specific node alright now let's talk about a transaction and exactly what goes in to identify each one of these specific products so first we're gonna have a timestamp in terms of when that exchange took place on this case between the manufacturer and the transportation company when that it change was initiated we're gonna look at a product ID so the product ID for the two yellow shirts would be identical something like 100 then we're gonna look at something called a product serial which is going to be unique to this specific item and this specific item only so the two yellow shirts would in theory have a different product cereal and that is how you identify this specific product it's gonna say who the manufacturer is so where it came from where it's going in terms of the transportation routes to the next destination for this product any messages like the product is in good order when a product is damaged or you know retailer look at it any flags that they say so if there's something wrong with it maybe you put a yes flag or a no flag and then finally a digital signature which basically approves the shirt from moving between the manufacturer and the transportation company so then the next question is well if I do have a block chain here itself how do we get agreement upon the manufacturer the transportation and the retailer like how does this actually work well in reality there's actually a duplicate copy of the exact same block chain with all three different entities in this point and when a new block comes the only way for it to get added to the end of the chain is if all three entities in this case agree and provide a digital signature now in the backend what's gonna happen is they're all gonna provide the previous hash from the last element or the last block in the blockchain and it's gonna do the calculation based on the rules we talked about and if successful it gets added to the blockchain only if 51% or more of the entities actually agree to it so if the manufacturer and transportation company agree to it it gets added to the blockchain but if it's less than 51% it's generally flagged as a suspicious transaction and not added to the chain and this is how you hold people accountable so when I have two different entities that agree to it I'm basically agreeing that when I'm transferring the goods from the manufacturer to the transportation company it is in good order and good condition and the transportation company then provides a digital signature and says yes I'm gonna receive this because is in good order alright now let's look at the data that we've been talking about so I've got some dummy data here where I've got the timestamp the Product ID the serial number these are the cotton pants the manufacturer the transportation any kind of message that we want to put a digital signature to say that it's approved and whether or not there's a flag to this or not and I've created that basically for the two cotton shirts as well so this is going to be for the transaction between the manufacturer because it's a coin from the manufacturer to the transportation company and then similarly I've created some data from the transportation company to the actual retailer with the exact same information and same attributes of it and in this case we're gonna simulate an event where they actually say hey the product is damaged so the retailer go ahead and review this and they flagged it as a yes and when it comes to the retailer the other two items look fine but this last one they actually have rejected it and it's basically going from the retailer back to the vendor so our T V stands for return to vendor so let's go ahead and see what this looks like from a blockchain perspective so I've created an instance of our blockchain class and I've basically said that is equal to B I said go ahead and add all these transactions of these manufacturer transactions to the blockchain go ahead and add transportation retailer and so now I'm going to go ahead and call my method and my method here to get all these transactions is called get transactions so I'm going to take this method and I'm gonna say B dot get transactions so B dot get transactions and I want all of them so what you see here is these are all the actual blockchain transactions and in each one of these what you'll see is you'll see the previous hash and this one belongs to the Genesis but the rule that I put in place so if we go back to the difficulty and the proof-of-work that I was talking about is that it has to end with a zero and it has to have a 5/5 in there somewhere so if I look at the hash for this one it ends in a zero and it has a 5/5 right over there and the same thing goes with the next one I can change its difficulty to two and before I run this I want you to also look at the nonce the nonce in this case means that it took me sixty five different iterations to actually come across to meet this rule and this is where we were talking about earlier how we need to make it somewhat difficult to solve some of these problems because if there's tampering that happens it's too easy to recalculate downstream and the nonce and the first one was 112 but now if I change the difficulty to two let's see what happens so that took a while to run and the reason is we made this a lot harder and so check this out when you look at this look at the nonce value here so the nonce value here was three hundred and three thousand so it took me three hundred and three thousand different iterations to meet those difficulty rules in order for me to generate a hash that will meet the rules if I look at transportation 88,000 different iterations and if when I look at the retail or one almost three hundred and sixty-eight thousand different iterations so now essentially I've got two zeros at the end because that's what I said because I've set my difficulty to it's gonna actually gonna say that I need four or five in a row and in this case we have four or five s right there we have two zeros at the end and then we have four or five s right there so it's actually meeting all the different requirements that we have which is why it takes a little bit longer and you can make this as easy or as hard as you want and within these individual nodes or blocks we actually have all the information we need and so from the manufacturer we're talking about the cotton pants for example it went from the manufacturer it went to the transportation company so all of this stuff is actually encoded through this hash value and this hash value here is actually used over here because now you can see that this previous hash is pointing to the hash value of the original one and all of this information here is actually used to go ahead and recalculate a new hash value so if one of these entities went in and tried to go ahead and change something like you know the flag on it to say no it wasn't actually damaged it's gonna go ahead and recalculate this hash but again if we go back to the previous slide the only way it's gonna get added to the blockchain is in fact if 51% or more of the people agree now because the other records show that it is in fact flagged and somebody changes to a no it's gonna get changed only on that one entity but the other two are still gonna show yes so when it calculates the hash because the calculation of Y or n here impacts this when it calculates the hash and it doesn't meet it's going to say nope somebody's lying or that's a fraudulent transaction so we're not gonna add it to the blockchain all right so let's go back and follow our example of the pans and two shirts what happened in this case is a transportation company said hey I digitally approve it everything looks fine the Box is good so the pants move over then next they also look at the first shirt and they say hey the shirt looks good so we're gonna move the shirt over as well now when the product actually moved from the manufacturer to the transportation company they originally approved it but somewhere along the way there's a digital signature that says wait a minute now this product is damaged so now there's a question mark now keep in mind when the products move from the manufacturer to the transportation company was all approved so everything was okay that means that the manufacturer really isn't at fault here something must have happened in transit because when they picked it up and looked good but now that they have it in their Depot there's something wrong with it so let's continue following this example all right so again looking at this information this was the information that the manufacturer sent or the transaction between the manufacturer and the transportation company so when you see that everything here was from the manufacturer went to the transportation company everything from a digital signature standpoint was approved and it looked good there was no flags within this transaction when I go from the transportation company now that they have received the product they flagged that wait a minute something's wrong with this product the retailer should actually go ahead and review this now this digital signature basically turns a liability on to the transportation company because both accepted the block in the blockchain and said everything was good between the manufacturer and the transportation company and now I can actually also trace back that specific one item because it's got its own unique product serial number which is right over here well the other two products are just fine so there's nothing wrong there so if this was ever sold to a customer I can look at this product serial number and go through the actual chain and see where that chain broke or where that issue arise which is right over here so if this did ever escape the human eye or did ever escape you know going through the supply chain and it hit the retailer and it went to the actual shelf and a customer bought it and you know they were upset because there was something missing or like it or like one of the pockets were ripped or something like that it'd be very hard to trace back to see where that happened if the due diligence is done along the blockchain this is a great way to add some accountability as well and then finally when it actually hits the retailer in this case the retailer says hey the box is damaged I'm gonna send this back to the vendor but how does the money actually get exchanged and who's accountable for what let's take a look again at that example alright so going back to our example here when the goods actually get transported from the transportation company to the retailer we said the pants were fine the first shirt was okay now the second one was flagged and it had some question marks but at the end of the day it's still gonna get shipped back to the retailer now the retailer takes a look at it they inspect it and they say you know what I have issues with this this shirts no good in which case you do an RVT which is a return to vendor so the shirt goes back to the vendor and then the vendor will go ahead and refund the money back to the retailer but when we looked at the blockchain we actually saw the manufacturer was technically off the hook the challenge or the issue was really on the transportation side now in this case the manufacturer would go ahead and say hey mister transportation company I had insurance with you I need the money back because you guys damaged a package and so this is the way that all this stuff happens but how does the money actually flow this is what we introduced something called smart contracts and smart contracts are predetermined rules that are set in place I will automatically move money back and forth so if the blockchain in this case said hey listen the issue was on the transportation side this movement of money would actually get executed through the smart contract itself based on certain rules that were put in and the money would move automatically eliminating a bunch of manual work and a ton of frustration well that is all for today guys so if you did like this video please like comment and don't forget to subscribe and I will see you next time thanks very much bye bye [Music]
Info
Channel: SATSifaction
Views: 6,181
Rating: 4.9587631 out of 5
Keywords: blockchain, python, introduction to blockchain, blockchain basics, block chain, intro to blockchain, what is block chain, how does blockchain work, python and block chain, blockchain in supply chain, blockchain supply chain, python programming, nonce, proof of work, blockchain simplified, bitcoin, ethereum, Python tutorial, Blockchain explained, Blockchain tutorial, Blockchain developer, Python blockchain smart contracts, data science, machine learning, ml, What is block chain?
Id: MyXndVDCIY8
Channel Id: undefined
Length: 21min 17sec (1277 seconds)
Published: Sat Jun 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.