A chaincode typically handles business logic agreed to by members of the network, so it similar to a “smart contract”. A chaincode can be invoked to update or query the ledger in a proposal transaction. Given the appropriate permission, a chaincode may invoke another chaincode, either in the same channel or in different channels, to access its state. Note that, if the called chaincode is on a different channel from the calling chaincode, only read query is allowed. That is, the called chaincode on a different channel is only a Query, which does not participate in state validation checks in subsequent commit phase
Constructure 系统链码 用户链码由应用程序开发人员根据不同场景需求及成员制定的相关规则,使用 Golang(或Java等)语言编写的基于操作区块链分布式账本的状态的业务处理逻辑代码,运行在链码容器中,通过 Fabric 提供的接口与账本状态进行交互。下可对账本数据进行操作,上可以给企业级应用程序提供调用接口
step1: 链码的安装
"# peer chaincode install −n mycc −v 1.0 −p github.com/chaincode/chaincode_example02/go"/step2: 链码的实例化
# peer chaincode instantiate -o orderer.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a", "100", "b","200"]}' -P "OR ('Org1MSP.peer','Org2MSP.peer')"step3: 链码处理响应请求
in response to receiving an invoke transaction to process transaction proposals.
# peer chaincode invoke -o orderer.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}'step4: 链码的打包
"# peer chaincode package −n exacc −v 1.0 −p github.com/chaincode/chaincode_example02/go/ −s −S −i \"AND(′Org1MSP.admin′)\" ccpack.out"step5: 链码的打包
# peer chaincode signpackage ccpack.out signedccpack.out # peer chaincode install signedccpack.outstep6: 链码的升级
# peer chaincode upgrade -o orderer.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n mycc -v 2.0 -c '{"Args":["init","a", "100", "b","200"]}' -P "OR ('Org1MSP.peer','Org2MSP.peer')"终端一 以 docker-compose-simple.yaml 启动网络,并以开发模式启动 peer节点。另外还启动了两个容器:一个 chaincode 容器,用于链码环境;一个 CLI 容器,用于与链码进行交互。
终端二 chaincode容器
终端三 cli 容器