首页 > 其他分享 >fabric监听落块事件

fabric监听落块事件

时间:2022-09-27 18:56:36浏览次数:45  
标签:落块 fabric nil panic err client com 监听

fabric 版本 v2.2
可以得到,区块编号,当前哈希,前块哈希

go.mod

require (
	github.com/hyperledger/fabric-gateway v1.1.0
	github.com/hyperledger/fabric-protos-go-apiv2 v0.0.0-20220615102044-467be1c7b2e7
	google.golang.org/grpc v1.47.0
)
/*
Copyright 2021 IBM All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/

package main

import (
	"context"
	"crypto/x509"
	"fmt"
	"io/ioutil"
	"log"
	"time"

	"github.com/hyperledger/fabric-gateway/pkg/client"
	"github.com/hyperledger/fabric-gateway/pkg/identity"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials"
)

const (
	mspID        = "Org1MSP"
	cryptoPath   = "../bcb-cc-mon/config/ysjg1-p0-u152302"
	certPath     = cryptoPath + "/cert.pem"
	keyPath      = cryptoPath + "/priv_sk"
	tlsCertPath  = cryptoPath + "/ca.crt"
	peerEndpoint = "peer0.org1.example.com:7051"
	gatewayPeer  = "peer0.org1.example.com"
	channelName  = "chl-02"
)

func main() {
	log.Println("============ application-golang starts ============")

	// The gRPC client connection should be shared by all Gateway connections to this endpoint
	clientConnection := newGrpcConnection()
	defer clientConnection.Close()

	id := newIdentity()
	sign := newSign()

	// Create a Gateway connection for a specific client identity
	gateway, err := client.Connect(
		id,quire (
	github.com/hyperledger/fabric-gateway v1.1.0
	github.com/hyperledger/fabric-protos-go-apiv2 v0.0.0-20220615102044-467be1c7b2e7
	google.golang.org/grpc v1.47.0
)
		client.WithSign(sign),
		client.WithClientConnection(clientConnection),
		// Default timeouts for different gRPC calls
		client.WithEvaluateTimeout(5*time.Second),
		client.WithEndorseTimeout(15*time.Second),
		client.WithSubmitTimeout(5*time.Second),
		client.WithCommitStatusTimeout(1*time.Minute),
	)
	if err != nil {
		panic(err)
	}
	defer gateway.Close()

	network := gateway.GetNetwork(channelName)

	ctx, cannel := context.WithCancel(context.Background())
	defer cannel()

	events, err := network.BlockEvents(ctx, client.WithStartBlock(10))
	if err != nil {
		panic(err)
	}
	for event := range events {
		fmt.Printf("---> 区块编号: %v\n", event.GetHeader().GetNumber())
	}
}

// newGrpcConnection creates a gRPC connection to the Gateway server.
func newGrpcConnection() *grpc.ClientConn {
	certificate, err := loadCertificate(tlsCertPath)
	if err != nil {
		panic(err)
	}

	certPool := x509.NewCertPool()
	certPool.AddCert(certificate)
	transportCredentials := credentials.NewClientTLSFromCert(certPool, gatewayPeer)

	connection, err := grpc.Dial(peerEndpoint, grpc.WithTransportCredentials(transportCredentials))
	if err != nil {
		panic(fmt.Errorf("failed to create gRPC connection: %w", err))
	}

	return connection
}

// newIdentity creates a client identity for this Gateway connection using an X.509 certificate.
func newIdentity() *identity.X509Identity {
	certificate, err := loadCertificate(certPath)
	if err != nil {
		panic(err)
	}

	id, err := identity.NewX509Identity(mspID, certificate)
	if err != nil {
		panic(err)
	}

	return id
}

func loadCertificate(filename string) (*x509.Certificate, error) {
	certificatePEM, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, fmt.Errorf("failed to read certificate file: %w", err)
	}
	return identity.CertificateFromPEM(certificatePEM)
}

// newSign creates a function that generates a digital signature from a message digest using a private key.
func newSign() identity.Sign {quire (
	github.com/hyperledger/fabric-gateway v1.1.0
	github.com/hyperledger/fabric-protos-go-apiv2 v0.0.0-20220615102044-467be1c7b2e7
	google.golang.org/grpc v1.47.0
)
	//files, err := ioutil.ReadDir(keyPath)
	//if err != nil {
	//panic(fmt.Errorf("failed to read private key directory: %w", err))
	//}
	//privateKeyPEM, err := ioutil.ReadFile(path.Join(keyPath, files[0].Name()))
	privateKeyPEM, err := ioutil.ReadFile(keyPath)
	if err != nil {
		panic(fmt.Errorf("failed to read private key file: %w", err))
	}

	privateKey, err := identity.PrivateKeyFromPEM(privateKeyPEM)
	if err != nil {
		panic(err)
	}

	sign, err := identity.NewPrivateKeySign(privateKey)
	if err != nil {
		panic(err)
	}

	return sign
}

标签:落块,fabric,nil,panic,err,client,com,监听
From: https://www.cnblogs.com/jiftle/p/16735570.html

相关文章

  • 监听localStorage值
    functiondispatchEventStroage(){constsignSetItem=localStorage.setItem;localStorage.setItem=function(key,val){letsetEvent=newE......
  • uniapp 返回上一页事件监听
    需求点击订单页---订单详情页----点击修改地址---来到地址列表,修改地址--- 成功以后返回订单详情页,并且更新界面数据 首先在订单详情页  设置事件  监听另一界面......
  • fabric compose文件解读(peer篇)
    peer是fabric中的基础单元,主要负责背书,验证交易合法性,保存区块链数据,查询数据。peer与orderer配合完成区块链的全部功能,orderer可以比作是管理员,peer属于是干货的员工,可以......
  • Oracle启动监听报错:The listener supports no services解决
    前几天一台项目开发使用的Oracle11g服务器重启系统之后,出现了几个莫名的报错,下面是其中一个,已解决。$lsnrctlstart报错提示:ThelistenersupportsnoservicesTheco......
  • fabric image 替换图片
    在之前的基础上调整部分代码,实现上传图片后的替换图片,及可以直接上传替换图片首先需要上传一张图片,后续操作的前提都需要canvas中有选中的image对象: 之后可以点击......
  • vue3中watch监听ref reactive响应式数据写法及注意点
    watch函数与vue2中watch配置一致两个小坑监视reactive定义的响应式数据时,oldvalue无法正确获取,强制开启了深度监视(deep配置失败)监视reactive定义的响应式数据中某个......
  • CTO 说了:谁再用 Redis 过期监听实现定时任务,立马滚蛋!
    作者:Finley来源:https://www.cnblogs.com/Finley/p/16395466.html前言日前拜读阿牛老师的大作《领导:谁再用定时任务实现关闭订单,立马滚蛋!》发现其方案有若干瑕疵,特此抛......
  • 监听div高度宽度的变化-自定义指令
    上篇内容说到,iframe嵌入其他项目页面,iframe实现自适应高度需要监听div页面高度的变化使用到了局部自定义指定directives:{//使用局部注册指令的方式resize:{//......
  • fabric compose文件解读(Orderer篇)
    orderer在fabric中的作用是排序,另外orderer有点像是管理节点一样,通道之类的都是在orderer的基础之上建立的,有点像比特币,以太坊上面的全节点一样,不过责任比全节点少很多,甚至......
  • Vue3:监听属性
    监听属性与vue2.x中的watch配置功能一致注意监视reactive定义的响应式数据时,oldvalue无法正确获取,强制开始了深度监视(deep的配置失效)监视reactive定义的响应......