Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
phearnot committed Sep 12, 2024
1 parent 4a2531e commit 4482f54
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 23 deletions.
6 changes: 6 additions & 0 deletions node/src/main/resources/network-defaults.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ waves.defaults {
network {
port = 6863

min-connections = 5

known-peers = [
"peers-testnet.wavesnodes.com:6868"
]
Expand Down Expand Up @@ -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"
]
Expand All @@ -152,6 +156,8 @@ waves.defaults {
network {
port = 6862

min-connections = 3

known-peers = [
"peers-stagenet.wavesnodes.com:6868"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -28,6 +27,7 @@ case class NetworkSettings(
maxInboundConnections: Int,
maxOutboundConnections: Int,
maxConnectionsPerHost: Int,
minConnections: Option[Int],
connectionTimeout: FiniteDuration,
maxUnverifiedPeers: Int,
enablePeersExchange: Boolean,
Expand Down Expand Up @@ -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")
Expand All @@ -88,6 +89,7 @@ object NetworkSettings {
maxInboundConnections,
maxOutboundConnections,
maxConnectionsFromSingleHost,
minConnections,
connectionTimeout,
maxUnverifiedPeers,
enablePeersExchange,
Expand Down
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -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))
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}
}
Expand Down

0 comments on commit 4482f54

Please sign in to comment.