This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 2
/
privdata.go
89 lines (76 loc) · 2.53 KB
/
privdata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package util
import (
"fmt"
"encoding/hex"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/core/ledger"
"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 {
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))
}