diff --git a/node/src/main/resources/network-defaults.conf b/node/src/main/resources/network-defaults.conf index ac578b8981..2412bdf84a 100644 --- a/node/src/main/resources/network-defaults.conf +++ b/node/src/main/resources/network-defaults.conf @@ -4,6 +4,8 @@ waves.defaults { network { port = 6863 + min-connections = 5 + known-peers = [ "peers-testnet.wavesnodes.com:6868" ] @@ -135,6 +137,8 @@ waves.defaults { # Node name to send during handshake. Comment this string out to set random node name. # node-name = "My MAINNET node" + min-connections = 5 + known-peers = [ "peers.wavesnodes.com:6868" ] @@ -152,6 +156,8 @@ waves.defaults { network { port = 6862 + min-connections = 3 + known-peers = [ "peers-stagenet.wavesnodes.com:6868" ] diff --git a/node/src/main/scala/com/wavesplatform/network/NetworkServer.scala b/node/src/main/scala/com/wavesplatform/network/NetworkServer.scala index ea34a48cf5..35e4425f5b 100644 --- a/node/src/main/scala/com/wavesplatform/network/NetworkServer.scala +++ b/node/src/main/scala/com/wavesplatform/network/NetworkServer.scala @@ -203,7 +203,7 @@ object NetworkServer extends ScorexLogging { } def scheduleConnectTask(): Unit = if (!shutdownInitiated) { - val delay = (if (peerConnectionsMap.isEmpty) AverageHandshakePeriod else 5.seconds) + + val delay = (if (peerConnectionsMap.isEmpty || networkSettings.minConnections.exists(_ < peerConnectionsMap.size())) AverageHandshakePeriod else 5.seconds) + (Random.nextInt(1000) - 500).millis // add some noise so that nodes don't attempt to connect to each other simultaneously workerGroup.schedule(delay) { diff --git a/node/src/main/scala/com/wavesplatform/settings/NetworkSettings.scala b/node/src/main/scala/com/wavesplatform/settings/NetworkSettings.scala index 0624d0003b..e5d8a54037 100644 --- a/node/src/main/scala/com/wavesplatform/settings/NetworkSettings.scala +++ b/node/src/main/scala/com/wavesplatform/settings/NetworkSettings.scala @@ -1,15 +1,14 @@ package com.wavesplatform.settings -import java.io.File -import java.net.{InetSocketAddress, URI} - import com.typesafe.config.Config import com.wavesplatform.network.TrafficLogger -import com.wavesplatform.utils._ -import net.ceedubs.ficus.Ficus._ -import net.ceedubs.ficus.readers.ArbitraryTypeReader._ +import com.wavesplatform.utils.* +import net.ceedubs.ficus.Ficus.* +import net.ceedubs.ficus.readers.ArbitraryTypeReader.* import net.ceedubs.ficus.readers.ValueReader +import java.io.File +import java.net.{InetSocketAddress, URI} import scala.concurrent.duration.FiniteDuration import scala.util.Random @@ -28,6 +27,7 @@ case class NetworkSettings( maxInboundConnections: Int, maxOutboundConnections: Int, maxConnectionsPerHost: Int, + minConnections: Option[Int], connectionTimeout: FiniteDuration, maxUnverifiedPeers: Int, enablePeersExchange: Boolean, @@ -64,6 +64,7 @@ object NetworkSettings { val maxInboundConnections = config.as[Int]("max-inbound-connections") val maxOutboundConnections = config.as[Int]("max-outbound-connections") val maxConnectionsFromSingleHost = config.as[Int]("max-single-host-connections") + val minConnections = config.getAs[Int]("max-single-host-connections") val connectionTimeout = config.as[FiniteDuration]("connection-timeout") val maxUnverifiedPeers = config.as[Int]("max-unverified-peers") val enablePeersExchange = config.as[Boolean]("enable-peers-exchange") @@ -88,6 +89,7 @@ object NetworkSettings { maxInboundConnections, maxOutboundConnections, maxConnectionsFromSingleHost, + minConnections, connectionTimeout, maxUnverifiedPeers, enablePeersExchange, diff --git a/node/src/test/scala/com/wavesplatform/network/BlacklistParallelSpecification.scala b/node/src/test/scala/com/wavesplatform/network/BlacklistParallelSpecification.scala index eb0a37d5db..356ed4e678 100644 --- a/node/src/test/scala/com/wavesplatform/network/BlacklistParallelSpecification.scala +++ b/node/src/test/scala/com/wavesplatform/network/BlacklistParallelSpecification.scala @@ -1,20 +1,20 @@ package com.wavesplatform.network -import java.net.{InetAddress, InetSocketAddress} - import com.typesafe.config.ConfigFactory import com.wavesplatform.settings.{NetworkSettings, loadConfig} import com.wavesplatform.test.FeatureSpec -import net.ceedubs.ficus.Ficus._ +import net.ceedubs.ficus.Ficus.* import org.scalatest.{GivenWhenThen, ParallelTestExecution} +import java.net.{InetAddress, InetSocketAddress} + class BlacklistParallelSpecification extends FeatureSpec with GivenWhenThen with ParallelTestExecution { private val config = loadConfig(ConfigFactory.parseString("""waves.network { - | known-peers = [] - | file = null - | black-list-residence-time: 1s - |}""".stripMargin)) + | known-peers = [] + | file = null + | black-list-residence-time: 1s + |}""".stripMargin)) private val networkSettings = config.as[NetworkSettings]("waves.network") @@ -34,24 +34,24 @@ class BlacklistParallelSpecification extends FeatureSpec with GivenWhenThen with val address3 = new InetSocketAddress(host3, 2) def isBlacklisted(address: InetSocketAddress): Boolean = - peerDatabase.blacklistedHosts.contains(address.getAddress) + peerDatabase.isBlacklisted(address.getAddress) Scenario("Peer blacklist another peer") { Given("Peer database is empty") assert(peerDatabase.knownPeers.isEmpty) - assert(peerDatabase.blacklistedHosts.isEmpty) + assert(peerDatabase.detailedBlacklist.isEmpty) When("Peer adds another peer to knownPeers") peerDatabase.touch(address1) assert(peerDatabase.knownPeers.contains(address1)) - assert(!peerDatabase.blacklistedHosts.contains(host1)) + assert(!peerDatabase.detailedBlacklist.contains(host1)) And("Peer blacklists another peer") val reason = "because" peerDatabase.blacklist(address1.getAddress, reason) assert(isBlacklisted(address1)) - assert(peerDatabase.blacklistedHosts.contains(host1)) + assert(peerDatabase.isBlacklisted(host1)) assert(peerDatabase.detailedBlacklist(host1)._2 == reason) assert(!peerDatabase.knownPeers.contains(address1)) assert(!peerDatabase.knownPeers.contains(address1)) @@ -70,7 +70,7 @@ class BlacklistParallelSpecification extends FeatureSpec with GivenWhenThen with Given("Peer database is empty") assert(peerDatabase.knownPeers.isEmpty) - assert(peerDatabase.blacklistedHosts.isEmpty) + assert(peerDatabase.detailedBlacklist.isEmpty) When("Peer adds other peers") peerDatabase.touch(address1) diff --git a/node/src/test/scala/com/wavesplatform/network/BlacklistSpecification.scala b/node/src/test/scala/com/wavesplatform/network/BlacklistSpecification.scala index 9d66f41e21..dcd3fdc75f 100644 --- a/node/src/test/scala/com/wavesplatform/network/BlacklistSpecification.scala +++ b/node/src/test/scala/com/wavesplatform/network/BlacklistSpecification.scala @@ -27,10 +27,10 @@ class BlacklistSpecification extends FeatureSpec with GivenWhenThen { Given("Peer database is empty") val peerDatabase = new PeerDatabaseImpl(networkSettings) - def isBlacklisted(address: InetSocketAddress) = peerDatabase.blacklistedHosts.contains(address.getAddress) + def isBlacklisted(address: InetSocketAddress) = peerDatabase.isBlacklisted(address.getAddress) assert(peerDatabase.knownPeers.isEmpty) - assert(peerDatabase.blacklistedHosts.isEmpty) + assert(peerDatabase.detailedBlacklist.isEmpty) When("Peer adds another peer to knownPeers") val address = new InetSocketAddress(InetAddress.getByName("localhost"), 1234) diff --git a/node/src/test/scala/com/wavesplatform/network/peer/PeerDatabaseImplSpecification.scala b/node/src/test/scala/com/wavesplatform/network/peer/PeerDatabaseImplSpecification.scala index 831f65bf18..413ce27bd1 100644 --- a/node/src/test/scala/com/wavesplatform/network/peer/PeerDatabaseImplSpecification.scala +++ b/node/src/test/scala/com/wavesplatform/network/peer/PeerDatabaseImplSpecification.scala @@ -147,7 +147,7 @@ class PeerDatabaseImplSpecification extends FreeSpec { val settings = config.as[NetworkSettings]("waves.network") val database = new PeerDatabaseImpl(settings) - database.blacklistedHosts shouldBe empty + database.detailedBlacklist shouldBe empty } "should not add nodes to the blacklist" in { @@ -164,7 +164,7 @@ class PeerDatabaseImplSpecification extends FreeSpec { val database = new PeerDatabaseImpl(settings) database.blacklist(address1.getAddress, "I don't like it") - database.blacklistedHosts shouldBe empty + database.detailedBlacklist shouldBe empty } } }