63 Star 183 Fork 3

Gitee 极速下载/hyperledger-fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/hyperledger/fabric
克隆/下载
privdata.go 2.81 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package util
import (
"encoding/hex"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/gossip"
"github.com/hyperledger/fabric/protos/ledger/rwset"
"github.com/pkg/errors"
)
// PvtDataCollections data type to encapsulate collections
// of private data
type PvtDataCollections []*ledger.TxPvtData
// Marshal encodes private collection into bytes array
func (pvt *PvtDataCollections) Marshal() ([][]byte, error) {
pvtDataBytes := make([][]byte, 0)
for index, each := range *pvt {
if each == nil {
errMsg := fmt.Sprintf("Mallformed private data payload, rwset index %d is nil", index)
return nil, errors.New(errMsg)
}
pvtBytes, err := proto.Marshal(each.WriteSet)
if err != nil {
errMsg := fmt.Sprintf("Could not marshal private rwset index %d, due to %s", index, err)
return nil, errors.New(errMsg)
}
// Compose gossip protobuf message with private rwset + index of transaction in the block
txSeqInBlock := each.SeqInBlock
pvtDataPayload := &gossip.PvtDataPayload{TxSeqInBlock: txSeqInBlock, Payload: pvtBytes}
payloadBytes, err := proto.Marshal(pvtDataPayload)
if err != nil {
errMsg := fmt.Sprintf("Could not marshal private payload with transaction index %d, due to %s", txSeqInBlock, err)
return nil, errors.New(errMsg)
}
pvtDataBytes = append(pvtDataBytes, payloadBytes)
}
return pvtDataBytes, nil
}
// Unmarshal read and unmarshal collection of private data
// from given bytes array
func (pvt *PvtDataCollections) Unmarshal(data [][]byte) error {
for _, each := range data {
payload := &gossip.PvtDataPayload{}
if err := proto.Unmarshal(each, payload); err != nil {
return err
}
pvtRWSet := &rwset.TxPvtReadWriteSet{}
if err := proto.Unmarshal(payload.Payload, pvtRWSet); err != nil {
return err
}
*pvt = append(*pvt, &ledger.TxPvtData{
SeqInBlock: payload.TxSeqInBlock,
WriteSet: pvtRWSet,
})
}
return nil
}
// PrivateRWSets creates an aggregated slice of RWSets
func PrivateRWSets(rwsets ...PrivateRWSet) [][]byte {
var res [][]byte
for _, rws := range rwsets {
res = append(res, []byte(rws))
}
return res
}
// PrivateRWSet contains the bytes of CollectionPvtReadWriteSet
type PrivateRWSet []byte
// Digest returns a deterministic and collision-free representation of the PrivateRWSet
func (rws PrivateRWSet) Digest() string {
return hex.EncodeToString(util.ComputeSHA256(rws))
}
// PrivateRWSetWithConfig encapsulates private read-write set
// among with relevant to collections config information
type PrivateRWSetWithConfig struct {
RWSet []PrivateRWSet
CollectionConfig *common.CollectionConfig
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mirrors/hyperledger-fabric.git
git@gitee.com:mirrors/hyperledger-fabric.git
mirrors
hyperledger-fabric
hyperledger-fabric
v1.4.1-rc1

搜索帮助