为了账号安全,请及时绑定邮箱和手机立即绑定

如何在Fabric SDK Go中添加集合配置?

如何在Fabric SDK Go中添加集合配置?

Go
aluckdog 2023-07-04 10:03:22
我正在使用 Fabric SDK Go 将私有数据添加到 Hyperledger 中,但在调用数据时出现错误。实例化链码ccPolicy, err := cauthdsl.FromString("AND ('Org1MSP.member','Org2MSP.member')")resMgmt.InstantiateCC(    setup.Org.ChannelID,    resmgmt.InstantiateCCRequest{        Name:    chaincodeId,        Path:    setup.Org.ChaincodePath,        Version: chaincodeVersion,        Args:    [][]byte{[]byte("init")},        Policy:  ccPolicy,    },resmgmt.WithRetry(retry.DefaultResMgmtOpts))集合配置.json[   {       "name": "collectionMedium",       "policy": "AND ('Org1MSP.member', 'Org2MSP.member')",       "requiredPeerCount": 0,       "maxPeerCount": 3,       "blockToLive":1000000   },   {       "name": "collectionPrivate",       "policy": "OR('Org2MSP.member')",       "requiredPeerCount": 0,       "maxPeerCount": 3,       "blockToLive":5   }]调用product := &model.Product{id, name, color, length, width}productBytes, err1 := json.Marshal(product)if err1 != nil {    return shim.Error(err1.Error())}   err2 := stub.PutPrivateData("collectionMedium", id, productBytes)if err2 != nil {    return shim.Error(err2.Error())}错误Chaincode status Code: (500) UNKNOWN. Description: PUT_STATE failed: collection config not defined for chaincode [CC_ORG_V00], pass the collection configuration upon chaincode definition/instantiation因此,它表示在链码实例化期间未定义集合配置。但是,我不知道如何在链代码实例化请求中添加集合配置。请建议我解决方案。
查看完整描述

1 回答

?
炎炎设计

TA贡献1808条经验 获得超4个赞

我能够通过以下方式创建 CollConfig 请求,并能够将集合配置添加到我的链代码实例化中。


我的解决方案


#CollConfig 1


var collCfg1RequiredPeerCount, collCfg1MaximumPeerCount int32

var collCfg1BlockToLive uint64


collCfg1Name              := "collectionMedium"

collCfg1BlockToLive       = 1000

collCfg1RequiredPeerCount = 0

collCfg1MaximumPeerCount  = 3

collCfg1Policy            := "OR('Org1MSP.member','Org2MSP.member')"


collCfg1, err := newCollectionConfig(collCfg1Name,collCfg1Policy, collCfg1RequiredPeerCount,collCfg1MaximumPeerCount,collCfg1BlockToLive)


if err != nil {

    return errors.WithMessage(err, "failed to create collection config 1")

}

#CollConfig 2


var collCfg2RequiredPeerCount, collCfg2MaximumPeerCount int32

var collCfg2BlockToLive uint64 


collCfg2Name              := "collectionPrivate"

collCfg2BlockToLive       = 100

collCfg2RequiredPeerCount = 0

collCfg2MaximumPeerCount  = 3

collCfg2Policy            := "OR('Org2MSP.member')"


collCfg2, err := newCollectionConfig(collCfg2Name,collCfg2Policy, collCfg2RequiredPeerCount,collCfg2MaximumPeerCount,collCfg2BlockToLive)


if err != nil {

    return errors.WithMessage(err, "failed to create collection config 1")

}

# 实例化链码


 cfg := []*cb.CollectionConfig{collCfg1,collCfg2}


resp, err := resMgmt.InstantiateCC(

    setup.Org.ChannelID,

    resmgmt.InstantiateCCRequest{


        Name:    chaincodeId,

        Path:    setup.Org.ChaincodePath,

        Version: chaincodeVersion,

        Args:    [][]byte{[]byte("init")},

        Policy:  ccPolicy,

        CollConfig: cfg,


},resmgmt.WithRetry(retry.DefaultResMgmtOpts))

#CollConfig 创建请求方法


func newCollectionConfig(colName, policy string, reqPeerCount, maxPeerCount int32, 


blockToLive uint64) (*cb.CollectionConfig, error) {

    p, err := cauthdsl.FromString(policy)

    if err != nil {

        return nil, err

    }

    cpc := &cb.CollectionPolicyConfig{

        Payload: &cb.CollectionPolicyConfig_SignaturePolicy{

            SignaturePolicy: p,

        },

    }

    return &cb.CollectionConfig{

        Payload: &cb.CollectionConfig_StaticCollectionConfig{

            StaticCollectionConfig: &cb.StaticCollectionConfig{

                Name:              colName,

                MemberOrgsPolicy:  cpc,

                RequiredPeerCount: reqPeerCount,

                MaximumPeerCount:  maxPeerCount,

                BlockToLive:       blockToLive,

            },

        },

    }, nil }


查看完整回答
反对 回复 2023-07-04
  • 1 回答
  • 0 关注
  • 110 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信