Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis Bettens committed Jun 13, 2022
1 parent 5136902 commit 2c7e7fa
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 31 deletions.
9 changes: 3 additions & 6 deletions be2-scala/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@ scalaVersion := "2.13.7"
// Recommended 2.13 Scala flags (https://nathankleyn.com/2019/05/13/recommended-scalac-flags-for-2-13) slightly adapted for PoP
scalacOptions ++= Seq(
"-deprecation", // Emit warning and location for usages of deprecated APIs.
"-explain-types", // Explain type errors in more detail.
//"-explain-types", // Explain type errors in more detail.
"-feature", // Emit warning and location for usages of features that should be imported explicitly.
"-language:existentials", // Existential types (besides wildcard types) can be written and inferred
"-language:experimental.macros", // Allow macro definition (besides implementation and application)
"-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.
//"-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 @@ -107,7 +104,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 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,12 +20,12 @@ 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](dbActor: AskableActorRef = DbActor.getInstance): Future[List[(Message, T)]] = {
for {
DbActor.DbActorCatchupAck(messages) <- dbActor ? DbActor.Catchup(channel)
result <- Future.traverse(messages.flatMap(message =>
message.decodedData match {
case Some(t: T) => Some((message, t))
case Some(t) => Some((message, t.asInstanceOf[T]))
case _ => None
})
) { message => Future(message) }
Expand Down
Original file line number Diff line number Diff line change
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 All @@ -74,7 +74,7 @@ case object LaoHandler extends MessageHandler {

def handleGreetLao(rpcMessage: JsonRpcRequest): GraphMessage = {
val ask: Future[GraphMessage] = dbAskWritePropagate(rpcMessage)
Await.result(ask, duration)
Await.result[GraphMessage](ask, duration)
}

def handleStateLao(rpcMessage: JsonRpcRequest): GraphMessage = {
Expand All @@ -85,6 +85,6 @@ case object LaoHandler extends MessageHandler {
def handleUpdateLao(rpcMessage: JsonRpcRequest): GraphMessage = {
//FIXME: the main channel is not updated
val ask: Future[GraphMessage] = dbAskWritePropagate(rpcMessage)
Await.result(ask, duration)
Await.result[GraphMessage](ask, duration)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class RollCallHandler(dbRef: => AskableActorRef) extends MessageHandler {

def handleCloseRollCall(rpcRequest: JsonRpcRequest): GraphMessage = {
val ask: Future[GraphMessage] = dbAskWritePropagate(rpcRequest)
Await.result(ask, duration) match {
Await.result[GraphMessage](ask, duration) match {
case Left(_) =>
rpcRequest.getParamsMessage match {
case Some(message: Message) =>
Expand Down 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,6 @@ 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))
}
}

Expand All @@ -81,7 +80,6 @@ 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))
}
}

Expand All @@ -104,7 +102,7 @@ class SocialMediaHandler(dbRef: => AskableActorRef) extends MessageHandler {

private def writeAndPropagate(rpcMessage: JsonRpcRequest): GraphMessage = {
val ask: Future[GraphMessage] = dbAskWritePropagate(rpcMessage)
Await.result(ask, duration)
Await.result[GraphMessage](ask, duration)
}

}
Expand Down
Original file line number Diff line number Diff line change
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
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
Original file line number Diff line number Diff line change
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

0 comments on commit 2c7e7fa

Please sign in to comment.