<commit_before>package scorex.network.peer

import java.net.{InetAddress, InetSocketAddress}
import java.nio.file.Files

import com.typesafe.config.ConfigFactory
import com.wavesplatform.network.PeerDatabaseImpl
import com.wavesplatform.settings.NetworkSettings
import net.ceedubs.ficus.Ficus._
import org.scalatest.{Matchers, path}

class PeerDatabaseImplSpecification extends path.FreeSpecLike with Matchers {

  private val config1 = ConfigFactory.parseString(
    """waves.network {
      |  file = null
      |  known-peers = []
      |  peers-data-residence-time: 2s
      |}""".stripMargin).withFallback(ConfigFactory.load()).resolve()
  private val settings1 = config1.as[NetworkSettings]("waves.network")

  private val config2 = ConfigFactory.parseString(
    """waves.network {
      |  file = null
      |  known-peers = []
      |  peers-data-residence-time: 10s
      |}""".stripMargin).withFallback(ConfigFactory.load()).resolve()
  private val settings2 = config2.as[NetworkSettings]("waves.network")

  val database = new PeerDatabaseImpl(settings1)
  val database2 = new PeerDatabaseImpl(settings2)
  val host1 = "1.1.1.1"
  val host2 = "2.2.2.2"
  val address1 = new InetSocketAddress(host1, 1)
  val address2 = new InetSocketAddress(host2, 2)

