代码拉取完成,页面将自动刷新
同步操作将从 huikunliu/pier-client-fabric 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package main
import (
"fmt"
"path/filepath"
"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
"github.com/hyperledger/fabric-sdk-go/pkg/client/event"
"github.com/hyperledger/fabric-sdk-go/pkg/client/ledger"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
)
type MessageHandler interface {
HandleMessage(deliveries *fab.CCEvent, payload []byte)
}
type Consumer struct {
eventClient *event.Client
meta *ContractMeta
msgH MessageHandler
channelProvider context.ChannelProvider
ChannelClient *channel.Client
registration fab.Registration
ctx chan bool
}
func NewConsumer(configPath string, meta *ContractMeta, msgH MessageHandler, ctx chan bool) (*Consumer, error) {
configProvider := config.FromFile(filepath.Join(configPath, "config.yaml"))
sdk, err := fabsdk.New(configProvider)
if err != nil {
return nil, fmt.Errorf("create sdk fail: %s\n", err)
}
channelProvider := sdk.ChannelContext(meta.ChannelID, fabsdk.WithUser(meta.Username), fabsdk.WithOrg(meta.ORG))
channelClient, err := channel.New(channelProvider)
if err != nil {
return nil, fmt.Errorf("create channel fabcli fail: %s\n", err.Error())
}
c := &Consumer{
msgH: msgH,
ChannelClient: channelClient,
channelProvider: channelProvider,
meta: meta,
ctx: ctx,
}
return c, nil
}
func (c *Consumer) Start() error {
var err error
ec, err := event.New(c.channelProvider, event.WithBlockEvents())
if err != nil {
return fmt.Errorf("failed to create fabcli, error: %v", err)
}
c.eventClient = ec
registration, notifier, err := ec.RegisterChaincodeEvent(c.meta.CCID, c.meta.EventFilter)
if err != nil {
return fmt.Errorf("failed to register chaincode event, error: %v", err)
}
c.registration = registration
// todo: add context
go func() {
for {
select {
case ccEvent := <-notifier:
if ccEvent != nil {
c.handle(ccEvent)
}
case <-c.ctx:
return
}
}
}()
return nil
}
func (c *Consumer) Shutdown() error {
c.eventClient.Unregister(c.registration)
return nil
}
func (c *Consumer) handle(deliveries *fab.CCEvent) {
l, err := ledger.New(c.channelProvider)
if err != nil {
return
}
t, err := l.QueryTransaction(fab.TransactionID(deliveries.TxID))
if err != nil {
return
}
pd := &common.Payload{}
if err := proto.Unmarshal(t.TransactionEnvelope.Payload, pd); err != nil {
return
}
pt := &peer.Transaction{}
if err := proto.Unmarshal(pd.Data, pt); err != nil {
return
}
c.msgH.HandleMessage(deliveries, pt.Actions[0].Payload)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。