 by Hyperledger Fabric Developer. Thank you again to him for volunteering to present today on the components of smart contracts within Hyperledger Fabric. Invite you to ask any questions here in the chat on the Zoom or if you're tuning in from the YouTube link, feel free to drop any questions in the chat and I will get them answered by Naveel whenever there's a break in the presentation. But Naveel, the floor is yours. Thanks again for presenting and again, feel free to reach out with any questions everyone. Thank you, Chris. Thank you for the introduction. So, hello everyone. My name is Naveel Rodriguez. I'm certified Hyperledger Fabric Developer from Linux Foundation and I've experienced in developing full stack applications as well as decentralized applications using Hyperledger Fabric and Ethereum. At present, I'm working at McKinley and Rice as a blockchain developer. My roles and responsibilities includes designing, developing and deploying solutions by gathering functional requirements from the client. So, this will be the agenda for our today's summit. We will be briefly discussing the overview of Hyperledger Fabric. Then we will be comparing it with the Ethereum which is public blockchain. We will be deep diving into the fabric smart contracts, the glass structure, and as well as the stub interface after which we will see different APIs that have been used for interacting with the ledger data. And after a while, we will go with the sample fabric contract that I have created for our today's meetup. So, we will be going through it and have discussions. So, moving on, Hyperledger Fabric. Okay, so in 2015, Linux Foundation founded the Hyperledger project as open source collaborative effort created to advance cross industry blockchain technologies. So, Hyperledger Fabric was first project that was contributed to Hyperledger umbrella and currently it is governed by a diverse set of maintenance from multiple organizations. So, what makes Hyperledger Fabric different is that it is private and permission, meaning that only authorized entities are allowed to access the network. Unlike permissions less blockchain, wherein anyone can participate and contribute to the network. So, the transactions must be written in the ledger in order in which they are cut and bad transaction should be rejected. So, this process is called consensus. So, in Hyperledger Fabric, we follow a three-way process. The first one is being endorsing. So, whenever transaction is being sent over the network, it is endorsed by the endorsing peers. After which, once they are endorsed, those, the endorsed transaction is again sent to the order node, wherein those are ordered as they come and after which, again, those transactions are validated. So, as we know, Hyperledger Fabric is permission. Identities are first enrolled through certificate authority. So, when we enroll using certificate authority, we get two keys, which are namely public and private key. So, private key is basically the one which is kept to be hidden and the public key is spread throughout the network or it is shared among the parties in the network. So, using private key and actor in the network, digitally science-based transactions and sends over the network. The MSP, on the other hand, is easily able to validate if the transaction is coming from the valid actor or node. So, this is the process usually goes through. So, the next is smart contracts in Hyperledger Fabric supports general purpose programming languages. So, for example, Java, Node.js, Go, these are general purpose programming language, which is used to design for building software that can be used among different application domains, rather than domain-specific language. For example, HTML, SQL. So, these are domain restricted languages. So, coming towards channels. So, these are sub-networks in the blockchain network. For example, some transaction parties in the network might be competitors and want their transactions to be hidden from the rest of the network. So, in such cases to maintain the privacy between the actors participating in the network can use a channel into the network. So, basically it is a network in the network. So, coming to a differentiation factor. So, the most common difference being is Ethereum is permissionless. So, as it is public and anyone can participate, any unknown identity can participate and contribute to the network to keep it up and running. So, they do these because they earn native coins after mining block on that network. Whereas, there is no such concept in permission blockchain. So, the identities that participate in the network are authorized, are already authorized by membership service provider and they are the only ones which are allowed to the network. So, next comes is the governance. So, in Ethereum, Ethereum developers are responsible for managing and implementing changes to the protocol. For example, as you might have heard, it is migrating to the proof of stake. So, it was decided by the Ethereum developers and now it is in the migration phase. So, but whereas in hyperlager fabric, it is governed by Linux foundation. So, coming to the decentralization part. Ethereum is completely decentralized as anyone can participate in the network. Whereas, in hyperlager fabric, a consortium or a group which forms a network is the one who controls the network. So, coming to the consensus mechanism. Currently, Ethereum uses proof of work. So, proof of work is basically when a miner into the network has to contribute, has to solve a puzzle in order to record the transactions onto the network. So, the miners basically compete with each other in order to record the transactions. And once the transaction is recorded, the other miners can easily validate that particular transaction. In hyperlager fabric, as it is a permission network, we can use a pluggable consensus mechanism like a Byzantine for tolerance, practical Byzantine for tolerance to in order to increase the transactions rate. So, coming to the throughput, as discussed, since proof of work requires a lot of computation power, the throughput achieved in this type of consensus algorithm is very less. So, it is around 30 transactions per TPS. And in hyperlager fabric, we can stretch these transactions per second to up to 20,000. So, in Ethereum smart contract language, which is commonly and mainly used in SolidD, whereas hyperlager fabric provides options like Node.js, Golang and Java. So, coming to the main part, fabrics contract overview. So, basically, before businesses can transact with each other, they must define a common set of contracts covering common terms, data rules, concept definition and processes. A smart contract defines the rules between different organization in executable board. They are used to implement the governance rules for the business object. So that they can be automatically enforced when the smart contract is executed. For example, smart contract might ensure that a car delivery is made in specified timeframe or funds which are to be released are arranged or are released in a specified terms, in a specific interval time. When smart contract is approved and verified by the transaction parties in the network, it is packaged and deployed onto the network. So, we can see this container, packaged container into the network and this packaged container is often referred as chain group. So, coming to the contract class, so this is how the contract class is in the fabric contract API. So, the first is constructor. So, the constructor in the contract class accepts the name as an optional input parameter. So, this is basically used to name your smart contract. The name is not provided, then the class name is used. Then we have isContractMethod. So, this function is used to identify if the object passed as an input is of type contract. If it is true, it returns successful check. So, then comes the before transaction. So, as you can see, there is a CTX parameter. So, this is basically a context object. So, whenever there is a transaction function, we need to pass this context object. It is the first argument, which contains the implement of chain code stub interface. We will be looking into this interface into the next page. So, this chain code stub interface has encapsulates API to interact with the world state, private collections, and the other transaction purposes. This CTX, which is context objects, also provides client identity of type client identity, which provides the details about the transaction creator like identity of the client and MSPAD. So, this before transaction is a function, which is invoked before any transaction functions in the smart contract and invoked. We can override this default function and can implement our own logic. Then comes the after transaction. The function is invoked when a particular transaction function is invoked. So, this comes at the later part. So, again, we can override this default method and use our own logic. Then is admin transaction. So, this is basically a function which is invoked when a requested transaction by transaction created does not match any of the function present in the smart contract. So, again, we can override this default method and use our own logic. Then comes the create context method. This function is used to create own custom transaction object, which can be accessed by additional processing, sorry, when transaction functions are invoked. Then comes the get name. This is a simple get function, which is used to return the contract. So, this is how contract is structured in chain code. So, we will be diving deep into it when we will be referring to sample smart contract. First up, this is the leap where a chain code resides. Then there is a test folder where we specify, where we write test cases in order to test a smart contract. We have index.js file, wherein this is basically an entry point which exposes the smart contract which is present in the leap directory. And then we have package.js, where we specify the dependencies. So, coming towards the chain code stub interface. So, it has two important methods, namely, init and invoke. So, these two methods are called in response to receive transaction. init method that will service initialization function during instantiation or during chain code upgrade process. If we specify this flag fabric ensures that init is invoked before any other transaction function. It allows the user to initialize the chain code and the ability to add initial data to the legend. So, then comes the invoke method. This is called by the clients to submit transaction proposal that allows to use the chain code to read and write data on the channel. So, coming to the distributed ledger data representation. So, in here, we represent data in two states. So, first is world state and then is blockchain. So, the world state in hyperledger fabric can be denoted with two database. One is level DB and the other is couch DB. So, level DB is the database that comes with default with hyperledger fabric. It is actually very fast and then comes the couch DB. So, it comes into play when you want to perform rich queries like, for example, greater than less than, et cetera. So, the world state basically holds current and latest values. It is used to fetch or the smart contract fetches data from the world state because it is rather quickly than traversing the blockchain and fetching the files from there. So, states recorded in the ledger are represented as key value pairs. So, in blockchain, the any record, so the world state, any modifications that are being made to the world state are stored as a transaction slot, as a blockchain. So, the transaction forms are collected inside the block and appended to the blockchain. The blockchain data structure is immutable and cannot be, and once altered, can never be modified. So, these are some of the common APIs which chain code, stub, interface provides. The first being port state. So, input state, we specify key and value. So, this is basically an insertion operation into the blockchain. So, this is when we specify, we give the key value pass that we want to update to the database. Then is the get state. It accepts key as an input parameter which is used to fetch the corresponding value to the key. Then there is a delete state function. So, this method is used to delete a data from the world state. So, whenever we use this delete state, the world state is updated, but the data is not deleted from the blockchain. So, yeah. So, logging in chain code. So, as discussed previously, so we need to package the chain code and then we need to deploy the chain code on the running container. So, once it is deployed, we get that particular chain code as a container, docker container. So, by running this command, docker logs and specifying the container ID, we will be able to see the logs that are being generated when that particular chain code is involved. So, this basically helps developer ease the development process for identifying the issues and resolving them. So, let's move on to the sample chain code that I have prepared. So, this is a live directory where sample contact is written. So, I will quickly start sharing my... As you can see, I've grown this to my visual editor. So, here is a simple contract, which extends the contract class from the Fabric Contact API. And we have defined three functions. Put function, which is used to update the laser. Then there is a get function, which is used to get the value for a corresponding key. And then there is a delete function, which is used to remove that particular key from the stack. So, as you can see, after defining these three functions, put get delete, we export this class. This class is later being imported into this index.js file. And you can see we have exported this simple contract as the simple contract file. And this model.export.contracts, we are basically specifying this simple contract as an array, array parameter, okay? Then we have this package.json file. So, this is the start command, which is ran by PS into the network in order to run the smart contract. And this is a test script, which we will be looking into it in few minutes. So, this is basically, yeah. So, then we have engines. So, node version should be greater than 12 and then the module should be greater than five. Then we have dependencies. So, this is Fabric Contact ABA, which I've discussed earlier, which contains the contract class. And then there is a Fabric Sheen ABA, which also is a dependency, which is needed when creating a contract. Then these are dependencies. So, we don't have to include them during a production build. So, these are basically used dependency when we want to test a chain code. We want to make sure that a chain code is properly tested and it is error free. So, coming to the test cases. So, this is initialization stuff. And we have written test cases for three functions. So, first being the boot function. So, in here we are taking the simple contract context, simple contract object. Then we are performing a boot operation by passing the transaction context. And then we pass the asset key and asset value. So, asset.key and asset.value is 100 and black. So, we are passing basically a 100 as a key and value is black. So, after this, we perform a get state operation in order to get value for the past corresponding key. And this should assert that the past value and the written value is true. Okay. Then we have a get function. In here, we first run the negative test case. As discussed earlier, it will first create one contract object. Then we will insert, we will perform insert operation by passing the transaction context, by passing the asset key and asset value. After which, we will try to get that particular asset using the wrong key, which is 101. As we have stored the asset with key 100 and value is black. So, these data won't be fetched and that's why we will be getting an error. So, this will be the error we will be getting, asset.zero does not exist. And then we will check for the positive test cases. So, in here, we declare as discussed, it will create one simple contract object, which is a contract object. Then we will perform the update operation and we will check if the written value is equal to the black, which is our value and it should pass. Similarly, this is a delete function. First we check for the delete operation with the... First we delete the wrong value and then we will check as a positive case. Like if the deleted value is not found in the world state. So, we will run these test cases. So, there is a script in package.json. So, after running npm test, it will run this script. And as you can see, all the test cases have been passed. So, we can be assured that the functions that we have wrote are working as expected. This was very basic example. And Nabil, we do have a quick question here. So, how can npm security package updates and fabric dependencies be managed? So, this actually has to be checked regularly, like what are the dependencies that are being currently used. And as you might be knowing that if there is any new version that is being released, they mention like what is the issue and what issues are being resolved. So, yeah. So, keeping regular check on the packages that has been added as dependencies and checking regularly what are the vulnerabilities that can be affected and mitigating those. Great, thank you. And then, what kind of digital certificate is used in Hyperledger? So, it is x509 format. So, this is the type of digital certificate that is being used, x.509 standard. Can you also explain examples of world state with examples and how the DAW state is useful? Sorry, may... Sure. Can you also explain examples of the world state and then how the DAW state is useful? Sure, sure. So, the world state basically, why are we using this? We want operations to be very quick, right? So, that's why any transactions that are being done are first represented on the world state, right? So, but if, in case of failure, the world state is deleted, we are using the blockchain that we have that is a laser that is distributed across the parties. We can reform the world state using that blockchain log file. So, yeah. Does that answer the question? I think so. Then, Jerry, feel free to reply there. A follow-up on what if the fabric does not support the new NPM package in regards to the original question around the package updates? I didn't quite understand the question. What happens if fabric does not support the new NPM package? Then, we need to use the NPM package that is supported like the LDS version. And as fabric is continuously evolving, like the latest version being 2.4, it is better to stick to the LDS that is being offered in order to be away from any vulnerabilities that are being introduced in the previous versions. Great, thank you. So, coming to the certifications that are provided by Linux Foundation. So, this depends on what aspects you want to be like, if you want to gradually master, if you want to be on the administrative side or if you want to develop a set. So, these are the two variations that are being provided. So, Fabric Administrative, Certified Appliance of Fabric Administrative is more focused on the DevOps perspective, like deploying the network and forming the consortium and all the network-related properties. And then there is CHFB, which is Certified Appliance of Fabric Developer. This is more focused on the developer perspective, like developing error-free chain codes and knowing the fundamentals. And also it includes how client interaction is being done using NPM packages that are being provided by the Fabric Appliance of Fabric. So, these are the two certifications and based on your likings, you can choose any of them or both of them if you want to master the blockchain Appliance of Fabric. So, regarding training and consulting, if you have any service-related queries, you can DM me or Matthew Zandt on LinkedIn. He is a leading author of Hands-On Smart Contracts Development in High-Pledge of Fabric. And it is acknowledged by a brand-name professor. So, yeah, that was it. So, this was the resources that went through in creating this meta. And yeah, thank you, everyone. And if there are any questions, I'm happy to answer them. Awesome. Thanks, Naveel. We have another one saying, you know, in Ethereum we have ERC standards. Is there something similar that we have for smart contracts within Hyperledger? We do have ERC support. So, you can refer to Fabric samples and that is very good example for ERC-20 representation in High-Pledge of Fabric. Yeah. But it will be like private, like it should be between the organization. So, it will be restricted to the consortium that has been formed and will not be public. Got it. This is probably more for like the overarching Hyperledger group, but is an admin certification still good to obtain if you're not necessarily a developer? I beg your pardon. So, within Hyperledger you have the certified Hyperledger of Fabric administrator and then the certified Hyperledger of Fabric developer that you were going into. So, if you're not a developer, is it still good to acquire an admin certification? Yes, it is based on their liking. So, developer perspective will be more of like programming and having a background in programming, but administrative will also have some aspects of programming, but it will be mostly related in maintaining the network, making sure that the network is up and running and seeing if all the containers and entities involved in the network are properly run. Yes, that's what based on making sure that the network is up and running. Great. So, Hyperledger Fabric certification is provided by the Linux Foundation and the Blockchain Council. Is one better than the other or they both equal? So, I would... This will be my personal opinion, but I would prefer going with the Linux Foundation work as those are really like challenging and it really tests you, like if you are really qualifiable for that and if you are ready with it. So, it will... You need to prepare it. You need to read the docs. Hyperledger docs, you need to perform all the fabric samples that are present in the GitHub representation for Hyperledger and you need to practice a lot in order to pass this examination. So, my preference will be going with the Linux Foundation ones. Got it. Can the sample chain code be run using mini-fabric rather than the full Hyperledger Fabric Docker setup? Yes, I think mini-fabric is a way to quickly make the network up without going into much more network data. Personally, though, I haven't had an opportunity to try mini-fabric. Any examples of before-transfer and after-transfer use cases of chain code? Okay. So, for example, as discussed, the digital certificate. So, what we can do at the client side is we can have some attributes. For example, attributes like client type equal to user and then there can be attributes like type equal to admin. Okay. So, using before-transaction, we can check this attribute. So, I had told whenever a transaction function is invoked on the smart contract level, it has the context object. Okay. From the context object, we get this client identity parameter using which we can face the attributes that are present in the certificate that is generated for the transaction data. And we can write a logic over there that who needs, who can be, who can access and who cannot. So, this can be one example. And after-transaction can be like, for example, like giving a console message like you have successfully completed executing this transaction function with the following parameters and we can maybe console those parameters that have been obtained after performing the transaction function. Great. Any tips or advice as you prepared for the High Browser Fabric for Developers training course and certification? Okay. There is one very good high pleasure fabric like contributor. You can search it. Okay, I will insist. Type it here. So, his name is Roland Bohl. You can watch his sessions and his GitHub repository. That is one thing. The other being high pleasure fabric docs. You can go through them. You can try to understand them. Then you can go through fabric samples. That is a very good source. And also, if you need specialized training, you can approach Matthew's end. He has very good resources and has very good industrial experience. Got it. As we were comparing Ethereum with High Browser Fabric, one is public, the other is private. So how can we determine the number of transactions per second regarding the number of nodes in a public and private blockchain not being the same? So, I think the overarching question is how do we deem it possible to compare a public blockchain with a private one? Okay. So, the transactions that are being executed, it all depends on the consensus mechanism that is being used. So, in Ethereum, currently it is proof of work. So, in proof of work, it is very like you can say energy consuming and very like compute intensive process. Okay. So, it takes time. Miners has to solve that puzzle and only the one who is able to solve the puzzle is able to record the transaction. So, at a particular instance of time, it can only have the limit. Like it cannot compute more than that. Similar thing happens with the Bitcoin as well. But in hyperlager fabric, as there is not such mechanism like miners are not competing with each other. So, it is governed with a set of organization itself. So, we can remove this like concept of solving the puzzle and that's when there are variations like practical Byzantine port dollars, crash port dollars which are the types that are used to execute the transactions very fast. For example, it can be stretched to 20,000 transactions per second. So, this is why a different mechanism consistence mechanism is used based on the network that is all. Awesome. Thank you. Any other questions right now? All right. Great. Thanks again for this great presentation and thanks again to everyone who attended and asked such thoughtful questions. Hello. I have one question. Go ahead. Yeah, it's regarding the hyperlager fabric developer certification. So, suppose if I'm going to give that examination, so does they are going to provide the network setup or did I have to do the network setup and write the whole chain code or they are going to provide the complete environment. I just have to write the chain code. So, that is my question. Yeah, so I will answer this question. So, you don't have to set up any network you don't have to make that network up. So, you will be giving given a programming set of questions and you have to solve them. Like, for example adding a function into the chain adding a function into the existing chain code or modifying the chain code function it is. Yeah, yes. So, basically, we just have to do the changes related to chain code, right? The network is already up. So, for detail you can go through the syllabus of that. So, it's not only about making changes to the chain code but it also includes things like executing the application chain code life cycle upgrading the chain code. Yeah, so, you can go through the syllabus you will get to know more. Yeah, but while giving the examination did we have to do the we just have to write the chain code, right? We should not be worried about the network stuff, right? Yeah, thank you. Okay, seems like we're all good here but again thanks to everyone who attended Apologies for the issues with the link to the zoom not getting posted properly in the meetup group but hopefully we were able to resolve that. Any questions, feel free to reach out to Nabil and again greatly appreciate everyone's participation and especially Nabil for presenting such an insightful meetup to that. Thank you. Thank you for attending it. Thanks everyone.