Hyperledger Fabric(4)链码ChainCode

Intelligent contract, is an abstract concept, intelligent history can be traced back contract 1990s era. It is almost the same age with the Internet by the philosophy Saab Nick (Nick Szabo) raised.

We are talking about smart contract only narrowly refers to the block chain. It can be deployed and run in the environment, block chain, a piece of code to describe the relevant service logic . Intelligent contract after deployment in the block chain can not be modified , perform intelligent contract completely determined by the code, without the interference of human factors. In general, the participants through intelligent contract rules given their rights and obligations , triggering the contract conditions and results , once the smart contract block chain environment running can be drawn on objective , accurate results.

What is ChainCode

  ChainCode (chain code) is realized in the form of a smart contract Fabric block chain network. Into user chain code and system code chains , generally it refers to a user chain code. Chain code is to access the books of the basic method is generally used Go and other high-level language, the code that implements the provisions of the interface . The upper layer application can call the chain code to initialize and manage the books of state . With proper permissions , chain code between can also call each other .

  Chain code is deployed on the network node Fabric, run in isolated sandbox (currently Docker vessel), and by interacting with the corresponding protocol gRPC Peer nodes to operate in a distributed data books.

  After starting Fabric network, chain code may be operated by a command line or the SDK, verify that the network is functioning.

  It plays a role as shown below:

    

Application by sending transactions to the block chain network to call intelligent contracts to operate in the state books.

ChainCode operation mode

  Process transactions in Fabric, the client will first send a proposal to endorse node, check the legality of the proposal endorsement node. If the legal case, endorsement node through the chain transaction code belongs to temporarily perform a transaction and execute a copy of the status of the local node endorsement holdings.

  Chaincode should only be mounted on the endorsement chaincode owner node, the chain code running on the node's sandbox (Docker container), and interact with a respective Peer node gRPC protocol, so that the logic of the whole network chaincode other members confidential.

  Make sure each on a channel you want to run the installation on your chaincode endorsement of nodes you chaincode , no chaincode other members will not be entitled to become a certified node transaction under the influence of chaincode (endorser). In other words, they can not perform chaincode . However, they can still verify the transaction and submit to the books .

  To run ChainCode block chain network, you need to go through the chain-attaching and chain code instantiates two steps.

  Mounting a plurality of times, once instantiated
  
in a block chain link, the network is "channel 1 + N + 1 th books peer nodes" component. If we want to build Fabric network manually, then that is to ChainCode installation and instantiated by the form of the command line. We need to repeatedly install, once instance. That is, for the entire Fabric network, let's say there are X number of nodes endorsement, then we need to give each node endorsement installation ChainCode, but in the whole process of building the network only once instance ChainCode. Because the install for that endorsement node, instance for a channel.

  install the object code of the chain endorsed the node is the main purpose is to facilitate the endorsement node running chain code, the transaction simulation.

  instance objects chain code is Channel , the main purpose of the mounted chain code instantiated call on the specified channel, the container is created on the start node, and performs an initialization operation. Examples of the process, the need to specify endorsement strategies to determine which transactions can be executed node is added to the books in the channel.

  The installation process is actually the specified code compiler package and the packaged file sent to Peer , waiting for the next instantiation. Chain code below refers to the life cycle of this process.

  Examples of the chain code is installed mainly on the front of the chain code on the packaging machine Peer, and generating a corresponding image and the Channel Docker Docker containers. And we can specify endorsement policy when instantiated.

  • Chaincode run among a protected Docker containers, endorsement running nodes isolated from each other.
  • Chaincode trading applications submitted by the state to initialize and manage the books.

ChainCode life cycle

Hyperledger Fabric API provides four command chaincode lifecycle management:

  • package
  • install
  • Instantiate
  • Upgrade

In future versions, the command is considering adding stop and start trading, so you can easily stop and restart chaincode, rather than have to uninstall it really the job. In a block chain network, packaged, installation, and examples of the upgrade process chaincode operable as a function call chaincode life cycle.

Here, we briefly discuss the package, signpackage, install, instantiate, upgrade, stop & start these life cycle. Specifically as shown below:

    

1) Packaging | Package

