fabric icon indicating copy to clipboard operation
fabric copied to clipboard

How to use 'qscc/GetBlockByHash' in peer CLI and Java gateway?

Open 1164926307 opened this issue 1 year ago • 2 comments

I failed to use qscc/GetBlockByHash on the Java gateway and peer CLI, and I couldn't find any relevant information on the network. But I can use 'blockchain explorer' to retrieve the correct information. {A4ZO2HHSLAH62I failed to use qscc/GetBlockByHash on the Java gateway and peer CLI, and I couldn't find any relevant information on the network. But I can use 'blockchain explorer' to retrieve the correct information.
{KB7V~R H%7TNSORU7JV@V{S9YLQM9V KS@_64QU38IDXP}C65T9 LQ

1164926307 avatar Sep 26 '24 12:09 1164926307

Hi, you cannot send the hash string directly. You need to decode it into bytes representing the hexadecimal, cast it to string and send it. Something like this:

String blockHash = "your_hex_string_here";  // Replace with your actual hex string
byte[] hashBytes = HexFormat.of().parseHex(blockHash);

Let me know whether you are able to get the block by hash that way. I am interested in this because I am running into problems with the same function, as outlined in #5003.

samuelvenzi avatar Oct 02 '24 22:10 samuelvenzi

@samuelvenzi Thank you for your answer. I want to use hash to obtain blocks, but this query is not supported in Java gateway。 I see that there are only evaluateTransaction (String name, String... args) and evaluateTransaction (String name, byte[]... args) in the gateway args)

ca9b9a216213ff5db351954839b91f0

1164926307 avatar Oct 12 '24 09:10 1164926307

To use mixed String and byte[] arguments in the Java client API, you might want to use newProposal instead of evaluateTransaction. For example:

var result = contract.newProposal(transactionName)
        .addArguments(stringArg)
        .addArguments(bytesArg)
        .build()
        .evaluate();

The addArguments method has overloads that accept either String or byte[] arguments. You can call this multiple times and arguments are added in the order you make the calls. See the Contract API documentation for details and an example usage at the top of the page.

In reality, all String arguments are converted into byte[] by the client when transmitted to Fabric, so you could do this translation yourself and use the byte[] overload of evaluateTransaction. For example:

var result = contract.evaluateTransaction(transactionName, stringArg.getBytes(StandardCharsets.UTF_8), bytesArg);

bestbeforetoday avatar Nov 03 '24 13:11 bestbeforetoday

@bestbeforetoday Thank you for your answer. I have tried this method before, but it may have been a coding error,It was the right thing to do and solved my problem

1164926307 avatar Nov 03 '24 15:11 1164926307

Good to hear you have it working now. I’ll close the issue.

bestbeforetoday avatar Nov 04 '24 07:11 bestbeforetoday