Hyperledger fabric入门 day3

Hyperledger fabric入门 day3

Fabric和Fabric-ca的编程接口

Fabric的Peer节点和Orderer节点都提供了基于Grpc协议的接口,通过这些接口可以和Peer节点以及Orderer节点进行交互。

Fabric所有的protocol buffers接口定义都存放在https://github.com/hyperledger/fabric/tree/release/protos

Fabric的Peer模块的接口功能划分

Fabric的Peer模块提供的Grpc接口按照功能大致可以分为两类:系统管理和Chaincode相关操作。

系统管理

  • 获取当前Peer接入了哪些Channel
  • 获取当前Peer加入了的某个Channel的区块数
  • 获取当前Peer加入的某个Channel中的某个区块号,获取区块的详细信息
  • 根据当前的Peer加入的某个Channel中的某个区块的哈希值,获取区块的详细信息
  • 根据当前的Peer加入的某个Channel中交易的哈希值,获取交易的详细信息
  • 获取当前Peer服务器中状态为install的Chaincode的信息
  • 根据当前的Peer加入的某个Channel中状态为Instantiate的Chaincode的详细信息

Chaincode相关操作

  • 调用Chaincode的query方法
  • 调用一个已经部署的Chaincode的invoke方法

Fabric-ca-server的RESTAPI接口

  • 注册一个Fabric账号
  • 加载一个已经注册过的Fabric账号

Fabric Go SDK

Fabric Golang的安装

mkdir -p $GOPATH/src/github.com/hyperledger
cd $GOPATH/src/github.com/hyperledger
git clone https://github.com/hyperledger/fabric-sdk-go

# 安装相关的依赖包
go get -u github.com/cloudflare/cfssl
go get -u github.com/miekg/pkcs11
go get -u github.com/mitchellh/mapstructure
go get -u github.com/pkg/errors
go get -u github.com/spf13/viper
go get -u github.com/x/crypto/sha3
go get -u github.com/x/net/context
go get -u github.com/x/sync/syncmap
go get -u google.golang.org/grpc

创建配置文件

Fabric Golang SDK需要依赖一个配置文件,该配置文件采用YAML的格式。

name: "robert fabric test"
x-loggingLevel: info
client:
organization: Org1
logging:
level: debug
cryptoconfig:
path: /project/opt_fabric/fabricconfig/crypro-config

credentialStore:
path: "/tmp/hfc-kvs"
cryptoStore:
path: /tmp/msp
BCCSP:
security:
enabled: true
default:
provider: "SW"
hashAlgorithm: "SHA2"
softVerify: true
ephemeral: false
level: 256
organizations:
Org1:
mspid: Org1MSP
cryptoPath: peerOrganizations/org1.robertfabrictest.com/users/{userName}@org1.robertfabrictest.com/msp
peers:
- peer0.org1.robertfabrictest.com
certificateAuthorities:
- ca-org1
adminPrivateKey:
pem: "/project/opt_fabric/fabricconfig/crypto-config/peerOrganizations/org1.robertfabrictest.com/users/Admin@org1.robertfabrictest.com/msp/keystore/4cb0b....7e6_sk"
signedCert:
path: "/project/opt_fabric/fabricconfig/crypto-config/peerOrganizations/org1.robertfabrictest.com/users/Admin@org1.robertfabrictest.com/msp/signcerts/Admin@org1.robertfabrictest.com-cert.pem"
certificateAuthorities:
ca-org1:
url: http://192.168.23.212:7054
httpOptions:
verify: true
registrar:
enrollId: admin
enrollSecret: adminpw
caName: ca-org1

一个简单的Golang访问Fabric的例子

