From 8063a1d61dd43ca35094d81df4c33c1fb0f56554 Mon Sep 17 00:00:00 2001 From: Dmitry Voronov Date: Sun, 20 Dec 2020 20:16:47 +0300 Subject: [PATCH 1/5] feat - cross-compilation for 2.13.4, fixed inexhaustive matches, removed deprecated stuff --- build.sc | 241 ++++++++++-------- .../ethereum/EthereumNodeRecord.scala | 5 +- .../ethereum/codecs/DefaultCodecs.scala | 16 ++ .../ethereum/v4/DiscoveryNetwork.scala | 10 +- .../io/iohk/scalanet/kademlia/KBuckets.scala | 20 +- .../io/iohk/scalanet/kademlia/KNetwork.scala | 1 - .../io/iohk/scalanet/kademlia/KRouter.scala | 2 +- .../io/iohk/scalanet/kademlia/TimeSet.scala | 5 +- .../src/io/iohk/scalanet/kconsole/App.scala | 1 - .../scalanet/kconsole/CommandParser.scala | 1 + .../io/iohk/scalanet/codec/FramingCodec.scala | 3 +- .../dynamictls/DynamicTLSPeerGroupUtils.scala | 8 +- .../peergroup/udp/DynamicUDPPeerGroup.scala | 3 +- .../peergroup/udp/StaticUDPPeerGroup.scala | 2 +- 14 files changed, 179 insertions(+), 139 deletions(-) diff --git a/build.sc b/build.sc index b3a7879a..f681773e 100644 --- a/build.sc +++ b/build.sc @@ -6,147 +6,164 @@ import coursier.maven.MavenRepository import mill.scalalib.{PublishModule, ScalaModule} import mill.scalalib.publish.{Developer, License, PomSettings, VersionControl} -trait ScalanetModule extends ScalaModule { - override def scalaVersion = "2.12.10" +object csm extends Cross[ScalanetModule]("2.12.10", "2.13.4") - override def repositories = - super.repositories ++ Seq( - MavenRepository("https://oss.sonatype.org/content/repositories/releases"), - MavenRepository("https://oss.sonatype.org/content/repositories/snapshots") - ) +class ScalanetModule(crossVersion: String) extends Module { + override def millSourcePath = super.millSourcePath / os.up / os.up - private val commonScalacOptions = Seq( - "-unchecked", - "-language:postfixOps", - "-deprecation", - "-feature", - "-Xfatal-warnings", + private val pointTwelveOptions: Seq[String] = Seq( "-Xlint:unsound-match", "-Ywarn-inaccessible", "-Ywarn-unused-import", "-Ypartial-unification", - "-J-Xmx1.5G", - "-J-Xms1.5G", - "-J-XX:MaxMetaspaceSize=512m", - "-encoding", - "utf-8" + "-Ywarn-value-discard" ) - override def scalacOptions = - commonScalacOptions ++ Seq("-Ywarn-value-discard") + private val pointThirteenOptions: Seq[String] = Nil + + trait ScalanetModule extends ScalaModule { + override def scalaVersion = crossVersion + + override def repositories = + super.repositories ++ Seq( + MavenRepository("https://oss.sonatype.org/content/repositories/releases"), + MavenRepository("https://oss.sonatype.org/content/repositories/snapshots") + ) + + private val commonScalacOptions = Seq( + "-unchecked", + "-language:postfixOps", + "-deprecation", + "-feature", + "-Xfatal-warnings", + "-J-Xmx1.5G", + "-J-Xms1.5G", + "-J-XX:MaxMetaspaceSize=512m", + "-encoding", + "utf-8" + ) - // `extends Tests` uses the context of the module in which it's defined, - // which is why the trait is defined here not within `scalanet`, otherwise - // it wouldn't work for `kademlia` for example. - trait TestModule extends Tests { - override def scalacOptions = - commonScalacOptions + private val versionOptions = crossVersion.take(4) match { + case "2.12" => pointTwelveOptions + case "2.13" => pointThirteenOptions + } - override def testFrameworks = - Seq("org.scalatest.tools.Framework") + override def scalacOptions = commonScalacOptions ++ versionOptions - override def ivyDeps = Agg( - ivy"org.scalatest::scalatest:3.0.5", - ivy"org.scalacheck::scalacheck:1.14.0", - ivy"ch.qos.logback:logback-core:1.2.3", - ivy"ch.qos.logback:logback-classic:1.2.3", - ivy"org.mockito:mockito-core:2.21.0" - ) + // `extends Tests` uses the context of the module in which it's defined, + // which is why the trait is defined here not within `scalanet`, otherwise + // it wouldn't work for `kademlia` for example. + trait TestModule extends Tests { + override def scalacOptions = Nil + //commonScalacOptions + + override def testFrameworks = + Seq("org.scalatest.tools.Framework") - override def moduleDeps: Seq[JavaModule] = - Seq(scalanet) + override def ivyDeps = Agg( + ivy"org.scalatest::scalatest:3.0.5", + ivy"org.scalacheck::scalacheck:1.14.0", + ivy"ch.qos.logback:logback-core:1.2.3", + ivy"ch.qos.logback:logback-classic:1.2.3", + ivy"org.mockito:mockito-core:2.21.0" + ) + + override def moduleDeps: Seq[JavaModule] = + Seq(scalanet) - def single(args: String*) = T.command { - super.runMain("org.scalatest.run", args: _*) + def single(args: String*) = T.command { + super.runMain("org.scalatest.run", args: _*) + } } } -} -// In objects inheriting this trait, use `override def moduleDeps: Seq[PublishModule]` -// to point at other modules that also get published. In other cases such as tests -// it can be `override def moduleDeps: Seq[JavaModule]`. -trait ScalanetPublishModule extends PublishModule { - def description: String - - override def publishVersion = "0.5.0-SNAPSHOT" - - override def pomSettings = PomSettings( - description = description, - organization = "io.iohk", - url = "https://github.com/input-output-hk/scalanet", - licenses = Seq(License.`Apache-2.0`), - versionControl = VersionControl.github("input-output-hk", "scalanet"), - developers = Seq() - ) -} -// ScoverageModule creates bug when using custom repositories: -// https://github.com/lihaoyi/mill/issues/620 -//object scalanet extends ScalaModule with PublishModule with ScoverageModule { -object scalanet extends ScalanetModule with ScalanetPublishModule { - - override val description = - "Asynchronous, strongly typed, resource-managed networking library, written in Scala with support for a variety of network technologies" - - override def ivyDeps = Agg( - ivy"io.monix::monix:3.2.2", - ivy"com.github.nscala-time::nscala-time:2.22.0", - ivy"com.chuusai::shapeless:2.3.3", - ivy"com.typesafe.scala-logging::scala-logging:3.9.2", - ivy"com.github.jgonian:commons-ip-math:1.32", - ivy"org.slf4j:slf4j-api:1.7.25", - ivy"io.netty:netty-all:4.1.51.Final", - ivy"org.eclipse.californium:scandium:2.0.0-M15", - ivy"org.eclipse.californium:element-connector:2.0.0-M15", - ivy"org.scodec::scodec-bits:1.1.12", - ivy"org.scodec::scodec-core:1.11.7", - ivy"org.bouncycastle:bcprov-jdk15on:1.64", - ivy"org.bouncycastle:bcpkix-jdk15on:1.64", - ivy"org.bouncycastle:bctls-jdk15on:1.64" - ) + // In objects inheriting this trait, use `override def moduleDeps: Seq[PublishModule]` + // to point at other modules that also get published. In other cases such as tests + // it can be `override def moduleDeps: Seq[JavaModule]`. + trait ScalanetPublishModule extends PublishModule { + def description: String - def scoverageVersion = "1.3.1" + override def publishVersion = "0.5.0-SNAPSHOT" - // Scoverage disabled - // object test extends ScoverageTests { - object ut extends TestModule + override def pomSettings = PomSettings( + description = description, + organization = "io.iohk", + url = "https://github.com/input-output-hk/scalanet", + licenses = Seq(License.`Apache-2.0`), + versionControl = VersionControl.github("input-output-hk", "scalanet"), + developers = Seq() + ) + } - object discovery extends ScalanetModule with ScalanetPublishModule { + // ScoverageModule creates bug when using custom repositories: + // https://github.com/lihaoyi/mill/issues/620 + //object scalanet extends ScalaModule with PublishModule with ScoverageModule { + object scalanet extends ScalanetModule with ScalanetPublishModule { override val description = - "Implementation of peer-to-peer discovery algorithms such as that of Ethereum." + "Asynchronous, strongly typed, resource-managed networking library, written in Scala with support for a variety of network technologies" - override def moduleDeps: Seq[PublishModule] = - Seq(scalanet) + override def ivyDeps = Agg( + ivy"io.monix::monix:3.3.0", + ivy"com.github.nscala-time::nscala-time:2.22.0", + ivy"com.chuusai::shapeless:2.3.3", + ivy"com.typesafe.scala-logging::scala-logging:3.9.2", + ivy"com.github.jgonian:commons-ip-math:1.32", + ivy"org.slf4j:slf4j-api:1.7.25", + ivy"io.netty:netty-all:4.1.51.Final", + ivy"org.eclipse.californium:scandium:2.0.0-M15", + ivy"org.eclipse.californium:element-connector:2.0.0-M15", + ivy"org.scodec::scodec-bits:1.1.12", + ivy"org.scodec::scodec-core:1.11.7", + ivy"org.bouncycastle:bcprov-jdk15on:1.64", + ivy"org.bouncycastle:bcpkix-jdk15on:1.64", + ivy"org.bouncycastle:bctls-jdk15on:1.64" + ) - object ut extends TestModule { - override def moduleDeps: Seq[JavaModule] = - super.moduleDeps ++ Seq(scalanet.discovery, scalanet.ut) - } + def scoverageVersion = "1.3.1" - object it extends TestModule { - override def moduleDeps: Seq[JavaModule] = - super.moduleDeps ++ Seq(scalanet.discovery.ut) - } - } + // Scoverage disabled + // object test extends ScoverageTests { + object ut extends TestModule - object examples extends ScalanetModule { - override def ivyDeps = Agg( - ivy"ch.qos.logback:logback-core:1.2.3", - ivy"ch.qos.logback:logback-classic:1.2.3", - ivy"org.mockito:mockito-core:2.21.0", - ivy"com.github.pureconfig::pureconfig:0.11.1", - ivy"com.github.scopt::scopt:3.7.1", - ivy"io.monix::monix:3.2.2", - ivy"org.scala-lang.modules::scala-parser-combinators:1.1.2" - ) + object discovery extends ScalanetModule with ScalanetPublishModule { + + override val description = + "Implementation of peer-to-peer discovery algorithms such as that of Ethereum." - override def moduleDeps: Seq[JavaModule] = - Seq(scalanet, scalanet.discovery) + override def moduleDeps: Seq[PublishModule] = + Seq(scalanet) + + object ut extends TestModule { + override def moduleDeps: Seq[JavaModule] = + super.moduleDeps ++ Seq(scalanet.discovery, scalanet.ut) + } + + object it extends TestModule { + override def moduleDeps: Seq[JavaModule] = + super.moduleDeps ++ Seq(scalanet.discovery.ut) + } + } + + object examples extends ScalanetModule { + override def ivyDeps = Agg( + ivy"ch.qos.logback:logback-core:1.2.3", + ivy"ch.qos.logback:logback-classic:1.2.3", + ivy"org.mockito:mockito-core:2.21.0", + ivy"com.github.pureconfig::pureconfig:0.11.1", + ivy"com.github.scopt::scopt:3.7.1", + ivy"io.monix::monix:3.2.2", + ivy"org.scala-lang.modules::scala-parser-combinators:1.1.2" + ) - object ut extends TestModule { override def moduleDeps: Seq[JavaModule] = - super.moduleDeps ++ Seq(scalanet.examples) + Seq(scalanet, scalanet.discovery) + + object ut extends TestModule { + override def moduleDeps: Seq[JavaModule] = + super.moduleDeps ++ Seq(scalanet.examples) + } } } } diff --git a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/EthereumNodeRecord.scala b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/EthereumNodeRecord.scala index 1e3b38e7..6195e560 100644 --- a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/EthereumNodeRecord.scala +++ b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/EthereumNodeRecord.scala @@ -4,6 +4,7 @@ import scodec.bits.ByteVector import scala.collection.SortedMap import java.nio.charset.StandardCharsets import io.iohk.scalanet.discovery.crypto.{Signature, PrivateKey, SigAlg} +import io.iohk.scalanet.discovery.ethereum.codecs.DefaultCodecs import scodec.{Codec, Attempt} import io.iohk.scalanet.discovery.crypto.PublicKey import java.net.Inet6Address @@ -16,8 +17,8 @@ case class EthereumNodeRecord( ) object EthereumNodeRecord { - implicit val byteOrdering: Ordering[ByteVector] = - Ordering.by[ByteVector, Iterable[Byte]](_.toIterable) + + implicit val byteOrdering: Ordering[ByteVector] = DefaultCodecs.byteVectorOrdering case class Content( // Nodes should increment this number whenever their properties change, like their address, and re-publish. diff --git a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/codecs/DefaultCodecs.scala b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/codecs/DefaultCodecs.scala index 2e3fd1e0..9e803bcc 100644 --- a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/codecs/DefaultCodecs.scala +++ b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/codecs/DefaultCodecs.scala @@ -13,6 +13,7 @@ import scala.collection.SortedMap import java.net.InetAddress object DefaultCodecs { + implicit val publicKeyCodec: Codec[PublicKey] = implicitly[Codec[BitVector]].xmap(PublicKey(_), identity) @@ -38,6 +39,21 @@ object DefaultCodecs { (sm: SortedMap[K, V]) => sm.toList ) + class ByteIterableOrdering extends Ordering[Iterable[Byte]] { + import scala.math.Ordering._ + final def compare(as: Iterable[Byte], bs: Iterable[Byte]) = { + val ai = as.iterator + val bi = bs.iterator + var result: Option[Int] = None + while (ai.hasNext && bi.hasNext && result.isEmpty) { + val res = Byte.compare(ai.next(), bi.next()) + if (res != 0) result = Some(res) + } + result.getOrElse( Boolean.compare(ai.hasNext, bi.hasNext) ) + } + } + + implicit val byteIterableOrdering = new ByteIterableOrdering implicit val byteVectorOrdering: Ordering[ByteVector] = Ordering.by[ByteVector, Iterable[Byte]](_.toIterable) diff --git a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryNetwork.scala b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryNetwork.scala index f4cea6bb..c93c597b 100644 --- a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryNetwork.scala +++ b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryNetwork.scala @@ -193,11 +193,11 @@ object DiscoveryNetwork { maybeResponse.attempt.flatMap { case Right(Some(response)) => f(response) - case Right(None) => - Task.unit case Left(NonFatal(ex)) => // Not responding to this one, but it shouldn't stop handling further requests. Task(logger.error(s"Error handling incoming request: $ex")) + case _ => // Right(None) or fatal? - exhaustive match + Task.unit } /** Serialize the payload to binary and sign the packet. */ @@ -385,6 +385,10 @@ object DiscoveryNetwork { // New response, fold it with the existing to decide if we need more. val next = (acc: Z) => Some(acc -> (count + 1)) Task.pure(f(acc, response).bimap(next, next)) + + case _ => + // Invalid state - this cannot happen + Task(logger.debug(s"Unexpected state while collecting responses from ${channel.to}")).as(Right(None)) } .map(_.map(_._1)) @@ -404,7 +408,7 @@ object DiscoveryNetwork { ) val expiration = System.currentTimeMillis - Stream + Iterator .iterate(List(sampleNode))(sampleNode :: _) .map { nodes => val payload = Neighbors(nodes, expiration) diff --git a/scalanet/discovery/src/io/iohk/scalanet/kademlia/KBuckets.scala b/scalanet/discovery/src/io/iohk/scalanet/kademlia/KBuckets.scala index 26c62d51..c67fbf5b 100644 --- a/scalanet/discovery/src/io/iohk/scalanet/kademlia/KBuckets.scala +++ b/scalanet/discovery/src/io/iohk/scalanet/kademlia/KBuckets.scala @@ -33,7 +33,7 @@ class KBuckets[T <: BitVector] private ( // then all of its elements are closer to nodeId that any element // from buckets further in the stream // virtual one-element bucket with baseId is added - def orderedBucketsStream: Stream[Seq[T]] = { + def orderedBucketsIterator: Iterator[Seq[T]] = { // That part is simple in implementation but complex conceptually. It bases on observation that buckets can // be ordered by closeness to nodeId, i.e. for buckets A and B either all elements its elements are closer // to nodeId than any element of B or all elements from A are farther from nodeId than any element from B. @@ -73,27 +73,27 @@ class KBuckets[T <: BitVector] private ( // starting our iteration // buckets with elements closer to nodeId that baseId, sorted appropriately - def closerBuckets: Stream[Seq[T]] = - Stream - .range(buckets.size - 1, -1, -1) + def closerBuckets: Iterator[Seq[T]] = + Range(buckets.size - 1, -1, -1) + .iterator .filter(i => nodeId(buckets.size - i - 1) != baseId(buckets.size - i - 1)) .map(i => buckets(i).toSeq) // buckets with elements farther from nodeId than baseId, sorted appropriately - def furtherBuckets: Stream[Seq[T]] = - Stream - .range(0, buckets.size, 1) + def furtherBuckets: Iterator[Seq[T]] = + Range(0, buckets.size, 1) + .iterator .filter(i => nodeId(buckets.size - i - 1) == baseId(buckets.size - i - 1)) .map(i => buckets(i).toSeq) - closerBuckets ++ (Seq(baseId) #:: furtherBuckets) + closerBuckets ++ Iterator.single(Seq(baseId)) ++ furtherBuckets } if (n == 1) { // special case to avoid sorting the bucket - orderedBucketsStream.find(_.nonEmpty).map(_.min(ordering)).toList + orderedBucketsIterator.find(_.nonEmpty).map(_.min(ordering)).toList } else { - orderedBucketsStream.flatMap(_.sorted(ordering)).take(n).toList + orderedBucketsIterator.flatMap(_.sorted(ordering)).take(n).toList } } diff --git a/scalanet/discovery/src/io/iohk/scalanet/kademlia/KNetwork.scala b/scalanet/discovery/src/io/iohk/scalanet/kademlia/KNetwork.scala index 3d438561..34bc8fa0 100644 --- a/scalanet/discovery/src/io/iohk/scalanet/kademlia/KNetwork.scala +++ b/scalanet/discovery/src/io/iohk/scalanet/kademlia/KNetwork.scala @@ -1,6 +1,5 @@ package io.iohk.scalanet.kademlia -import cats.implicits._ import io.iohk.scalanet.kademlia.KMessage.{KRequest, KResponse} import io.iohk.scalanet.kademlia.KMessage.KRequest.{FindNodes, Ping} import io.iohk.scalanet.kademlia.KMessage.KResponse.{Nodes, Pong} diff --git a/scalanet/discovery/src/io/iohk/scalanet/kademlia/KRouter.scala b/scalanet/discovery/src/io/iohk/scalanet/kademlia/KRouter.scala index 438bfb9f..055703eb 100644 --- a/scalanet/discovery/src/io/iohk/scalanet/kademlia/KRouter.scala +++ b/scalanet/discovery/src/io/iohk/scalanet/kademlia/KRouter.scala @@ -113,7 +113,7 @@ class KRouter[A]( case Right(nodes) => Task { - val nodeIds = nodes.map(_.id) + val nodeIds = nodes.toSeq.map(_.id) val bootIds = config.knownPeers.map(_.id) val countSelf = nodeIds.count(myself) val countBoot = nodeIds.count(bootIds) diff --git a/scalanet/discovery/src/io/iohk/scalanet/kademlia/TimeSet.scala b/scalanet/discovery/src/io/iohk/scalanet/kademlia/TimeSet.scala index e84714fa..ddf79201 100644 --- a/scalanet/discovery/src/io/iohk/scalanet/kademlia/TimeSet.scala +++ b/scalanet/discovery/src/io/iohk/scalanet/kademlia/TimeSet.scala @@ -3,7 +3,7 @@ package io.iohk.scalanet.kademlia import java.time.Clock import java.time.Clock.systemUTC -import scala.collection.AbstractSet +import scala.collection.{Set, AbstractSet} import scala.collection.immutable.{HashMap, ListSet} class TimeSet[T] private (val clock: Clock, val timestamps: HashMap[T, Long], val underlying: ListSet[T]) @@ -28,6 +28,9 @@ class TimeSet[T] private (val clock: Clock, val timestamps: HashMap[T, Long], va override def iterator: Iterator[T] = underlying.iterator + def diff(that: Set[T]): Set[T] = + underlying &~ that + def touch(elem: T): TimeSet[T] = this + elem diff --git a/scalanet/examples/src/io/iohk/scalanet/kconsole/App.scala b/scalanet/examples/src/io/iohk/scalanet/kconsole/App.scala index 4417320a..b49cc928 100644 --- a/scalanet/examples/src/io/iohk/scalanet/kconsole/App.scala +++ b/scalanet/examples/src/io/iohk/scalanet/kconsole/App.scala @@ -2,7 +2,6 @@ package io.iohk.scalanet.kconsole import java.io.File import java.nio.file.Path import cats.effect.ExitCode -import cats.implicits._ import io.iohk.scalanet.kconsole.Utils.{configToStr, generateRandomConfig} import io.iohk.scalanet.peergroup.InetMultiAddress import io.iohk.scalanet.kademlia.KRouter diff --git a/scalanet/examples/src/io/iohk/scalanet/kconsole/CommandParser.scala b/scalanet/examples/src/io/iohk/scalanet/kconsole/CommandParser.scala index d3e41389..f8ef8458 100644 --- a/scalanet/examples/src/io/iohk/scalanet/kconsole/CommandParser.scala +++ b/scalanet/examples/src/io/iohk/scalanet/kconsole/CommandParser.scala @@ -9,6 +9,7 @@ import scodec.bits.BitVector import scala.concurrent.{Await, Promise} import scala.util.parsing.combinator._ import scala.concurrent.duration._ +import scala.language.postfixOps trait CommandParser extends RegexParsers { diff --git a/scalanet/src/io/iohk/scalanet/codec/FramingCodec.scala b/scalanet/src/io/iohk/scalanet/codec/FramingCodec.scala index 4ba385cf..37b88448 100644 --- a/scalanet/src/io/iohk/scalanet/codec/FramingCodec.scala +++ b/scalanet/src/io/iohk/scalanet/codec/FramingCodec.scala @@ -15,11 +15,12 @@ class FramingCodec[T](val messageCodec: Codec[T], lengthFieldLength: Int = 32) e private val atomicBuffer = AtomicAny[BitVector](BitVector.empty) override def streamDecode(newBits: BitVector): Either[String, Seq[T]] = { - val result = atomicBuffer.transformAndExtract { state => + val stateFn: BitVector => (Either[String, Seq[T]], BitVector) = state => { val newBuffer = state ++ newBits val (resultingBuffer, decodedMessages) = processBuffer(newBuffer, codec) (decodedMessages, resultingBuffer) } + val result = atomicBuffer.transformAndExtract(s => stateFn(s)) result } diff --git a/scalanet/src/io/iohk/scalanet/peergroup/dynamictls/DynamicTLSPeerGroupUtils.scala b/scalanet/src/io/iohk/scalanet/peergroup/dynamictls/DynamicTLSPeerGroupUtils.scala index d3dc16fb..1c288da9 100644 --- a/scalanet/src/io/iohk/scalanet/peergroup/dynamictls/DynamicTLSPeerGroupUtils.scala +++ b/scalanet/src/io/iohk/scalanet/peergroup/dynamictls/DynamicTLSPeerGroupUtils.scala @@ -10,11 +10,11 @@ import io.netty.handler.ssl.{ClientAuth, SslContext, SslContextBuilder} import javax.net.ssl._ import scodec.bits.BitVector -import scala.collection.JavaConverters._ +import java.util.Arrays private[scalanet] object DynamicTLSPeerGroupUtils { // supporting only ciphers which are used in TLS 1.3 - val supportedCipherSuites: Seq[String] = Seq( + val supportedCipherSuites: java.lang.Iterable[String] = Arrays.asList( "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" ) @@ -87,7 +87,7 @@ private[scalanet] object DynamicTLSPeerGroupUtils { .forServer(config.connectionKeyPair.getPrivate, List(config.connectionCertificate): _*) .trustManager(new CustomTrustManagerFactory(None)) .clientAuth(ClientAuth.REQUIRE) - .ciphers(supportedCipherSuites.asJava) + .ciphers(supportedCipherSuites) .protocols("TLSv1.2") .build() @@ -96,7 +96,7 @@ private[scalanet] object DynamicTLSPeerGroupUtils { .forClient() .keyManager(config.connectionKeyPair.getPrivate, List(config.connectionCertificate): _*) .trustManager(new CustomTrustManagerFactory(Some(info.id))) - .ciphers(supportedCipherSuites.asJava) + .ciphers(supportedCipherSuites) .protocols("TLSv1.2") .build() } diff --git a/scalanet/src/io/iohk/scalanet/peergroup/udp/DynamicUDPPeerGroup.scala b/scalanet/src/io/iohk/scalanet/peergroup/udp/DynamicUDPPeerGroup.scala index bc378e32..d3148442 100644 --- a/scalanet/src/io/iohk/scalanet/peergroup/udp/DynamicUDPPeerGroup.scala +++ b/scalanet/src/io/iohk/scalanet/peergroup/udp/DynamicUDPPeerGroup.scala @@ -4,7 +4,6 @@ import java.io.IOException import java.net.{InetSocketAddress, PortUnreachableException} import java.util.concurrent.ConcurrentHashMap import cats.effect.Resource -import cats.syntax.functor._ import com.typesafe.scalalogging.StrictLogging import io.iohk.scalanet.peergroup.{Channel, InetMultiAddress, CloseableQueue} import io.iohk.scalanet.peergroup.Channel.{ChannelEvent, DecodingError, MessageReceived, UnexpectedError} @@ -157,7 +156,7 @@ class DynamicUDPPeerGroup[M] private (val config: DynamicUDPPeerGroup.Config)( serverChannel, localAddress, remoteAddress, - makeMessageQueue, + makeMessageQueue(), ServerChannel ) try { diff --git a/scalanet/src/io/iohk/scalanet/peergroup/udp/StaticUDPPeerGroup.scala b/scalanet/src/io/iohk/scalanet/peergroup/udp/StaticUDPPeerGroup.scala index c0df7509..830e7c7d 100644 --- a/scalanet/src/io/iohk/scalanet/peergroup/udp/StaticUDPPeerGroup.scala +++ b/scalanet/src/io/iohk/scalanet/peergroup/udp/StaticUDPPeerGroup.scala @@ -369,7 +369,7 @@ object StaticUDPPeerGroup extends StrictLogging { override val to = InetMultiAddress(remoteAddress) - override def nextChannelEvent() = + override def nextChannelEvent = messageQueue.next private val raiseIfClosed = From 0a908755b1fa0f1c88b93347f97fc10655817b55 Mon Sep 17 00:00:00 2001 From: Dmitry Voronov Date: Tue, 22 Dec 2020 01:13:55 +0300 Subject: [PATCH 2/5] feat - tests fixed for both compiler targets, default codecs no longer shared, fixed some build.sc issues --- build.sc | 5 ++-- .../ethereum/EthereumNodeRecord.scala | 4 +-- .../ethereum/codecs/DefaultCodecs.scala | 19 ++----------- .../scalanet/discovery/util/package.scala | 27 +++++++++++++++++++ .../ut/src/io/iohk/scalanet/NetUtils.scala | 13 ++++++--- .../scalanet/codec/FramingCodecSpec.scala | 8 +++--- .../crypto/SignatureVerificationSpec.scala | 2 +- .../dynamictls/CustomTlsValidatorSpec.scala | 3 +-- .../dynamictls/SignedKeyExtensionSpec.scala | 2 +- .../peergroup/DynamicTLSPeerGroupSpec.scala | 9 ++++--- .../scalanet/peergroup/StandardTestPack.scala | 1 - .../TransportPeerGroupAsyncSpec.scala | 3 +++ .../peergroup/udp/UDPPeerGroupSpec.scala | 3 ++- 13 files changed, 60 insertions(+), 39 deletions(-) create mode 100644 scalanet/discovery/src/io/iohk/scalanet/discovery/util/package.scala diff --git a/build.sc b/build.sc index f681773e..5a0e6a79 100644 --- a/build.sc +++ b/build.sc @@ -54,14 +54,13 @@ class ScalanetModule(crossVersion: String) extends Module { // which is why the trait is defined here not within `scalanet`, otherwise // it wouldn't work for `kademlia` for example. trait TestModule extends Tests { - override def scalacOptions = Nil - //commonScalacOptions + override def scalacOptions = ScalanetModule.this.scalacOptions override def testFrameworks = Seq("org.scalatest.tools.Framework") override def ivyDeps = Agg( - ivy"org.scalatest::scalatest:3.0.5", + ivy"org.scalatest::scalatest:3.0.9", ivy"org.scalacheck::scalacheck:1.14.0", ivy"ch.qos.logback:logback-core:1.2.3", ivy"ch.qos.logback:logback-classic:1.2.3", diff --git a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/EthereumNodeRecord.scala b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/EthereumNodeRecord.scala index 6195e560..b2c15480 100644 --- a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/EthereumNodeRecord.scala +++ b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/EthereumNodeRecord.scala @@ -4,7 +4,7 @@ import scodec.bits.ByteVector import scala.collection.SortedMap import java.nio.charset.StandardCharsets import io.iohk.scalanet.discovery.crypto.{Signature, PrivateKey, SigAlg} -import io.iohk.scalanet.discovery.ethereum.codecs.DefaultCodecs +import io.iohk.scalanet.discovery.util.byteVectorOrdering import scodec.{Codec, Attempt} import io.iohk.scalanet.discovery.crypto.PublicKey import java.net.Inet6Address @@ -18,7 +18,7 @@ case class EthereumNodeRecord( object EthereumNodeRecord { - implicit val byteOrdering: Ordering[ByteVector] = DefaultCodecs.byteVectorOrdering + implicit val byteOrdering: Ordering[ByteVector] = byteVectorOrdering case class Content( // Nodes should increment this number whenever their properties change, like their address, and re-publish. diff --git a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/codecs/DefaultCodecs.scala b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/codecs/DefaultCodecs.scala index 9e803bcc..e937ab8f 100644 --- a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/codecs/DefaultCodecs.scala +++ b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/codecs/DefaultCodecs.scala @@ -1,6 +1,7 @@ package io.iohk.scalanet.discovery.ethereum.codecs import io.iohk.scalanet.discovery.hash.Hash +import io.iohk.scalanet.discovery.util import io.iohk.scalanet.discovery.crypto.{PublicKey, Signature} import io.iohk.scalanet.discovery.ethereum.{Node, EthereumNodeRecord} import io.iohk.scalanet.discovery.ethereum.v4.Payload @@ -39,23 +40,7 @@ object DefaultCodecs { (sm: SortedMap[K, V]) => sm.toList ) - class ByteIterableOrdering extends Ordering[Iterable[Byte]] { - import scala.math.Ordering._ - final def compare(as: Iterable[Byte], bs: Iterable[Byte]) = { - val ai = as.iterator - val bi = bs.iterator - var result: Option[Int] = None - while (ai.hasNext && bi.hasNext && result.isEmpty) { - val res = Byte.compare(ai.next(), bi.next()) - if (res != 0) result = Some(res) - } - result.getOrElse( Boolean.compare(ai.hasNext, bi.hasNext) ) - } - } - - implicit val byteIterableOrdering = new ByteIterableOrdering - implicit val byteVectorOrdering: Ordering[ByteVector] = - Ordering.by[ByteVector, Iterable[Byte]](_.toIterable) + implicit val byteVectorOrdering: Ordering[ByteVector] = util.byteVectorOrdering implicit val attrCodec: Codec[SortedMap[ByteVector, ByteVector]] = sortedMapCodec[ByteVector, ByteVector] diff --git a/scalanet/discovery/src/io/iohk/scalanet/discovery/util/package.scala b/scalanet/discovery/src/io/iohk/scalanet/discovery/util/package.scala new file mode 100644 index 00000000..3a4254a1 --- /dev/null +++ b/scalanet/discovery/src/io/iohk/scalanet/discovery/util/package.scala @@ -0,0 +1,27 @@ +package io.iohk.scalanet.discovery + +import scodec.bits.ByteVector +import scala.math.Ordering._ + + +package object util { + + private class ByteIterableOrdering extends Ordering[Iterable[Byte]] { + final def compare(as: Iterable[Byte], bs: Iterable[Byte]) = { + val ai = as.iterator + val bi = bs.iterator + var result: Option[Int] = None + while (ai.hasNext && bi.hasNext && result.isEmpty) { + val res = Byte.compare(ai.next(), bi.next()) + if (res != 0) result = Some(res) + } + result.getOrElse( Boolean.compare(ai.hasNext, bi.hasNext) ) + } + } + + private implicit val byteIterableOrdering = new ByteIterableOrdering + + val byteVectorOrdering: Ordering[ByteVector] = + Ordering.by[ByteVector, Iterable[Byte]](_.toIterable) + +} diff --git a/scalanet/ut/src/io/iohk/scalanet/NetUtils.scala b/scalanet/ut/src/io/iohk/scalanet/NetUtils.scala index a8e8ccbb..47688316 100644 --- a/scalanet/ut/src/io/iohk/scalanet/NetUtils.scala +++ b/scalanet/ut/src/io/iohk/scalanet/NetUtils.scala @@ -6,15 +6,22 @@ import java.security.KeyStore import java.security.cert.Certificate import io.iohk.scalanet.peergroup.InetPeerGroupUtils -import scala.collection.JavaConverters._ import scala.util.Random +import scala.collection.mutable.ArrayBuffer object NetUtils { val keyStore: KeyStore = loadKeyStore("keystore.p12", "password") val trustStore: KeyStore = loadKeyStore("truststore.p12", "password") - val trustedCerts: Array[Certificate] = - trustStore.aliases().asScala.toArray.map(alias => trustStore.getCertificate(alias)) + val trustedCerts: Array[Certificate] = { + val aliases = trustStore.aliases() + val buffer = ArrayBuffer[Certificate]() + while(aliases.hasMoreElements()) { + val alias = aliases.nextElement() + buffer.append(trustStore.getCertificate(alias)) + } + buffer.toArray + } def loadKeyStore(keystoreLocation: String, keystorePassword: String): KeyStore = { val keystore = KeyStore.getInstance("PKCS12") diff --git a/scalanet/ut/src/io/iohk/scalanet/codec/FramingCodecSpec.scala b/scalanet/ut/src/io/iohk/scalanet/codec/FramingCodecSpec.scala index e300ed69..f38f708e 100644 --- a/scalanet/ut/src/io/iohk/scalanet/codec/FramingCodecSpec.scala +++ b/scalanet/ut/src/io/iohk/scalanet/codec/FramingCodecSpec.scala @@ -10,7 +10,7 @@ import scodec.codecs.implicits._ import scodec.codecs._ import scala.util.Random -import org.scalatest.prop.GeneratorDrivenPropertyChecks._ +import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks._ import FramingCodecSpec._ import scodec.codecs.Discriminated @@ -23,7 +23,7 @@ class FramingCodecSpec extends FlatSpec with EitherValues { forAll(genString()) { listOfString => val enc = listOfString.map(encoder.encode(_).require).foldLeft(BitVector.empty)((acc, bit) => acc ++ bit) - val decoded = encoder.streamDecode(enc).right.value + val decoded = encoder.streamDecode(enc).toOption.get decoded.toList shouldEqual listOfString } } @@ -35,7 +35,7 @@ class FramingCodecSpec extends FlatSpec with EitherValues { val enc = listOfString.map(encoder.encode(_).require).foldLeft(BitVector.empty)((acc, bit) => acc ++ bit) val encPacketed = enc.grouped(packetSize).toList val decoded = - encPacketed.foldLeft(List(): List[String])((acc, vec) => acc ++ encoder.streamDecode(vec).right.value) + encPacketed.foldLeft(List(): List[String])((acc, vec) => acc ++ encoder.streamDecode(vec).toOption.get) decoded shouldEqual listOfString } } @@ -48,7 +48,7 @@ class FramingCodecSpec extends FlatSpec with EitherValues { val enc = listOfMessages.map(encoder.encode(_).require).foldLeft(BitVector.empty)((acc, bit) => acc ++ bit) val encPacketed = enc.grouped(packetSize).toList val decoded = - encPacketed.foldLeft(List(): List[RandomMessage])((acc, vec) => acc ++ encoder.streamDecode(vec).right.value) + encPacketed.foldLeft(List(): List[RandomMessage])((acc, vec) => acc ++ encoder.streamDecode(vec).toOption.get) decoded shouldEqual listOfMessages } } diff --git a/scalanet/ut/src/io/iohk/scalanet/crypto/SignatureVerificationSpec.scala b/scalanet/ut/src/io/iohk/scalanet/crypto/SignatureVerificationSpec.scala index 287d8f62..0b6af537 100644 --- a/scalanet/ut/src/io/iohk/scalanet/crypto/SignatureVerificationSpec.scala +++ b/scalanet/ut/src/io/iohk/scalanet/crypto/SignatureVerificationSpec.scala @@ -4,7 +4,7 @@ import java.security.SecureRandom import io.iohk.scalanet.peergroup.dynamictls.Secp256k1 import org.scalatest.{FlatSpec, Matchers} -import org.scalatest.prop.GeneratorDrivenPropertyChecks._ +import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks._ import io.iohk.scalanet.testutils.GeneratorUtils import org.scalacheck.Gen diff --git a/scalanet/ut/src/io/iohk/scalanet/dynamictls/CustomTlsValidatorSpec.scala b/scalanet/ut/src/io/iohk/scalanet/dynamictls/CustomTlsValidatorSpec.scala index 7ef56152..28eef6b2 100644 --- a/scalanet/ut/src/io/iohk/scalanet/dynamictls/CustomTlsValidatorSpec.scala +++ b/scalanet/ut/src/io/iohk/scalanet/dynamictls/CustomTlsValidatorSpec.scala @@ -16,8 +16,7 @@ import io.iohk.scalanet.peergroup.dynamictls.{CustomTlsValidator, DynamicTLSExte import io.iohk.scalanet.testutils.GeneratorUtils import org.bouncycastle.asn1.DERGeneralString import org.joda.time.DateTime -import org.scalatest.prop.GeneratorDrivenPropertyChecks.forAll -import org.scalatest.prop.GeneratorDrivenPropertyChecks._ +import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks._ import org.scalatest.{FlatSpec, Matchers} import scodec.bits.BitVector diff --git a/scalanet/ut/src/io/iohk/scalanet/dynamictls/SignedKeyExtensionSpec.scala b/scalanet/ut/src/io/iohk/scalanet/dynamictls/SignedKeyExtensionSpec.scala index b05eab62..603c8b74 100644 --- a/scalanet/ut/src/io/iohk/scalanet/dynamictls/SignedKeyExtensionSpec.scala +++ b/scalanet/ut/src/io/iohk/scalanet/dynamictls/SignedKeyExtensionSpec.scala @@ -12,7 +12,7 @@ import io.iohk.scalanet.peergroup.dynamictls.DynamicTLSExtension.{ import io.iohk.scalanet.peergroup.dynamictls.Secp256k1 import io.iohk.scalanet.testutils.GeneratorUtils import org.scalatest.{FlatSpec, Matchers} -import org.scalatest.prop.GeneratorDrivenPropertyChecks._ +import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks._ class SignedKeyExtensionSpec extends FlatSpec with Matchers { val rnd = new SecureRandom() diff --git a/scalanet/ut/src/io/iohk/scalanet/peergroup/DynamicTLSPeerGroupSpec.scala b/scalanet/ut/src/io/iohk/scalanet/peergroup/DynamicTLSPeerGroupSpec.scala index efb1b6a3..f7eba648 100644 --- a/scalanet/ut/src/io/iohk/scalanet/peergroup/DynamicTLSPeerGroupSpec.scala +++ b/scalanet/ut/src/io/iohk/scalanet/peergroup/DynamicTLSPeerGroupSpec.scala @@ -143,6 +143,7 @@ class DynamicTLSPeerGroupSpec extends AsyncFlatSpec with BeforeAndAfterAll { resp1b shouldEqual randomString2 resp1c shouldEqual randomString3 } + case _ => fail() } } @@ -151,7 +152,7 @@ class DynamicTLSPeerGroupSpec extends AsyncFlatSpec with BeforeAndAfterAll { DynamicTLSPeerGroup[String](getCorrectConfig()).use { client => client.client(serverConfig.peerInfo).use(_ => Task.unit).attempt.map { result => result.isLeft shouldEqual true - result.left.get shouldBe a[ChannelSetupException[_]] + result.swap.toOption.get shouldBe a[ChannelSetupException[_]] } } } @@ -166,7 +167,7 @@ class DynamicTLSPeerGroupSpec extends AsyncFlatSpec with BeforeAndAfterAll { result <- ch1.sendMessage("wow").attempt } yield { result.isLeft shouldEqual true - result.left.get shouldBe a[ChannelBrokenException[_]] + result.swap.toOption.get shouldBe a[ChannelBrokenException[_]] } } } @@ -184,7 +185,7 @@ class DynamicTLSPeerGroupSpec extends AsyncFlatSpec with BeforeAndAfterAll { serverHandshake <- server.serverEventObservable.collectHandshakeFailure.headL } yield { ch1.isLeft shouldEqual true - ch1.left.get shouldBe a[HandshakeException[_]] + ch1.swap.toOption.get shouldBe a[HandshakeException[_]] serverHandshake shouldBe a[HandshakeException[_]] } } @@ -201,7 +202,7 @@ class DynamicTLSPeerGroupSpec extends AsyncFlatSpec with BeforeAndAfterAll { serverHandshake <- server.serverEventObservable.collectHandshakeFailure.headL } yield { ch1.isLeft shouldEqual true - ch1.left.get shouldBe a[HandshakeException[_]] + ch1.swap.toOption.get shouldBe a[HandshakeException[_]] serverHandshake shouldBe a[HandshakeException[_]] } } diff --git a/scalanet/ut/src/io/iohk/scalanet/peergroup/StandardTestPack.scala b/scalanet/ut/src/io/iohk/scalanet/peergroup/StandardTestPack.scala index 160eddaa..01b8dad6 100644 --- a/scalanet/ut/src/io/iohk/scalanet/peergroup/StandardTestPack.scala +++ b/scalanet/ut/src/io/iohk/scalanet/peergroup/StandardTestPack.scala @@ -1,6 +1,5 @@ package io.iohk.scalanet.peergroup -import cats.implicits._ import io.iohk.scalanet.peergroup.implicits._ import io.iohk.scalanet.peergroup.Channel.MessageReceived import io.iohk.scalanet.peergroup.PeerGroup.ChannelSetupException diff --git a/scalanet/ut/src/io/iohk/scalanet/peergroup/TransportPeerGroupAsyncSpec.scala b/scalanet/ut/src/io/iohk/scalanet/peergroup/TransportPeerGroupAsyncSpec.scala index 09d4c372..92af1071 100644 --- a/scalanet/ut/src/io/iohk/scalanet/peergroup/TransportPeerGroupAsyncSpec.scala +++ b/scalanet/ut/src/io/iohk/scalanet/peergroup/TransportPeerGroupAsyncSpec.scala @@ -55,6 +55,7 @@ class TransportPeerGroupAsyncSpec extends AsyncFlatSpec with BeforeAndAfterAll { resp2 shouldEqual msg3 ++ msg3 resp3 shouldEqual (msg3 ++ msg1) ++ (msg3 ++ msg1) } + case _ => fail() } } @@ -86,6 +87,7 @@ class TransportPeerGroupAsyncSpec extends AsyncFlatSpec with BeforeAndAfterAll { r3 shouldEqual 2 * k responses shouldEqual (1 to 4).map(2 * _) } + case _ => fail() } } @@ -113,6 +115,7 @@ class TransportPeerGroupAsyncSpec extends AsyncFlatSpec with BeforeAndAfterAll { resp2 shouldEqual client2Numbers.map(2 * _) resp3 shouldEqual client3Numbers.map(2 * _) } + case _ => fail() } } } diff --git a/scalanet/ut/src/io/iohk/scalanet/peergroup/udp/UDPPeerGroupSpec.scala b/scalanet/ut/src/io/iohk/scalanet/peergroup/udp/UDPPeerGroupSpec.scala index 824a2e8a..9326f570 100644 --- a/scalanet/ut/src/io/iohk/scalanet/peergroup/udp/UDPPeerGroupSpec.scala +++ b/scalanet/ut/src/io/iohk/scalanet/peergroup/udp/UDPPeerGroupSpec.scala @@ -125,7 +125,7 @@ abstract class UDPPeerGroupSpec[PG <: UDPPeerGroupSpec.TestGroup[_]](name: Strin .use { _ => initUdpPeerGroup[String](address).allocated.attempt.map { result => assert(result.isLeft) - result.left.get shouldBe a[InitializationError] + result.swap.toOption.get shouldBe a[InitializationError] } } .runSyncUnsafe() @@ -230,6 +230,7 @@ abstract class UDPPeerGroupSpec[PG <: UDPPeerGroupSpec.TestGroup[_]](name: Strin } } + case _ => fail() } .runSyncUnsafe() } From cce3f28ff016421f12b203820ab6802e084c2888 Mon Sep 17 00:00:00 2001 From: Dmitry Voronov Date: Tue, 22 Dec 2020 01:50:20 +0300 Subject: [PATCH 3/5] feat - fixes regarding the monix.Tasks, added build.sc comments --- build.sc | 30 +++++++++++++++---- .../ethereum/v4/DiscoveryNetwork.scala | 14 ++++----- .../dynamictls/DynamicTLSPeerGroupUtils.scala | 2 +- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/build.sc b/build.sc index 5a0e6a79..381e179b 100644 --- a/build.sc +++ b/build.sc @@ -9,9 +9,27 @@ import mill.scalalib.publish.{Developer, License, PomSettings, VersionControl} object csm extends Cross[ScalanetModule]("2.12.10", "2.13.4") class ScalanetModule(crossVersion: String) extends Module { + + // A cross build is now a `root` of the module: + // > mill show csm[2.13.4].scalanet.sources + // [1/1] show + // [1/1] show > [1/1] csm[2.13.4].scalanet.sources + // [ + // "ref:c984eca8: ../csm/2.13.4/scalanet/src" + // ] + // As soon as there is no such folder as /csm/2.13.4/ + // We have to navigate up two times on the module tree. + // + // > mill show csm[2.13.4].scalanet.sources + // // some output omitted // + // "ref:c984eca8: ../scalanet/src" override def millSourcePath = super.millSourcePath / os.up / os.up - private val pointTwelveOptions: Seq[String] = Seq( + + // scala 2.12 - only options + // it causes errors when built against 2.13 (and vice-versa): + // [error] bad option: '-Ywarn-unused-import' + private val `scala 2.12 options`: Seq[String] = Seq( "-Xlint:unsound-match", "-Ywarn-inaccessible", "-Ywarn-unused-import", @@ -19,7 +37,9 @@ class ScalanetModule(crossVersion: String) extends Module { "-Ywarn-value-discard" ) - private val pointThirteenOptions: Seq[String] = Nil + + // scala 2.13 might have another set of options + private val `scala 2.13 options`: Seq[String] = Nil trait ScalanetModule extends ScalaModule { override def scalaVersion = crossVersion @@ -44,8 +64,8 @@ class ScalanetModule(crossVersion: String) extends Module { ) private val versionOptions = crossVersion.take(4) match { - case "2.12" => pointTwelveOptions - case "2.13" => pointThirteenOptions + case "2.12" => `scala 2.12 options` + case "2.13" => `scala 2.13 options` } override def scalacOptions = commonScalacOptions ++ versionOptions @@ -83,7 +103,7 @@ class ScalanetModule(crossVersion: String) extends Module { trait ScalanetPublishModule extends PublishModule { def description: String - override def publishVersion = "0.5.0-SNAPSHOT" + override def publishVersion = "0.5.1-SNAPSHOT" override def pomSettings = PomSettings( description = description, diff --git a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryNetwork.scala b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryNetwork.scala index c93c597b..9616d67c 100644 --- a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryNetwork.scala +++ b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryNetwork.scala @@ -190,15 +190,11 @@ object DiscoveryNetwork { private def maybeRespond[Res](maybeResponse: Task[Option[Res]])( f: Res => Task[Unit] ): Task[Unit] = - maybeResponse.attempt.flatMap { - case Right(Some(response)) => - f(response) - case Left(NonFatal(ex)) => + maybeResponse.onErrorRecoverWith { + case NonFatal(ex) => // Not responding to this one, but it shouldn't stop handling further requests. - Task(logger.error(s"Error handling incoming request: $ex")) - case _ => // Right(None) or fatal? - exhaustive match - Task.unit - } + Task(logger.error(s"Error handling incoming request: $ex")).as(None) + }.flatMap(_.fold(Task.unit)(f)) /** Serialize the payload to binary and sign the packet. */ private def pack(payload: Payload): Task[Packet] = @@ -388,7 +384,7 @@ object DiscoveryNetwork { case _ => // Invalid state - this cannot happen - Task(logger.debug(s"Unexpected state while collecting responses from ${channel.to}")).as(Right(None)) + Task.raiseError(new IllegalStateException(s"Unexpected state while collecting responses from ${channel.to}")) } .map(_.map(_._1)) diff --git a/scalanet/src/io/iohk/scalanet/peergroup/dynamictls/DynamicTLSPeerGroupUtils.scala b/scalanet/src/io/iohk/scalanet/peergroup/dynamictls/DynamicTLSPeerGroupUtils.scala index 1c288da9..85c1f1b9 100644 --- a/scalanet/src/io/iohk/scalanet/peergroup/dynamictls/DynamicTLSPeerGroupUtils.scala +++ b/scalanet/src/io/iohk/scalanet/peergroup/dynamictls/DynamicTLSPeerGroupUtils.scala @@ -14,7 +14,7 @@ import java.util.Arrays private[scalanet] object DynamicTLSPeerGroupUtils { // supporting only ciphers which are used in TLS 1.3 - val supportedCipherSuites: java.lang.Iterable[String] = Arrays.asList( + private val supportedCipherSuites: java.lang.Iterable[String] = Arrays.asList( "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" ) From a63a2874948581a96c438ef58c35290faec310fa Mon Sep 17 00:00:00 2001 From: Dmitry Voronov Date: Tue, 22 Dec 2020 20:05:00 +0300 Subject: [PATCH 4/5] feat - added nowarn for JavaConverters and the tests with Either --- .../ethereum/EthereumNodeRecord.scala | 5 ++-- .../ethereum/codecs/DefaultCodecs.scala | 6 +++-- .../ethereum/v4/DiscoveryNetwork.scala | 2 +- .../scalanet/discovery/util/package.scala | 27 ------------------- .../ut/src/io/iohk/scalanet/NetUtils.scala | 16 +++++------ .../scalanet/codec/FramingCodecSpec.scala | 8 +++--- .../peergroup/DynamicTLSPeerGroupSpec.scala | 10 ++++--- .../peergroup/udp/UDPPeerGroupSpec.scala | 4 ++- 8 files changed, 29 insertions(+), 49 deletions(-) delete mode 100644 scalanet/discovery/src/io/iohk/scalanet/discovery/util/package.scala diff --git a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/EthereumNodeRecord.scala b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/EthereumNodeRecord.scala index b2c15480..f326d800 100644 --- a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/EthereumNodeRecord.scala +++ b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/EthereumNodeRecord.scala @@ -2,9 +2,9 @@ package io.iohk.scalanet.discovery.ethereum import scodec.bits.ByteVector import scala.collection.SortedMap +import scala.math.Ordering.Implicits._ import java.nio.charset.StandardCharsets import io.iohk.scalanet.discovery.crypto.{Signature, PrivateKey, SigAlg} -import io.iohk.scalanet.discovery.util.byteVectorOrdering import scodec.{Codec, Attempt} import io.iohk.scalanet.discovery.crypto.PublicKey import java.net.Inet6Address @@ -18,7 +18,8 @@ case class EthereumNodeRecord( object EthereumNodeRecord { - implicit val byteOrdering: Ordering[ByteVector] = byteVectorOrdering + implicit val byteVectorOrdering: Ordering[ByteVector] = + Ordering.by[ByteVector, Iterable[Byte]](_.toIterable) case class Content( // Nodes should increment this number whenever their properties change, like their address, and re-publish. diff --git a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/codecs/DefaultCodecs.scala b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/codecs/DefaultCodecs.scala index e937ab8f..5905e88a 100644 --- a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/codecs/DefaultCodecs.scala +++ b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/codecs/DefaultCodecs.scala @@ -1,7 +1,6 @@ package io.iohk.scalanet.discovery.ethereum.codecs import io.iohk.scalanet.discovery.hash.Hash -import io.iohk.scalanet.discovery.util import io.iohk.scalanet.discovery.crypto.{PublicKey, Signature} import io.iohk.scalanet.discovery.ethereum.{Node, EthereumNodeRecord} import io.iohk.scalanet.discovery.ethereum.v4.Payload @@ -11,8 +10,10 @@ import scodec.codecs.{Discriminated, Discriminator, uint4} import scodec.codecs.implicits._ import scodec.bits.{BitVector, ByteVector} import scala.collection.SortedMap +import scala.math.Ordering.Implicits._ import java.net.InetAddress + object DefaultCodecs { implicit val publicKeyCodec: Codec[PublicKey] = @@ -40,7 +41,8 @@ object DefaultCodecs { (sm: SortedMap[K, V]) => sm.toList ) - implicit val byteVectorOrdering: Ordering[ByteVector] = util.byteVectorOrdering + implicit val byteVectorOrdering: Ordering[ByteVector] = + Ordering.by[ByteVector, Iterable[Byte]](_.toIterable) implicit val attrCodec: Codec[SortedMap[ByteVector, ByteVector]] = sortedMapCodec[ByteVector, ByteVector] diff --git a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryNetwork.scala b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryNetwork.scala index 9616d67c..992b63ca 100644 --- a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryNetwork.scala +++ b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryNetwork.scala @@ -382,7 +382,7 @@ object DiscoveryNetwork { val next = (acc: Z) => Some(acc -> (count + 1)) Task.pure(f(acc, response).bimap(next, next)) - case _ => + case (None, _) => // Invalid state - this cannot happen Task.raiseError(new IllegalStateException(s"Unexpected state while collecting responses from ${channel.to}")) } diff --git a/scalanet/discovery/src/io/iohk/scalanet/discovery/util/package.scala b/scalanet/discovery/src/io/iohk/scalanet/discovery/util/package.scala deleted file mode 100644 index 3a4254a1..00000000 --- a/scalanet/discovery/src/io/iohk/scalanet/discovery/util/package.scala +++ /dev/null @@ -1,27 +0,0 @@ -package io.iohk.scalanet.discovery - -import scodec.bits.ByteVector -import scala.math.Ordering._ - - -package object util { - - private class ByteIterableOrdering extends Ordering[Iterable[Byte]] { - final def compare(as: Iterable[Byte], bs: Iterable[Byte]) = { - val ai = as.iterator - val bi = bs.iterator - var result: Option[Int] = None - while (ai.hasNext && bi.hasNext && result.isEmpty) { - val res = Byte.compare(ai.next(), bi.next()) - if (res != 0) result = Some(res) - } - result.getOrElse( Boolean.compare(ai.hasNext, bi.hasNext) ) - } - } - - private implicit val byteIterableOrdering = new ByteIterableOrdering - - val byteVectorOrdering: Ordering[ByteVector] = - Ordering.by[ByteVector, Iterable[Byte]](_.toIterable) - -} diff --git a/scalanet/ut/src/io/iohk/scalanet/NetUtils.scala b/scalanet/ut/src/io/iohk/scalanet/NetUtils.scala index 47688316..5fed6bc9 100644 --- a/scalanet/ut/src/io/iohk/scalanet/NetUtils.scala +++ b/scalanet/ut/src/io/iohk/scalanet/NetUtils.scala @@ -7,22 +7,20 @@ import java.security.cert.Certificate import io.iohk.scalanet.peergroup.InetPeerGroupUtils import scala.util.Random -import scala.collection.mutable.ArrayBuffer +import scala.annotation.nowarn object NetUtils { val keyStore: KeyStore = loadKeyStore("keystore.p12", "password") val trustStore: KeyStore = loadKeyStore("truststore.p12", "password") - val trustedCerts: Array[Certificate] = { - val aliases = trustStore.aliases() - val buffer = ArrayBuffer[Certificate]() - while(aliases.hasMoreElements()) { - val alias = aliases.nextElement() - buffer.append(trustStore.getCertificate(alias)) - } - buffer.toArray + @nowarn + val trustedCerts: Array[Certificate] = { + import scala.collection.JavaConverters._ + trustStore.aliases().asScala.toArray.map(trustStore.getCertificate(_)) } + + def loadKeyStore(keystoreLocation: String, keystorePassword: String): KeyStore = { val keystore = KeyStore.getInstance("PKCS12") keystore.load(NetUtils.getClass.getClassLoader.getResourceAsStream(keystoreLocation), keystorePassword.toCharArray) diff --git a/scalanet/ut/src/io/iohk/scalanet/codec/FramingCodecSpec.scala b/scalanet/ut/src/io/iohk/scalanet/codec/FramingCodecSpec.scala index f38f708e..3da8a0c0 100644 --- a/scalanet/ut/src/io/iohk/scalanet/codec/FramingCodecSpec.scala +++ b/scalanet/ut/src/io/iohk/scalanet/codec/FramingCodecSpec.scala @@ -13,7 +13,9 @@ import scala.util.Random import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks._ import FramingCodecSpec._ import scodec.codecs.Discriminated +import scala.annotation.nowarn +@nowarn class FramingCodecSpec extends FlatSpec with EitherValues { behavior of "FramingCodec" @@ -23,7 +25,7 @@ class FramingCodecSpec extends FlatSpec with EitherValues { forAll(genString()) { listOfString => val enc = listOfString.map(encoder.encode(_).require).foldLeft(BitVector.empty)((acc, bit) => acc ++ bit) - val decoded = encoder.streamDecode(enc).toOption.get + val decoded = encoder.streamDecode(enc).right.value decoded.toList shouldEqual listOfString } } @@ -35,7 +37,7 @@ class FramingCodecSpec extends FlatSpec with EitherValues { val enc = listOfString.map(encoder.encode(_).require).foldLeft(BitVector.empty)((acc, bit) => acc ++ bit) val encPacketed = enc.grouped(packetSize).toList val decoded = - encPacketed.foldLeft(List(): List[String])((acc, vec) => acc ++ encoder.streamDecode(vec).toOption.get) + encPacketed.foldLeft(List(): List[String])((acc, vec) => acc ++ encoder.streamDecode(vec).right.value) decoded shouldEqual listOfString } } @@ -48,7 +50,7 @@ class FramingCodecSpec extends FlatSpec with EitherValues { val enc = listOfMessages.map(encoder.encode(_).require).foldLeft(BitVector.empty)((acc, bit) => acc ++ bit) val encPacketed = enc.grouped(packetSize).toList val decoded = - encPacketed.foldLeft(List(): List[RandomMessage])((acc, vec) => acc ++ encoder.streamDecode(vec).toOption.get) + encPacketed.foldLeft(List(): List[RandomMessage])((acc, vec) => acc ++ encoder.streamDecode(vec).right.value) decoded shouldEqual listOfMessages } } diff --git a/scalanet/ut/src/io/iohk/scalanet/peergroup/DynamicTLSPeerGroupSpec.scala b/scalanet/ut/src/io/iohk/scalanet/peergroup/DynamicTLSPeerGroupSpec.scala index f7eba648..4aa40653 100644 --- a/scalanet/ut/src/io/iohk/scalanet/peergroup/DynamicTLSPeerGroupSpec.scala +++ b/scalanet/ut/src/io/iohk/scalanet/peergroup/DynamicTLSPeerGroupSpec.scala @@ -30,7 +30,9 @@ import scodec.codecs.implicits._ import scala.concurrent.Future import scala.concurrent.duration._ import scala.util.Random +import scala.annotation.nowarn +@nowarn class DynamicTLSPeerGroupSpec extends AsyncFlatSpec with BeforeAndAfterAll { val timeOutConfig = 5.seconds @@ -152,7 +154,7 @@ class DynamicTLSPeerGroupSpec extends AsyncFlatSpec with BeforeAndAfterAll { DynamicTLSPeerGroup[String](getCorrectConfig()).use { client => client.client(serverConfig.peerInfo).use(_ => Task.unit).attempt.map { result => result.isLeft shouldEqual true - result.swap.toOption.get shouldBe a[ChannelSetupException[_]] + result.left.get shouldBe a[ChannelSetupException[_]] } } } @@ -167,7 +169,7 @@ class DynamicTLSPeerGroupSpec extends AsyncFlatSpec with BeforeAndAfterAll { result <- ch1.sendMessage("wow").attempt } yield { result.isLeft shouldEqual true - result.swap.toOption.get shouldBe a[ChannelBrokenException[_]] + result.left.get shouldBe a[ChannelBrokenException[_]] } } } @@ -185,7 +187,7 @@ class DynamicTLSPeerGroupSpec extends AsyncFlatSpec with BeforeAndAfterAll { serverHandshake <- server.serverEventObservable.collectHandshakeFailure.headL } yield { ch1.isLeft shouldEqual true - ch1.swap.toOption.get shouldBe a[HandshakeException[_]] + ch1.left.get shouldBe a[HandshakeException[_]] serverHandshake shouldBe a[HandshakeException[_]] } } @@ -202,7 +204,7 @@ class DynamicTLSPeerGroupSpec extends AsyncFlatSpec with BeforeAndAfterAll { serverHandshake <- server.serverEventObservable.collectHandshakeFailure.headL } yield { ch1.isLeft shouldEqual true - ch1.swap.toOption.get shouldBe a[HandshakeException[_]] + ch1.left.get shouldBe a[HandshakeException[_]] serverHandshake shouldBe a[HandshakeException[_]] } } diff --git a/scalanet/ut/src/io/iohk/scalanet/peergroup/udp/UDPPeerGroupSpec.scala b/scalanet/ut/src/io/iohk/scalanet/peergroup/udp/UDPPeerGroupSpec.scala index 9326f570..bb7c1f4d 100644 --- a/scalanet/ut/src/io/iohk/scalanet/peergroup/udp/UDPPeerGroupSpec.scala +++ b/scalanet/ut/src/io/iohk/scalanet/peergroup/udp/UDPPeerGroupSpec.scala @@ -22,7 +22,9 @@ import scala.concurrent.duration._ import scodec.bits.ByteVector import scodec.Codec import scodec.codecs.implicits._ +import scala.annotation.nowarn +@nowarn abstract class UDPPeerGroupSpec[PG <: UDPPeerGroupSpec.TestGroup[_]](name: String) extends FlatSpec with EitherValues @@ -125,7 +127,7 @@ abstract class UDPPeerGroupSpec[PG <: UDPPeerGroupSpec.TestGroup[_]](name: Strin .use { _ => initUdpPeerGroup[String](address).allocated.attempt.map { result => assert(result.isLeft) - result.swap.toOption.get shouldBe a[InitializationError] + result.left.get shouldBe a[InitializationError] } } .runSyncUnsafe() From 7a94987c0ea7e5da2d3b7e09f5ddf8fe41f7ed05 Mon Sep 17 00:00:00 2001 From: Dmitry Voronov Date: Tue, 22 Dec 2020 21:16:47 +0300 Subject: [PATCH 5/5] feat - fixed all the tests and ordering errors --- .../v4/DiscoveryKademliaIntegrationSpec.scala | 4 ++-- .../ethereum/EthereumNodeRecord.scala | 2 +- .../ethereum/codecs/DefaultCodecs.scala | 2 +- .../ethereum/v4/DiscoveryNetworkSpec.scala | 6 +++--- .../ethereum/v4/DiscoveryServiceSpec.scala | 18 +++++++++--------- .../ethereum/v4/mocks/MockPeerGroup.scala | 2 +- .../iohk/scalanet/kademlia/KBucketsSpec.scala | 2 +- .../iohk/scalanet/kademlia/KNetworkSpec.scala | 2 +- .../iohk/scalanet/kademlia/KRouterSpec.scala | 2 +- .../iohk/scalanet/kademlia/TimeSetSpec.scala | 4 ++-- .../io/iohk/scalanet/kademlia/XorSpec.scala | 2 +- 11 files changed, 23 insertions(+), 23 deletions(-) diff --git a/scalanet/discovery/it/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryKademliaIntegrationSpec.scala b/scalanet/discovery/it/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryKademliaIntegrationSpec.scala index 14a39e2c..ecd49e75 100644 --- a/scalanet/discovery/it/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryKademliaIntegrationSpec.scala +++ b/scalanet/discovery/it/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryKademliaIntegrationSpec.scala @@ -33,8 +33,8 @@ class DiscoveryKademliaIntegrationSpec extends KademliaIntegrationSpec("Discover // Not dealing with non-conforming clients here. implicit val packetCoded = Packet.packetCodec(allowDecodeOverMaxPacketSize = false) - override def generatePeerRecordWithKey() = { - val address = NetUtils.aRandomAddress + override def generatePeerRecordWithKey = { + val address = NetUtils.aRandomAddress() val (publicKey, privateKey) = sigalg.newKeyPair val node = Node(publicKey, Node.Address(address.getAddress, address.getPort, address.getPort)) node -> privateKey diff --git a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/EthereumNodeRecord.scala b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/EthereumNodeRecord.scala index f326d800..7be0e4b7 100644 --- a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/EthereumNodeRecord.scala +++ b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/EthereumNodeRecord.scala @@ -19,7 +19,7 @@ case class EthereumNodeRecord( object EthereumNodeRecord { implicit val byteVectorOrdering: Ordering[ByteVector] = - Ordering.by[ByteVector, Iterable[Byte]](_.toIterable) + Ordering.by[ByteVector, Seq[Byte]](_.toSeq) case class Content( // Nodes should increment this number whenever their properties change, like their address, and re-publish. diff --git a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/codecs/DefaultCodecs.scala b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/codecs/DefaultCodecs.scala index 5905e88a..b8060c94 100644 --- a/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/codecs/DefaultCodecs.scala +++ b/scalanet/discovery/src/io/iohk/scalanet/discovery/ethereum/codecs/DefaultCodecs.scala @@ -42,7 +42,7 @@ object DefaultCodecs { ) implicit val byteVectorOrdering: Ordering[ByteVector] = - Ordering.by[ByteVector, Iterable[Byte]](_.toIterable) + Ordering.by[ByteVector, Seq[Byte]](_.toSeq) implicit val attrCodec: Codec[SortedMap[ByteVector, ByteVector]] = sortedMapCodec[ByteVector, ByteVector] diff --git a/scalanet/discovery/ut/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryNetworkSpec.scala b/scalanet/discovery/ut/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryNetworkSpec.scala index b079a1a3..86e24af1 100644 --- a/scalanet/discovery/ut/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryNetworkSpec.scala +++ b/scalanet/discovery/ut/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryNetworkSpec.scala @@ -420,7 +420,7 @@ class DiscoveryNetworkSpec extends AsyncFlatSpec with Matchers { // from earlier channels are exhausted before it would handle later ones. it should "handle multiple channels in concurrently" in test { new Fixture { - val remotes = List.fill(5)(aRandomAddress -> sigalg.newKeyPair) + val remotes = List.fill(5)(aRandomAddress() -> sigalg.newKeyPair) override val test = for { _ <- network.startHandling { @@ -797,7 +797,7 @@ object DiscoveryNetworkSpec extends Matchers { lazy val config = defaultConfig - lazy val localAddress = aRandomAddress + lazy val localAddress = aRandomAddress() // Keys for the System Under Test. lazy val (publicKey, privateKey) = sigalg.newKeyPair @@ -814,7 +814,7 @@ object DiscoveryNetworkSpec extends Matchers { ) // A random peer to talk to. - lazy val remoteAddress = aRandomAddress + lazy val remoteAddress = aRandomAddress() lazy val (remotePublicKey, remotePrivateKey) = sigalg.newKeyPair lazy val remotePeer = Peer(remotePublicKey, remoteAddress) diff --git a/scalanet/discovery/ut/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryServiceSpec.scala b/scalanet/discovery/ut/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryServiceSpec.scala index 2ff2bec1..d2481c04 100644 --- a/scalanet/discovery/ut/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryServiceSpec.scala +++ b/scalanet/discovery/ut/src/io/iohk/scalanet/discovery/ethereum/v4/DiscoveryServiceSpec.scala @@ -89,7 +89,7 @@ class DiscoveryServiceSpec extends AsyncFlatSpec with Matchers { } it should "return false for nodes that changed their address" in test { new IsBondedFixture { - override def peer = Peer(remotePublicKey, aRandomAddress) + override def peer = Peer(remotePublicKey, aRandomAddress()) override def expected = false override def setupState = _.withLastPongTimestamp( @@ -354,7 +354,7 @@ class DiscoveryServiceSpec extends AsyncFlatSpec with Matchers { ping = _ => _ => Task.pure(Some(None)), enrRequest = _ => _ => Task(Some(remoteENR)) ) - val previousAddress = aRandomAddress + val previousAddress = aRandomAddress() val previousNode = makeNode(remotePublicKey, previousAddress) // Say it had the same ENR SEQ, but a different address. val previousEnr = EthereumNodeRecord.fromNode(previousNode, remotePrivateKey, seq = remoteENR.content.seq).require @@ -394,7 +394,7 @@ class DiscoveryServiceSpec extends AsyncFlatSpec with Matchers { List.fill(5)(service.fetchEnr(remotePeer)) ) } yield { - callCount.get shouldBe 1 + callCount.get() shouldBe 1 } } } @@ -463,7 +463,7 @@ class DiscoveryServiceSpec extends AsyncFlatSpec with Matchers { if (Node.kademliaId(publicKey)(0) == Node.kademliaId(localPublicKey)(0)) makePeerInFirstBucket else { - val address = aRandomAddress + val address = aRandomAddress() val node = makeNode(publicKey, address) val peer = Peer(publicKey, address) val enr = EthereumNodeRecord.fromNode(node, privateKey, seq = 1).require @@ -569,7 +569,7 @@ class DiscoveryServiceSpec extends AsyncFlatSpec with Matchers { new Fixture { val caller = { val (callerPublicKey, _) = sigalg.newKeyPair - val callerAddress = aRandomAddress + val callerAddress = aRandomAddress() Peer(callerPublicKey, callerAddress) } @@ -623,7 +623,7 @@ class DiscoveryServiceSpec extends AsyncFlatSpec with Matchers { def newRandomNode = { val (publicKey, privateKey) = sigalg.newKeyPair - val address = aRandomAddress + val address = aRandomAddress() val node = makeNode(publicKey, address) val enr = EthereumNodeRecord.fromNode(node, privateKey, seq = 1).require node -> enr @@ -747,7 +747,7 @@ class DiscoveryServiceSpec extends AsyncFlatSpec with Matchers { new Fixture { val localNodes = List.fill(config.kademliaBucketSize) { val (publicKey, privateKey) = sigalg.newKeyPair - val address = aRandomAddress + val address = aRandomAddress() val node = makeNode(publicKey, address) val enr = EthereumNodeRecord.fromNode(node, privateKey, seq = 1).require node -> enr @@ -1058,12 +1058,12 @@ object DiscoveryServiceSpec { Node(publicKey, Node.Address(address.getAddress, address.getPort, address.getPort)) lazy val (localPublicKey, localPrivateKey) = sigalg.newKeyPair - lazy val localAddress = aRandomAddress + lazy val localAddress = aRandomAddress() lazy val localNode = makeNode(localPublicKey, localAddress) lazy val localPeer = Peer(localPublicKey, localAddress) lazy val localENR = EthereumNodeRecord.fromNode(localNode, localPrivateKey, seq = 1).require - lazy val remoteAddress = aRandomAddress + lazy val remoteAddress = aRandomAddress() lazy val (remotePublicKey, remotePrivateKey) = sigalg.newKeyPair lazy val remoteNode = makeNode(remotePublicKey, remoteAddress) lazy val remotePeer = Peer(remotePublicKey, remoteAddress) diff --git a/scalanet/discovery/ut/src/io/iohk/scalanet/discovery/ethereum/v4/mocks/MockPeerGroup.scala b/scalanet/discovery/ut/src/io/iohk/scalanet/discovery/ethereum/v4/mocks/MockPeerGroup.scala index 8f8249ec..18a6de30 100644 --- a/scalanet/discovery/ut/src/io/iohk/scalanet/discovery/ethereum/v4/mocks/MockPeerGroup.scala +++ b/scalanet/discovery/ut/src/io/iohk/scalanet/discovery/ethereum/v4/mocks/MockPeerGroup.scala @@ -62,7 +62,7 @@ class MockChannel[A, M]( private val messagesToSUT = ConcurrentQueue[Task].unsafe[ChannelEvent[M]](BufferCapacity.Unbounded()) def isClosed: Boolean = - refCount.get == 0 + refCount.get() == 0 // Messages coming from the System Under Test. override def sendMessage(message: M): Task[Unit] = diff --git a/scalanet/discovery/ut/src/io/iohk/scalanet/kademlia/KBucketsSpec.scala b/scalanet/discovery/ut/src/io/iohk/scalanet/kademlia/KBucketsSpec.scala index 08eb3b92..a54aa8c3 100644 --- a/scalanet/discovery/ut/src/io/iohk/scalanet/kademlia/KBucketsSpec.scala +++ b/scalanet/discovery/ut/src/io/iohk/scalanet/kademlia/KBucketsSpec.scala @@ -7,7 +7,7 @@ import io.iohk.scalanet.kademlia.Generators._ import io.iohk.scalanet.kademlia.KBucketsSpec._ import org.scalatest.FlatSpec import org.scalatest.Matchers._ -import org.scalatest.prop.GeneratorDrivenPropertyChecks._ +import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks._ import scodec.bits.BitVector import scala.util.Random diff --git a/scalanet/discovery/ut/src/io/iohk/scalanet/kademlia/KNetworkSpec.scala b/scalanet/discovery/ut/src/io/iohk/scalanet/kademlia/KNetworkSpec.scala index ae37555e..a0675822 100644 --- a/scalanet/discovery/ut/src/io/iohk/scalanet/kademlia/KNetworkSpec.scala +++ b/scalanet/discovery/ut/src/io/iohk/scalanet/kademlia/KNetworkSpec.scala @@ -14,7 +14,7 @@ import monix.eval.Task import monix.reactive.Observable import org.scalatest.FlatSpec import org.scalatest.Matchers._ -import org.scalatest.mockito.MockitoSugar._ +import org.scalatestplus.mockito.MockitoSugar._ import org.mockito.Mockito.{when} import scala.concurrent.duration._ diff --git a/scalanet/discovery/ut/src/io/iohk/scalanet/kademlia/KRouterSpec.scala b/scalanet/discovery/ut/src/io/iohk/scalanet/kademlia/KRouterSpec.scala index 1ac515d5..f7c628b5 100644 --- a/scalanet/discovery/ut/src/io/iohk/scalanet/kademlia/KRouterSpec.scala +++ b/scalanet/discovery/ut/src/io/iohk/scalanet/kademlia/KRouterSpec.scala @@ -23,7 +23,7 @@ import org.scalatest.FreeSpec import org.scalatest.Matchers._ import org.scalatest.concurrent.Eventually import org.scalatest.concurrent.ScalaFutures._ -import org.scalatest.mockito.MockitoSugar._ +import org.scalatestplus.mockito.MockitoSugar._ import scodec.bits._ import scala.concurrent.TimeoutException diff --git a/scalanet/discovery/ut/src/io/iohk/scalanet/kademlia/TimeSetSpec.scala b/scalanet/discovery/ut/src/io/iohk/scalanet/kademlia/TimeSetSpec.scala index 2c5e491c..4947d188 100644 --- a/scalanet/discovery/ut/src/io/iohk/scalanet/kademlia/TimeSetSpec.scala +++ b/scalanet/discovery/ut/src/io/iohk/scalanet/kademlia/TimeSetSpec.scala @@ -5,8 +5,8 @@ import java.time.Clock import org.mockito.Mockito.when import org.scalatest.FlatSpec import org.scalatest.Matchers._ -import org.scalatest.mockito.MockitoSugar._ -import org.scalatest.prop.GeneratorDrivenPropertyChecks._ +import org.scalatestplus.mockito.MockitoSugar._ +import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks._ import scala.util.Random diff --git a/scalanet/discovery/ut/src/io/iohk/scalanet/kademlia/XorSpec.scala b/scalanet/discovery/ut/src/io/iohk/scalanet/kademlia/XorSpec.scala index 93b8d9c7..df483f52 100644 --- a/scalanet/discovery/ut/src/io/iohk/scalanet/kademlia/XorSpec.scala +++ b/scalanet/discovery/ut/src/io/iohk/scalanet/kademlia/XorSpec.scala @@ -5,7 +5,7 @@ import io.iohk.scalanet.kademlia.Xor._ import org.scalacheck.Gen.posNum import org.scalatest.FlatSpec import org.scalatest.Matchers._ -import org.scalatest.prop.GeneratorDrivenPropertyChecks._ +import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks._ import scodec.bits.BitVector class XorSpec extends FlatSpec {