golang
nodejs:https://fabric-sdk-node.github.io/
java:https://github.com/hyperledger/fabric-sdk-java
https://hyperledger-fabric.readthedocs.io/en/latest/chaincode4ade.html
二、概述
2.1、概念
Chaincode是一个用Go编写的程序,后期也将支持Java中实现了一个规定的接口。
链码运行在与承认对等体进程隔离的安全Docker容器中。 Chaincode通过应用程序提交的交易来初始化和管理分类帐状态。
链码通常处理网络成员所同意的业务逻辑,因此可被视为“智能合同”。 由链码创建的状态仅限于该链码,不能被另一个链码直接访问。 然而,在同一个网络中,给定适当的权限,链码可以调用另一个链码来访问其状态。
2.2、两个角色
我们对链码提供两个不同的观点。 一个,从应用程序开发人员的角度来看,开发一个名为Chaincode for Developers的块链应用程序/解决方案,另一个是针对负责管理块链网络的块链网络运营商的运营商链码,以及谁将利用Hyperledger Fabric API 链式代码的安装,实例化和升级,但可能不会涉及开发链码应用程序。
2.3、api
每个链码程序都必须实现Chaincode接口,其响应于接收到的事务调用其方法。 特别地,当链码接收到instantiate 实例化或upgrade 升级事务时,调用Init方法,使得链码可以执行任何必要的初始化,包括应用程序状态的初始化。 调用Invoke方法响应于接收到一个调用事务来处理事务提议。
初始化:http://godoc.org/github.com/hyperledger/fabric/core/chaincode/shim#Chaincode
chaincode“shim”API中的另一个接口是ChaincodeStubInterface,用于访问和修改分类帐,并在链码之间进行调用。
api:http://godoc.org/github.com/hyperledger/fabric/core/chaincode/shim#ChaincodeStub
2.3.1、简单资产链码
基本键值对链码
1、选择代码的位置
安装go,并且配置了环境变量
现在,要为chaincode应用程序创建一个名为$ GOPATH / src /的子目录的目录。 以及创建源文件
mkdir -p $GOPATH/src/sacc && cd $GOPATH/src/sacc
touch sacc.go
2、必要的包
package main import ( "fmt" "github.com/hyperledger/fabric/core/chaincode/shim" "github.com/hyperledger/fabric/protos/peer" )
3、初始化链码
// Init is called during chaincode instantiation to initialize any data. func (t *SimpleAsset) Init(stub shim.ChaincodeStubInterface) peer.Response { }
注意,chaincode升级还会调用此功能。 当编写一个将升级现有的链码时,请确保适当地修改Init函数。 特别是,如果没有“迁移”,或者没有任何内容作为升级的一部分进行初始化,请提供一个空的“Init”方法。
接下来,我们将使用ChaincodeStubInterface.GetStringArgs函数检索Init调用的参数,并检查其有效性。在我们的例子中,我们期待着一个键值对。
// Init is called during chaincode instantiation to initialize any // data. Note that chaincode upgrade also calls this function to reset // or to migrate data, so be careful to avoid a scenario where you // inadvertently clobber your ledger's data! func (t *SimpleAsset) Init(stub shim.ChaincodeStubInterface) peer.Response { // Get the args from the transaction proposal args := stub.GetStringArgs() if len(args) != 2 { return shim.Error("Incorrect arguments. Expecting a key and a value") } }