fabric-samples
fabric-samples copied to clipboard
Blocklevel events are not generated in Golang
Hi
I was trying to use off_chain_data Repository and trying to listen block level events in Golang. Sorry to inform you that 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 provide the fix for the above mentioned problem.
Sample function 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)
}
}()
}
See this StackOverflow answer to the same question, demonstrating block eventing working with the Golang client application:
https://stackoverflow.com/a/77835368/3680198