Fabric1.4: chain code management and testing

A chain code Introduction

Smart contract called chain code (chaincode) in the Hyperledger Fabric is to provide a distributed processing logic state of the books. Chain codes are deployed in a network node in the fabric, it can be run independently Docker containers protected with security features in order gRPC protocol to communicate with the respective peer nodes to operate in a distributed data books.

Chain code is generally divided into two types: Systems chain code and the user chain code .

1.1 System chain code

Responsible for their own processing logic Fabric nodes, including system configuration, endorsement, check, etc., will be automatically registered and deployed in a Peer node starts. System Chain code divided into the following five categories:

  • Chain code configuration system (the System Chaincode the Configuration, CSCC) : responsible for handling end Peer Channel configuration;
  • The life cycle of the system chain code (Lifecycle System Chaincode, LSCC) : responsible for the life cycle chain code of conduct user management;

  • Inquiry System chain code (Query System Chaincode, QSCC) : books provide query API. Such as block access to information and transactions;
  • Endorsement chain code management system (Endorsement System Chaincode, ESCC) : responsible for endorsement (signature) process, and to support the management strategy for endorsement;
  • Verification system chain code (Validation System Chaincode, VSCC) : transaction verification process, including checking endorsement strategy and multi-version concurrency control.


1.2 User chain code

Chain code is different from the user system chain code, system code is a fabric built chain chain code, the chain codes are user service processing logic of the code books are distributed based on the state by the application developers to write different scenes needs to run in the chain code container, by interacting with the interface state Fabric books provided.

User downwardly chain code may operate on data books, call interface may be provided up to the enterprise applications.


1.3 life cycle chain code

Life cycle management Chaincode total of five commands:

  • install: the chain code had been prepared and installed in a network node;

  • instantiate: to chain code installed instantiated;

  • upgrade: for upgrading existing chain code, the chain codes may be upgraded according to a change in the specific needs of the installation;

  • package: the specified operation packed chain code.

  • singnpackage: Signing of the packaged files.


2 using a chain code

We use fabric v1.4.3 version of the first-network network fabric-samples provided by explanation, modify first-network/scripts/script.shthe script in the following code:

# 将判断语句中的 true 改为 false,first-network 网络就不会进行链码的安装、实例化等操作
if [ "${NO_CHAINCODE}" != "false" ]; then
    ## Install chaincode on peer0.org1 and peer0.org2
    echo "Installing chaincode on peer0.org1..."
    installChaincode 0 1       
    echo "Install chaincode on peer0.org2..."
    installChaincode 0 2       
    #...
    #...
fi

Start first-network network:

$ ./byfn.sh up

Enter the CLI client container, CLI client default identity connected to Admin.org1 peer0.org1 node:

$ docker exec -it cli bash

Check the current node (peer0.org1.example.com) to which channels are added:

# peer channel list

The results returned:

Channels peers has joined: 
mychannel

Description of the current node has joined the channel mychannel.

Chain code installed 2.1

Installation install command chain code:

# peer chaincode install -n mycc -v 1.0 -p github.com/chaincode/chaincode_example02/go/
  • -n: Specifies the name of the chain code to be installed
  • : -v version Specifies the chain code
  • -p: Specifies the path to install chain code source

The results returned:

[chaincodeCmd] install -> INFO 04c Installed remotely response:<status:200 payload:"OK" > 

DESCRIPTION successfully mounted to the chain code peer node.

Note : chain code needs to be installed according to the specified policy endorsement in all peer nodes require endorsement in. Node chain code is not installed can not perform a logical chain code, but can still be submitted to verify the transaction and account book.


Examples of the chain code 2.2

Set the environment variable channel name:

# export CHANNEL_NAME=mychannel
# echo $CHANNEL_NAME

Set orderer node certification path environment variable:

# export ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
# echo $ORDERER_CA

Instantiate using the command chain code is instantiated:

