Skip to content

Commit

Permalink
[BE2] migrate code to scala3
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis Bettens committed Jun 13, 2022
1 parent 5136902 commit 66603e5
Show file tree
Hide file tree
Showing 22 changed files with 37 additions and 45 deletions.
14 changes: 5 additions & 9 deletions be2-scala/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import sbtsonar.SonarPlugin.autoImport.sonarProperties

name := "pop"

scalaVersion := "2.13.7"
scalaVersion := "3.1.2"

// Recommended 2.13 Scala flags (https://nathankleyn.com/2019/05/13/recommended-scalac-flags-for-2-13) slightly adapted for PoP
scalacOptions ++= Seq(
Expand All @@ -15,11 +15,7 @@ scalacOptions ++= Seq(
"-language:higherKinds", // Allow higher-kinded types
"-language:implicitConversions", // Allow definition of implicit functions called views
"-unchecked", // Enable additional warnings where generated code depends on assumptions.
"-check-init", // Wrap field accessors to throw an exception on uninitialized access.
"-Xfatal-warnings", // Fail the compilation if there are any warnings.
"-Ywarn-extra-implicit", // Warn when more than one implicit parameter section is defined.
"-Ycache-plugin-class-loader:last-modified", // Enables caching of classloaders for compiler plugins
"-Ycache-macro-class-loader:last-modified", // and macro definitions. This can lead to performance improvements.
)

// Reload changes automatically
Expand Down Expand Up @@ -85,9 +81,9 @@ sonarProperties := Map(
"sonar.tests" -> "src/test/scala",

"sonar.sourceEncoding" -> "UTF-8",
"sonar.scala.version" -> "2.13.7",
"sonar.scala.version" -> scalaVersion.value,
// Paths to the test and coverage reports
"sonar.scala.coverage.reportPaths" -> "./target/scala-2.13/scoverage-report/scoverage.xml",
"sonar.scala.coverage.reportPaths" -> "./target/scala-3.1/scoverage-report/scoverage.xml",
)

assembly/ assemblyMergeStrategy := {
Expand All @@ -107,7 +103,7 @@ val AkkaHttpVersion = "10.2.9"

libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-stream-typed" % AkkaVersion, // Akka streams (Graph)
"com.typesafe.akka" %% "akka-http" % AkkaHttpVersion, // Akka http (WebSockets)
("com.typesafe.akka" %% "akka-http" % AkkaHttpVersion).cross(CrossVersion.for3Use2_13), // Akka http (WebSockets)
"com.typesafe.akka" %% "akka-cluster-tools" % AkkaVersion, // Akka distributed publish/subscribe cluster

"ch.qos.logback" % "logback-classic" % "1.1.3" % Runtime, // Akka logging library
Expand All @@ -127,7 +123,7 @@ libraryDependencies += "com.google.crypto.tink" % "tink" % "1.5.0"
libraryDependencies += "ch.epfl.dedis" % "cothority" % "3.3.1"

// Scala unit tests
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.9" % Test
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.12" % Test

// Json Schema Validator w/ Jackson Databind
libraryDependencies += "com.networknt" % "json-schema-validator" % "1.0.60"
Expand Down
1 change: 0 additions & 1 deletion be2-scala/project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.6.1")
addSbtPlugin("com.sonar-scala" % "sbt-sonar" % "2.3.0")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "1.1.0")
addSbtPlugin("com.rallyhealth.sbt" % "sbt-git-versioning" % "1.6.0")
addSbtPlugin("ch.epfl.scala" % "sbt-scala3-migrate" % "0.5.1")
2 changes: 1 addition & 1 deletion be2-scala/src/main/scala/ch/epfl/pop/Server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import akka.actor.{ActorRef, Props}
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.{RequestContext, RouteResult}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.util.Timeout
import ch.epfl.pop.config.{RuntimeEnvironment, ServerConf}
import ch.epfl.pop.pubsub.{MessageRegistry, PubSubMediator, PublishSubscribe}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ object HighLevelProtocol extends DefaultJsonProtocol {
Broadcast(params.channel, params.message)
}

override def write(obj: Broadcast): JsValue = obj.toJson(ParamsFormat.write)
override def write(obj: Broadcast): JsValue = obj.toJson(ParamsFormat.write _)
}

implicit object CatchupFormat extends RootJsonFormat[Catchup] {
override def read(json: JsValue): Catchup = Catchup(json.convertTo[Params].channel)

override def write(obj: Catchup): JsValue = obj.toJson(ParamsFormat.write)
override def write(obj: Catchup): JsValue = obj.toJson(ParamsFormat.write _)
}

