github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/ledger/kvledger/coll_elg_notifier_test.go (about) 1 /* 2 Copyright hechain. 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/hechain20/hechain/core/ledger" 13 "github.com/hechain20/hechain/core/ledger/mock" 14 "github.com/hyperledger/fabric-protos-go/common" 15 "github.com/hyperledger/fabric-protos-go/ledger/rwset/kvrwset" 16 "github.com/hyperledger/fabric-protos-go/peer" 17 "github.com/stretchr/testify/require" 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}), 31 }, nil) 32 33 // post commit - returns 4 collections 34 mockDeployedChaincodeInfoProvider.ChaincodeInfoReturnsOnCall(1, 35 &ledger.DeployedChaincodeInfo{ 36 ExplicitCollectionConfigPkg: testutilPrepapreMockCollectionConfigPkg( 37 map[string]bool{"coll1": false, "coll2": true, "coll3": true, "coll4": true}), 38 }, nil) 39 40 mockMembershipInfoProvider := &mock.MembershipInfoProvider{} 41 mockMembershipInfoProvider.AmMemberOfStub = func(channel string, p *peer.CollectionPolicyConfig) (bool, error) { 42 return testutilIsEligibleForMockPolicy(p), nil 43 } 44 45 mockCollElgListener := &mockCollElgListener{} 46 47 collElgNotifier := &collElgNotifier{ 48 mockDeployedChaincodeInfoProvider, 49 mockMembershipInfoProvider, 50 make(map[string]collElgListener), 51 } 52 collElgNotifier.registerListener("testLedger", mockCollElgListener) 53 54 err := collElgNotifier.HandleStateUpdates(&ledger.StateUpdateTrigger{ 55 LedgerID: "testLedger", 56 CommittingBlockNum: uint64(500), 57 StateUpdates: map[string]*ledger.KVStateUpdates{ 58 "doesNotMatterNS": { 59 PublicUpdates: []*kvrwset.KVWrite{ 60 { 61 Key: "doesNotMatterKey", 62 Value: []byte("doesNotMatterVal"), 63 }, 64 }, 65 }, 66 }, 67 }) 68 require.NoError(t, err) 69 70 // event triggered should only contain "coll3" as this is the only collection 71 // for which peer became from ineligile to eligible by upgrade tx 72 require.Equal(t, uint64(500), mockCollElgListener.receivedCommittingBlk) 73 require.Equal(t, 74 map[string][]string{ 75 "cc1": {"coll3"}, 76 }, 77 mockCollElgListener.receivedNsCollMap, 78 ) 79 } 80 81 type mockCollElgListener struct { 82 receivedCommittingBlk uint64 83 receivedNsCollMap map[string][]string 84 } 85 86 func (m *mockCollElgListener) ProcessCollsEligibilityEnabled(commitingBlk uint64, nsCollMap map[string][]string) error { 87 m.receivedCommittingBlk = commitingBlk 88 m.receivedNsCollMap = nsCollMap 89 return nil 90 } 91 92 func testutilPrepapreMockCollectionConfigPkg(collEligibilityMap map[string]bool) *peer.CollectionConfigPackage { 93 pkg := &peer.CollectionConfigPackage{} 94 for collName, isEligible := range collEligibilityMap { 95 var version int32 96 if isEligible { 97 version = 1 98 } 99 policy := &peer.CollectionPolicyConfig{ 100 Payload: &peer.CollectionPolicyConfig_SignaturePolicy{ 101 SignaturePolicy: &common.SignaturePolicyEnvelope{Version: version}, 102 }, 103 } 104 sCollConfig := &peer.CollectionConfig_StaticCollectionConfig{ 105 StaticCollectionConfig: &peer.StaticCollectionConfig{ 106 Name: collName, 107 MemberOrgsPolicy: policy, 108 }, 109 } 110 config := &peer.CollectionConfig{Payload: sCollConfig} 111 pkg.Config = append(pkg.Config, config) 112 } 113 return pkg 114 } 115 116 func testutilIsEligibleForMockPolicy(p *peer.CollectionPolicyConfig) bool { 117 return p.GetSignaturePolicy().Version == 1 118 }