-
Notifications
You must be signed in to change notification settings - Fork 1
/
vscc_validator.go
383 lines (274 loc) · 9.94 KB
/
vscc_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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package txvalidator
import (
"fmt"
"github.com/golang/protobuf/proto"
"github.com/mcc-github/blockchain/common/cauthdsl"
commonerrors "github.com/mcc-github/blockchain/common/errors"
coreUtil "github.com/mcc-github/blockchain/common/util"
"github.com/mcc-github/blockchain/core/common/ccprovider"
"github.com/mcc-github/blockchain/core/common/sysccprovider"
"github.com/mcc-github/blockchain/core/handlers/validation/api"
"github.com/mcc-github/blockchain/core/ledger/kvledger/txmgmt/rwsetutil"
"github.com/mcc-github/blockchain/protos/common"
"github.com/mcc-github/blockchain/protos/peer"
"github.com/mcc-github/blockchain/protos/utils"
"github.com/pkg/errors"
)
type VsccValidatorImpl struct {
chainID string
support Support
sccprovider sysccprovider.SystemChaincodeProvider
pluginValidator *PluginValidator
}
func newVSCCValidator(chainID string, support Support, sccp sysccprovider.SystemChaincodeProvider, pluginValidator *PluginValidator) *VsccValidatorImpl {
return &VsccValidatorImpl{
chainID: chainID,
support: support,
sccprovider: sccp,
pluginValidator: pluginValidator,
}
}
func (v *VsccValidatorImpl) VSCCValidateTx(seq int, payload *common.Payload, envBytes []byte, block *common.Block) (error, peer.TxValidationCode) {
chainID := v.chainID
logger.Debugf("[%s] VSCCValidateTx starts for bytes %p", chainID, envBytes)
hdrExt, err := utils.GetChaincodeHeaderExtension(payload.Header)
if err != nil {
return err, peer.TxValidationCode_BAD_HEADER_EXTENSION
}
chdr, err := utils.UnmarshalChannelHeader(payload.Header.ChannelHeader)
if err != nil {
return err, peer.TxValidationCode_BAD_CHANNEL_HEADER
}
writesToLSCC := false
writesToNonInvokableSCC := false
respPayload, err := utils.GetActionFromEnvelope(envBytes)
if err != nil {
return errors.WithMessage(err, "GetActionFromEnvelope failed"), peer.TxValidationCode_BAD_RESPONSE_PAYLOAD
}
txRWSet := &rwsetutil.TxRwSet{}
if err = txRWSet.FromProtoBytes(respPayload.Results); err != nil {
return errors.WithMessage(err, "txRWSet.FromProtoBytes failed"), peer.TxValidationCode_BAD_RWSET
}
if hdrExt.ChaincodeId == nil {
return errors.New("nil ChaincodeId in header extension"), peer.TxValidationCode_INVALID_OTHER_REASON
}
if respPayload.ChaincodeId == nil {
return errors.New("nil ChaincodeId in ChaincodeAction"), peer.TxValidationCode_INVALID_OTHER_REASON
}
ccID := hdrExt.ChaincodeId.Name
ccVer := respPayload.ChaincodeId.Version
if ccID == "" {
err = errors.New("invalid chaincode ID")
logger.Errorf("%+v", err)
return err, peer.TxValidationCode_INVALID_OTHER_REASON
}
if ccID != respPayload.ChaincodeId.Name {
err = errors.Errorf("inconsistent ccid info (%s/%s)", ccID, respPayload.ChaincodeId.Name)
logger.Errorf("%+v", err)
return err, peer.TxValidationCode_INVALID_OTHER_REASON
}
if ccVer == "" {
err = errors.New("invalid chaincode version")
logger.Errorf("%+v", err)
return err, peer.TxValidationCode_INVALID_OTHER_REASON
}
var wrNamespace []string
alwaysEnforceOriginalNamespace := v.support.Capabilities().V1_2Validation()
if alwaysEnforceOriginalNamespace {
wrNamespace = append(wrNamespace, ccID)
if respPayload.Events != nil {
ccEvent := &peer.ChaincodeEvent{}
if err = proto.Unmarshal(respPayload.Events, ccEvent); err != nil {
return errors.Wrapf(err, "invalid chaincode event"), peer.TxValidationCode_INVALID_OTHER_REASON
}
if ccEvent.ChaincodeId != ccID {
return errors.Errorf("chaincode event chaincode id does not match chaincode action chaincode id"), peer.TxValidationCode_INVALID_OTHER_REASON
}
}
}
for _, ns := range txRWSet.NsRwSets {
if !v.txWritesToNamespace(ns) {
continue
}
if ns.NameSpace != ccID || !alwaysEnforceOriginalNamespace {
wrNamespace = append(wrNamespace, ns.NameSpace)
}
if !writesToLSCC && ns.NameSpace == "lscc" {
writesToLSCC = true
}
if !writesToNonInvokableSCC && v.sccprovider.IsSysCCAndNotInvokableCC2CC(ns.NameSpace) {
writesToNonInvokableSCC = true
}
if !writesToNonInvokableSCC && v.sccprovider.IsSysCCAndNotInvokableExternal(ns.NameSpace) {
writesToNonInvokableSCC = true
}
}
if !v.sccprovider.IsSysCC(ccID) {
if writesToLSCC {
return errors.Errorf("chaincode %s attempted to write to the namespace of LSCC", ccID),
peer.TxValidationCode_ILLEGAL_WRITESET
}
if writesToNonInvokableSCC {
return errors.Errorf("chaincode %s attempted to write to the namespace of a system chaincode that cannot be invoked", ccID),
peer.TxValidationCode_ILLEGAL_WRITESET
}
for _, ns := range wrNamespace {
txcc, vscc, policy, err := v.GetInfoForValidate(chdr, ns)
if err != nil {
logger.Errorf("GetInfoForValidate for txId = %s returned error: %+v", chdr.TxId, err)
return err, peer.TxValidationCode_INVALID_OTHER_REASON
}
if ns == ccID && txcc.ChaincodeVersion != ccVer {
err = errors.Errorf("chaincode %s:%s/%s didn't match %s:%s/%s in lscc", ccID, ccVer, chdr.ChannelId, txcc.ChaincodeName, txcc.ChaincodeVersion, chdr.ChannelId)
logger.Errorf("%+v", err)
return err, peer.TxValidationCode_EXPIRED_CHAINCODE
}
ctx := &Context{
Seq: seq,
Envelope: envBytes,
Block: block,
TxID: chdr.TxId,
Channel: chdr.ChannelId,
Namespace: ns,
Policy: policy,
VSCCName: vscc.ChaincodeName,
}
if err = v.VSCCValidateTxForCC(ctx); err != nil {
switch err.(type) {
case *commonerrors.VSCCEndorsementPolicyError:
return err, peer.TxValidationCode_ENDORSEMENT_POLICY_FAILURE
default:
return err, peer.TxValidationCode_INVALID_OTHER_REASON
}
}
}
} else {
if v.sccprovider.IsSysCCAndNotInvokableExternal(ccID) {
return errors.Errorf("committing an invocation of cc %s is illegal", ccID),
peer.TxValidationCode_ILLEGAL_WRITESET
}
_, vscc, policy, err := v.GetInfoForValidate(chdr, ccID)
if err != nil {
logger.Errorf("GetInfoForValidate for txId = %s returned error: %+v", chdr.TxId, err)
return err, peer.TxValidationCode_INVALID_OTHER_REASON
}
ctx := &Context{
Seq: seq,
Envelope: envBytes,
Block: block,
TxID: chdr.TxId,
Channel: chdr.ChannelId,
Namespace: ccID,
Policy: policy,
VSCCName: vscc.ChaincodeName,
}
if err = v.VSCCValidateTxForCC(ctx); err != nil {
switch err.(type) {
case *commonerrors.VSCCEndorsementPolicyError:
return err, peer.TxValidationCode_ENDORSEMENT_POLICY_FAILURE
default:
return err, peer.TxValidationCode_INVALID_OTHER_REASON
}
}
}
logger.Debugf("[%s] VSCCValidateTx completes env bytes %p", chainID, envBytes)
return nil, peer.TxValidationCode_VALID
}
func (v *VsccValidatorImpl) VSCCValidateTxForCC(ctx *Context) error {
logger.Debug("Validating", ctx, "with plugin")
err := v.pluginValidator.ValidateWithPlugin(ctx)
if err == nil {
return nil
}
if e, isExecutionError := err.(*validation.ExecutionFailureError); isExecutionError {
return &commonerrors.VSCCExecutionFailureError{Err: e}
}
return &commonerrors.VSCCEndorsementPolicyError{Err: err}
}
func (v *VsccValidatorImpl) getCDataForCC(chid, ccid string) (ccprovider.ChaincodeDefinition, error) {
l := v.support.Ledger()
if l == nil {
return nil, errors.New("nil ledger instance")
}
qe, err := l.NewQueryExecutor()
if err != nil {
return nil, errors.WithMessage(err, "could not retrieve QueryExecutor")
}
defer qe.Done()
bytes, err := qe.GetState("lscc", ccid)
if err != nil {
return nil, &commonerrors.VSCCInfoLookupFailureError{
Reason: fmt.Sprintf("Could not retrieve state for chaincode %s, error %s", ccid, err),
}
}
if bytes == nil {
return nil, errors.Errorf("lscc's state for [%s] not found.", ccid)
}
cd := &ccprovider.ChaincodeData{}
err = proto.Unmarshal(bytes, cd)
if err != nil {
return nil, errors.Wrap(err, "unmarshalling ChaincodeQueryResponse failed")
}
if cd.Vscc == "" {
return nil, errors.Errorf("lscc's state for [%s] is invalid, vscc field must be set", ccid)
}
if len(cd.Policy) == 0 {
return nil, errors.Errorf("lscc's state for [%s] is invalid, policy field must be set", ccid)
}
return cd, err
}
func (v *VsccValidatorImpl) GetInfoForValidate(chdr *common.ChannelHeader, ccID string) (*sysccprovider.ChaincodeInstance, *sysccprovider.ChaincodeInstance, []byte, error) {
cc := &sysccprovider.ChaincodeInstance{
ChainID: chdr.ChannelId,
ChaincodeName: ccID,
ChaincodeVersion: coreUtil.GetSysCCVersion(),
}
vscc := &sysccprovider.ChaincodeInstance{
ChainID: chdr.ChannelId,
ChaincodeName: "vscc",
ChaincodeVersion: coreUtil.GetSysCCVersion(),
}
var policy []byte
var err error
if !v.sccprovider.IsSysCC(ccID) {
cd, err := v.getCDataForCC(chdr.ChannelId, ccID)
if err != nil {
msg := fmt.Sprintf("Unable to get chaincode data from ledger for txid %s, due to %s", chdr.TxId, err)
logger.Errorf(msg)
return nil, nil, nil, err
}
cc.ChaincodeName = cd.CCName()
cc.ChaincodeVersion = cd.CCVersion()
vscc.ChaincodeName, policy = cd.Validation()
} else {
p := cauthdsl.SignedByAnyMember(v.support.GetMSPIDs(chdr.ChannelId))
policy, err = utils.Marshal(p)
if err != nil {
return nil, nil, nil, err
}
}
return cc, vscc, policy, nil
}
func (v *VsccValidatorImpl) txWritesToNamespace(ns *rwsetutil.NsRwSet) bool {
if ns.KvRwSet != nil && len(ns.KvRwSet.Writes) > 0 {
return true
}
if v.support.Capabilities().PrivateChannelData() {
for _, c := range ns.CollHashedRwSets {
if c.HashedRwSet != nil && len(c.HashedRwSet.HashedWrites) > 0 {
return true
}
if v.support.Capabilities().KeyLevelEndorsement() {
if c.HashedRwSet != nil && len(c.HashedRwSet.MetadataWrites) > 0 {
return true
}
}
}
}
if v.support.Capabilities().KeyLevelEndorsement() {
if ns.KvRwSet != nil && len(ns.KvRwSet.MetadataWrites) > 0 {
return true
}
}
return false
}