github.com/true-sqn/fabric@v2.1.1+incompatible/core/ledger/kvledger/coll_elg_notifier_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package kvledger 8 9 import ( 10 "testing" 11 12 "github.com/hyperledger/fabric-protos-go/common" 13 "github.com/hyperledger/fabric-protos-go/ledger/rwset/kvrwset" 14 "github.com/hyperledger/fabric-protos-go/peer" 15 "github.com/hyperledger/fabric/core/ledger" 16 "github.com/hyperledger/fabric/core/ledger/mock" 17 "github.com/stretchr/testify/assert" 18 ) 19 20 func TestCollElgNotifier(t *testing.T) { 21 mockDeployedChaincodeInfoProvider := &mock.DeployedChaincodeInfoProvider{} 22 mockDeployedChaincodeInfoProvider.UpdatedChaincodesReturns([]*ledger.ChaincodeLifecycleInfo{ 23 {Name: "cc1"}, 24 }, nil) 25 26 // Returns 3 collections - the bool value indicates the eligibility of peer for corresponding collection 27 mockDeployedChaincodeInfoProvider.ChaincodeInfoReturnsOnCall(0, 28 &ledger.DeployedChaincodeInfo{ 29 ExplicitCollectionConfigPkg: testutilPrepapreMockCollectionConfigPkg( 30 map[string]bool{"coll1": true, "coll2": true, "coll3": false})}, nil) 31 32 // post commit - returns 4 collections 33 mockDeployedChaincodeInfoProvider.ChaincodeInfoReturnsOnCall(1, 34 &ledger.DeployedChaincodeInfo{ 35 ExplicitCollectionConfigPkg: testutilPrepapreMockCollectionConfigPkg( 36 map[string]bool{"coll1": false, "coll2": true, "coll3": true, "coll4": true})}, nil) 37 38 mockMembershipInfoProvider := &mock.MembershipInfoProvider{} 39 mockMembershipInfoProvider.AmMemberOfStub = func(channel string, p *peer.CollectionPolicyConfig) (bool, error) { 40 return testutilIsEligibleForMockPolicy(p), nil 41 } 42 43 mockCollElgListener := &mockCollElgListener{} 44 45 collElgNotifier := &collElgNotifier{ 46 mockDeployedChaincodeInfoProvider, 47 mockMembershipInfoProvider, 48 make(map[string]collElgListener), 49 } 50 collElgNotifier.registerListener("testLedger", mockCollElgListener) 51 52 collElgNotifier.HandleStateUpdates(&ledger.StateUpdateTrigger{ 53 LedgerID: "testLedger", 54 CommittingBlockNum: uint64(500), 55 StateUpdates: map[string]*ledger.KVStateUpdates{ 56 "doesNotMatterNS": { 57 PublicUpdates: []*kvrwset.KVWrite{ 58 { 59 Key: "doesNotMatterKey", 60 Value: []byte("doesNotMatterVal"), 61 }, 62 }, 63 }, 64 }, 65 }) 66 67 // event triggered should only contain "coll3" as this is the only collection 68 // for which peer became from ineligile to eligible by upgrade tx 69 assert.Equal(t, uint64(500), mockCollElgListener.receivedCommittingBlk) 70 assert.Equal(t, 71 map[string][]string{ 72 "cc1": {"coll3"}, 73 }, 74 mockCollElgListener.receivedNsCollMap, 75 ) 76 } 77 78 type mockCollElgListener struct { 79 receivedCommittingBlk uint64 80 receivedNsCollMap map[string][]string 81 } 82 83 func (m *mockCollElgListener) ProcessCollsEligibilityEnabled(commitingBlk uint64, nsCollMap map[string][]string) error { 84 m.receivedCommittingBlk = commitingBlk 85 m.receivedNsCollMap = nsCollMap 86 return nil 87 } 88 89 func testutilPrepapreMockCollectionConfigPkg(collEligibilityMap map[string]bool) *peer.CollectionConfigPackage { 90 pkg := &peer.CollectionConfigPackage{} 91 for collName, isEligible := range collEligibilityMap { 92 var version int32 93 if isEligible { 94 version = 1 95 } 96 policy := &peer.CollectionPolicyConfig{ 97 Payload: &peer.CollectionPolicyConfig_SignaturePolicy{ 98 SignaturePolicy: &common.SignaturePolicyEnvelope{Version: version}, 99 }, 100 } 101 sCollConfig := &peer.CollectionConfig_StaticCollectionConfig{ 102 StaticCollectionConfig: &peer.StaticCollectionConfig{ 103 Name: collName, 104 MemberOrgsPolicy: policy, 105 }, 106 } 107 config := &peer.CollectionConfig{Payload: sCollConfig} 108 pkg.Config = append(pkg.Config, config) 109 } 110 return pkg 111 } 112 113 func testutilIsEligibleForMockPolicy(p *peer.CollectionPolicyConfig) bool { 114 return p.GetSignaturePolicy().Version == 1 115 }