Exploring Hyperledger Composer



hey guys welcome to another coding video here at block geeks my name is Neelu I'm one of the instructors here at block EAC's and today we're gonna do something a little bit different than the kind of coding videos that we've done in the past we're gonna actually look at programming on private blockchains and more specifically hyper ledger fabric so the problem with hyper ledger fabric has always been that it was very not very user friendly in terms of just like allowing new developers to engage with a platform and engage with building their own private blockchains and the reason for that is that there wasn't really ever a user interface kind of like if you think about solidity programming on solidity how you had an environment like remix where you could kind of write some code tested deploy it kind of get a sense of what it's like to write a smart contract on the blockchain and how that smart contract works with hyper ledger fabric this was really difficult because hyper because it's a private blockchain network it required a lot of like system administration stuff and so everything was also done from the command line so it was really difficult you know so you spent so much of your time just kind of like trying to like get the permissions for this account and then switching to another account and then just kind of like writing commands on the command line and not really getting a sense of what it's like to use that network and what it's like to build a private blockchain so it made it really difficult for new developers to engage with it fortunately hyper ledger actually created this new kind of playground called hyper ledger composer and it's like an integrated development environment basically exactly like remix for solidity and it's for hyper ledger fabric and it's for allowing us to kind of build simple applications test them build simple blockchain networks and kind of test them and then as we want to kind of do more you know like take it to the real world and like publish it for real then we can move it to a hyper ledger fabric but it creates a really perfect testing ground and a really great place to start our learning so that's essentially what we're going to look at today we're going to look at how to play with hyper ledger composer and create our very own simple private blockchain Network cool so let's get started so the get started the very first thing you guys need is to actually go I'm gonna see if I can zoom I can't really zoom into the into here but so you have to go to hyper light github io / composer easier thing to do is if you just go to google and type hyper ledger composer you'll just go to this page here and then you just say installing hyper ledger and then you just say try the composer online playground and it'll just bring you to the composer playground cool and so this is like a network that I created in the past so we can remove that and we can kind of create our own network this is like a sample one they give you if you just want to like test the network but we're actually gonna do more than that today we're going to create our own network and so to get started let me just see to get started we are going to click on deploy a new business network and then in here we just have to give some basic information so again like I mentioned hyper ledger fabric is a private network so you're gonna have your own business network with its own logic and its own actors and we're gonna walk through all the steps of how to do that cool so we basically call it we can call it like test or maybe like sample network or something like that you guys can call it whatever you want and then you have like a little description button where you can kind of describe whatever you want your network to be so sample network to test how hyper ledger composer works and then you can give the network admin card which will be created I don't think you need to really put anything in here but at this point just going to call it an empty business network and we can just call it like admin at sample dot sample that or something and you can yeah you can just put like a this is basically like your admin like your administrator account essentially so if you had a real network it would be like the admin from that network but in this case since we don't actually have one we can just make something up because we're just testing along cool so we created our sample network and to get into it we're just gonna go in here cool so now that we're inside here we can actually start deploying like start adding files and then basically saying deploy changes and then testing it over here so we have a couple of files so we have an about file so this would just be kind of like a readme type thing typically if you guys have done any web development he would just put some information about what your system or network or whatever is you have the model file so the model file is basically where you define all of your assets and all of your participants and all of your kind of transaction types so this is kind of like if you think about it sort of like your data and in a weird way it's kind of like your where you define your data type so for example you'll say like these are the types of assets that I want to move around these are the actors that I want to have in this network and these are the types of transactions I want to allow so this is essentially where you would define all of that and then you have the access control file which basically just says who is allowed to access the network who's allowed to access which functions in which capacity so you define all of that here we're not going to touch this file so much today just because so this is like typical setting just says that the network admin can contract it can access all the functions and we're the network admin so we're just going to do that but if you wanted to do like if you had like a real network or you want to test different kinds of permissions you could start playing around with this file again because this goes a little bit into system administration territory I don't really want to deal with it today I just want to deal with like actually building applications using hyper ledger fabrics so let's get started cool so we are in our business network now so the first thing we want to do we're gonna go to our model file if you guys are coding along definitely follow along with me so let's delete all this kind of like extra code or like boilerplate code and we're just gonna write our own code and the great thing about this is that you guys will notice it's the text editor basically tells you if there's any errors or anything like that and the model file is actually written if you guys notice the dot CTO it's written in the kind of composed language so they've actually made this language specifically for creating these files so let's just start we're gonna do some comments first and we're gonna say okay I'm gonna call it my commodity so commodity trading network and we're basically just creating like a really simple application that allows us to have some commodities and allow that commodity to be traded from one participant on the network to another participant on the network and so that's essentially what we're building and we're gonna create a very very simple version of that so always with hyper ledger compose you're gonna start with your namespace namespace or example dot my network for example and so this is basically just telling you what network you're operating from and then you're gonna define your different types so first we're going to find an asset so like I said we basically have three types of data types or models essentially on hyper ledger fabric we have assets which are basically kind of like the basically exactly like asset so you could be a cryptocurrency it could be you know like a car or a lot of produce good or whatever kind of thing that has value that exists on the blockchain right and then we also have participants which essentially can affect change or can own an asset or can trade an asset or whatever we want and then we also have transactions which are basically an action that a participant can perform on top on an asset so we're gonna see exactly how that works in a bit but so the way you would define it you just basically say asset because you want to ask that type and then the name of your asset is commodity and then you're gonna say identified by and the identified by just tells you like what the ID of that acid is so we're gonna say trading symbol and you're gonna see exactly how that works so we're creating our object like this and we just start with a little like davio and then we're gonna say string trading symbol so we've defined what the type is and then we define the variable basically so this is essentially if you guys think about it like an object with a list of attributes in it so inside it has a string called trading symbol and as we said identified by trading symbols so that means that the trading symbol is it's like the identification so if you're looking for specific so you can essentially ID them by the trading symbol right look at a typical relational database this would be like the ID which would be like 1 2 3 4 5 but here we're basically saying this attribute on this object is its identifier and then we want to also define a string and notice the capitals okay so we're actually capitalizing the name the variable type and then we're going to have a description which is just going to be a description of what that commodity is and then we're going to have a string called main exchange which is just going to be like where you can exchange it and then we're gonna have a type called double so double is like a double integer so like integer with the decimals but not it's not an integer obvious it's a little bit like a number a decimal number essentially quantity no quantity okay and that's basically just like yeah how much of it basically and then we can do this thing and it'll be trader owner so we're saying we also have a participant in it who is of type traitor and the name of that variable is the owner cool so now that we have this let's define their actual trader data type so if you guys notice this is kind of similar to creating structs on solidity where you're essentially like creating a type like trade or like below and then you're essentially calling like a type trader and then giving it a name for that variable to store that type trader cool so we're going to call this again participant trader and it's gonna say identified by trade ID okay so similar you can see that we're still using the very same format for defining all of these asset all of these like data types or models essentially and here we're just gonna do again oh string trade ID and then we also want to string for first name and then a string remember the capitalizations string for last name and if you guys see like this basically just looks like defining datatypes in a typical relational database right so you have your like table essentially and then you have each of the kind of like columns in the table right and this is essentially the same thing and then we also have a transaction type which is called a trade so the name of the transaction is a trade the trade transaction and then we're going to define that here as well like that and we're just going to have that it belongs to the commodity commodity like the type commodity which you've defined up here and we can just name it commodity and also to trader and we're gonna call the name so again type trader so again you see the commodity has a type trader variable named owner and the transaction data model has a trader type variable named new owner and the way that we want to do this is that we essentially want every asset to essentially have a trader who owns it and then whenever a transaction happens we assign a new owner to that transaction and then what we want to happen is that we want the owner on the commodity to get updated by the new owner attribute inside inside the transaction so essentially what we want our application to do is we have a list of commodities and we have the list of traders and each kind of commodity is owned by a trader and what we want to do is we want to say move this commodity from trader a to transfer it from trader a to trader B and then we want the attribute trader or owner on the commodity to get changed and updated with the new trader right so like now we've basically done the work of defining our data types and so the next step that we want to do is we want to actually add the logic that I just described so this just basically tells us what are the things that we can do but it doesn't really tell us what happens sorry it doesn't really tell us what happens when we actually complete a trade transaction so to do that we actually have to define a new type of file here called a script file a dot script file and the script file is actually just written in plain old JavaScript so that's kind of nice because I'm most hopeful you guys are familiar with that so you just come here you say add a file and then you create a script file and so it says you define the logic of transaction executions so what what what happens when you do a transaction and we're going to define that right now so let's create a new JavaScript file using the script file like sorry add file here and then we got some sample script here so let's delete that and we'll just add our own script into it so let's just do a little comment tag it's usually a really good idea to do common tags because it's just you know just good practice and it tells everybody else what you're doing so we're gonna say track the trade of a commodity commodity from one trader to another and then we're gonna create another one we're gonna say a variable so we're gonna say what parameters it's gonna take so I'm going to call it params and we're gonna say or example my network trade so it's essentially we're basically saying like this is how you would make a transaction call right so you're essentially calling the network and then you're calling dot trade which is the name of this transaction so if you go back to the model file you see the transaction trade so we're basically saying we're we're defining this the logic of the transaction trade and this is how you would essentially call it cool and then we're gonna say trade and then the trade to be processed and then yeah you're passing a variable trade which is like the trade to be processed and then it's a type transaction so we're gonna say transaction cool and then we're just gonna close our comments like that and then we can just get started and to do that we're just going to create an async function if you guys are not familiar with async await it's basically the new asynchronous format on JavaScript and it's really really easy because it makes the process of like waiting for callback functions and stuff look back to waiting for asynchronous functions to deliver data a lot easier because it basically just says start the function and wait until I get my result and then continue so that's really really nice that's basically making async like not it's making asynchronous synchronous because it's basically saying like wait until I get my results and then continue with the function so we're just gonna use async away so when I say async function and then we're going to call our function trade commodity and then we're gonna pass it the variable trade just like we said so we said this is our how we call our function and then we pass it the variable trade which is the trade to be processed and then we're gonna go like this one thing that's not great about this IDE is that it doesn't do autocomplete brackets and stuff so definitely takes a little bit of getting used to so we're gonna say trade dot commodity owner so the trade is essentially like the trade transaction right remember this thing that we defined here so we're so we essentially create this object and then we pass it to this absurd yeah trade this object then we pass it to this async function which then performs this logic so we're passing this trade data object so we're saying the trade commodity owner so we're essentially getting the commodities owner attribute so whoever the owner is owner and we're gonna name it a trade new owner so if you guys see we basically just did the function that we wanted to so we basically took this attribute so we have so our transaction object has a commodity field and a trade and a new owner field right so these things are both filled and that's what we passed into our function here and then so we took the commodity and we took its owner and we basically updated it with a new owner attribute on the trade on the transaction so we basically kind of like updated the owner of the commodity cool and then now we're gonna assign it to a registry which basically lists all the kind of like changes to the commodity as it goes on because one of the important things about a blockchain is that you still want to keep a record of everything that happened in the past so you don't want to overwrite that information so you also want to include the whatever new information you updated or like the record of that transaction into a registry so let's say asset registry and we're gonna say await so we're using a wait and then get asset registry and then so we're getting the Org example Network commodity so we're getting the commodity objects so like this is the full commodity data type right so we're getting the register asset registry I've come on commodity data type and then we're basically saying a weight asset registry update trade commodity cool so we basically like we had an asset registry for all of our commodities which included all the updates that we made to the commodity object and then what we've done now so we've basically gotten it and then once we've gotten it we're basically calling an update function to it to add our new commodity which we got here into in the new commodity which we also updated with a new owner into our list of registries cool so we can see like all the different instances of our commodity as they've changed throughout time okay cool so it looks like there is a syntax error here let me see where I have let me just take it off then yeah okay well that was there's just something wrong with my comment I guess cool so we've created our async file and then the next thing we want to do is like I said typically at this point you would be talking about changing your access control or updating your access control we can take a look at it right now so our network admin we just basically define rules right so you define rules and each rule has a description it has the participant of who's allowed to participate in it it has an operation and then resources and then action whatever type you want it to be and the current system that we have is basically giving our current participant which is the network admin full access to the network and this is the current settings that we all have already and if you guys are interested you can definitely check out the hyper ledger compose from a breaks documentation for learning more about how to create the access control file or changing the access control file but again like I said since we're not trying to get into the whole crazy world of system admin in this short video I'm not gonna spend any time going over it cool so like now that we've kind of got our model file and we've got a little bit of logic to define how to work it we can basically deploy our changes so really simple so we can just say deploy changes and if you guys see our changes are deployed and now to test it we actually just go to this test test link like sorry test tab right here and now we can actually try to test and play around with our basically Business Network and see how that works cool so the first thing that we want to do once we've created it is we're gonna go to the trader tab and we're going to create some new participants so we're going to create some traders that we can actually use to trade commodities right and we're gonna see how that's going to work so we're gonna create a new participant like this and so it basically opens a datatype where you can define the things that you want and so you can see this has a couple of like items I guess in the data in the data type and so the first one is the class of the class is or example my network trader so if you guys remember we gave the namespace or example my network trader so that just means the network that this kind of like these attributes belong to so like whatever the name of your hyper ledger fabric network is would be the namespace and inside there you would define your assets and participants and all these other kinds of things so we're gonna have our trader so they gave us a random trader ID so we can just change it to like trader one if we want to and then we can kind of give a name to it so we can call this one Jenny maybe Jones or something that's good so we've created our first participant so when you say create new you just see your trader right here with all the information about them and what I really love about this is just how easy and user-friendly it is and if you guys have any experience with hyper ledger fabric before this it was just like the most difficult thing just because it basically is all super server-side and it has no kind of it has no like helpful user interface it just helps you understand what's going on so this is really really like a big step up for them and I'm really happy about that so again we're creating a second participant now I'm going to call this guy trader – and you know what let's make this one a girl – we'll call her Amy Amy Amy and then maybe Williams or something new Leone's okay cool yeah so that's great both of them nice so we have two participants and now you can see in this like trader view that your participants exist if you want to you can add more traders but for our instance we just want to show one trade transactions oh we don't really need to do that and the next thing we're going to do is we're going to create our asset and we're gonna go here into the commodities tab so you guys can see you see the three types of they describe participant ask that transaction and then you can create instances of that here if you want so we're gonna create our new asset and we're gonna basically say yeah again in our classes inside the network of type commodity and our trading symbol we're just gonna make it ABC you know it could also be like cat or something if it was a currency right but we're just gonna say ABC and our description is going to be like a test ooop commodity cool and then the exchange could be like Euronext or something I don't know just some random exchange and the quantity we're just going to put some decimal number two nine seven cool on our owner instead of having having it be so you again you can see kind of like how we're defining so we're saying resource and then we're giving the name of our network or example than my network and then we're giving the trader ID so instead of giving trader that we're just gonna say trader so the way that you would always define a specific instance would be the name with a hashtag sign and then the ID or the true identified ass remember when we're saying identified by the trade ID so we're gonna call a trader one so we're basically going to say the owner of this current asset is trader one so let's create it cool and you can kind of see and make sure that it says trader one there perfect cool so now that we're done we have two traders we have a commodity and all we really want to do is create a transaction that changes the ownership of this commodity from trader one to trade or two so let's do that so we're gonna go to submit a transaction and again it creates a new data and you can see the transaction type trade right so you can actually define multiple commodities multiple types of participants multiple types of commodities multiple types of transactions and here you would just essentially select which one you wanna which type of transaction you want so since we only have the one we just have type trade and we can see our data type preview isn't like the data that we're gonna pass to our transaction object and so we're just gonna make sure that everything is good abduct type trade so we're gonna color just make sure that these IDs are updated because it's just prefilled with some data but we're gonna fill it with the IDS that we gain so we're going to say type 'abc again you see the name of the class type or whatever the data type hashtag and then the ID of that particular item traitor and then we're passing the new owner we want the new owner to be traitor a traitor – perfect so we're gonna submit this transaction couldn't I find any functions to execute the transaction what is this yeah so basically I found out what the problem was and I think it's really important to to tell you guys so remember when I was first creating this file remember I said like oh this is just like a comment and you can and there was some problem with the comment so I deleted it apparently if you don't include this here it doesn't this script file doesn't recognize that its transaction logic so this is actually not really a cup it's not just like a regular comment for good practice it's actually like it needs to be there for this file to essentially work so what was actually happening is because I didn't have this file this information up here what's happening is that it wasn't understanding that the script file was meant to be the logic for that transaction so when I was clicking the transaction it was just saying like there's no there's no there's no logic for me to implement here though there's I don't know what to do essentially and that was like really interesting because I didn't know that myself so I just had to take a little second go find out what happened but that's essentially what it is so definitely make sure that you don't delete the spot this this information because this is what you need for the network to understand that you're defining the transaction logic cool so now that I fixed that issue and again like this is the information here you you basically define what it does and then you give the parameters and then you say that it's a type transaction cool and for the syntax you guys can look at the hyper ledger compose composer documentation to get the exact syntax to so let's submit a transaction so now we're type trade and then we're going to add our commodity type ABC trader to and then we're going to submit so you can see our transactions successfully submit it and we can go back to our commodity and we can see up here I'm let me zoom in so it might be clearer for you guys to see that the owner of our commodity is now trade or to even though it was trader one before and we can actually go here and just kind of see a history of all the transactions that we've done or we can kind of view the record of it so each record is essentially like a data object with like a timestamp a transaction ID which is essentially a hash and then it says it's a type trade right so it's essentially the record is a type trade plus the idea of a transaction and then the timestamp to kind of place it in history essentially cool so this is essentially that's essentially it for everything that we can do with hyper ledger composer and again like I said it's built on top of the hyper ledger fabric network and it's a really great place for you guys if you just want to get your feet a little bit wet about understanding how to work with a kind of private blockchain that work what what our assets what our participants what our transactions how do these things interact with each other how to set up access control in sort of looking a safe environment in the browser you don't have to install anything you definitely don't have to do any system administer it and it just gives you a little simple thing where you can play around test it and yeah I found it to be a really useful tool undef initely much much better than working with I fertilize your fabric on its own hopefully you guys enjoy it have a great day bye you

8 thoughts on “Exploring Hyperledger Composer

  1. This is an awesome šŸ‘šŸ½ video ! Iā€™m so glad Iā€™m enrolled in the hyperledger accelerator! Thank you!

  2. great video! do you know if there are any modules to run python functions or attach csv/SQL databases to Hyperledger

  3. Thank you – one of the best videos I've found as an intro to Hyperledger. I liked the references to Ethereum too, felt like that helped me understand concepts far quicker than, say, reading the documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *