I was trying to listen block level events in Golang. I am not able to receive blocklevel events at all. It is just showing block 0 always and not moving ahead. I have given the sample code. Please let me know if there is any problem with the code or any other setting to be performed at network level. One more observation is if the code is written in nodejs than I am able to listen block level event but not in Golang.
Please let me know if any solution is available. We are using hyperledgr fabric 2.4
func startBlocklevelEventListening(ctx context.Context, network client.Network) {
blockEvents, blockErr := network.BlockEvents(ctx, client.WithStartBlock(1))
if blockErr != nil {
panic(fmt.Errorf("failed to start chaincode event listening: %w", blockErr))
}
fmt.Println("\n** Start Block event listening")
go func() {
for event := range blockEvents {
hashBytes := event.GetHeader().GetDataHash()
hashString := fmt.Sprintf("%x", hashBytes)
blockNumber := event.GetHeader().GetNumber()
fmt.Printf("\n<-- Block event received: \n Received block number : %d \n Received block hash - %s\n", blockNumber, hashString)
}
}()
I tried this out using Fabric v2.5 and fabric-gateway v1.4, and I am able to receive block events. I made the following modifications to the existing asset-transfer-events/application-gateway-go sample:
Modified the deleteAsset function so that it returned the block number in which the transaction is committed (similar to the existing createAsset function).
Added a replayBlockEvents function, similar to the existing replayChaincodeEvents function.
Modified the last part of the main function to add replay of block events.
Running this application produced the usual console output, plus this additional output from the replayBlockEvents function, clearly indicating that block eventing is working:
Using a simpler for-range loop in the replayBlockEvents function also worked.