I am trying to get the block number and the block hash on Hyperledger fabric network from my channel, but i was able to get the details using cli with the same transaction ID, when i try with the latest fabric-gateway i get the following error
cause: Error: 2 UNKNOWN: evaluate call to endorser returned error: chaincode response 500, Failed to get block for txID "95841fc8ba7c49bf333430e0fd9ed052a97eb9910610bfd22faa05802c2d2cc5"
, error no such transaction ID ["95841fc8ba7c49bf333430e0fd9ed052a97eb9910610bfd22faa05802c2d2cc5"
] in index
The exact version of gateway are
"@hyperledger/fabric-gateway": "^1.7.1",
"@hyperledger/fabric-protos": "^0.2.2",
The below code works for cli
peer chaincode query -C mychannel -n qscc -c '{"Args":["GetBlockByTxID","mychannel","95841fc8ba7c49bf333430e0fd9ed052a97eb9910610bfd22faa05802c2d2cc5"]}'
Iam using the fabric sample network and modifying the asset transfer basic gateway code.
app.get("/getBlockByTxId/:txId", async (req, res) => {
try {
const { txId } = req.params;
const network = gateway.getNetwork(channelName);
const contract = network.getContract("qscc");
// Use `GetBlockByTxID` to fetch the block containing the transaction
console.log("NETWORKKK", network.getName());
console.log(
"Methods:",
Object.getOwnPropertyNames(Object.getPrototypeOf(network))
);
const result = await contract.evaluateTransaction(
"GetBlockByTxID",
network.getName(),
txId
);
// Parse the result as a block
const block = Block.decode(result); // Decoding block using Hyperledger protobuf
res.send(block);
} catch (error) {
console.error("Error in getBlockByTxId:", error);
res.status(500).send("Failed to fetch block by transaction ID");
}
});