1 Star 0 Fork 0

妥協/fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
util.go 3.53 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package tests
import (
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/cauthdsl"
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
"github.com/hyperledger/fabric/common/flogging"
lutils "github.com/hyperledger/fabric/core/ledger/util"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/ledger/rwset"
protopeer "github.com/hyperledger/fabric/protos/peer"
prototestutils "github.com/hyperledger/fabric/protos/testutils"
"github.com/hyperledger/fabric/protos/utils"
)
var logger = flogging.MustGetLogger("test2")
// collConf helps writing tests with less verbose code by specifying coll configuration
// in a simple struct in place of 'common.CollectionConfigPackage'. (the test heplers' apis
// use 'collConf' as parameters and return values and transform back and forth to/from proto
// message internally (using func 'convertToCollConfigProtoBytes' and 'convertFromCollConfigProto')
type collConf struct {
name string
btl uint64
}
type txAndPvtdata struct {
Txid string
Envelope *common.Envelope
Pvtws *rwset.TxPvtReadWriteSet
}
func convertToCollConfigProtoBytes(collConfs []*collConf) ([]byte, error) {
var protoConfArray []*common.CollectionConfig
for _, c := range collConfs {
protoConf := &common.CollectionConfig{Payload: &common.CollectionConfig_StaticCollectionConfig{
StaticCollectionConfig: &common.StaticCollectionConfig{
Name: c.name,
BlockToLive: c.btl,
MemberOrgsPolicy: getAccessPolicy([]string{"peer0", "peer1"})}}}
protoConfArray = append(protoConfArray, protoConf)
}
return proto.Marshal(&common.CollectionConfigPackage{Config: protoConfArray})
}
func getAccessPolicy(signers []string) *common.CollectionPolicyConfig {
var data [][]byte
for _, signer := range signers {
data = append(data, []byte(signer))
}
policyEnvelope := cauthdsl.Envelope(cauthdsl.Or(cauthdsl.SignedBy(0), cauthdsl.SignedBy(1)), data)
return createCollectionPolicyConfig(policyEnvelope)
}
func createCollectionPolicyConfig(accessPolicy *common.SignaturePolicyEnvelope) *common.CollectionPolicyConfig {
cpcSp := &common.CollectionPolicyConfig_SignaturePolicy{
SignaturePolicy: accessPolicy,
}
cpc := &common.CollectionPolicyConfig{
Payload: cpcSp,
}
return cpc
}
func convertFromCollConfigProto(collConfPkg *common.CollectionConfigPackage) []*collConf {
var collConfs []*collConf
protoConfArray := collConfPkg.Config
for _, protoConf := range protoConfArray {
name, btl := protoConf.GetStaticCollectionConfig().Name, protoConf.GetStaticCollectionConfig().BlockToLive
collConfs = append(collConfs, &collConf{name, btl})
}
return collConfs
}
func constructTransaction(txid string, simulationResults []byte) (*common.Envelope, error) {
channelid := "dummyChannel"
ccid := &protopeer.ChaincodeID{
Name: "dummyCC",
Version: "dummyVer",
}
txenv, _, err := prototestutils.ConstructUnsignedTxEnv(channelid, ccid, &protopeer.Response{Status: 200}, simulationResults, txid, nil, nil)
return txenv, err
}
func constructTestGenesisBlock(channelid string) (*common.Block, error) {
blk, err := configtxtest.MakeGenesisBlock(channelid)
if err != nil {
return nil, err
}
setBlockFlagsToValid(blk)
return blk, nil
}
func setBlockFlagsToValid(block *common.Block) {
utils.InitBlockMetadata(block)
block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER] =
lutils.NewTxValidationFlagsSetValue(len(block.Data.Data), protopeer.TxValidationCode_VALID)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/liurenhao/fabric.git
git@gitee.com:liurenhao/fabric.git
liurenhao
fabric
fabric
v1.4.0-rc1

搜索帮助