In the block chain, the chain code to install Peer node, and we install the normal procedure is not the same. Fabric strict authentication and security mechanisms installed chain code.

Mounted chain code, who is eligible to instantiate? What should I use to call the endorsement strategy ChainCode it?

All this requires good regulations prior to installation. We call these additional source information + ChainCode collectively referred to as ChainCode package.

Generally, ChainCode package consists of the following three components:

  • chaincode itself, which is defined by ChaincodeDeploymentSpec (Chaincode deployment specification). ChaincodeDeploymentSpec, hereinafter referred to as CDS. CDS are defined chaincode according to the code and a number of other attributes (name, version, etc.).
  • An optional example of strategy, which may be described endorsement policy.
  • A group indicates that the signature chaincode ownership.

The process of packing, these three pieces of information is packed into a whole. In general, packing chaincode in two ways:

  • The first is when you want to make chaincode have multiple owners, then you need to make chaincode package is more than one owner signature. We need to create a package to be signed chaincode (SignedCDS) In this case, the signature of each package in turn is the owner.
  • Another relatively simple, let chaincode have an owner's time, this time as long as the signature of a person can be.

To create a signature had chaincode package, please use the following instructions:

peer chaincode package -n mycc -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 -v 0 -s -S -i "AND('OrgA.admin')" ccpack.out
  • -s: option to create a signature can be multiple owners of the package, rather than simply create a CDS. If you use -s, then when the other owners to sign when, -S must be used simultaneously. Otherwise, the process will create a signature chaincode package that contains only instantiation strategy (SignedCDS).
  • -S option can be localMspid property values ​​are defined in the relevant documents core.yaml good MSP sign the package. This option is optional. However, if we have created a package is not signed, then it can not be any other owners signed with signpackage instruction.
  • -i option is also optional, and it allows us to specify instantiation strategy is chaincode. Examples of the same format strategy and policy endorsement, which indicates who can be instantiated chaincode.

2) signpackage | signed and packaged

When you create a signature chaincode package was handed over to other owners can check the signature. Specific workflow support channel outside sign chaincode package.

ChaincodeDeploymentSpec can choose to be signed for all owners and create a SignedChaincodeDeploymentSpec (SignedCDS), SignedCDS consists of three parts:

  • CDS contains chaincode source code, name and version
  • Chaincode example of a strategy, which is expressed as an endorsement strategy
  • List chaincode owner, as defined by Endorsement

A chaincode owner can create a good band before the signature of the package is signed, use the following specific instructions:

peer chaincode signpackage ccpack.out signedccpack.out

And signedccpack.out ccpack.out instruction are input and output packets.

signedccpack.out contains a signature with an additional local MSP of the package.

3) install | installation

The install process will chaincode source code in a called ChaincodeDeploymentSpec (CDS) in the prescribed form package, and install it in a peer node that will run on the chaincode of.

In Fabric network, we need a channel in every run you want to install on your chaincode endorsement of node chaincode.

Chaincode should only be installed on the endorsement node chaincode owner, so that the chaincode logic confidentiality to other members of the entire network. Chaincode no other members will not be entitled to become a certified node trading under the chaincode influence (endorser). For authentication node, they can not perform chaincode. However, they can still verify the transaction and submit to the books.

When installing chaincode below, using the CLI stored in a mounted sacc chaincode directory, the following command

peer chaincode install -n asset_mgmt -v 1.0 -p sacc

-n: name of the chain of command code

-v: version specifies the version chaincode

-p: path indicates the path chainCode

It creates SignedChaincodeDeploymentSpec to sacc within the CLI, and send it to the local peer node. These nodes will call the Install method on the LSCC.

4) instantiate | instantiated chaincode

Examples of ChainCode process calls the system's life cycle chaincode (LSCC) creates and initializes some chaincode on a channel. Examples of the process will generate the corresponding Channel to Docker mirror and Docker container. And we can specify endorsement policy when instantiated.

Of course, the creator of an instance of the transaction must comply with the instantiation strategy chaincode in SignedCDS in, and must act as a channel of the writer (this will be part of creating a channel configuration), which is essential for the security of the channel.

Example CLI alias to use the mycc chaincode, instructions as follows:

