@@ -1207,3 +1207,141 @@ func TestP2PwsStreamHandlerDedup(t *testing.T) {
1207
1207
require .False (t , netA .hasPeers ())
1208
1208
require .False (t , netB .hasPeers ())
1209
1209
}
1210
+
1211
+ // TestP2PEnableGossipService_NodeDisable ensures that a node with EnableGossipService=false
1212
+ // still can participate in the network by sending and receiving messages.
1213
+ func TestP2PEnableGossipService_NodeDisable (t * testing.T ) {
1214
+ partitiontest .PartitionTest (t )
1215
+
1216
+ log := logging .TestingLog (t )
1217
+
1218
+ // prepare configs
1219
+ cfg := config .GetDefaultLocal ()
1220
+ cfg .DNSBootstrapID = "" // disable DNS lookups since the test uses phonebook addresses
1221
+
1222
+ relayCfg := cfg
1223
+ relayCfg .NetAddress = "127.0.0.1:0"
1224
+
1225
+ nodeCfg := cfg
1226
+ nodeCfg .EnableGossipService = false
1227
+ nodeCfg2 := nodeCfg
1228
+ nodeCfg2 .NetAddress = "127.0.0.1:0"
1229
+
1230
+ tests := []struct {
1231
+ name string
1232
+ relayCfg config.Local
1233
+ nodeCfg config.Local
1234
+ }{
1235
+ {"non-listening-node" , relayCfg , nodeCfg },
1236
+ {"listening-node" , relayCfg , nodeCfg2 },
1237
+ }
1238
+ for _ , test := range tests {
1239
+ t .Run (test .name , func (t * testing.T ) {
1240
+ relayCfg := test .relayCfg
1241
+ netA , err := NewP2PNetwork (log , relayCfg , "" , nil , genesisID , config .Devtestnet , & nopeNodeInfo {}, nil )
1242
+ require .NoError (t , err )
1243
+ netA .Start ()
1244
+ defer netA .Stop ()
1245
+
1246
+ peerInfoA := netA .service .AddrInfo ()
1247
+ addrsA , err := peer .AddrInfoToP2pAddrs (& peerInfoA )
1248
+ require .NoError (t , err )
1249
+ require .NotZero (t , addrsA [0 ])
1250
+ multiAddrStr := addrsA [0 ].String ()
1251
+ phoneBookAddresses := []string {multiAddrStr }
1252
+
1253
+ // start netB with gossip service disabled
1254
+ nodeCfg := test .nodeCfg
1255
+ netB , err := NewP2PNetwork (log , nodeCfg , "" , phoneBookAddresses , genesisID , config .Devtestnet , & nopeNodeInfo {}, nil )
1256
+ require .NoError (t , err )
1257
+ netB .Start ()
1258
+ defer netB .Stop ()
1259
+
1260
+ require .Eventually (t , func () bool {
1261
+ return netA .hasPeers () && netB .hasPeers ()
1262
+ }, 1 * time .Second , 50 * time .Millisecond )
1263
+
1264
+ testTag := protocol .AgreementVoteTag
1265
+
1266
+ var handlerCountA atomic.Uint32
1267
+ passThroughHandlerA := []TaggedMessageHandler {
1268
+ {Tag : testTag , MessageHandler : HandlerFunc (func (msg IncomingMessage ) OutgoingMessage {
1269
+ handlerCountA .Add (1 )
1270
+ return OutgoingMessage {Action : Broadcast }
1271
+ })},
1272
+ }
1273
+ var handlerCountB atomic.Uint32
1274
+ passThroughHandlerB := []TaggedMessageHandler {
1275
+ {Tag : testTag , MessageHandler : HandlerFunc (func (msg IncomingMessage ) OutgoingMessage {
1276
+ handlerCountB .Add (1 )
1277
+ return OutgoingMessage {Action : Broadcast }
1278
+ })},
1279
+ }
1280
+ netA .RegisterHandlers (passThroughHandlerA )
1281
+ netB .RegisterHandlers (passThroughHandlerB )
1282
+
1283
+ // send messages from both nodes to each other and confirm they are received.
1284
+ for i := 0 ; i < 10 ; i ++ {
1285
+ err = netA .Broadcast (context .Background (), testTag , []byte (fmt .Sprintf ("hello from A %d" , i )), false , nil )
1286
+ require .NoError (t , err )
1287
+ err = netB .Broadcast (context .Background (), testTag , []byte (fmt .Sprintf ("hello from B %d" , i )), false , nil )
1288
+ require .NoError (t , err )
1289
+ }
1290
+
1291
+ require .Eventually (
1292
+ t ,
1293
+ func () bool {
1294
+ return handlerCountA .Load () == 10 && handlerCountB .Load () == 10
1295
+ },
1296
+ 2 * time .Second ,
1297
+ 50 * time .Millisecond ,
1298
+ )
1299
+ })
1300
+ }
1301
+ }
1302
+
1303
+ // TestP2PEnableGossipService_BothDisable checks if both relay and node have EnableGossipService=false
1304
+ // they do not gossip to each other.
1305
+ //
1306
+ // Note, this test checks a configuration where node A (relay) does not know about node B,
1307
+ // and node B is configured to connect to A, and this scenario rejecting logic is guaranteed to work.
1308
+ func TestP2PEnableGossipService_BothDisable (t * testing.T ) {
1309
+ partitiontest .PartitionTest (t )
1310
+
1311
+ log := logging .TestingLog (t )
1312
+
1313
+ // prepare configs
1314
+ cfg := config .GetDefaultLocal ()
1315
+ cfg .DNSBootstrapID = "" // disable DNS lookups since the test uses phonebook addresses
1316
+ cfg .EnableGossipService = false // disable gossip service by default
1317
+
1318
+ relayCfg := cfg
1319
+ relayCfg .NetAddress = "127.0.0.1:0"
1320
+
1321
+ netA , err := NewP2PNetwork (log .With ("net" , "netA" ), relayCfg , "" , nil , genesisID , config .Devtestnet , & nopeNodeInfo {}, nil )
1322
+ require .NoError (t , err )
1323
+ netA .Start ()
1324
+ defer netA .Stop ()
1325
+
1326
+ peerInfoA := netA .service .AddrInfo ()
1327
+ addrsA , err := peer .AddrInfoToP2pAddrs (& peerInfoA )
1328
+ require .NoError (t , err )
1329
+ require .NotZero (t , addrsA [0 ])
1330
+ multiAddrStr := addrsA [0 ].String ()
1331
+ phoneBookAddresses := []string {multiAddrStr }
1332
+
1333
+ nodeCfg := cfg
1334
+ nodeCfg .NetAddress = ""
1335
+
1336
+ netB , err := NewP2PNetwork (log .With ("net" , "netB" ), nodeCfg , "" , phoneBookAddresses , genesisID , config .Devtestnet , & nopeNodeInfo {}, nil )
1337
+ require .NoError (t , err )
1338
+ netB .Start ()
1339
+ defer netB .Stop ()
1340
+
1341
+ require .Eventually (t , func () bool {
1342
+ return len (netA .service .Conns ()) > 0 && len (netB .service .Conns ()) > 0
1343
+ }, 1 * time .Second , 50 * time .Millisecond )
1344
+
1345
+ require .False (t , netA .hasPeers ())
1346
+ require .False (t , netB .hasPeers ())
1347
+ }
0 commit comments