forked from hyperledger/fabric
/
types.go
112 lines (101 loc) · 3.29 KB
/
types.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package validation
import (
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/core/ledger/internal/version"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/privacyenabledstate"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/rwsetutil"
)
// block is used to used to hold the information from its proto format to a structure
// that is more suitable/friendly for validation
type block struct {
num uint64
txs []*transaction
}
// transaction is used to hold the information from its proto format to a structure
// that is more suitable/friendly for validation
type transaction struct {
indexInBlock int
id string
rwset *rwsetutil.TxRwSet
validationCode peer.TxValidationCode
containsPostOrderWrites bool
}
// publicAndHashUpdates encapsulates public and hash updates. The intended use of this to hold the updates
// that are to be applied to the statedb as a result of the block commit
type publicAndHashUpdates struct {
publicUpdates *privacyenabledstate.PubUpdateBatch
hashUpdates *privacyenabledstate.HashedUpdateBatch
}
// newPubAndHashUpdates constructs an empty PubAndHashUpdates
func newPubAndHashUpdates() *publicAndHashUpdates {
return &publicAndHashUpdates{
privacyenabledstate.NewPubUpdateBatch(),
privacyenabledstate.NewHashedUpdateBatch(),
}
}
// containsPvtWrites returns true if this transaction is not limited to affecting the public data only
func (t *transaction) containsPvtWrites() bool {
for _, ns := range t.rwset.NsRwSets {
for _, coll := range ns.CollHashedRwSets {
if coll.PvtRwSetHash != nil {
return true
}
}
}
return false
}
// retrieveHash returns the hash of the private write-set present
// in the public data for a given namespace-collection
func (t *transaction) retrieveHash(ns string, coll string) []byte {
if t.rwset == nil {
return nil
}
for _, nsData := range t.rwset.NsRwSets {
if nsData.NameSpace != ns {
continue
}
for _, collData := range nsData.CollHashedRwSets {
if collData.CollectionName == coll {
return collData.PvtRwSetHash
}
}
}
return nil
}
// applyWriteSet adds (or deletes) the key/values present in the write set to the publicAndHashUpdates
func (u *publicAndHashUpdates) applyWriteSet(
txRWSet *rwsetutil.TxRwSet,
txHeight *version.Height,
db *privacyenabledstate.DB,
containsPostOrderWrites bool,
) error {
u.publicUpdates.ContainsPostOrderWrites =
u.publicUpdates.ContainsPostOrderWrites || containsPostOrderWrites
txops, err := prepareTxOps(txRWSet, u, db)
logger.Debugf("txops=%#v", txops)
if err != nil {
return err
}
for compositeKey, keyops := range txops {
if compositeKey.coll == "" {
ns, key := compositeKey.ns, compositeKey.key
if keyops.isDelete() {
u.publicUpdates.Delete(ns, key, txHeight)
} else {
u.publicUpdates.PutValAndMetadata(ns, key, keyops.value, keyops.metadata, txHeight)
}
} else {
ns, coll, keyHash := compositeKey.ns, compositeKey.coll, []byte(compositeKey.key)
if keyops.isDelete() {
u.hashUpdates.Delete(ns, coll, keyHash, txHeight)
} else {
u.hashUpdates.PutValHashAndMetadata(ns, coll, keyHash, keyops.value, keyops.metadata, txHeight)
}
}
}
return nil
}