peer chaincode instantiate -n mycc -v 1.0 -c '{"Args":["john","0"]}' -P "OR ('Org1.member','Org2.member')"

Examples of the chain code name of -n

DETAILED execution parameter -c --ctor, chain code, the format is json

-P --Policy specified instance strategy

5) upgrade | upgrade chaincode

Some chaincode by changing its version (part of SignedCDS) be updated at any time. As for the other part SignedCDS, such as the owner of the strategy and examples, it is optional. However, chaincode name must be consistent, otherwise it will be completely different as another section of chaincode.

Before the upgrade, the new version chaincode must be installed on the needs of its endorsement node. Upgrading is a similar example of trading transactions, it will be a new version of chaincode and channel binding. Other bundled with older versions of the channel is still running an older version of chaincode. In other words, the transaction will only upgrade a primary influence submitted its channel.

6) stop & star | stop and start

Note that the stop and start the life cycle of trading functions not yet implemented. However, you can stop by removing chaincode chaincode container and delete SignedCDS endorsement package from each node. Specifically, it is to remove all hosts or virtual machines running on a peer node in chaincode container which subsequently deleted SignedCDS endorsement from each node.

chainCode status summary

In the above process, ChainCode state are the following. Here to do a small summary:

CDS-ChainCodeSpec chain contains source code, name, version, etc.

SignedCDS-SignedChaincodeDeploymentSpec, contains CDS, a chaincode instantiation strategy, which is expressed as an endorsement strategy, a listing chaincode owner, as defined by Endorsement.

ChainCode species

In Fabric chain code may be divided into two types, system chaincode the user chaincode .

1) System ChainCode

The system is responsible for its own chain code processing logic Fabric nodes, including system configuration, endorsement, calibration and so on. Initially these processes by hard-coded (Hard-Coded) in a manner curing system. Fabric is achieved by the system in the form of chain code, the main process running in Peer, both the flexibility of the logic implementation and management, and communication performance.

For example: a system can only be upgraded by chaincode binary peer nodes. Meanwhile, the system can only be registered to chaincode compiled a set of specific parameters, and does not have the endorsement of policy-related functions.

The following table is a system chaincode:

Fabric five types of chain code system. Chain code system currently only supports the Go language, will be automatically registered and deployed in a Peer node starts to form in-process logic to interact with the main process.

2) User chaincode

User application developers chain code is very important, which provides the status of the distributed processing logic block chain based on the books, which can be developed based on a variety of complex applications.

In the super books Fabric project, users can use the Go language to develop chain code, the future will support a variety of high-level languages, including Java, JavaScript, including.

Chain code associated user code at a core / chaincode path. Wherein the core / chaincode / shim pack code mainly for calls using container side chain code, other code used primarily Peer side.

3) comparison of the two

Same point

Chaincode system user chaincode two identical programming models,

difference

Chaincode operating system and the user node in the peer chaincode run in an isolated container . Thus, the system does not follow chaincode construct described above chaincode life cycle within the node. Installation, instantiation, the upgrade is not available for these three operating systems chaincode.

Chain code workflow

We know that, in Fabric chain run sandboxed code (Docker container) in the node, when the basic workflow is called as shown in FIG.

  • First, the user by the client (SDK or CLI), the Fabric of endorsement node issue (endorser) call chain code of the transaction proposal (Proposal) .
  • Then, the node of the proposal includes a variety of tests, including checking ACL permissions, then create mock execution environment after the deal through.
  • Next, endorsement node and the chain code of the container between gRPC interact through message, the simulation execution and gives the transaction endorsement conclusions.
  • Finally, the client receive adequate support node endorsement after, you can send the deal to sort node (orderer) sort, and final written block chain.

Chain code container shim layer is a node to interact with the chain code of the intermediate layer . When the code reader requires a logic chain code books , the chain code will pass through the shim layer sends a corresponding operation type ChaincodeMessage to node returns a response message after the local node operation books. In the above process, there has been an important part, Shim layer. It should be emphasized once again that, shim chain code container layer is the intermediate layer of interacting nodes chain code.

 

Guess you like

Origin www.cnblogs.com/xdyixia/p/11726987.html