pakage main
import (
	fabricapi "github.com/hyperledger/fabric-sdk-go/def/fabapi"
    "github.com/hyperledger/fabric-sdk-go/pkg/errors"
    "github.com/hyperledger/fabric-sdk-go/fabric-client/orderer"
    "fmt"
    identityImpl "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/identity"
    "io/ioutil"
    "encoding/hex"
    afc "github.com/hyperledger/fabric-sdk-go/api/apifabclient"
    "github.com/hyperledger/fabric-sdk-go/api/apitxn"
    fab "github.com/hyperledger/fabric-sdk-go/api/apifabclient"
    peerapi "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/peer"
    "github.com/hyperledger/fabric-sdk-go/pkg/fatic-client/events"
    fabricCAClient "github.com/hyperledger/fabric-sdk-go/pkg/fabric-ca-client"
    "github.com/hyperledger/fabric-sdk-go/pkg/config"
    "github.com/hyperledger/fabric-sdk-go/pkg/logging"
    "github.com/hyperledger/fabric-sdk-go/pkg/logging/deflogger"
)
func main() {
    // 初始化日志系统
    if ! logging.IsLoggerInitialized() {
        logging.InitLogger(deflogger.GetLoggingProvider())
    }
    // fabric_ca()
    fabric_local()
}
/*
通过fabric-ca-server 获取账号信息,并利用获取的账号访问Peer服务器节点
*/
func fabric_ca() {
    // 读取SDK配置文件
    sdkOptions := fabricapi.Options{ConfigFile: "./fabricsdk/config_test/yaml", }
    // 创建SDK代理
    sdk, _ := fabricapi.NewSDK(sdkOptins)
    context, _ := sdk.NewContext("Org1MSP")
    user := identityImpl.NewUser("Admin", "Org1MSP")
    // 读取fabric-ca的配置文件
    configImpl, _ := config.InitConfig("./fabricsdk/config_test.yaml")
    caClient, _ := fabricCAClient.NewFabricCAClient(configImpl, "Org1")
    
    key, cert := caClient.Enroll("user88", "peer2wd")
    user.SetEnrollmentCertificate(cert)
    user.SetPrivateKey(key)
    session, _ := sdk.NewSession(context, user)
    // 创建fabric客户端代理
    client, _ := sdk.NewSystemClient(session)
    // 创建通道代理,通道名为:roberttestchannel12
    channel, _ := client.NewChannel("roberttestchannel12")
    // 创建Orderer节点代理
    orderer, _ := orderer.NewOrderer("grpc://192.168.23.212:7050", "", "", client.Config())
    channel.AddOrderer(orderer)
    // 创建Peer节点代理
    newpeer, _ := fabricapi.NewPeer("grpc://192.168.23.212:7051", "", "", client.Config())
    channel.AddPeer(newpeer)
    // 获取当前通道的信息
    blockchaininfo, _ := channel.QueryInfo()
    fmt.Println("the peer block height %d", blcokchaininfo.Height)
}

/*
通过cryptogen模块生成的账号访问Peer服务器节点
*/
func fabric_local() {
    // 读取配置文件
    sdkOptins := fabricapi.Options{ConfigFile: "./fabricsdk/config_test.yaml", }
    // 创建SDK代理
    sdk, _ := fabricapi.NewSDK(sdkOptions)
    session, _ := sdk.NewPreEnrolledUserSession("Org1", "Admin")
    // 创建fabric客户端代理
    client, _ := sdk.NewSystemClient(session)
    // 创建通道代理,通道名为:roberttestchannel12
    channel, _ := client.NewChannel("roberttestchannel12")
    // 创建Orderer节点代理
    orderer, _ := orderer.NewOrderer("grpc://192.168.23.212:7050", "", "", client.Config())
    channel.AddOrderer(orderer)
    // 创建Peer节点代理
    peer, _ := fabricapi.NewPeer("grpc://192.168.23.212:7051", "", "", client.Config())
    channel.AddPeer(peer)
    // 获取当前通道的信息
    blockchainInfo := channel.QueryInfo()
    fmt.Println("the block height %d", blockchainInfo.Height)
}

上述代码中的两个方法——fabric_ca和fabric_local代表了两种获取账号的方式:

  • fabric_local:通过使用cryptogen模块生成的账号访问Peer服务器节点
  • fabric_ca:通过使用fabric-ca-server授权的账号访问Peer服务器节点

Grpc协议简介

Grpc一开始由Google开发,是一款语言中立、平台中立、开源的远程过程调用RPC系统。目前Grpc提供Java、Go、C、C++、Node.js、Python、Ruby、Objective-C、PHP和C#等语言支持。Grpc基于HTTP/2标准设计,带来诸如双向流、流控、头部压缩、单TCP连接上的多复用请求等特性。这些特性使得其表现出很好的性能,而且报文占用的空间也比较小。Grpc默认使用protocol buffers作为接口定义语言来描述服务接口和消息结构。

posted @ 2020-03-09 00:09  NykuvL  阅读(380)  评论(0编辑  收藏  举报