Skip to content

Commit d6987ff

Browse files
committed
[FAB-11769] validate cc ns change for meta writes
Currently, transactions are validated by a validation plugin for a namespace if they write to keys into that namespace. More specifically, if the write set is not the empty set for that namespace. With state-based endorsement, this conditions should be changed to ensure that transactions that only modify metadata in a namespace also undergo validation. Change-Id: I89009ecb4a08510e9b1f2deb2c2b9b7ec7c9f7bf Signed-off-by: Alessandro Sorniotti <ale.linux@sopit.net> Signed-off-by: Matthias Neugschwandtner <eug@zurich.ibm.com>
1 parent 05ac04d commit d6987ff

File tree

2 files changed

+91
-6
lines changed

2 files changed

+91
-6
lines changed

core/committer/txvalidator/validator_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,77 @@ func TestInvokeOKPvtDataOnly(t *testing.T) {
402402
assertInvalid(b, t, peer.TxValidationCode_ENDORSEMENT_POLICY_FAILURE)
403403
}
404404

405+
func TestInvokeOKMetaUpdateOnly(t *testing.T) {
406+
plugin := &mocks.Plugin{}
407+
plugin.On("Init", mock.Anything, mock.Anything, mock.Anything).Return(nil)
408+
409+
l, v := setupLedgerAndValidatorExplicit(t, &mockconfig.MockApplicationCapabilities{}, plugin)
410+
defer ledgermgmt.CleanupTestEnv()
411+
defer l.Close()
412+
413+
v.(*txvalidator.TxValidator).Support.(struct {
414+
*mocktxvalidator.Support
415+
*semaphore.Weighted
416+
}).ACVal = &mockconfig.MockApplicationCapabilities{KeyLevelEndorsementRv: true}
417+
418+
ccID := "mycc"
419+
420+
putCCInfo(l, ccID, signedByAnyMember([]string{"SampleOrg"}), t)
421+
422+
rwsetBuilder := rwsetutil.NewRWSetBuilder()
423+
rwsetBuilder.AddToMetadataWriteSet(ccID, "somekey", map[string][]byte{})
424+
rwset, err := rwsetBuilder.GetTxSimulationResults()
425+
assert.NoError(t, err)
426+
rwsetBytes, err := rwset.GetPubSimulationBytes()
427+
assert.NoError(t, err)
428+
429+
tx := getEnv(ccID, nil, rwsetBytes, t)
430+
b := &common.Block{Data: &common.BlockData{Data: [][]byte{utils.MarshalOrPanic(tx)}}}
431+
432+
plugin.On("Validate", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(errors.New("tx is invalid"))
433+
434+
err = v.Validate(b)
435+
assert.NoError(t, err)
436+
assertInvalid(b, t, peer.TxValidationCode_ENDORSEMENT_POLICY_FAILURE)
437+
}
438+
439+
func TestInvokeOKPvtMetaUpdateOnly(t *testing.T) {
440+
plugin := &mocks.Plugin{}
441+
plugin.On("Init", mock.Anything, mock.Anything, mock.Anything).Return(nil)
442+
443+
l, v := setupLedgerAndValidatorExplicit(t, &mockconfig.MockApplicationCapabilities{}, plugin)
444+
defer ledgermgmt.CleanupTestEnv()
445+
defer l.Close()
446+
447+
v.(*txvalidator.TxValidator).Support.(struct {
448+
*mocktxvalidator.Support
449+
*semaphore.Weighted
450+
}).ACVal = &mockconfig.MockApplicationCapabilities{
451+
KeyLevelEndorsementRv: true,
452+
PrivateChannelDataRv: true,
453+
}
454+
455+
ccID := "mycc"
456+
457+
putCCInfo(l, ccID, signedByAnyMember([]string{"SampleOrg"}), t)
458+
459+
rwsetBuilder := rwsetutil.NewRWSetBuilder()
460+
rwsetBuilder.AddToHashedMetadataWriteSet(ccID, "mycollection", "somekey", map[string][]byte{})
461+
rwset, err := rwsetBuilder.GetTxSimulationResults()
462+
assert.NoError(t, err)
463+
rwsetBytes, err := rwset.GetPubSimulationBytes()
464+
assert.NoError(t, err)
465+
466+
tx := getEnv(ccID, nil, rwsetBytes, t)
467+
b := &common.Block{Data: &common.BlockData{Data: [][]byte{utils.MarshalOrPanic(tx)}}}
468+
469+
plugin.On("Validate", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(errors.New("tx is invalid"))
470+
471+
err = v.Validate(b)
472+
assert.NoError(t, err)
473+
assertInvalid(b, t, peer.TxValidationCode_ENDORSEMENT_POLICY_FAILURE)
474+
}
475+
405476
func TestInvokeOKSCC(t *testing.T) {
406477
l, v := setupLedgerAndValidator(t)
407478
defer ledgermgmt.CleanupTestEnv()

core/committer/txvalidator/vscc_validator.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,14 +362,28 @@ func (v *VsccValidatorImpl) txWritesToNamespace(ns *rwsetutil.NsRwSet) bool {
362362
return true
363363
}
364364

365-
// do not look at collection data if we don't support that capability
366-
if !v.support.Capabilities().PrivateChannelData() {
367-
return false
365+
// only look at collection data if we support that capability
366+
if v.support.Capabilities().PrivateChannelData() {
367+
// check for private writes for all collections
368+
for _, c := range ns.CollHashedRwSets {
369+
if c.HashedRwSet != nil && len(c.HashedRwSet.HashedWrites) > 0 {
370+
return true
371+
}
372+
373+
// only look at private metadata writes if we support that capability
374+
if v.support.Capabilities().KeyLevelEndorsement() {
375+
// private metadata updates
376+
if c.HashedRwSet != nil && len(c.HashedRwSet.MetadataWrites) > 0 {
377+
return true
378+
}
379+
}
380+
}
368381
}
369382

370-
// check for private writes for all collections
371-
for _, c := range ns.CollHashedRwSets {
372-
if c.HashedRwSet != nil && len(c.HashedRwSet.HashedWrites) > 0 {
383+
// only look at metadata writes if we support that capability
384+
if v.support.Capabilities().KeyLevelEndorsement() {
385+
// public metadata updates
386+
if ns.KvRwSet != nil && len(ns.KvRwSet.MetadataWrites) > 0 {
373387
return true
374388
}
375389
}

0 commit comments

Comments
 (0)