|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2016 All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package gossip |
| 18 | + |
| 19 | +import ( |
| 20 | + "sync" |
| 21 | + "sync/atomic" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/hyperledger/fabric/gossip/api" |
| 25 | + "github.com/hyperledger/fabric/gossip/comm" |
| 26 | + "github.com/hyperledger/fabric/gossip/common" |
| 27 | + "github.com/hyperledger/fabric/gossip/discovery" |
| 28 | + "github.com/hyperledger/fabric/gossip/gossip/channel" |
| 29 | + "github.com/hyperledger/fabric/gossip/gossip/msgstore" |
| 30 | + "github.com/hyperledger/fabric/gossip/gossip/pull" |
| 31 | + "github.com/hyperledger/fabric/gossip/proto" |
| 32 | + "github.com/hyperledger/fabric/gossip/util" |
| 33 | +) |
| 34 | + |
| 35 | +type channelState struct { |
| 36 | + stopping int32 |
| 37 | + sync.RWMutex |
| 38 | + channels map[string]channel.GossipChannel |
| 39 | + g *gossipServiceImpl |
| 40 | +} |
| 41 | + |
| 42 | +func (cs *channelState) stop() { |
| 43 | + if cs.isStopping() { |
| 44 | + return |
| 45 | + } |
| 46 | + atomic.StoreInt32(&cs.stopping, int32(1)) |
| 47 | + cs.Lock() |
| 48 | + defer cs.Unlock() |
| 49 | + for _, gc := range cs.channels { |
| 50 | + gc.Stop() |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func (cs *channelState) isStopping() bool { |
| 55 | + return atomic.LoadInt32(&cs.stopping) == int32(1) |
| 56 | +} |
| 57 | + |
| 58 | +func (cs *channelState) getGossipChannelByChainID(chainID common.ChainID) channel.GossipChannel { |
| 59 | + if cs.isStopping() { |
| 60 | + return nil |
| 61 | + } |
| 62 | + cs.Lock() |
| 63 | + defer cs.Unlock() |
| 64 | + return cs.channels[string(chainID)] |
| 65 | +} |
| 66 | + |
| 67 | +func (cs *channelState) joinChannel(joinMsg api.JoinChannelMessage, chainID common.ChainID) { |
| 68 | + if cs.isStopping() { |
| 69 | + return |
| 70 | + } |
| 71 | + cs.Lock() |
| 72 | + defer cs.Unlock() |
| 73 | + if gc, exists := cs.channels[string(chainID)]; !exists { |
| 74 | + cs.channels[string(chainID)] = channel.NewGossipChannel(cs.g.mcs, chainID, &gossipAdapterImpl{gossipServiceImpl: cs.g, Discovery: cs.g.disc}, joinMsg) |
| 75 | + } else { |
| 76 | + gc.ConfigureChannel(joinMsg) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +type gossipAdapterImpl struct { |
| 81 | + *gossipServiceImpl |
| 82 | + discovery.Discovery |
| 83 | +} |
| 84 | + |
| 85 | +func (ga *gossipAdapterImpl) GetConf() channel.Config { |
| 86 | + return channel.Config{ |
| 87 | + ID: ga.conf.ID, |
| 88 | + MaxBlockCountToStore: ga.conf.MaxBlockCountToStore, |
| 89 | + PublishStateInfoInterval: ga.conf.PublishStateInfoInterval, |
| 90 | + PullInterval: ga.conf.PullInterval, |
| 91 | + PullPeerNum: ga.conf.PullPeerNum, |
| 92 | + RequestStateInfoInterval: ga.conf.RequestStateInfoInterval, |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// Gossip gossips a message |
| 97 | +func (ga *gossipAdapterImpl) Gossip(msg *proto.GossipMessage) { |
| 98 | + ga.gossipServiceImpl.emitter.Add(msg) |
| 99 | +} |
| 100 | + |
| 101 | +// ValidateStateInfoMessage returns error if a message isn't valid |
| 102 | +// nil otherwise |
| 103 | +func (ga *gossipAdapterImpl) ValidateStateInfoMessage(msg *proto.GossipMessage) error { |
| 104 | + return ga.gossipServiceImpl.validateStateInfoMsg(msg.GetStateInfo()) |
| 105 | +} |
| 106 | + |
| 107 | +// OrgByPeerIdentity extracts the organization identifier from a peer's identity |
| 108 | +func (ga *gossipAdapterImpl) OrgByPeerIdentity(identity api.PeerIdentityType) api.OrgIdentityType { |
| 109 | + return ga.gossipServiceImpl.secAdvisor.OrgByPeerIdentity(identity) |
| 110 | +} |
| 111 | + |
| 112 | +// GetOrgOfPeer returns the organization identifier of a certain peer |
| 113 | +func (ga *gossipAdapterImpl) GetOrgOfPeer(PKIID common.PKIidType) api.OrgIdentityType { |
| 114 | + return ga.gossipServiceImpl.getOrgOfPeer(PKIID) |
| 115 | +} |
| 116 | + |
| 117 | +// Adapter enables the gossipChannel |
| 118 | +// to communicate with gossipServiceImpl. |
| 119 | + |
| 120 | +// Adapter connects a GossipChannel to the gossip implementation |
| 121 | +type Adapter interface { |
| 122 | + |
| 123 | + // GetConf returns the configuration |
| 124 | + // of the GossipChannel |
| 125 | + GetConf() Config |
| 126 | + |
| 127 | + // Gossip gossips a message |
| 128 | + Gossip(*proto.GossipMessage) |
| 129 | + |
| 130 | + // DeMultiplex publishes a message to subscribers |
| 131 | + DeMultiplex(interface{}) |
| 132 | + |
| 133 | + // GetMembership returns the peers that are considered alive |
| 134 | + GetMembership() []discovery.NetworkMember |
| 135 | + |
| 136 | + // Send sends a message to a list of peers |
| 137 | + Send(msg *proto.GossipMessage, peers ...*comm.RemotePeer) |
| 138 | + |
| 139 | + // ValidateStateInfoMessage returns error if a message isn't valid |
| 140 | + // nil otherwise |
| 141 | + ValidateStateInfoMessage(*proto.GossipMessage) error |
| 142 | + |
| 143 | + // OrgByPeerIdentity extracts the organization identifier from a peer's identity |
| 144 | + OrgByPeerIdentity(identity api.PeerIdentityType) api.OrgIdentityType |
| 145 | + |
| 146 | + // GetOrgOfPeer returns the organization identifier of a certain peer |
| 147 | + GetOrgOfPeer(common.PKIidType) api.OrgIdentityType |
| 148 | +} |
| 149 | + |
| 150 | +type gossipChannel struct { |
| 151 | + Adapter |
| 152 | + sync.RWMutex |
| 153 | + shouldGossipStateInfo int32 |
| 154 | + stopChan chan struct{} |
| 155 | + stateInfoMsg *proto.GossipMessage |
| 156 | + orgs []api.OrgIdentityType |
| 157 | + joinMsg api.JoinChannelMessage |
| 158 | + blockMsgStore msgstore.MessageStore |
| 159 | + stateInfoMsgStore msgstore.MessageStore |
| 160 | + chainID common.ChainID |
| 161 | + blocksPuller pull.Mediator |
| 162 | + logger *util.Logger |
| 163 | + stateInfoPublishScheduler *time.Ticker |
| 164 | + stateInfoRequestScheduler *time.Ticker |
| 165 | +} |
0 commit comments