-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
val.go
113 lines (100 loc) · 3.45 KB
/
val.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
113
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package internal
import (
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/privacyenabledstate"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/rwsetutil"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
"github.com/hyperledger/fabric/protos/peer"
)
var logger = flogging.MustGetLogger("valinternal")
// Validator is supposed to validate the transactions based on public data and hashes present in a block
// and returns a batch that should be used to update the state
type Validator interface {
ValidateAndPrepareBatch(block *Block, doMVCCValidation bool) (*PubAndHashUpdates, error)
}
// 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
}
// PubAndHashUpdates 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 PubAndHashUpdates struct {
PubUpdates *privacyenabledstate.PubUpdateBatch
HashUpdates *privacyenabledstate.HashedUpdateBatch
}
// NewPubAndHashUpdates constructs an empty PubAndHashUpdates
func NewPubAndHashUpdates() *PubAndHashUpdates {
return &PubAndHashUpdates{
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 PubAndHashUpdates
func (u *PubAndHashUpdates) ApplyWriteSet(txRWSet *rwsetutil.TxRwSet, txHeight *version.Height, db privacyenabledstate.DB) error {
txops, err := prepareTxOps(txRWSet, txHeight, 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.PubUpdates.Delete(ns, key, txHeight)
} else {
u.PubUpdates.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
}