• fabric监听落块事件


    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
    }
    
  • 相关阅读:
    jQuery+ThinkPHP+Ajax实现即时消息提醒功能
    依赖注入(DI)
    控制反转(IoC)
    ajax以base64上传图片到django
    python 多进程、多线程、协程
    python3 实现tcp/udp局域网通信
    同源策略与跨域访问
    nginx+uwsgi阿里云ubuntu服务器上简单部署flask RESTful风格项目
    python3 实现 websocket server 解决中文乱码
    python IO 多路复用 epoll简单模型
  • 原文地址:https://www.cnblogs.com/jiftle/p/16735570.html
Copyright © 2020-2023  润新知