Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scala 2.13 cross-build upgrade #112

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
264 changes: 150 additions & 114 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,147 +6,183 @@ 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"

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",
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
aakoshh marked this conversation as resolved.
Show resolved Hide resolved


// 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",
"-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")

// `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
// 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

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"
)

private val versionOptions = crossVersion.take(4) match {
case "2.12" => `scala 2.12 options`
case "2.13" => `scala 2.13 options`
}

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 = ScalanetModule.this.scalacOptions

override def moduleDeps: Seq[JavaModule] =
Seq(scalanet)
override def testFrameworks =
Seq("org.scalatest.tools.Framework")

def single(args: String*) = T.command {
super.runMain("org.scalatest.run", args: _*)
override def ivyDeps = Agg(
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",
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: _*)
}
}
}
}

// 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.1-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[PublishModule] =
Seq(scalanet)

override def moduleDeps: Seq[JavaModule] =
Seq(scalanet, scalanet.discovery)
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)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.util.byteVectorOrdering
import scodec.{Codec, Attempt}
import io.iohk.scalanet.discovery.crypto.PublicKey
import java.net.Inet6Address
Expand All @@ -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] = byteVectorOrdering

case class Content(
// Nodes should increment this number whenever their properties change, like their address, and re-publish.
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,6 +14,7 @@ import scala.collection.SortedMap
import java.net.InetAddress

object DefaultCodecs {

implicit val publicKeyCodec: Codec[PublicKey] =
implicitly[Codec[BitVector]].xmap(PublicKey(_), identity)

Expand All @@ -38,8 +40,7 @@ object DefaultCodecs {
(sm: SortedMap[K, V]) => sm.toList
)

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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 Right(None) =>
Task.unit
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"))
}
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] =
Expand Down Expand Up @@ -385,6 +381,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 _ =>
aakoshh marked this conversation as resolved.
Show resolved Hide resolved
// Invalid state - this cannot happen
Task.raiseError(new IllegalStateException(s"Unexpected state while collecting responses from ${channel.to}"))
}
.map(_.map(_._1))

Expand All @@ -404,7 +404,7 @@ object DiscoveryNetwork {
)
val expiration = System.currentTimeMillis

Stream
Iterator
.iterate(List(sampleNode))(sampleNode :: _)
.map { nodes =>
val payload = Neighbors(nodes, expiration)
Expand Down
Loading