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

我正在使用 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

因此,它表示在链码实例化期间未定义集合配置。但是,我不知道如何在链代码实例化请求中添加集合配置。


请建议我解决方案。


aluckdog
浏览 111回答 1
1回答

炎炎设计

我能够通过以下方式创建 CollConfig 请求,并能够将集合配置添加到我的链代码实例化中。我的解决方案#CollConfig 1var collCfg1RequiredPeerCount, collCfg1MaximumPeerCount int32var collCfg1BlockToLive uint64collCfg1Name              := "collectionMedium"collCfg1BlockToLive       = 1000collCfg1RequiredPeerCount = 0collCfg1MaximumPeerCount  = 3collCfg1Policy            := "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 2var collCfg2RequiredPeerCount, collCfg2MaximumPeerCount int32var collCfg2BlockToLive uint64 collCfg2Name              := "collectionPrivate"collCfg2BlockToLive       = 100collCfg2RequiredPeerCount = 0collCfg2MaximumPeerCount  = 3collCfg2Policy            := "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 }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go