@@ -223,7 +223,7 @@ func (c *testComm) ListNodes() []NodeID {
223223
224224func (c * testComm ) SendMessage (msg * Message , destination NodeID ) {
225225 // cannot send if either [from] or [destination] is not connected
226- if ! c .net .IsConnected (destination ) || ! c .net .IsConnected (c .from ) {
226+ if c .net .IsDisconnected (destination ) || c .net .IsDisconnected (c .from ) {
227227 return
228228 }
229229
@@ -239,16 +239,13 @@ func (c *testComm) SendMessage(msg *Message, destination NodeID) {
239239}
240240
241241func (c * testComm ) Broadcast (msg * Message ) {
242- if ! c .net .IsConnected (c .from ) {
242+ if c .net .IsDisconnected (c .from ) {
243243 return
244244 }
245245
246246 for _ , instance := range c .net .instances {
247- // Skip sending the message to yourself
248- if bytes .Equal (c .from , instance .e .ID ) {
249- continue
250- }
251- if ! c .net .IsConnected (instance .e .ID ) {
247+ // Skip sending the message to yourself or disconnected nodes
248+ if bytes .Equal (c .from , instance .e .ID ) || c .net .IsDisconnected (instance .e .ID ) {
252249 continue
253250 }
254251
@@ -263,22 +260,18 @@ type inMemNetwork struct {
263260 t * testing.T
264261 nodes []NodeID
265262 instances []* testNode
266- connected map [string ]bool
263+ lock sync.Mutex
264+ disconnected map [string ]struct {}
267265}
268266
269267// newInMemNetwork creates an in-memory network. Node IDs must be provided before
270268// adding instances, as nodes require prior knowledge of all participants.
271269func newInMemNetwork (t * testing.T , nodes []NodeID ) * inMemNetwork {
272- connected := make (map [string ]bool )
273- for _ , node := range nodes {
274- connected [string (node )] = true
275- }
276-
277270 net := & inMemNetwork {
278271 t : t ,
279272 nodes : nodes ,
280273 instances : make ([]* testNode , 0 ),
281- connected : connected ,
274+ disconnected : make ( map [ string ] struct {}) ,
282275 }
283276 return net
284277}
@@ -295,21 +288,26 @@ func (n *inMemNetwork) addNode(node *testNode) {
295288 n .instances = append (n .instances , node )
296289}
297290
298- func (n * inMemNetwork ) IsConnected (node NodeID ) bool {
299- for _ , instance := range n .instances {
300- if bytes .Equal (instance .e .ID , node ) {
301- return n .connected [string (node )]
302- }
303- }
304- return false
291+ func (n * inMemNetwork ) IsDisconnected (node NodeID ) bool {
292+ n .lock .Lock ()
293+ defer n .lock .Unlock ()
294+
295+ _ , ok := n .disconnected [string (node )]
296+ return ok
305297}
306298
307299func (n * inMemNetwork ) Connect (node NodeID ) {
308- n .connected [string (node )] = true
300+ n .lock .Lock ()
301+ defer n .lock .Unlock ()
302+
303+ delete (n .disconnected , string (node ))
309304}
310305
311306func (n * inMemNetwork ) Disconnect (node NodeID ) {
312- n .connected [string (node )] = false
307+ n .lock .Lock ()
308+ defer n .lock .Unlock ()
309+
310+ n .disconnected [string (node )] = struct {}{}
313311}
314312
315313// startInstances starts all instances in the network.
0 commit comments