我正在尝试从我的频道获取 Hyperledger Fabric 网络上的区块编号和区块哈希,但我能够使用具有相同交易 ID 的 cli 获取详细信息,当我尝试使用最新的 Fabric 网关时,我得到以下信息错误
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
网关的具体版本是
"@hyperledger/fabric-gateway": "^1.7.1",
"@hyperledger/fabric-protos": "^0.2.2",
以下代码适用于 cli
peer chaincode query -C mychannel -n qscc -c '{"Args":["GetBlockByTxID","mychannel","95841fc8ba7c49bf333430e0fd9ed052a97eb9910610bfd22faa05802c2d2cc5"]}'
我正在使用fabric示例网络并修改资产转移基本网关代码。
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");
}
});
我没有立即发现您在 qscc 链码上调用 GetBlockByTxID 的方式有任何问题,因此我必须假设账本状态或对该状态的访问存在问题。也许自从您成功使用 CLI 以来状态已被清除,或者您正在查看错误的交易分类帐(通道)。
我修改了asset-transfer-basic示例代码的逻辑,以查找我刚刚成功提交的交易的块,如下:
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)}`
);
这会成功运行并产生预期的输出:
Got block for transaction ID d4b5126a5b99a0da432101a7ee86e6877639cf1bcccc429d414cb85fa8cf2345 with length 4503
我使用 Fabric v3.0.0 进行测试,但 v2.5.x 的行为应该完全相同。
供参考,相关依赖版本为:
[email protected] fabric-samples/asset-transfer-basic/application-gateway-typescript
├── @grpc/[email protected]
└── @hyperledger/[email protected]
└── @hyperledger/[email protected]