You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hyperledger Fabric 2.5中连续区块Previous Hash与Data Hash不匹配问题求助

Hyperledger Fabric 2.5中连续区块Previous Hash与Data Hash不匹配问题求助

我现在在Fabric 2.5测试网络里,用新版Fabric Gateway API做区块查询:按道理后一个区块的Previous Hash应该和前一个区块的Data Hash完全一致,但实际通过API获取的连续区块信息里,两者完全对不上。比如下面是两次交易后返回的区块数据:

{
    "Block Number": "10",
    "Previous Hash": "KSDy3jiB5Y15AT3W4PRqQWUTOarT50oKE8S100na0Ys=",
    "Data Hash": "VBZBIkTTggb23aBuC2xs7+bfCk1EYeKhMPfvQNCL0ZI="
}

{
    "Block Number": "11",
    "Previous Hash": "Zf8YhOJlgTcrWbqm+8up4kGcrNffHMgDFnF++gnI3Dk=",
    "Data Hash": "hY1pO7ljvlsNklmZBKksmTFreLRKNRX18dbvFk+5Y1g="
}

可以看到区块11的Previous Hash和区块10的Data Hash完全没有关联。

之后我用Fabric CLI直接查询通道信息:

peer channel getinfo -c mychannel

得到的结果里,后一个区块的Previous Hash和前一个区块的currentBlockHash是匹配的,但API返回的区块Data Hash和CLI里的currentBlockHash完全不同,比如CLI输出:

2024-12-27 11:43:47.409 IST 0001 INFO [channelCmd] InitCmdFactory -> Endorser and orderer connections initialized
Blockchain info: {"height":11,"currentBlockHash":"Zf8YhOJlgTcrWbqm+8up4kGcrNffHMgDFnF++gnI3Dk=","previousBlockHash":"KSDy3jiB5Y15AT3W4PRqQWUTOarT50oKE8S100na0Ys="}

2024-12-27 11:49:19.022 IST 0001 INFO [channelCmd] InitCmdFactory -> Endorser and orderer connections initialized
Blockchain info: {"height":12,"currentBlockHash":"1lbnSQSEWoJok327h6me1b7a5vgSdY8TGXA/uRUQDYo=","previousBlockHash":"Zf8YhOJlgTcrWbqm+8up4kGcrNffHMgDFnF++gnI3Dk="}

比如区块11的currentBlockHash是Zf8YhOJlgTcrWbqm+8up4kGcrNffHMgDFnF++gnI3Dk=,这个值和API返回的区块11的Previous Hash一致,但和API返回的区块10的Data Hash完全不匹配。

我使用的SDK和依赖版本为:

"@hyperledger/fabric-gateway": "^1.7.1"
"@hyperledger/fabric-protos": "^0.2.2"

代码是基于Fabric官方示例网络的Asset Transfer Basic Gateway代码修改的,区块查询的接口实现如下:

app.get("/getBlockByNumber/:blockNumber", async (req, res) => {
  try {
    let { blockNumber } = req.params;
    blockNumber = blockNumber.replace(/"/g, "");
    blockNumber = blockNumber.replace(/\s+/g, " ").trim();

    const network = gateway.getNetwork("mychannel");
    const blockBytes = await network
      .getContract("qscc")
      .evaluateTransaction("GetBlockByNumber", network.getName(), blockNumber);

    console.log(
      `Got block for block number ${blockNumber} with length ${String(
        blockBytes.length
      )}`
    );
    const protos = require("fabric-protos");

    // Deserialize the block
    const block = protos.common.Block.decode(blockBytes);

    // Access block details
    const data = {
      "Block Number": block.header.number.toString(),
      "Previous Hash": Buffer.from(block.header.previous_hash).toString(
        "base64"
      ),
      "Data Hash": Buffer.from(block.header.data_hash).toString("base64"),
    };


    res.send(data);
  } catch (error) {
    console.error("Error in getBlockByNumber:", error);
    res.status(500).send("Failed to fetch block by block number");
  }
});

有没有大佬能帮我排查下问题?为什么API返回的Data Hash和实际的区块哈希(CLI中的currentBlockHash)对不上,导致区块链的哈希关联断裂了?

备注:内容来源于stack exchange,提问作者Rithick Guru

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 17:14:34