# peer chaincode instantiate -o orderer.example.com:7050 --tls --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a", "100", "b","200"]}' -P "OR ('Org1MSP.peer','Org2MSP.peer')"
  • -o: Specifies the service node address Oderer
  • --tls: open TLS authentication
  • : --cafile the path specified TLS_CA certificate
  • -n: Specifies the name of the instance of the chain code, the same must be specified during installation of the chain code name
  • -v: Specifies the chain code to instantiate the version number, the same must be specified during installation of the chain code version number
  • -C: Channel Name
  • -c: Specifies the chain code parameter instantiation
  • -P: Specifies the endorsement policy

Strategy endorsement endorsement entity generally expressed as: MSP.ROLEwhere, the MSP is MSP ID, ROLE support client, peer, admin and member four roles. For example: Org1MSP.adminrepresents any administrator at Org1 the MSP; Org1MSP.membermeans any one member of Org1 under the MSP.

Endorsement strategy grammatical structure is as follows:

// 基础表达式形式,EXPR 可以是 AND、OR 和 OutOf 逻辑符,E 是实体或者嵌套的表达式
EXPR(E[, E...])
// 需要三个组织 org1、org2 和 org3 的 member 共同背书签名
AND('Org1MSP.member', 'Org2MSP.member', 'Org3MSP.member')
// 需要 org1 和 org2 其中一个组织的 member 背书签名
OR('Org1MSP.member', 'Org2MSP.member')
// 需要 Org1 的 admin 背书,或者 Org2 和 Org3 下的 member 共同背书签名
OR('Org1MSP.admin', AND('Org2MSP.member', 'Org3MSP.member'))
// 需要 org1、org2、org3 的 member 的至少两个背书签名
OutOf(2, 'Org1MSP.member', 'Org2MSP.member', 'Org3MSP.member') 

Note: chain code needs to be installed in the plurality of nodes Peer endorsement but performed only once instantiated.


2.3 Query chain code

After the successful deployment chain code can be invoked through a specific chain of command code to initiate a transaction or query request for books manipulate data.

Use the query command to query the chain code:

# peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'
  • -n: Specifies the chain code name to be called
  • -C: Channel Name
  • -c specifies the parameters needed to call the chain code

After it succeeds, the return output 100.


2.4 call chain code

Client initiates a transaction on the books data changes, you need to send the transaction after endorsement to the sorting node chain. Thus, the need to open and TLS authentication designate corresponding orderer certification path.

It should be noted, chain code to execute the query and execute operations Affairs (change data books) process operations are different:

  • Chain code query: the client receives the transaction request is not submitted to the proposed transaction endorsement Orderer node node response, i.e., the operation does not need to query the chain, optionally a chain code endorsement node can query;
  • Chain code transaction: the client first needs to be collected according to the specified policy endorsement endorsement signatures enough to deal proposal, submitted to the Orderer node trading after the endorsement then, that trade transaction operations needed to block the chain.

Invoke the command chain code using the call:

# peer chaincode invoke -o orderer.example.com:7050 --tls --cafile $ORDERER_CA  -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}'
  • -o: Specifies the orderer node address
  • --tls: open TLS authentication
  • --cafile: Specifies the certificate path TLS_CA
  • -n: Specifies the chain code name
  • -C: Channel Name
  • -c: required parameter specifies the call chain code

Execution returns the following results, indicating that the transaction is executed successfully:

[chaincodeCmd] chaincodeInvokeOrQuery -> INFO 04c Chaincode invoke successful. result: status:200

Account of the balance of a query again, if the results returned 90, indicating that the transaction was executed correctly:

# peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'

Note: If the transaction is subject to endorsement endorsement multiple nodes, you can use the --peerAddressesflag specified node. For example: transaction requires the joint endorsement of peer0.org1 and peer0.org2:

# peer chaincode invoke -o orderer.example.com:7050 --tls --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}'


Packaged with the chain code signature 2.5

Chain code deployed in addition to normal installation, examples of operation steps, there is a deployment, i.e. first chain code packaged, then the packaged file is signed, then the final installation and operation example.

Using the following command packaging operation:

