BTCU lesson5

mac2025-08-22  3

链码作用及对链码的管理与测试

What is Chaincode?

Chaincode is a program, written in Go, node.js, or Java that implements a prescribed interface. Chaincode runs in a secured Docker container isolated from the endorsing peer process. Chaincode initializes and manages the ledger state through transactions submitted by applications.

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 提供的接口与账本状态进行交互。下可对账本数据进行操作,上可以给企业级应用程序提供调用接口

How to manage?

管理链码的五个命令: install:将已编写完成的链码安装在网络节点中。instantiate:对已安装的链码进行实例化。upgrade:对已有链码进行升级。链代码可以在安装后根据具体需求的变化进行升级。package:对指定的链码进行打包的操作。singnpackage:签名。

Experiments

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.out

step6: 链码的升级

# 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 容器

最新回复(0)