Hands On: Chaincode Interface

Rajeev Sakhuja
A free video tutorial from Rajeev Sakhuja
IT Consultant, Mentor, Innovation evangelist
4.4 instructor rating • 6 courses • 54,873 students

Lecture description

Students will learn about the required functions in the chaincode. After that students will test the chaincode using the dev environment and the utility scripts.

Learn more from the full course

Mastering Hyperledger Chaincode Development using GoLang

Design and develop Fabric 2.1 applications from end-to-end using GoLang & Fabric Node SDK

10:04:53 of on-demand video • Updated April 2020

  • Chaincode development using GoLang
  • Application Development using Fabric Node SDK
  • Use of Private Data
  • Use of Instantiation & Endorsement policies
  • Building support for Rich Queries & Asset History
  • Building Programmatic access control with "Client Identity Library"
  • Testing tools for Chaincode such as Golang Testing framework & MockStub
English [Auto] Chen called Interface functions in this walk through an exercise. I will be showing you the code in the minimal implementation of a chain code. You will go through the code and in it invoke and the main function. And then you will execute commands against this minimalistic implementation of the chain code. In this lecture I'll be referring to the Virgin One of the token chain code. The testing instructions that I'll follow in this lecture are available in the V1 read me dot MDT file. In the last lecture I talked about the fact that all chain code instances must register with the fabric runtime. This registration is carried out in the main method by way of the invocation of the shim dot stock function. The Shim dark stock function registers the instance of a chain code with the fabric runtime and it takes an instance of the type structure that represents the chain code instance chain code interface functions in it and invoke have the same function signature. Both of these functions apply to the instance of the structure representing the chain code. They both take the argument of type shamed or chain code stop interface and responds back to the caller with an instance of period or response. I will be covering the shim Daw Chen court stop interface NPR door response in later lectures open the version one of token dark go file and in this file you will find that a tree import statements the first one is the import of the format package. This is for printing or messages to the console. Second one is the shim package and third one is the pivot package. I will discussing the pivot package in a later lecture type token Chan code represents the chain code object. In this setup to which the Chen code interface will apply it is the init method which takes as input an object of type Chen code style interface and response back with a period or response within the implementation of the in it method. We are simply printing out a message to the console in it executed and then returning success by executing Shim success which indicates to the fabric runtime that the execution of the chain code was successful. This is the in work method that takes as input an object of type Shim da chain code stub interface and responds backward pure dot response type object in the implementation. We're simply printing a message with the console invoke executed and then returning success as a response. This is the main function that gets executed when the chin code is launched. The first thing it does is print started Chen code and then in walks the chamber start function that registers the chain code with the fabric runtime. If there is an error the error message is printed out to the console and the execution of the Chen code stops. So version one of the Chen code has all the required functions implemented and it is ready to be launched and tested and to test this Chen code. We're going to use the instructions in the Read Me dot MDT file. I suggest that at this point you pause the video and try out the instructions in the read me Dot. Emily on your own. Opened read me not Emily file. First thing you need to do is launch the dev environment with the dash e options so that we have access to the explorer. This will take a minute or so. Our environment has launched let's clear the window. Reset the next step. We need to set up the environment so we will use these said that Ian Ian as asset scope set the context to act to set up the chain code environment. Some will copy the line from here for setting up the chain code environment. Please notice we are setting the name of the chain code to token and the path is token slash. We won after this. We need to install the chain code. But first we have to package it. So I'm going to copy the gendered assets package from here and execute gender a search package after the package is created. We need to carry out the installation. So I'm going to copy gendered assets install and pasted here chained as such install is successful. So let me just reset the screen. The next step. We have to approve and commend the chain code to approve. We will use the command chain dog as such a pro. OK. And this will approve the chain code for the Acme organization to carry out the comment. We will use the command chain dog search comment and the comment was successful. Now let us go ahead and check out the transactions in the Explorer. Our orders follow link. You will see that there are five transactions. These first three transactions are for the channel creation and channel joints for Acme and budget. This transaction is for the approval. And this one is for the comic. Now we're on stuff for which requires us to read the logs from the chain code container. So C.C. dash logs dot asset dash t 10 dash F.. Now we are reading the log messages as they will come in from the chain code for the next step. We need to open up another terminal window and do that. Click on the split terminal. Log into vagrant vagrant SS edge. Said the environment so copy this and set up the environment as a next step. We need to initialize the chain code because if you don't initialize the chain code it would lead to an error. So six step is in it. And now observe communal number one and you can see this in it executed in V1. Wants to enter it is executed. You ready to execute the change out a search query and take chained out as such invokes so let's execute query and as you can see here invoke executed in V1. Similarly if I execute in Moke I will see the same message there. Now if you want you can check out the explorer as well and you will see additional transactions for the invoke and these are the two transactions for invoke and you will observe that these two transactions have the chain put cocaine in it because they were executed on the coke and chain code. Time to go over the key points. An empty structure is used as a representation of the chain code. Chain code instances register with the fabric runtime by executing the shim dot stock function within the main function both in it and invoke functions take an argument of pipe shimmered or chain code stub interface and the respawn back with an instance of type pure dot response.