Hyperledger Fabric 2.0 Chaincode Dev

Based Fabric v2.0.1 , the previous version should also be universal.

First, the preparatory work

Fabric Network

  • Peer single node
  • Single-node Orderer
  • Channel has been created
  • Peer Join 该 Channel
  • Peer enable external builder Reference Fabric 2.0 external builder
  • Peer preinstalled Fake Chaincode

Mac or Linux environment to quickly build script based on Fabric Debug Environment

Second, start Peer to Dev mode

When you start to bring Peer dev mode identification, through command-line options --peer-chaincodedevdev mode is enabled.

Start command is as follows:

bin/peer node start --peer-chaincodedev

Correct start following key log, note WARN log peer will choose a card address to listen 7052 port (see the log information peer.chaincodeListenAddress) for registrationChaincode

2020-03-17 17:38:00.099 CST [nodeCmd] serve -> INFO 00b Running in chaincode development mode
2020-03-17 17:38:00.099 CST [nodeCmd] serve -> INFO 00c Disable loading validity system chaincode
2020-03-17 17:38:00.099 CST [nodeCmd] createChaincodeServer -> WARN 00f peer.chaincodeListenAddress is not set, using

Third, start Chaincode

Peer to dev mode operation, allowing Chaincodedirect connection to Peer registered from the outside.

2.0 Chaincode start change is Chaincode PackageID to identify registered to start, will detect Chaincode is installed when you call, so to install a fake Chaincode PackageID use this fake chaincode registration.

The PackageID to set environment variables FAKE_PACKAGE_IDfor use below.

Chaincode start following information is required:

  • CORE_CHAINCODE_ID_NAME: Environment variable to specify ChaincodethePackageIDHere use $FAKE_PACKAGE_IDvalue
  • CORE_PEER_TLS_ENABLED: Environment variables , specify whether to enable TLS, enable TLS also need to configure TLS Key, Cert file, run here do not enable TLS (peer can not configure TLS)
  • --peer.address: Command line options , registered address peer, see peer boot log

note, Need to debug Chaincode also need approve, commitin order to call,And you want to commitstart the registration before (or peer will automatically run fake chaincode)

First run Chaincode (note replace their peer address)


IDE runs
Here Insert Picture Description

approve & commit

# 设置配置文件路径
export FABRIC_CFG_PATH=/tmp/fabric
# approve 替换下面的变量,--init-required 选项根据自己chaincode觉得是否需要
bin/peer lifecycle chaincode approveformyorg --channelID $CHANNEL_NAME --name $CC_NAME --version $CC_VERSION --init-required --package-id $FAKE_PACKAGE_ID --sequence $CC_SEQUENCE --waitForEvent

# commit
bin/peer lifecycle chaincode commit --channelID $CHANNEL_NAME --name $CC_NAME --version $CC_VERSION --init-required --sequence $CC_SEQUENCE

I set up here --init-requiredso you need toInit

# init
bin/peer chaincode invoke -o --peerAddresses -C wagaga -n $CC_NAME -I -c '{"Args":["Init", "a","1000","b", "1000"]}'

2020-03-17 17:59:32.799 CST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
# query
bin/peer chaincode query -C wagaga -n $CC_NAME -c '{"Args":["query","a"]}'

Here Insert Picture Description

IV Notes

Since the runtime will automatically load 2.0 Peer has started commitoff ChaincodeIf Peer restart too, will automatically start Fake Chaincode, their Chaincode fail to register.

After starting his Chaincode failure, stop and start again, stopping to Peer will Deregisterstart automatically Chaincode will also chaincodeDeregister

Fifth, another way

2.0 adds Chaincode AS Service , Peer Support initiative to connect Chaincode (requires external builder with support).

Reference official website configuration Chaincode as Service

Published 10 original articles · won praise 1 · views 625

Guess you like

Origin blog.csdn.net/DAOSHUXINDAN/article/details/104924631