# peer chaincode package -n exacc -v 1.0 -p github.com/chaincode/chaincode_example02/go/  -s -S -i "AND('Org1MSP.admin')" ccpack.out

Parameter Description:

  • -s: Create a signature can be multiple owners package
  • -S: an optional parameter file is used core.yaml localMspId correlation value defines the MSP sign the package
  • -i: instantiate policy specifies chain code (specify who can instantiate chain code)

Packaged files can be used directly install command to install, such as: peer chaincode install ccpack.out, but generally packaged signature on file further installation.

Use the following command to sign the package file operations (add the current MSP signature to the signature list):

# peer chaincode signpackage ccpack.out signedccpack.out

signedccpack.out It contains an additional signature by the local MSP for the package.

Installing signed package file:

# peer chaincode install signedccpack.out

Instantiate operation of chain code has been installed, specify endorsement strategy:

# peer chaincode instantiate -o orderer.example.com:7050 --tls --cafile $ORDERER_CA -C $CHANNEL_NAME -n exacc -v 1.0 -c '{"Args":["init","a", "100", "b","200"]}' -P "OR ('Org1MSP.peer','Org2MSP.peer')"

test:

Use the following command to query the chain code, the output 100 is:

# peer chaincode query -C $CHANNEL_NAME -n exacc -c '{"Args":["query","a"]}'

Use the command codes call chain transfer operations:

# peer chaincode invoke -o orderer.example.com:7050  --tls --cafile $ORDERER_CA  -C $CHANNEL_NAME -n exacc -c '{"Args":["invoke","a","b","10"]}'

Use the following command to query the chain code again, output is 90:

# peer chaincode query -C $CHANNEL_NAME -n exacc -c '{"Args":["query","a"]}'


2.6 Upgrade chain code

First, on the first chain code modifications after installation:

# peer chaincode install -n mycc -v 2.0 -p github.com/chaincode/chaincode_example02/go/

Then, using the following command chain code upgrade installed:

# peer chaincode upgrade -o orderer.example.com:7050 --tls --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -v 2.0 -c '{"Args":["init","a", "100", "b","200"]}' -P "OR ('Org1MSP.peer','Org2MSP.peer')"

test:

Use the following command to query the chain code, the output 100 is:

# peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'

Use the command codes call chain transfer operations:

# peer chaincode invoke -o orderer.example.com:7050  --tls --cafile $ORDERER_CA  -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}'

Use the following command to query the chain code again, output is 90:

# peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'

Note that, during the upgrade, chaincode the Init function is called to perform data-related operations, or re-initialized data; you need to be careful to avoid when upgrading chaincode reset the status information.


2.7 View log chain code

Use docker pscommand to view the current network has the following three chain code container to start:

  • dev-peer0.org1.example.com-mycc-1.0
  • dev-peer0.org1.example.com-mycc-2.0

  • dev-peer0.org1.example.com-exacc-1.0

Use docker logscommand to view the log chain code:

$ docker logs dev-peer0.org1.example.com-mycc-1.0
ex02 Init
Aval = 100, Bval = 200
ex02 Invoke
Query Response:{"Name":"a","Amount":"100"}
ex02 Invoke
Aval = 90, Bval = 210
ex02 Invoke
Query Response:{"Name":"a","Amount":"90"}
$ docker logs dev-peer0.org1.example.com-mycc-2.0
ex02 Init
Aval = 100, Bval = 200
ex02 Invoke
Query Response:{"Name":"a","Amount":"100"}
ex02 Invoke
Aval = 90, Bval = 210
ex02 Invoke
Query Response:{"Name":"a","Amount":"90"}
$ docker logs dev-peer0.org1.example.com-exacc-1.0
ex02 Init
Aval = 100, Bval = 200
ex02 Invoke
Query Response:{"Name":"a","Amount":"100"}
ex02 Invoke
Aval = 90, Bval = 210
ex02 Invoke
Query Response:{"Name":"a","Amount":"90"}


Chain code in the test mode 3 dev

3.1 Start test network

