|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2017 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 election |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "strconv" |
| 22 | + "sync" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/hyperledger/fabric/gossip/api" |
| 26 | + "github.com/hyperledger/fabric/gossip/comm" |
| 27 | + "github.com/hyperledger/fabric/gossip/common" |
| 28 | + "github.com/hyperledger/fabric/gossip/discovery" |
| 29 | + "github.com/hyperledger/fabric/gossip/proto" |
| 30 | + "github.com/op/go-logging" |
| 31 | +) |
| 32 | + |
| 33 | +type msgImpl struct { |
| 34 | + msg *proto.GossipMessage |
| 35 | +} |
| 36 | + |
| 37 | +func (mi *msgImpl) SenderID() string { |
| 38 | + return string(mi.msg.GetLeadershipMsg().GetMembership().PkiID) |
| 39 | +} |
| 40 | + |
| 41 | +func (mi *msgImpl) IsProposal() bool { |
| 42 | + return !mi.IsDeclaration() |
| 43 | +} |
| 44 | + |
| 45 | +func (mi *msgImpl) IsDeclaration() bool { |
| 46 | + isDeclaration, _ := strconv.ParseBool(string(mi.msg.GetLeadershipMsg().GetMembership().Metadata)) |
| 47 | + return isDeclaration |
| 48 | +} |
| 49 | + |
| 50 | +type peerImpl struct { |
| 51 | + member *discovery.NetworkMember |
| 52 | +} |
| 53 | + |
| 54 | +func (pi *peerImpl) ID() string { |
| 55 | + return string(pi.member.PKIid) |
| 56 | +} |
| 57 | + |
| 58 | +type gossip interface { |
| 59 | + // Peers returns the NetworkMembers considered alive |
| 60 | + Peers() []discovery.NetworkMember |
| 61 | + |
| 62 | + // Accept returns a dedicated read-only channel for messages sent by other nodes that match a certain predicate. |
| 63 | + // If passThrough is false, the messages are processed by the gossip layer beforehand. |
| 64 | + // If passThrough is true, the gossip layer doesn't intervene and the messages |
| 65 | + // can be used to send a reply back to the sender |
| 66 | + Accept(acceptor common.MessageAcceptor, passThrough bool) (<-chan *proto.GossipMessage, <-chan comm.ReceivedMessage) |
| 67 | + |
| 68 | + // Gossip sends a message to other peers to the network |
| 69 | + Gossip(msg *proto.GossipMessage) |
| 70 | +} |
| 71 | + |
| 72 | +// MsgCrypto used to sign messages and verify received messages signatures |
| 73 | +type MsgCrypto interface { |
| 74 | + // Sign signs a message, returns a signed message on success |
| 75 | + // or an error on failure |
| 76 | + Sign(msg []byte) ([]byte, error) |
| 77 | + |
| 78 | + // Verify verifies a signed message |
| 79 | + Verify(vkID, signature, message []byte) error |
| 80 | + |
| 81 | + // Get returns the identity of a given pkiID, or error if such an identity |
| 82 | + // isn't found |
| 83 | + Get(pkiID common.PKIidType) (api.PeerIdentityType, error) |
| 84 | +} |
| 85 | + |
| 86 | +type adapterImpl struct { |
| 87 | + gossip gossip |
| 88 | + self *discovery.NetworkMember |
| 89 | + |
| 90 | + incTime uint64 |
| 91 | + seqNum uint64 |
| 92 | + |
| 93 | + mcs MsgCrypto |
| 94 | + |
| 95 | + channel common.ChainID |
| 96 | + |
| 97 | + logger *logging.Logger |
| 98 | + |
| 99 | + doneCh chan struct{} |
| 100 | + stopOnce *sync.Once |
| 101 | +} |
| 102 | + |
| 103 | +// NewAdapter creates new leader election adapter |
| 104 | +func NewAdapter(gossip gossip, self *discovery.NetworkMember, mcs MsgCrypto, channel common.ChainID) LeaderElectionAdapter { |
| 105 | + return &adapterImpl{ |
| 106 | + gossip: gossip, |
| 107 | + self: self, |
| 108 | + |
| 109 | + incTime: uint64(time.Now().UnixNano()), |
| 110 | + seqNum: uint64(0), |
| 111 | + |
| 112 | + mcs: mcs, |
| 113 | + |
| 114 | + channel: channel, |
| 115 | + |
| 116 | + logger: logging.MustGetLogger("LeaderElectionAdapter"), |
| 117 | + |
| 118 | + doneCh: make(chan struct{}), |
| 119 | + stopOnce: &sync.Once{}, |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func (ai *adapterImpl) Gossip(msg Msg) { |
| 124 | + ai.gossip.Gossip(msg.(*msgImpl).msg) |
| 125 | +} |
| 126 | + |
| 127 | +func (ai *adapterImpl) Accept() <-chan Msg { |
| 128 | + adapterCh, _ := ai.gossip.Accept(func(message interface{}) bool { |
| 129 | + // Get only leadership org and channel messages |
| 130 | + validMsg := message.(*proto.GossipMessage).Tag == proto.GossipMessage_CHAN_AND_ORG && |
| 131 | + message.(*proto.GossipMessage).IsLeadershipMsg() && |
| 132 | + bytes.Equal(message.(*proto.GossipMessage).Channel, ai.channel) |
| 133 | + if validMsg { |
| 134 | + leadershipMsg := message.(*proto.GossipMessage).GetLeadershipMsg() |
| 135 | + |
| 136 | + verifier := func(identity []byte, signature, message []byte) error { |
| 137 | + return ai.mcs.Verify(identity, signature, message) |
| 138 | + } |
| 139 | + identity, err := ai.mcs.Get(leadershipMsg.GetMembership().PkiID) |
| 140 | + if err != nil { |
| 141 | + ai.logger.Error("Failed verify, can't get identity", leadershipMsg, ":", err) |
| 142 | + return false |
| 143 | + } |
| 144 | + |
| 145 | + if err := message.(*proto.GossipMessage).Verify(identity, verifier); err != nil { |
| 146 | + ai.logger.Error("Failed verify", leadershipMsg, ":", err) |
| 147 | + return false |
| 148 | + } |
| 149 | + return true |
| 150 | + } |
| 151 | + return false |
| 152 | + }, false) |
| 153 | + |
| 154 | + msgCh := make(chan Msg) |
| 155 | + |
| 156 | + go func(inCh <-chan *proto.GossipMessage, outCh chan Msg, stopCh chan struct{}) { |
| 157 | + for { |
| 158 | + select { |
| 159 | + case <-stopCh: |
| 160 | + return |
| 161 | + case gossipMsg, ok := <-inCh: |
| 162 | + if ok { |
| 163 | + outCh <- &msgImpl{gossipMsg} |
| 164 | + } else { |
| 165 | + return |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + }(adapterCh, msgCh, ai.doneCh) |
| 170 | + return msgCh |
| 171 | +} |
| 172 | + |
| 173 | +func (ai *adapterImpl) CreateMessage(isDeclaration bool) Msg { |
| 174 | + ai.seqNum++ |
| 175 | + seqNum := ai.seqNum |
| 176 | + |
| 177 | + metadata := []byte{} |
| 178 | + metadata = strconv.AppendBool(metadata, isDeclaration) |
| 179 | + |
| 180 | + leadershipMsg := &proto.LeadershipMessage{ |
| 181 | + Membership: &proto.Member{ |
| 182 | + PkiID: ai.self.PKIid, |
| 183 | + Endpoint: ai.self.Endpoint, |
| 184 | + Metadata: metadata, |
| 185 | + }, |
| 186 | + Timestamp: &proto.PeerTime{ |
| 187 | + IncNumber: ai.incTime, |
| 188 | + SeqNum: seqNum, |
| 189 | + }, |
| 190 | + } |
| 191 | + |
| 192 | + msg := &proto.GossipMessage{ |
| 193 | + Nonce: 0, |
| 194 | + Tag: proto.GossipMessage_CHAN_AND_ORG, |
| 195 | + Content: &proto.GossipMessage_LeadershipMsg{LeadershipMsg: leadershipMsg}, |
| 196 | + Channel: ai.channel, |
| 197 | + } |
| 198 | + |
| 199 | + signer := func(msg []byte) ([]byte, error) { |
| 200 | + return ai.mcs.Sign(msg) |
| 201 | + } |
| 202 | + |
| 203 | + msg.Sign(signer) |
| 204 | + return &msgImpl{msg} |
| 205 | +} |
| 206 | + |
| 207 | +func (ai *adapterImpl) Peers() []Peer { |
| 208 | + peers := ai.gossip.Peers() |
| 209 | + |
| 210 | + var res []Peer |
| 211 | + for _, peer := range peers { |
| 212 | + res = append(res, &peerImpl{&peer}) |
| 213 | + } |
| 214 | + |
| 215 | + return res |
| 216 | +} |
| 217 | + |
| 218 | +func (ai *adapterImpl) Stop() { |
| 219 | + stopFunc := func() { |
| 220 | + close(ai.doneCh) |
| 221 | + } |
| 222 | + ai.stopOnce.Do(stopFunc) |
| 223 | +} |
0 commit comments