Hyperledger Fabric Chain Code development (Chaincode for developers)

Source: Internet
Author: User
Tags reset stub hyperledger fabric

What are chaincode (what are chain codes)?

A chain code (CHAINCODE) is a program written in go language that implements a defined interface. In fact, it also supports other languages, such as Java. The chain code (CHAINCODE) runs in a secure Docker container that is isolated from other programs. The chain code (CHAINCODE) initializes and manages the ledger status (the ledger state) through the transactions submitted by the application.

A chain code (CHAINCODE) typically handles the business logic that is agreed by the network members, so it is similar to the "Smart contract" (Smart contract). The classification state created by the chain Code (CHAINCODE) is limited to that chain code and cannot be accessed directly by other chain codes (Chaincode). Given the appropriate permissions, a chain code (CHAINCODE) can invoke another link code (CHAINCODE) to access its status on the same network.

In the following chapters, we will explore the chain code (CHAINCODE) in the eyes of the developer of the program. We will simply introduce a chain code (CHAINCODE) Application example and describe the purpose of each method of the chain code (CHAINCODE) Shim API.
Chaincode API (link code API)

Each chain code (CHAINCODE) program must implement a chain code (Chaincode) interface whose method is called to respond to the received transaction. The Init method performs the necessary initialization when the chain code (CHAINCODE) receives a instantiate or upgrade transaction, including initializing the state of the application. Call the Invoke method to handle the transaction that handles the application submission.

Another interface in the Shim API of the chain Code (CHAINCODE) is Chaincodestubinterface, which is used to access and modify the classification ledger and to make calls between the chain codes.

In this tutorial, we will demonstrate how to use these APIs by implementing a simple "asset" managed chain code.

Simple Asset Chaincode (Easy asset chain code)

Our program is a simple creation of assets (key-value pairs) in the ledger.

Choosing a location for the code (where to select the codes)

If you haven't used the go language to encode, you may need to determine if you have the Go programming language installed and your system is configured correctly.

Now you need to create a subdirectory under your $gopath/src/to store your link code program.

To make things simple, we provide a simple instruction:

Mkdir-p $GOPATH/src/sacc && cd $GOPATH/SRC/SACC

We create a source file:

Touch Sacc.go

Housekeeping (coded)

First let's prepare the code. Like every chain code, it implements the Init and invoke functions of the chain code. So let's use the GO Import statement to import the necessary dependency library for the chain code, we will import the shim package of the chain code and the peer Protobuf package. Next let's add a struct simpleasset as the receiver of the chain Code shim function.

Package main

Import (
    "FMT"

    "Github.com/hyperledger/fabric/core/chaincode/shim" "
    github.com/ Hyperledger/fabric/protos/peer "
)

//Simpleasset implements a simple chaincode to manage an asset
type Simpleasset struct {
}


Initializing the Chaincode (initialize chain code)

Next we implement the INIT function.

Init is called during Chaincode instantiation to initialize any data.
Func (t *simpleasset) Init (stub shim). Chaincodestubinterface) Peer. Response {

}

Please note that. The init function is also called when the chain code is upgraded, and when you write a chain code that will upgrade an existing chain code, modify the INIT function appropriately. In particular, if there is no migration or nothing is initialized as part of the upgrade, provide an empty Init method.

Next we will use the Chaincodestubinterface.getstringargs function to retrieve the parameters of the INIT call and check its validity, and in our case we should accept a key-value pair.

Init is called during Chaincode instantiation to initialize any
//data.  Note that Chaincode upgrade also calls this function to reset
//or to migrate data, so is careful to avoid a scenario where you
//inadvertently clobber your ledger ' s data!
Func (t *simpleasset) Init (stub shim). Chaincodestubinterface) Peer. Response {
  //Get The args from the transaction proposal
  args: = Stub. Getstringargs ()
  If Len (args)! = 2 {
    return shim. Error ("Incorrect arguments. Expecting a key and a value ")
  }
}

Next we determine that the call is valid and we will store the initialization state into the ledger. To do this we will call Chaincodestubinterface.putstate and pass the key and value as arguments. Assuming everything goes well, return a successful peer with initialization. The response object.

Init is called during Chaincode instantiation to initialize any//data. Note that Chaincode upgrade also calls this function to reset//or to migrate data, so is careful to avoid a scenario whe
Re you//inadvertently clobber your ledger ' s data! Func (t *simpleasset) Init (stub shim). Chaincodestubinterface) Peer. Response {//Get the args from the transaction proposal args: = Stub. Getstringargs () If Len (args)! = 2 {return shim. Error ("Incorrect arguments. Expecting a key and a value ")}//Set up any variables or assets this by calling stub. Putstate ()//We store the key and the value on the ledger err: = Stub. Putstate (Args[0], []byte (Args[1])) if err! = Nil {return shim. Error (FMT. Sprintf ("Failed to create asset:%s", Args[0])} return shim. Success (Nil)}


invoking the Chaincode (call chain code)

First we add the signature of the Invoke function.

Invoke is called per transaction on the Chaincode. Each transaction are
//either a ' get ' or a ' set ' on the asset created by Init function. The ' Set '
//method may create a new asset by specifying a new Key-value pair.
Func (t *simpleasset) Invoke (stub shim. Chaincodestubinterface) Peer. Response {

}


As with the Init function above, we need to extract the parameters from the Chaincodestubinterface. The arguments to the Invoke function will be the name of the function of the Chaincode application to invoke. In our case, our application has only two parameters: Set and get, which allow you to set the value of an asset or retrieve the current state. We first call Chaincodestubinterface.getfunctionandparameters to extract the function name and parameters of the chain Code application function.

//Invoke is called per transaction on the Chaincode. Each transaction are//either a ' get ' or a ' set ' on the asset created by Init function.
The Set/method may create a new asset by specifying a new Key-value pair. Func (

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.