implicit object PublishFormat extends RootJsonFormat[Publish] {
Expand All @@ -99,19 +99,19 @@ object HighLevelProtocol extends DefaultJsonProtocol {
Publish(params.channel, params.message)
}

override def write(obj: Publish): JsValue = obj.toJson(ParamsFormat.write)
override def write(obj: Publish): JsValue = obj.toJson(ParamsFormat.write _)
}

implicit object SubscribeFormat extends RootJsonFormat[Subscribe] {
override def read(json: JsValue): Subscribe = Subscribe(json.convertTo[Params].channel)

override def write(obj: Subscribe): JsValue = obj.toJson(ParamsFormat.write)
override def write(obj: Subscribe): JsValue = obj.toJson(ParamsFormat.write _)
}

implicit object UnsubscribeFormat extends RootJsonFormat[Unsubscribe] {
override def read(json: JsValue): Unsubscribe = Unsubscribe(json.convertTo[Params].channel)

override def write(obj: Unsubscribe): JsValue = obj.toJson(ParamsFormat.write)
override def write(obj: Unsubscribe): JsValue = obj.toJson(ParamsFormat.write _)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ object ElectionChannel {
* @tparam T type of data to extract from the election channel
* @return Future of a list of tuple containing the message and the data extracted
*/
def extractMessages[T: Manifest](dbActor: AskableActorRef = DbActor.getInstance): Future[List[(Message, T)]] = {
def extractMessages[T: reflect.ClassTag](dbActor: AskableActorRef = DbActor.getInstance): Future[List[(Message, T)]] = {
for {
DbActor.DbActorCatchupAck(messages) <- dbActor ? DbActor.Catchup(channel)
result <- Future.traverse(messages.flatMap(message =>
Expand Down
4 changes: 2 additions & 2 deletions be2-scala/src/main/scala/ch/epfl/pop/pubsub/ClientActor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ch.epfl.pop.pubsub

import akka.actor.{Actor, ActorLogging, ActorRef, Props}
import akka.event.LoggingReceive
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import ch.epfl.pop.model.objects.Channel
import ch.epfl.pop.pubsub.ClientActor._
import ch.epfl.pop.pubsub.PubSubMediator._
Expand Down Expand Up @@ -93,7 +93,7 @@ object ClientActor {
final case class ConnectWsHandle(wsClient: ActorRef) extends Event

// unsubscribe from all channels
final case object DisconnectWsHandle extends Event
case object DisconnectWsHandle extends Event

// subscribe to a particular channel
final case class SubscribeTo(channel: Channel) extends Event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object Answerer {
// Send the ClientAnswer to clientActorRef. Whenever the stream between the client
// actor and the actual client (front-end) is broken, the message DisconnectWsHandle
// is sent to clientActorRef
.to(Sink.actorRef(clientActorRef, DisconnectWsHandle, { t: Throwable => println(t); DisconnectWsHandle }))
.to(Sink.actorRef(clientActorRef, DisconnectWsHandle, { (t: Throwable) => println(t); DisconnectWsHandle }))

// Integration point between Akka Streams and above actor
val source: Source[TextMessage, NotUsed] = Source
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ case object LaoHandler extends MessageHandler {
_ <- dbActor ? DbActor.WriteLaoData(laoChannel, message, address)
//after creating the lao, we need to send a lao#greet message to the frontend
greet: GreetLao = GreetLao(data.id, params.get.sender, address.get, List.empty)
broadcastGreet: Base64Data = Base64Data.encode(GreetLaoFormat.write(greet).toString())
broadcastGreet: Base64Data = Base64Data.encode(GreetLaoFormat.write(greet).toString)
_ <- dbBroadcast(rpcMessage, laoChannel, broadcastGreet, laoChannel)
} yield ()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ch.epfl.pop.pubsub.graph.handlers

import akka.NotUsed
import akka.actor.ActorRef
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.stream.FlowShape
import akka.stream.scaladsl.{Flow, GraphDSL, Merge, Partition}
import ch.epfl.pop.model.network.method.{Catchup, Subscribe, Unsubscribe}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class RollCallHandler(dbRef: => AskableActorRef) extends MessageHandler {
))
}
case error@Right(_) => error
case _ => Right(PipelineError(ErrorCodes.SERVER_ERROR.id, unknownAnswer, rpcRequest.id))
case null => Right(PipelineError(ErrorCodes.SERVER_ERROR.id, unknownAnswer, rpcRequest.id))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class SocialMediaHandler(dbRef: => AskableActorRef) extends MessageHandler {
case None => Right(PipelineError(ErrorCodes.SERVER_ERROR.id, "Server failed to extract LAO id for the broadcast", rpcMessage.id))
}
case error@Right(_) => error
case _ => Right(PipelineError(ErrorCodes.SERVER_ERROR.id, unknownAnswerDatabase, rpcMessage.id))
case null => Right(PipelineError(ErrorCodes.SERVER_ERROR.id, unknownAnswerDatabase, rpcMessage.id))
}
}

Expand All @@ -81,7 +81,7 @@ class SocialMediaHandler(dbRef: => AskableActorRef) extends MessageHandler {
case None => Right(PipelineError(ErrorCodes.SERVER_ERROR.id, "Server failed to extract LAO id for the broadcast", rpcMessage.id))
}
case error@Right(_) => error
case _ => Right(PipelineError(ErrorCodes.SERVER_ERROR.id, unknownAnswerDatabase, rpcMessage.id))
case null => Right(PipelineError(ErrorCodes.SERVER_ERROR.id, unknownAnswerDatabase, rpcMessage.id))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ class ChannelSuite extends AnyFunSuite with should.Matchers {
def channel = Channel("/root/full/pop")

val expected = Hash(Base64Data("pop"))
an[IllegalArgumentException] shouldNot be(thrownBy(channel))
channel.extractChildChannel should equal(expected)
}
test("Encoded LaoId extraction channel test") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.epfl.pop.pubsub.graph

import akka.actor.{Actor, ActorSystem, Props, Status}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
import ch.epfl.pop.model.network.method.message.Message
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.epfl.pop.pubsub.graph.handlers

import akka.actor.{Actor, ActorSystem, Props, Status}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
import ch.epfl.pop.model.network.method.message.Message
Expand Down Expand Up @@ -36,12 +36,12 @@ class ElectionHandlerTest extends TestKit(ActorSystem("Election-DB-System")) wit
private val keyPair: KeyPair = KeyPair()
private val electionData: ElectionData = ElectionData(Hash(Base64Data.encode("election")), keyPair)

private final val sender: PublicKey = SetupElectionExamples.SENDER_SETUPELECTION
private final val SENDER: PublicKey = SetupElectionExamples.SENDER_SETUPELECTION

private final val PUBLIC_KEY: PublicKey = PublicKey(Base64Data("jsNj23IHALvppqV1xQfP71_3IyAHzivxiCz236_zzQc="))
private final val PRIVATE_KEY: PrivateKey = PrivateKey(Base64Data("qRfms3wzSLkxAeBz6UtwA-L1qP0h8D9XI1FSvY68t7Y="))
private final val PK_OWNER: PublicKey = PublicKey(Base64Data.encode("wrongOwner"))
private final val laoDataRight: LaoData = LaoData(sender, List(sender), PRIVATE_KEY, PUBLIC_KEY, List.empty)
private final val laoDataRight: LaoData = LaoData(SENDER, List(SENDER), PRIVATE_KEY, PUBLIC_KEY, List.empty)

private final val channelDataWithSetupAndOpenAndCastMessage: ChannelData = ChannelData(ObjectType.ELECTION, List(DATA_CAST_VOTE_MESSAGE, DATA_SET_UP_OPEN_BALLOT, DATA_OPEN_MESSAGE))
private final val messages: List[Message] = List(MESSAGE_CAST_VOTE_ELECTION_WORKING, MESSAGE_SETUPELECTION_OPEN_BALLOT_WORKING, MESSAGE_OPEN_ELECTION_WORKING, MESSAGE_END_ELECTION_WORKING)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.epfl.pop.pubsub.graph.handlers

import akka.actor.{Actor, ActorSystem, Props, Status}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
import ch.epfl.pop.model.objects.DbActorNAckException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.epfl.pop.pubsub.graph.handlers

import akka.actor.{Actor, ActorSystem, Props, Status}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
import ch.epfl.pop.model.objects.DbActorNAckException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.epfl.pop.pubsub.graph.validators

import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
import ch.epfl.pop.config.RuntimeEnvironment.deleteRecursively
Expand Down Expand Up @@ -46,12 +46,12 @@ class ElectionValidatorSuite extends TestKit(ActorSystem("electionValidatorTestA
deleteRecursively(directory)
}

private final val sender: PublicKey = SetupElectionExamples.SENDER_SETUPELECTION
private final val SENDER: PublicKey = SetupElectionExamples.SENDER_SETUPELECTION

private final val PUBLIC_KEY: PublicKey = PublicKey(Base64Data("jsNj23IHALvppqV1xQfP71_3IyAHzivxiCz236_zzQc="))
private final val PRIVATE_KEY: PrivateKey = PrivateKey(Base64Data("qRfms3wzSLkxAeBz6UtwA-L1qP0h8D9XI1FSvY68t7Y="))
private final val PK_WRONG: PublicKey = PublicKey(Base64Data.encode("wrongOwner"))
private final val laoDataRight: LaoData = LaoData(sender, List(sender), PRIVATE_KEY, PUBLIC_KEY, List.empty)
private final val laoDataRight: LaoData = LaoData(SENDER, List(SENDER), PRIVATE_KEY, PUBLIC_KEY, List.empty)
private final val laoDataWrong: LaoData = LaoData(PK_WRONG, List(PK_WRONG), PRIVATE_KEY, PUBLIC_KEY, List.empty)
private final val channelDataRightSetup: ChannelData = ChannelData(ObjectType.LAO, List.empty)
private final val channelDataWrongSetup: ChannelData = ChannelData(ObjectType.ELECTION, List.empty)
Expand Down Expand Up @@ -433,7 +433,7 @@ class ElectionValidatorSuite extends TestKit(ActorSystem("electionValidatorTestA
test("Open up an election with invalid lao id fails") {
val dbActorRef = mockDbWorking
val message: GraphMessage = new ElectionValidator(dbActorRef).validateOpenElection(OPEN_ELECTION_WRONG_LAO_ID_RPC)
val messageStandardActor: GraphMessage = ElectionValidator.validateOpenElection(OPEN_ELECTION_WRONG_LAO_ID_RPC)
val messageStandardActor: GraphMessage = ElectionValidator.validateOpenElection(OPEN_ELECTION_WRONG_LAO_ID_RPC)
message shouldBe a[Right[_, PipelineError]]
messageStandardActor shouldBe a[Right[_, PipelineError]]
system.stop(dbActorRef.actorRef)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ch.epfl.pop.config.RuntimeEnvironment.deleteRecursively
import ch.epfl.pop.model.network.method.message.data.ObjectType
import ch.epfl.pop.model.objects.{Base64Data, ChannelData, LaoData, PrivateKey, PublicKey}
import ch.epfl.pop.storage.{DbActor, InMemoryStorage}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
import ch.epfl.pop.pubsub.graph.{GraphMessage, PipelineError}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.epfl.pop.pubsub.graph.validators

import akka.actor.{Actor, ActorSystem, Props, Status}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
import ch.epfl.pop.model.network.method.message.data.ObjectType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.epfl.pop.pubsub.graph.validators

import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
import ch.epfl.pop.config.RuntimeEnvironment.deleteRecursively
Expand Down Expand Up @@ -46,13 +46,11 @@ class RollCallValidatorSuite extends TestKit(ActorSystem("rollcallValidatorTestA
deleteRecursively(directory)
}

private final val sender: PublicKey = SENDER

private final val PUBLIC_KEY: PublicKey = PublicKey(Base64Data("jsNj23IHALvppqV1xQfP71_3IyAHzivxiCz236_zzQc="))
private final val PRIVATE_KEY: PrivateKey = PrivateKey(Base64Data("qRfms3wzSLkxAeBz6UtwA-L1qP0h8D9XI1FSvY68t7Y="))
private final val PK_OWNER: PublicKey = PublicKey(Base64Data.encode("wrongOwner"))
private final val laoDataRight: LaoData = LaoData(sender, List(sender), PRIVATE_KEY, PUBLIC_KEY, List.empty)
private final val laoDataWrong: LaoData = LaoData(sender, List(PK_OWNER), PRIVATE_KEY, PUBLIC_KEY, List.empty)
private final val laoDataRight: LaoData = LaoData(SENDER, List(SENDER), PRIVATE_KEY, PUBLIC_KEY, List.empty)
private final val laoDataWrong: LaoData = LaoData(SENDER, List(PK_OWNER), PRIVATE_KEY, PUBLIC_KEY, List.empty)
private final val channelDataWrong: ChannelData = ChannelData(ObjectType.INVALID, List.empty)
private final val channelDataRight: ChannelData = ChannelData(ObjectType.LAO, List.empty)
private final val rollcallDataCreate: RollCallData = RollCallData(CreateRollCallExamples.R_ID, ActionType.CREATE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.io.File
import java.util.concurrent.TimeUnit

import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
import ch.epfl.pop.config.RuntimeEnvironment.deleteRecursively
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.epfl.pop.storage

import akka.actor.{ActorRef, ActorSystem, Props}
import akka.pattern.AskableActorRef
import akka.pattern.{AskableActorRef, ask}
import akka.testkit.{ImplicitSender, TestKit}
import ch.epfl.pop.model.network.method.message.Message
import ch.epfl.pop.model.network.method.message.data.ActionType.{ActionType, CREATE}
Expand Down

0 comments on commit 66603e5

Please sign in to comment.