-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_comm.go
196 lines (157 loc) · 4.82 KB
/
mock_comm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package mock
import (
"time"
"github.com/hyperledger/fabric/gossip/api"
"github.com/hyperledger/fabric/gossip/comm"
"github.com/hyperledger/fabric/gossip/common"
"github.com/hyperledger/fabric/gossip/util"
proto "github.com/hyperledger/fabric/protos/gossip"
)
// Mock which aims to simulate socket
type socketMock struct {
// socket endpoint
endpoint string
// To simulate simple tcp socket
socket chan interface{}
}
// Mock of primitive tcp packet structure
type packetMock struct {
// Sender channel message sent from
src *socketMock
// Destination channel sent to
dst *socketMock
msg interface{}
}
type channelMock struct {
accept common.MessageAcceptor
channel chan proto.ReceivedMessage
}
type commMock struct {
id string
members map[string]*socketMock
acceptors []*channelMock
deadChannel chan common.PKIidType
done chan struct{}
}
var logger = util.GetLogger(util.LoggingMockModule, "")
// NewCommMock creates mocked communication object
func NewCommMock(id string, members map[string]*socketMock) comm.Comm {
res := &commMock{
id: id,
members: members,
acceptors: make([]*channelMock, 0),
done: make(chan struct{}),
deadChannel: make(chan common.PKIidType),
}
// Start communication service
go res.start()
return res
}
// Respond sends a GossipMessage to the origin from which this ReceivedMessage was sent from
func (packet *packetMock) Respond(msg *proto.GossipMessage) {
sMsg, _ := msg.NoopSign()
packet.src.socket <- &packetMock{
src: packet.dst,
dst: packet.src,
msg: sMsg,
}
}
// Ack returns to the sender an acknowledgement for the message
func (packet *packetMock) Ack(err error) {
}
// GetSourceEnvelope Returns the Envelope the ReceivedMessage was
// constructed with
func (packet *packetMock) GetSourceEnvelope() *proto.Envelope {
return nil
}
// GetGossipMessage returns the underlying GossipMessage
func (packet *packetMock) GetGossipMessage() *proto.SignedGossipMessage {
return packet.msg.(*proto.SignedGossipMessage)
}
// GetConnectionInfo returns information about the remote peer
// that sent the message
func (packet *packetMock) GetConnectionInfo() *proto.ConnectionInfo {
return nil
}
func (mock *commMock) start() {
logger.Debug("Starting communication mock module...")
for {
select {
case <-mock.done:
{
// Got final signal, exiting...
logger.Debug("Exiting...")
return
}
case msg := <-mock.members[mock.id].socket:
{
logger.Debug("Got new message", msg)
packet := msg.(*packetMock)
for _, channel := range mock.acceptors {
// if message acceptor agrees to get
// new message forward it to the received
// messages channel
if channel.accept(packet) {
channel.channel <- packet
}
}
}
}
}
}
// GetPKIid returns this instance's PKI id
func (mock *commMock) GetPKIid() common.PKIidType {
return common.PKIidType(mock.id)
}
// Send sends a message to remote peers
func (mock *commMock) Send(msg *proto.SignedGossipMessage, peers ...*comm.RemotePeer) {
for _, peer := range peers {
logger.Debug("Sending message to peer ", peer.Endpoint, "from ", mock.id)
mock.members[peer.Endpoint].socket <- &packetMock{
src: mock.members[mock.id],
dst: mock.members[peer.Endpoint],
msg: msg,
}
}
}
func (mock *commMock) SendWithAck(_ *proto.SignedGossipMessage, _ time.Duration, _ int, _ ...*comm.RemotePeer) comm.AggregatedSendResult {
panic("not implemented")
}
// Probe probes a remote node and returns nil if its responsive,
// and an error if it's not.
func (mock *commMock) Probe(peer *comm.RemotePeer) error {
return nil
}
// Handshake authenticates a remote peer and returns
// (its identity, nil) on success and (nil, error)
func (mock *commMock) Handshake(peer *comm.RemotePeer) (api.PeerIdentityType, error) {
return nil, nil
}
// Accept returns a dedicated read-only channel for messages sent by other nodes that match a certain predicate.
// Each message from the channel can be used to send a reply back to the sender
func (mock *commMock) Accept(accept common.MessageAcceptor) <-chan proto.ReceivedMessage {
ch := make(chan proto.ReceivedMessage)
mock.acceptors = append(mock.acceptors, &channelMock{accept, ch})
return ch
}
// PresumedDead returns a read-only channel for node endpoints that are suspected to be offline
func (mock *commMock) PresumedDead() <-chan common.PKIidType {
return mock.deadChannel
}
// CloseConn closes a connection to a certain endpoint
func (mock *commMock) CloseConn(peer *comm.RemotePeer) {
// NOOP
}
// Stop stops the module
func (mock *commMock) Stop() {
logger.Debug("Stopping communication module, closing all accepting channels.")
for _, accept := range mock.acceptors {
close(accept.channel)
}
logger.Debug("[XXX]: Sending done signal to close the module.")
mock.done <- struct{}{}
}