6

I used to set up a Fabric network and deployed a fabric network and basic application using a Fabric and Fabric GoLang SDK. I'm able to do the query and write to the chain. Is there any way to retrieve the Block Info? Like block height and current hash?

+ I'm unable to find out a documentation for GoLang Fabric SDK.

I followed following code and tutorial,

Fabric Basic App - Tutorial https://chainhero.io/2017/07/tutorial-build-blockchain-app/

Fabric Basic App using GoLang SDK - Code https://github.com/chainHero/heroes-service/

GoLang SDK - Official SDK https://github.com/hyperledger/fabric-sdk-go

Marking
  • 184
  • 1
  • 2
  • 16
Tittu Vaghese
  • 233
  • 3
  • 13

3 Answers3

7

In general, the sdk will provide the basic method such you said GetBlockInfo,I have search for the GoLang SDK, it can not be found. While Java sdk provide this such method reference this java test .

Another way to use these method(you must know a little fabric source code), in fact these method are included in the system chaincode, you could invoke the system chancode just like you invoke the normal chaincode.

A example is following: from the go sdk test ,you could see this,

response, err := chClient.Query(chclient.Request{ChaincodeID: ccID, Fcn: "invoke", Args: integration.ExampleCCQueryArgs()})

just change the params

response, err := chClient.Query(chclient.Request{ChaincodeID: "qscc", Fcn: "invoke", Args: integration.ExampleCCQueryArgs("GetChainInfo")})

qscc is a system chancode,you could download the fabric source code,and from qscc file,you could see(it provide many invoke service):

GetChainInfo       string = "GetChainInfo"
GetBlockByNumber   string = "GetBlockByNumber"
GetBlockByHash     string = "GetBlockByHash"
GetTransactionByID string = "GetTransactionByID"
GetBlockByTxID     string = "GetBlockByTxID"
Jim Green
  • 1,088
  • 3
  • 15
  • 40
  • Hello Jim, Thanks for this answer. It's still helpful. I have a question that Is there any alternative available now to get the data using TxnID in `fabric-sdk-go`? For example, I have stored `A` in the ledger and got `TxnHash B` and now I want to query the ledger using `TxnHash B` to fetch the stored data. I came across `ledger.QueryTransaction()` but it requires a good parsing logic in order to get the details from it. – metadata Dec 12 '19 at 17:16
1

Go sdk(fabric-sdk-go/pkg/client/ledger) provides several methods for getting information about blockchain. For example:

...
client, err := ledger.New(channelContext)
block, err := client.QueryBlockByHash(blockHash)
block, err = client.QueryBlock(blockNumber)
Chernilin
  • 33
  • 8
-2

The Go SDK now includes methods for querying block information. These methods are contained within the ledger client package.

You can see an example in the ledger client integration test.

Paul O'Mahony
  • 6,740
  • 1
  • 10
  • 15