  "Peer database" - {
    "new peer should not appear in internal buffer but does not appear in database" in {
      database.knownPeers shouldBe empty
      database.addCandidate(address1)
      database.randomPeer(Set()) should contain(address1)
      database.knownPeers shouldBe empty
    }

    "new peer should move from internal buffer to database" in {
      database.knownPeers shouldBe empty
      database.addCandidate(address1)
      database.knownPeers shouldBe empty
      database.touch(address1)
      database.knownPeers.keys should contain(address1)
    }

    "peer should should became obsolete after time" in {
      database.touch(address1)
      database.knownPeers.keys should contain(address1)
      sleepLong()
      database.knownPeers shouldBe empty
      database.randomPeer(Set()) shouldBe empty
    }

    "touching peer prevent it from obsoleting" in {
      database.addCandidate(address1)
      database.touch(address1)
      sleepLong()
      database.touch(address1)
      sleepShort()
      database.knownPeers.keys should contain(address1)
    }

    "blacklisted peer should disappear from internal buffer and database" in {
      database.touch(address1)
      database.addCandidate(address2)
      database.knownPeers.keys should contain(address1)
      database.knownPeers.keys should not contain address2

      database.blacklist(InetAddress.getByName(host1), "")
      database.knownPeers.keys should not contain address1
      database.knownPeers should be(empty)

      database.randomPeer(Set()) should contain(address2)
      database.blacklist(InetAddress.getByName(host2), "")
      database.randomPeer(Set()) should not contain address2
      database.randomPeer(Set()) should be(empty)
    }

    "random peer should return peers from both from database and buffer" in {
      database2.touch(address1)
      database2.addCandidate(address2)
      val keys = database2.knownPeers.keys
      keys should contain(address1)
      keys should not contain address2

      val set = (1 to 10).flatMap(i => database2.randomPeer(Set())).toSet

      set should contain(address1)
      set should contain(address2)
    }

    "filters out excluded candidates" in {
      database.addCandidate(address1)
      database.addCandidate(address1)
      database.addCandidate(address2)

      database.randomPeer(Set(address1)) should contain(address2)
    }

    "if blacklisting is disable" - {
      "should clear blacklist at start" in {
        val databaseFile = Files.createTempFile("waves-tests", "PeerDatabaseImplSpecification-blacklisting-clear")

        val prevConfig = ConfigFactory.parseString(
          s"""waves.network {
             |  file = ${databaseFile.toAbsolutePath}
             |  known-peers = []
             |  peers-data-residence-time: 100s
             |}""".stripMargin).withFallback(ConfigFactory.load()).resolve()
        val prevSettings = prevConfig.as[NetworkSettings]("waves.network")
        val prevDatabase = new PeerDatabaseImpl(prevSettings)
        prevDatabase.blacklist(address1.getAddress, "I don't like it")
        prevDatabase.close()

        val config = ConfigFactory.parseString(
          s"""waves.network {
            |  file = ${databaseFile.toAbsolutePath}
            |  known-peers = []
            |  peers-data-residence-time: 100s
            |  enable-blacklisting = no
            |}""".stripMargin).withFallback(ConfigFactory.load()).resolve()
        val settings = config.as[NetworkSettings]("waves.network")
        val database = new PeerDatabaseImpl(settings)

        database.blacklistedHosts shouldBe empty<commit_msg>NODE-269: fix separator chars<commit_after>package scorex.network.peer

import java.io.File
import java.net.{InetAddress, InetSocketAddress}
import java.nio.file.Files

import com.typesafe.config.ConfigFactory
import com.wavesplatform.network.PeerDatabaseImpl
import com.wavesplatform.settings.NetworkSettings
import net.ceedubs.ficus.Ficus._
import org.scalatest.{Matchers, path}

class PeerDatabaseImplSpecification extends path.FreeSpecLike with Matchers {

  private val config1 = ConfigFactory.parseString(
    """waves.network {
      |  file = null
      |  known-peers = []
      |  peers-data-residence-time: 2s
      |}""".stripMargin).withFallback(ConfigFactory.load()).resolve()
  private val settings1 = config1.as[NetworkSettings]("waves.network")

  private val config2 = ConfigFactory.parseString(
    """waves.network {
      |  file = null
      |  known-peers = []
      |  peers-data-residence-time: 10s
      |}""".stripMargin).withFallback(ConfigFactory.load()).resolve()
  private val settings2 = config2.as[NetworkSettings]("waves.network")

  val database = new PeerDatabaseImpl(settings1)
  val database2 = new PeerDatabaseImpl(settings2)
  val host1 = "1.1.1.1"
  val host2 = "2.2.2.2"
  val address1 = new InetSocketAddress(host1, 1)
  val address2 = new InetSocketAddress(host2, 2)

  "Peer database" - {
    "new peer should not appear in internal buffer but does not appear in database" in {
      database.knownPeers shouldBe empty
      database.addCandidate(address1)
      database.randomPeer(Set()) should contain(address1)
      database.knownPeers shouldBe empty
    }

    "new peer should move from internal buffer to database" in {
      database.knownPeers shouldBe empty
      database.addCandidate(address1)
      database.knownPeers shouldBe empty
      database.touch(address1)
      database.knownPeers.keys should contain(address1)
    }

    "peer should should became obsolete after time" in {
      database.touch(address1)
      database.knownPeers.keys should contain(address1)
      sleepLong()
      database.knownPeers shouldBe empty
      database.randomPeer(Set()) shouldBe empty
    }

    "touching peer prevent it from obsoleting" in {
      database.addCandidate(address1)
      database.touch(address1)
      sleepLong()
      database.touch(address1)
      sleepShort()
      database.knownPeers.keys should contain(address1)
    }

    "blacklisted peer should disappear from internal buffer and database" in {
      database.touch(address1)
      database.addCandidate(address2)
      database.knownPeers.keys should contain(address1)
      database.knownPeers.keys should not contain address2

      database.blacklist(InetAddress.getByName(host1), "")
      database.knownPeers.keys should not contain address1
      database.knownPeers should be(empty)

      database.randomPeer(Set()) should contain(address2)
      database.blacklist(InetAddress.getByName(host2), "")
      database.randomPeer(Set()) should not contain address2
      database.randomPeer(Set()) should be(empty)
    }

    "random peer should return peers from both from database and buffer" in {
      database2.touch(address1)
      database2.addCandidate(address2)
      val keys = database2.knownPeers.keys
      keys should contain(address1)
      keys should not contain address2

      val set = (1 to 10).flatMap(i => database2.randomPeer(Set())).toSet

      set should contain(address1)
      set should contain(address2)
    }

    "filters out excluded candidates" in {
      database.addCandidate(address1)
      database.addCandidate(address1)
      database.addCandidate(address2)

      database.randomPeer(Set(address1)) should contain(address2)
    }

    "if blacklisting is disable" - {
      "should clear blacklist at start" in {
        val databaseFile = Files.createTempFile("waves-tests", "PeerDatabaseImplSpecification-blacklisting-clear").toAbsolutePath.toString
        val path = if (File.separatorChar == '\\') databaseFile.replace('\\', '/') else databaseFile
        val prevConfig = ConfigFactory.parseString(
          s"""waves.network {
             |  file = "$path"
             |  known-peers = []
             |  peers-data-residence-time: 100s
             |}""".stripMargin).withFallback(ConfigFactory.load()).resolve()
        val prevSettings = prevConfig.as[NetworkSettings]("waves.network")
        val prevDatabase = new PeerDatabaseImpl(prevSettings)
        prevDatabase.blacklist(address1.getAddress, "I don't like it")
        prevDatabase.close()

        val config = ConfigFactory.parseString(
          s"""waves.network {
             |  file = "$path"
             |  known-peers = []
             |  peers-data-residence-time: 100s
             |  enable-blacklisting = no
             |}""".stripMargin).withFallback(ConfigFactory.load()).resolve()
        val settings = config.as[NetworkSettings]("waves.network")
        val database = new PeerDatabaseImpl(settings)

        database.blacklistedHosts shouldBe empty
{'fraction_non_alphanumeric': 0.09811434909750932, 'fraction_numerical': 0.015024705051931028, 'mean_word_length': 3.1865766146053187, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1891798', 'n_tokens_mistral': 2866, 'n_tokens_neox': 2765, 'n_words': 627}