event包支持訪問Fabric網(wǎng)絡(luò)上的通道事件。事件客戶端可以接收區(qū)塊事件,過濾區(qū)塊事件,鏈碼事件和交易狀態(tài)事件。
官方文檔:
https://godoc.org/github.com/hyperledger/fabric-sdk-go/pkg/client/event
目前創(chuàng)新互聯(lián)已為上1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站托管、服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計、果洛州網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
event使用基本流程如下:
A、準備通道客戶端上下文
B、創(chuàng)建事件客戶端
C、注冊事件
D、處理事件(或超時)
E、注銷事件
使用示例:
ec, err := New(mockChannelProvider("mychannel"))
if err != nil {
fmt.Println("failed to create client")
}
registration, notifier, err := ec.RegisterChaincodeEvent("examplecc", "event123")
if err != nil {
fmt.Println("failed to register chaincode event")
}
defer ec.Unregister(registration)
select {
case ccEvent := <-notifier:
fmt.Printf("received chaincode event %v\n", ccEvent)
case <-time.After(time.Second * 5):
fmt.Println("timeout while waiting for chaincode event")
}
// Timeout is expected since there is no event producer
// output:
// timeout while waiting for chaincode event
type Client struct {
eventService fab.EventService
permitBlockEvents bool
fromBlock uint64
seekType seek.Type
}
func New(channelProvider context.ChannelProvider, opts ...ClientOption) (*Client, error)
返回事件客戶端實例??蛻舳私邮諈^(qū)塊事件,過濾區(qū)塊事件,鏈碼事件和交易狀態(tài)事件。
使用示例:
ctx := mockChannelProvider("mychannel")
ec, err := New(ctx, WithBlockEvents())
if err != nil {
fmt.Println(err)
}
if ec != nil {
fmt.Println("event client created")
} else {
fmt.Println("event client is nil")
}
// output:
// event client created
func (c *Client) RegisterBlockEvent(filter ...fab.BlockFilter) (fab.Registration, <-chan *fab.BlockEvent, error)
注冊塊事件。如果調(diào)用者沒有注冊區(qū)塊事件的權(quán)限,則返回錯誤。不再需要注冊時,必須調(diào)用取消注冊。
參數(shù):
filter是一個可選的過濾器,可過濾掉不需要的事件。只能指定一個過濾器。
返回:注冊對象和用于接收事件的通道。調(diào)用Unregister時,通道將關(guān)閉。
使用示例:
ec, err := New(mockChannelProvider("mychannel"), WithBlockEvents())
if err != nil {
fmt.Println("failed to create client")
}
registration, _, err := ec.RegisterBlockEvent()
if err != nil {
fmt.Println("failed to register block event")
}
defer ec.Unregister(registration)
fmt.Println("block event registered successfully")
// output:
// block event registered successfully
func (c *Client) RegisterChaincodeEvent(ccID, eventFilter string) (fab.Registration, <-chan *fab.CCEvent, error)
注冊鏈代碼事件。不再需要注冊時,必須調(diào)用Unregister。
參數(shù):
ccID是用于接收事件的鏈碼ID
eventFilter是用于接收事件的鏈碼事件過濾器(正則表達式)
返回:注冊對象和用于接收事件的通道。 調(diào)用取消注冊時,通道將關(guān)閉。
使用示例:
ec, err := New(mockChannelProvider("mychannel"))
if err != nil {
fmt.Println("failed to create client")
}
registration, _, err := ec.RegisterChaincodeEvent("examplecc", "event123")
if err != nil {
fmt.Println("failed to register chaincode event")
}
defer ec.Unregister(registration)
fmt.Println("chaincode event registered successfully")
// output:
// chaincode event registered successfully
使用示例:
// If you require payload for chaincode events you have to use WithBlockEvents() option
ec, err := New(mockChannelProvider("mychannel"), WithBlockEvents())
if err != nil {
fmt.Println("failed to create client")
}
registration, _, err := ec.RegisterChaincodeEvent("examplecc", "event123")
if err != nil {
fmt.Println("failed to register chaincode event")
}
defer ec.Unregister(registration)
fmt.Println("chaincode event registered successfully")
// output:
// chaincode event registered successfully
func (c *Client) RegisterFilteredBlockEvent() (fab.Registration, <-chan *fab.FilteredBlockEvent, error)
注冊過濾區(qū)塊事件。當(dāng)不再需要注冊時,必須調(diào)用Unregister。
返回:注冊對象和用于接收事件的通道。調(diào)用Unregister時,通道將關(guān)閉。
使用示例:
ec, err := New(mockChannelProvider("mychannel"))
if err != nil {
fmt.Println("failed to create client")
}
registration, _, err := ec.RegisterFilteredBlockEvent()
if err != nil {
fmt.Println("failed to register filtered block event")
}
defer ec.Unregister(registration)
fmt.Println("filtered block event registered successfully")
// output:
// filtered block event registered successfully
func (c *Client) RegisterTxStatusEvent(txID string) (fab.Registration, <-chan *fab.TxStatusEvent, error)
注冊交易狀態(tài)事件。不再需要注冊時,必須調(diào)用Unregister。
參數(shù):
txID是用于接收事件的交易ID
返回:注冊對象和用于接收事件的通道。調(diào)用Unregister時,通道將關(guān)閉。
使用示例:
ec, err := New(mockChannelProvider("mychannel"))
if err != nil {
fmt.Println("failed to create client")
}
registration, _, err := ec.RegisterTxStatusEvent("tx123")
if err != nil {
fmt.Println("failed to register tx status event")
}
defer ec.Unregister(registration)
fmt.Println("tx status event registered successfully")
// output:
// tx status event registered successfully
func (c *Client) Unregister(reg fab.Registration)
刪除給定的注冊對象并關(guān)閉事件通道。
參數(shù):
reg是從其中一個注冊函數(shù)返回的注冊句柄
type ClientOption func(*Client) error
func WithBlockEvents() ClientOption
WithBlockEvents指示要接收的區(qū)塊事件。 注意,調(diào)用者必須具有此選項的足夠權(quán)限。func WithBlockNum(from uint64) ClientOption
WithBlockNum表示用于接收事件的區(qū)塊編號。只有deliverclient支持次選項。
func WithSeekType(seek seek.Type) ClientOption
WithSeekType表示所需的搜索類型,最新,最舊或來自給定區(qū)塊,只有deliverclient支持此項。
var (
sdk *fabsdk.FabricSDK
channelName = "assetchannel"
org = "org1"
user = "Admin"
)
ctx := sdk.ChannelContext(channelName, fabsdk.WithOrg(org), fabsdk.WithUser(user))
cli, err := event.New(ctx)
if err != nil {
panic(err)
}
// 區(qū)塊事件
reg, blkevent, err := cli.RegisterBlockEvent()
if err != nil {
panic(err)
}
defer cli.Unregister(reg)
timeoutctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
for {
select {
case evt := <-blkevent:
fmt.Printf("received a block", evt)
case <-timeoutctx.Done():
fmt.Println("event timeout, exit!")
return
}