首页 > 解决方案 > 不同的超级账本结构链码可以查看世界状态中的所有键/值对吗?

问题描述

我目前在同一个频道上提交了两个智能合约定义。两者都是用 Go 编写的,有些基础,只做基本的 CRUD 操作。但是,我注意到用一个链码编写的键/值对对另一个链码不可用。

因此,go-audit我创建了以下记录:

在此处输入图像描述

但是后来,我尝试ping使用 chaincode对 key 执行 get 操作go-asset,但没有找到以下错误(由 chaincode 返回)

bad request: failed to invoke go-asset: Error: No valid responses from any peers. Errors: **someurl***, status=500, message=the asset ping does not exist.

这是这样的交易:

func (c *GoAssetContract) ReadGoAsset(ctx contractapi.TransactionContextInterface, goAssetID string) (*GoAsset, error) {

    exists, err := c.GoAssetExists(ctx, hlpAssetID)
    if err != nil {
        return nil, fmt.Errorf("could not read from world state. %s", err)
    } else if !exists {
        return nil, fmt.Errorf("the asset %s does not exist", goAssetID)
    }

    bytes, _ := ctx.GetStub().GetState(goAssetID)

    goAsset := new(GoAsset)

    err = json.Unmarshal(bytes, goAsset)

    if err != nil {
        return nil, fmt.Errorf("could not unmarshal world state data to type GoAsset")
    }

    return GoAsset, nil
}

和 GoAsset 结构

// GoAsset stores a value
type GoAsset struct {
    Value string `json:"value"`
}

世界状态不应该对通道上批准/提交的所有链码可用吗?

标签: hyperledger-fabrichyperledger-chaincode

解决方案


部署到同一通道的链代码是命名空间的,因此它们的键仍然特定于使用它们的链代码。因此,您看到部署到同一通道的 2 个链代码按设计工作,它们无法看到彼此的密钥。

然而,一个链码可以由多个不同的合约组成,在这种情况下,这些合约可以访问彼此的密钥,因为它们仍然在同一个链码部署中。


推荐阅读