github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/gossip/service/eventer.go (about) 1 /* 2 Copyright hechain. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package service 8 9 import ( 10 "reflect" 11 12 "github.com/hechain20/hechain/common/channelconfig" 13 "github.com/hechain20/hechain/msp" 14 "github.com/hyperledger/fabric-protos-go/peer" 15 ) 16 17 // A ConfigUpdate holds the portions of channelconfig Bundle update used by 18 // gossip. 19 type ConfigUpdate struct { 20 ChannelID string 21 Organizations map[string]channelconfig.ApplicationOrg 22 Sequence uint64 23 OrdererAddresses []string 24 } 25 26 // ConfigProcessor receives config updates 27 type ConfigProcessor interface { 28 // ProcessConfigUpdate should be invoked whenever a channel's configuration is initialized or updated 29 ProcessConfigUpdate(configUpdate ConfigUpdate) 30 } 31 32 type configStore struct { 33 anchorPeers []*peer.AnchorPeer 34 orgMap map[string]channelconfig.ApplicationOrg 35 } 36 37 type configEventReceiver interface { 38 updateAnchors(config ConfigUpdate) 39 } 40 41 type configEventer struct { 42 lastConfig *configStore 43 receiver configEventReceiver 44 } 45 46 func newConfigEventer(receiver configEventReceiver) *configEventer { 47 return &configEventer{ 48 receiver: receiver, 49 } 50 } 51 52 // ProcessConfigUpdate should be invoked whenever a channel's configuration is initialized or updated 53 // it invokes the associated method in configEventReceiver when configuration is updated 54 // but only if the configuration value actually changed 55 // Note, that a changing sequence number is ignored as changing configuration 56 func (ce *configEventer) ProcessConfigUpdate(configUpdate ConfigUpdate) { 57 logger.Debugf("Processing new config for channel %s", configUpdate.ChannelID) 58 orgMap := cloneOrgConfig(configUpdate.Organizations) 59 if ce.lastConfig != nil && reflect.DeepEqual(ce.lastConfig.orgMap, orgMap) { 60 logger.Debugf("Ignoring new config for channel %s because it contained no anchor peer updates", configUpdate.ChannelID) 61 } else { 62 63 var newAnchorPeers []*peer.AnchorPeer 64 for _, group := range configUpdate.Organizations { 65 newAnchorPeers = append(newAnchorPeers, group.AnchorPeers()...) 66 } 67 68 newConfig := &configStore{ 69 orgMap: orgMap, 70 anchorPeers: newAnchorPeers, 71 } 72 ce.lastConfig = newConfig 73 74 logger.Debugf("Calling out because config was updated for channel %s", configUpdate.ChannelID) 75 ce.receiver.updateAnchors(configUpdate) 76 } 77 } 78 79 func cloneOrgConfig(src map[string]channelconfig.ApplicationOrg) map[string]channelconfig.ApplicationOrg { 80 clone := make(map[string]channelconfig.ApplicationOrg) 81 for k, v := range src { 82 clone[k] = &appGrp{ 83 name: v.Name(), 84 mspID: v.MSPID(), 85 anchorPeers: v.AnchorPeers(), 86 } 87 } 88 return clone 89 } 90 91 type appGrp struct { 92 name string 93 mspID string 94 anchorPeers []*peer.AnchorPeer 95 } 96 97 func (ag *appGrp) Name() string { 98 return ag.name 99 } 100 101 func (ag *appGrp) MSPID() string { 102 return ag.mspID 103 } 104 105 func (ag *appGrp) AnchorPeers() []*peer.AnchorPeer { 106 return ag.anchorPeers 107 } 108 109 func (ag *appGrp) MSP() msp.MSP { 110 return nil 111 }