The above process chain code is tested in first-network network, but the chain code of the network under test is too complex, you need to specify a number of parameters, if you just want to test the correctness of the written chain code, you can use dev development model.

Enter the chaincode-docker-devmodedirectory:

$ cd ./fabric-samples/chaincode-docker-devmode/

There is a five files in this directory:

  • docker-compose-simple.yaml: network initiated dependent configuration file, the configuration file specifies the four containers, are: orderer, peer, cli, chaincode
  • msp: MSP network environment, contains a series of certificates and private keys
  • myc.block: channel configuration block file
  • myc.tx: Channel transaction profiles
  • orderer.block: Sort initial service profile blocks

Start the network using the following command:

$ docker-compose -f docker-compose-simple.yaml up -d
Creating network "chaincodedockerdevmode_default" with the default driver
Creating orderer ... 
Creating orderer ... done
Creating peer ... 
Creating peer ... done
Creating cli ... 
Creating chaincode ... 
Creating cli
Creating cli ... done

Use the peer opened to the development mode, the two containers also launched: chaincode container for chain code environment; CLI container for interacting with a chain code, wherein creating and connecting channel CLI command has been embedded in the container, can be direct chain code calls.


3.2 Build and start the chain code

Using the following command to enter the vessel chaincode:

$ docker exec -it chaincode bash

The following results appear, run successfully:

root@d32997378218:/opt/gopath/src/chaincode#

Check the definition of the chaincode container:

 chaincode:
    container_name: chaincode
    image: hyperledger/fabric-ccenv
    tty: true
    environment:
      - GOPATH=/opt/gopath
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - FABRIC_LOGGING_SPEC=DEBUG
      - CORE_PEER_ID=example02
      - CORE_PEER_ADDRESS=peer:7051
      - CORE_PEER_LOCALMSPID=DEFAULT
      - CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp
    working_dir: /opt/gopath/src/chaincode
    command: /bin/sh -c 'sleep 6000000'
    volumes:
        - /var/run/:/host/var/run/
        - ./msp:/etc/hyperledger/msp
        - ./../chaincode:/opt/gopath/src/chaincode
    depends_on:
      - orderer
      - peer

Current directory of the container /opt/gopath/src/chaincodecorresponding to the local system ./../chaincode, we use chaincode_example02 chain code in the test directory. Enter the chaincode_example02/go/directory compiler chain code:

# cd chaincode_example02/go/
# go build

Up and running chain code using the following command:

# CORE_PEER_ADDRESS=peer:7052 CORE_CHAINCODE_ID_NAME=mycc:0 ./go

Command Meaning:

  • CORE_PEER_ADDRESS: used to specify the peer, where 7052 is a dedicated port chain code listening port (7051 is the peer node listens network port)

  • CORE_CHAINCODE_ID_NAME: a chain code registered peer
    • mycc: Specifies the chain code name
    • 0: Specifies initial version chain code
    • ./go: Specifies the chain code file

Opening a new terminal, using the following CLI commands containers:

$ docker exec -it cli bash

CLI into the container, using the following command to install the chain code:

# peer chaincode install -p chaincodedev/chaincode/chaincode_example02/go -n mycc -v 0

Examples of using the command chain code:

# peer chaincode instantiate -n mycc -v 0 -c '{"Args":["init","a", "100", "b","200"]}' -C myc

test:

Use the following command to query the chain code, the output 100 is:

# peer chaincode query -n mycc  -c '{"Args":["query","a"]}' -C myc

Use the command codes call chain transfer operations:

# peer chaincode invoke -n mycc -c '{"Args":["invoke","a","b","10"]}' -C myc

Use the following command to query the chain code again, output is 90:

# peer chaincode query -n mycc  -c '{"Args":["query","a"]}' -C myc


3.3 Close the test network

Use the following command to turn off the test network:

$ docker-compose -f docker-compose-simple.yaml down


reference

  1. "Hyperledger Fabric rookie Advanced Raiders"

Guess you like

Origin www.cnblogs.com/zongmin/p/11842562.html