$ peer channel create [flags], 常用参数为:
`-o, --orderer: orderer节点的地址
`-c, --channelID: 要创建的通道的ID, 必须小写, 在250个字符以内
`-f, --file: 由configtxgen 生成的通道文件, 用于提交给orderer
-t, --timeout: 创建通道的超时时长, 默认为5s
`--tls: 通信时是否使用tls加密
`--cafile: 当前orderer节点pem格式的tls证书文件, 要使用绝对路径.
# orderer节点pem格式的tls证书文件路径参考:
crypto-config/ordererOrganizations/zjnu.com/orderers/orderer.zjnu.com/msp/tlscacerts/tlsca.zjnu.com-cert.pem
# example
$ peer channel create -o orderer节点地址:端口 -c 通道名 -f 通道文件 --tls true --cafile orderer节点pem格式的证书文件
- orderer节点地址: 可以是IP地址或者域名
- orderer节点监听的是7050端口
$ peer channel create -o orderer.zjnu.com:7050 -c zjnuchannel -f ./channel-artifacts/channel.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/zjnu.com/msp/tlscacerts/tlsca.zjnu.com-cert.pem
# 在当前工作目录下生成一个文件: 通道名.block, 本例: zjnuchannel.block
$ ls
channel-artifacts crypto `zjnuchannel.block` --> 生成的文件
$ peer channel join[flags], 常用参数为:
`-b, --blockpath: 通过 peer channel create 命令生成的通道文件
# example
$ peer channel join -b 生成的通道block文件
$ peer channel join -b zjnuchannel.block
其他的节点,加入通道
# 第1个节点 Go组织的 peer0
export CORE_PEER_ADDRESS=peer0.orggo.zjnu.com:7051
export CORE_PEER_LOCALMSPID=OrgGoMSP
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.zjnu.com/users/[email protected]/msp
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.zjnu.com/peers/peer0.orggo.zjnu.com/tls/ca.crt
export CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.zjnu.com/peers/peer0.orggo.zjnu.com/tls/server.crt
export CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.zjnu.com/peers/peer0.orggo.zjnu.com/tls/server.key
# 第2个节点 Go组织的 peer1
export CORE_PEER_ADDRESS=peer1.orggo.zjnu.com:7051
export CORE_PEER_LOCALMSPID=OrgGoMSP
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.zjnu.com/users/[email protected]/msp
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.zjnu.com/peers/peer1.orggo.zjnu.com/tls/ca.crt
export CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.zjnu.com/peers/peer1.orggo.zjnu.com/tls/server.crt
export CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.zjnu.com/peers/peer1.orggo.zjnu.com/tls/server.key
# 第3个节点 Cpp组织的 peer0 注意:cli为自己定义的客户端名称,根据自己的命名进行修改
export CORE_PEER_ID=cli
export CORE_PEER_ADDRESS=peer0.orgcpp.zjnu.com:7051
export CORE_PEER_LOCALMSPID=OrgCppMSP
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgcpp.zjnu.com/users/[email protected]/msp
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgcpp.zjnu.com/peers/peer0.orgcpp.zjnu.com/tls/ca.crt
export CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgcpp.zjnu.com/peers/peer0.orgcpp.zjnu.com/tls/server.crt
export CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgcpp.zjnu.com/peers/peer0.orgcpp.zjnu.com/tls/server.key
# 第4个节点 Cpp组织的 peer1 注意:cli为自己定义的客户端名称,根据自己的命名进行修改
export CORE_PEER_ID=cli
export CORE_PEER_ADDRESS=peer1.orgcpp.zjnu.com:7051
export CORE_PEER_LOCALMSPID=OrgCppMSP
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgcpp.zjnu.com/users/[email protected]/msp
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgcpp.zjnu.com/peers/peer1.orgcpp.zjnu.com/tls/ca.crt
export CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgcpp.zjnu.com/peers/peer1.orgcpp.zjnu.com/tls/server.crt
export CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgcpp.zjnu.com/peers/peer1.orgcpp.zjnu.com/tls/server.key
$ peer channel update [flags], 常用参数为:
`-o, --orderer: orderer节点的地址
`-c, --channelID: 要创建的通道的ID, 必须小写, 在250个字符以内
`-f, --file: 由configtxgen 生成的组织锚节点文件, 用于提交给orderer
`--tls: 通信时是否使用tls加密
`--cafile: 当前orderer节点pem格式的tls证书文件, 要使用绝对路径.
# orderer节点pem格式的tls证书文件路径参考:
crypto-config/ordererOrganizations/zjnu.com/orderers/orderer.zjnu.com/msp/tlscacerts/tlsca.zjnu.com-cert.pem
# example
$ peer channel update -o orderer节点地址:端口 -c 通道名 -f 锚节点更新文件 --tls true --cafile orderer节点pem格式的证书文件
$ peer chaincode install [flags], 常用参数为:
-c, --ctor: JSON格式的构造参数, 默认是"{}"
`-l, --lang: 编写chaincode的编程语言, 默认值是 golang
`-n, --name: chaincode的名字
`-p, --path: chaincode源代码的目录, 从 $GOPATH/src 路径后开始写
`-v, --version: 当前操作的chaincode的版本, 适用这些命令install/instantiate/upgrade
# example
$ peer chaincode install -n 链码的名字 -v 链码的版本 -l 链码的语言 -p 链码的位置
- 链码名字自己起
- 链码的版本, 自己根据实际情况指定
$ peer chaincode install -n testcc -v 1.0 -l golang -p github.com/chaincode
$ peer chaincode instantiate [flags], 常用参数为:
`-C,--channelID:当前命令运行的通道,默认值是“testchainid"。
`-c, --ctor:JSON格式的构造参数,默认值是“{}"
`-l,--lang:编写Chaincode的编程语言,默认值是golang
`-n,--name:Chaincode的名字。
`-P,--policy:当前Chaincode的背书策略。
`-v,--version:当前操作的Chaincode的版本,适用于install/instantiate/upgrade等命令
`--tls: 通信时是否使用tls加密
`--cafile: 当前orderer节点pem格式的tls证书文件, 要使用绝对路径.
# example
# -c '{"Args":["init","a","100","b","200"]}'
# -P "AND ('OrgGoMSP.member', 'OrgCppMSP.member')"
$ peer chaincode instantiate -o orderer节点地址:端口 --tls true --cafile orderer节点pem格式的证书文件 -C 通道名称 -n 链码名称 -l 链码语言 -v 链码版本 -c 链码Init函数调用 -P 背书策略
$ peer chaincode instantiate -o orderer.zjnu.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/zjnu.com/msp/tlscacerts/tlsca.zjnu.com-cert.pem -C zjnu
channel -n testcc -l golang -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "AND ('OrgGoMSP.member', 'OrgCppMSP.member')"
$ peer chaincode query [flags], 常用参数为:
`-n,--name:Chaincode的名字。
`-C,--channelID:当前命令运行的通道,默认值是“testchainid"
`-c, --ctor:JSON格式的构造参数,默认值是“{}"
-x,--hex:是否对输出的内容进行编码处理
-r,--raw:是否输出二进制内容
-t, --tid: 指定当前查询的编号
# example
# '{"Args":["query","a"]}'
$ peer chaincode query -C 通道名称 -n 链码名称 -c 链码调用
$ peer chaincode invoke [flags], 常用参数为:
`-o, --orderer: orderer节点的地址
`-C,--channelID:当前命令运行的通道,默认值是“testchainid"
`-c, --ctor:JSON格式的构造参数,默认值是“{}"
`-n,--name:Chaincode的名字
`--tls: 通信时是否使用tls加密
`--cafile: 当前orderer节点pem格式的tls证书文件, 要使用绝对路径.
`--peerAddresses: 指定要连接的peer节点的地址
`--tlsRootCertFiles: 连接的peer节点的TLS根证书
# 连接的peer节点的TLS根证书查找路径参考:
/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.zjnu.com/peers/peer0.orggo.zjnu.com/tls/ca.crt
# example
# -c '{"Args":["invoke","a","b","10"]}'
$ peer chaincode invoke -o orderer节点地址:端口 --tls true --cafile orderer节点pem格式的证书文件 -C 通道名称 -n 链码名称 --peerAddresses 背书节点1:端口 --tlsRootCertFiles 背书节点1的TLS根证书 --peerAddresses 背书节点2:端口 --tlsRootCertFiles 背书节点2的TLS根证书 -c 交易链码调用
客户端对Peer节点的操作流程:
创建通道, 通过客户端节点来完成
将每个组织的每个节点都加入到通道中 -> 客户端来完成的
给每个peer节点安装智能合约 -> 链代码(程序: go, node.js, java)
(从官方示例中拷贝一个智能合约到手动开发的项目里)
对智能合约进行初始化 , 对应智能合约中的 Init 函数
对数据进行查询 -> 读
对数据进行调用 -> 写
经过前面的讲解我们都知道, 一个客户端只能连接一个指定的节点, 如果想要该客户端连接其他节点, 那么就必须修改当前客户端中相关的环境变量
创建通道的操作只要登录到客户端容器中就可以进行操作.
echo $CORE_PEER_ADDRESS
peer channel join -b xxx.block
peer chaincode install [flags]
peer chaincode instantiate [flag]
echo $CORE_PEER_ADDRESS
peer channel join -b xxx.block
peer chaincode install [flags]
echo $CORE_PEER_ADDRESS
peer channel join -b xxx.block
peer chaincode install [flags]
echo $CORE_PEER_ADDRESS
peer channel join -b xxx.block
peer chaincode install [flags]
链代码的包名的指定
// xxx.go
package main
必须要引入的包
// go get github.com/hyperledger/fabric/core/chaincode/shim
import (
// 客户端需要和 Fabric框架通信
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
)
链码的书写要求
// 自定义一个结构体 - 类, 基于这个类实现一些接口函数
type Test struct {
// 空着即可
}
func (t* Test) Init(stub ChaincodeStubInterface) pb.Response;
func (t* Test) Invoke(stub ChaincodeStubInterface) pb.Response;
链码 API 查询
https://godoc.org/github.com/hyperledger/fabric-chaincode-go/shim
背书策略
# 指定背书策略
peer chaincode instantiate -o oraderer.test.com:7050 -C mychannel -n mycc —v 1.0 -c '{"Args":["init", "a", "100", "b", "200"]}' -P "AND ('OrgGoMSP.member', 'OrgCppMSP.member')"
# 调用
# 调用示例
$ peer chaincode invoke -o orderer.zjnu.com:7050 -C mychannel -n testcc --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/zjnu.com/orderers/orderer.zjnu.com/msp/tlscacerts/tlsca.zjnu.com-cert.pem --peerAddresses peer0.orggo.zjnu.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.zjnu.com/peers/peer0.orggo.zjnu.com/tls/ca.crt --peerAddresses peer0.orgcpp.zjnu.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgcpp.zjnu.com/peers/peer0.orgcpp.zjnu.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}'
AND("org1.member", "org2.member")
OR("org1.member", "org2.member")
AND(" .member", OR("org1.member", "org2.member"))
func main() {
// SimpleChaincode 自定义 的结构体
err := shim.Start(new(SimpleChaincode)) // 最重要的一句话
if err != nil {
fmt.Printf("Error starting Simple chaincode: %s", err)
}
}
如果要自定义函数,函数格式
func (t* xxx) funcName(stub shim.ChaincodeStubInterface, args []string) pb.Response {
}
// 操作账本的
// shim -> ChaincodeStubInterface
package main
import (
"fmt"
"strconv"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
)
// SimpleChaincode example simple Chaincode implementation
type SimpleChaincode struct {
}
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
fmt.Println("ex02 Init")
_, args := stub.GetFunctionAndParameters()
var A, B string // Entities
var Aval, Bval int // Asset holdings
var err error
if len(args) != 4 {
return shim.Error("Incorrect number of arguments. Expecting 4")
}
// Initialize the chaincode
A = args[0]
Aval, err = strconv.Atoi(args[1])
if err != nil {
return shim.Error("Expecting integer value for asset holding")
}
B = args[2]
Bval, err = strconv.Atoi(args[3])
if err != nil {
return shim.Error("Expecting integer value for asset holding")
}
fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)
// Write the state to the ledger
err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
if err != nil {
return shim.Error(err.Error())
}
err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
fmt.Println("ex02 Invoke")
function, args := stub.GetFunctionAndParameters()
if function == "invoke" {
// Make payment of X units from A to B
return t.invoke(stub, args)
} else if function == "delete" {
// Deletes an entity from its state
return t.delete(stub, args)
} else if function == "query" {
// the old "Query" is now implemtned in invoke
return t.query(stub, args)
}
return shim.Error("Invalid invoke function name. Expecting \"invoke\" \"delete\" \"query\"")
}
// Transaction makes payment of X units from A to B
func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var A, B string // Entities
var Aval, Bval int // Asset holdings
var X int // Transaction value
var err error
if len(args) != 3 {
return shim.Error("Incorrect number of arguments. Expecting 3")
}
A = args[0]
B = args[1]
// Get the state from the ledger
// TODO: will be nice to have a GetAllState call to ledger
Avalbytes, err := stub.GetState(A)
if err != nil {
return shim.Error("Failed to get state")
}
if Avalbytes == nil {
return shim.Error("Entity not found")
}
Aval, _ = strconv.Atoi(string(Avalbytes))
Bvalbytes, err := stub.GetState(B)
if err != nil {
return shim.Error("Failed to get state")
}
if Bvalbytes == nil {
return shim.Error("Entity not found")
}
Bval, _ = strconv.Atoi(string(Bvalbytes))
// Perform the execution
X, err = strconv.Atoi(args[2])
if err != nil {
return shim.Error("Invalid transaction amount, expecting a integer value")
}
Aval = Aval - X
Bval = Bval + X
fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)
// Write the state back to the ledger
err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
if err != nil {
return shim.Error(err.Error())
}
err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
// Deletes an entity from state
func (t *SimpleChaincode) delete(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1")
}
A := args[0]
// Delete the key from the state in ledger
err := stub.DelState(A)
if err != nil {
return shim.Error("Failed to delete state")
}
return shim.Success(nil)
}
// query callback representing the query of a chaincode
func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var A string // Entities
var err error
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting name of the person to query")
}
A = args[0]
// Get the state from the ledger
Avalbytes, err := stub.GetState(A)
if err != nil {
jsonResp := "{\"Error\":\"Failed to get state for " + A + "\"}"
return shim.Error(jsonResp)
}
if Avalbytes == nil {
jsonResp := "{\"Error\":\"Nil amount for " + A + "\"}"
return shim.Error(jsonResp)
}
jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}"
fmt.Printf("Query Response:%s\n", jsonResp)
return shim.Success(Avalbytes)
}
func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil {
fmt.Printf("Error starting Simple chaincode: %s", err)
}
}
) pb.Response {
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1")
}
A := args[0]
// Delete the key from the state in ledger
err := stub.DelState(A)
if err != nil {
return shim.Error("Failed to delete state")
}
return shim.Success(nil)
}
// query callback representing the query of a chaincode
func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var A string // Entities
var err error
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting name of the person to query")
}
A = args[0]
// Get the state from the ledger
Avalbytes, err := stub.GetState(A)
if err != nil {
jsonResp := "{\"Error\":\"Failed to get state for " + A + "\"}"
return shim.Error(jsonResp)
}
if Avalbytes == nil {
jsonResp := "{\"Error\":\"Nil amount for " + A + "\"}"
return shim.Error(jsonResp)
}
jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}"
fmt.Printf("Query Response:%s\n", jsonResp)
return shim.Success(Avalbytes)
}
func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil {
fmt.Printf("Error starting Simple chaincode: %s", err)
}
}