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");
}
});
I don't immediately see anything wrong with the way you have invoked the GetBlockByTxID on the qscc chaincode, but it does look like you have included the surrounding quotes in the transaction ID you supply. The error message generated by the qscc chaincode implementation does not add surrounding quotes to the transaction ID yet your error message does contain surrounding quotes.
"95841fc8ba7c49bf333430e0fd9ed052a97eb9910610bfd22faa05802c2d2cc5"
Is not a valid transaction ID. 95841fc8ba7c49bf333430e0fd9ed052a97eb9910610bfd22faa05802c2d2cc5
is a valid transaction ID.
I modified the logic of the asset-transfer-basic sample code to lookup the block for a transaction that I have just successfully submitted, as follows:
const network = gateway.getNetwork(channelName);
const contract = network.getContract(chaincodeName);
const commit = await contract.submitAsync('CreateAsset', {
arguments: [assetId, 'yellow', '5', 'Tom', '1300'],
});
const status = await commit.getStatus();
if (!status.successful) {
throw new Error(
`Transaction ${
status.transactionId
} failed to commit with status code ${String(status.code)}`
);
}
const blockBytes = await network
.getContract('qscc')
.evaluateTransaction(
'GetBlockByTxID',
network.getName(),
status.transactionId
);
console.log(
`Got block for transaction ID ${
status.transactionId
} with length ${String(blockBytes.length)}`
);
This runs successfully and produces the expected output:
Got block for transaction ID d4b5126a5b99a0da432101a7ee86e6877639cf1bcccc429d414cb85fa8cf2345 with length 4503
I used Fabric v3.0.0 for my testing, but v2.5.x should behave in exactly the same way.
For reference, relevant dependency versions are:
asset-transfer-basic@1.0.0 fabric-samples/asset-transfer-basic/application-gateway-typescript
├── @grpc/grpc-js@1.12.5
└── @hyperledger/fabric-gateway@1.7.1
└── @hyperledger/fabric-protos@0.3.4