/
plugin_validator.go
266 lines (223 loc) · 7.9 KB
/
plugin_validator.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package txvalidator
import (
"fmt"
"sync"
"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric/common/cauthdsl"
ledger2 "github.com/hyperledger/fabric/common/ledger"
vp "github.com/hyperledger/fabric/core/committer/txvalidator/plugin"
validation "github.com/hyperledger/fabric/core/handlers/validation/api"
vc "github.com/hyperledger/fabric/core/handlers/validation/api/capabilities"
vi "github.com/hyperledger/fabric/core/handlers/validation/api/identities"
vs "github.com/hyperledger/fabric/core/handlers/validation/api/state"
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/protoutil"
"github.com/pkg/errors"
)
//go:generate mockery -dir . -name Mapper -case underscore -output mocks/
// Mapper local interface use to generate mock for foreign interface.
type Mapper interface {
vp.Mapper
}
//go:generate mockery -dir . -name PluginFactory -case underscore -output mocks/
// PluginFactory local interface used to generate mock for foreign interface.
type PluginFactory interface {
validation.PluginFactory
}
//go:generate mockery -dir . -name Plugin -case underscore -output mocks/
// Plugin local interface used to generate mock for foreign interface.
type Plugin interface {
validation.Plugin
}
//go:generate mockery -dir . -name QueryExecutorCreator -case underscore -output mocks/
// QueryExecutorCreator creates new query executors
type QueryExecutorCreator interface {
NewQueryExecutor() (ledger.QueryExecutor, error)
}
// Context defines information about a transaction
// that is being validated
type Context struct {
Seq int
Envelope []byte
TxID string
Channel string
VSCCName string
Policy []byte
Namespace string
Block *common.Block
}
// String returns a string representation of this Context
func (c Context) String() string {
return fmt.Sprintf("Tx %s, seq %d out of %d in block %d for channel %s with validation plugin %s", c.TxID, c.Seq, len(c.Block.Data.Data), c.Block.Header.Number, c.Channel, c.VSCCName)
}
// PluginValidator values transactions with validation plugins
type PluginValidator struct {
sync.Mutex
pluginChannelMapping map[vp.Name]*pluginsByChannel
vp.Mapper
QueryExecutorCreator
msp.IdentityDeserializer
capabilities vc.Capabilities
}
//go:generate mockery -dir . -name Capabilities -case underscore -output mocks/
// Capabilities local interface used to generate mock for foreign interface.
type Capabilities interface {
vc.Capabilities
}
//go:generate mockery -dir . -name IdentityDeserializer -case underscore -output mocks/
// IdentityDeserializer local interface used to generate mock for foreign interface.
type IdentityDeserializer interface {
msp.IdentityDeserializer
}
// NewPluginValidator creates a new PluginValidator
func NewPluginValidator(pm vp.Mapper, qec QueryExecutorCreator, deserializer msp.IdentityDeserializer, capabilities vc.Capabilities) *PluginValidator {
return &PluginValidator{
capabilities: capabilities,
pluginChannelMapping: make(map[vp.Name]*pluginsByChannel),
Mapper: pm,
QueryExecutorCreator: qec,
IdentityDeserializer: deserializer,
}
}
func (pv *PluginValidator) ValidateWithPlugin(ctx *Context) error {
plugin, err := pv.getOrCreatePlugin(ctx)
if err != nil {
return &validation.ExecutionFailureError{
Reason: fmt.Sprintf("plugin with name %s couldn't be used: %v", ctx.VSCCName, err),
}
}
err = plugin.Validate(ctx.Block, ctx.Namespace, ctx.Seq, 0, vp.SerializedPolicy(ctx.Policy))
validityStatus := "valid"
if err != nil {
validityStatus = fmt.Sprintf("invalid: %v", err)
}
logger.Debug("Transaction", ctx.TxID, "appears to be", validityStatus)
return err
}
func (pv *PluginValidator) getOrCreatePlugin(ctx *Context) (validation.Plugin, error) {
pluginFactory := pv.FactoryByName(vp.Name(ctx.VSCCName))
if pluginFactory == nil {
return nil, errors.Errorf("plugin with name %s wasn't found", ctx.VSCCName)
}
pluginsByChannel := pv.getOrCreatePluginChannelMapping(vp.Name(ctx.VSCCName), pluginFactory)
return pluginsByChannel.createPluginIfAbsent(ctx.Channel)
}
func (pv *PluginValidator) getOrCreatePluginChannelMapping(plugin vp.Name, pf validation.PluginFactory) *pluginsByChannel {
pv.Lock()
defer pv.Unlock()
endorserChannelMapping, exists := pv.pluginChannelMapping[vp.Name(plugin)]
if !exists {
endorserChannelMapping = &pluginsByChannel{
pluginFactory: pf,
channels2Plugins: make(map[string]validation.Plugin),
pv: pv,
}
pv.pluginChannelMapping[vp.Name(plugin)] = endorserChannelMapping
}
return endorserChannelMapping
}
type pluginsByChannel struct {
sync.RWMutex
pluginFactory validation.PluginFactory
channels2Plugins map[string]validation.Plugin
pv *PluginValidator
}
func (pbc *pluginsByChannel) createPluginIfAbsent(channel string) (validation.Plugin, error) {
pbc.RLock()
plugin, exists := pbc.channels2Plugins[channel]
pbc.RUnlock()
if exists {
return plugin, nil
}
pbc.Lock()
defer pbc.Unlock()
plugin, exists = pbc.channels2Plugins[channel]
if exists {
return plugin, nil
}
pluginInstance := pbc.pluginFactory.New()
plugin, err := pbc.initPlugin(pluginInstance, channel)
if err != nil {
return nil, err
}
pbc.channels2Plugins[channel] = plugin
return plugin, nil
}
func (pbc *pluginsByChannel) initPlugin(plugin validation.Plugin, channel string) (validation.Plugin, error) {
pe := &PolicyEvaluator{IdentityDeserializer: pbc.pv.IdentityDeserializer}
sf := &StateFetcherImpl{QueryExecutorCreator: pbc.pv}
if err := plugin.Init(pe, sf, pbc.pv.capabilities, &legacyCollectionInfoProvider{}); err != nil {
return nil, errors.Wrap(err, "failed initializing plugin")
}
return plugin, nil
}
// legacyCollectionInfoProvider implements a provider for collection
// information for the legacy lifecycle. It will never be called but
// it is necessary to have this dependency passed at init time to the
// default plugin
type legacyCollectionInfoProvider struct {
}
func (*legacyCollectionInfoProvider) CollectionValidationInfo(chaincodeName, collectionName string, state vs.State) ([]byte, error, error) {
panic("programming error")
}
type PolicyEvaluator struct {
msp.IdentityDeserializer
}
// Evaluate takes a set of SignedData and evaluates whether this set of signatures satisfies the policy
func (id *PolicyEvaluator) Evaluate(policyBytes []byte, signatureSet []*protoutil.SignedData) error {
pp := cauthdsl.NewPolicyProvider(id.IdentityDeserializer)
policy, _, err := pp.NewPolicy(policyBytes)
if err != nil {
return err
}
return policy.EvaluateSignedData(signatureSet)
}
// DeserializeIdentity unmarshals the given identity to msp.Identity
func (id *PolicyEvaluator) DeserializeIdentity(serializedIdentity []byte) (vi.Identity, error) {
mspIdentity, err := id.IdentityDeserializer.DeserializeIdentity(serializedIdentity)
if err != nil {
return nil, err
}
return &identity{Identity: mspIdentity}, nil
}
type identity struct {
msp.Identity
}
func (i *identity) GetIdentityIdentifier() *vi.IdentityIdentifier {
identifier := i.Identity.GetIdentifier()
return &vi.IdentityIdentifier{
Id: identifier.Id,
Mspid: identifier.Mspid,
}
}
type StateFetcherImpl struct {
QueryExecutorCreator
}
func (sf *StateFetcherImpl) FetchState() (vs.State, error) {
qe, err := sf.NewQueryExecutor()
if err != nil {
return nil, err
}
return &StateImpl{qe}, nil
}
type StateImpl struct {
ledger.QueryExecutor
}
func (s *StateImpl) GetStateRangeScanIterator(namespace string, startKey string, endKey string) (vs.ResultsIterator, error) {
it, err := s.QueryExecutor.GetStateRangeScanIterator(namespace, startKey, endKey)
if err != nil {
return nil, err
}
return &ResultsIteratorImpl{ResultsIterator: it}, nil
}
type ResultsIteratorImpl struct {
ledger2.ResultsIterator
}
func (it *ResultsIteratorImpl) Next() (vs.QueryResult, error) {
return it.ResultsIterator.Next()
}