Skip to content

Commit

Permalink
style fix for NetworkActor
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed May 12, 2018
1 parent de9baba commit eb9eda0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.github.fluency03.blockchain.api._
import com.github.fluency03.blockchain.core.Block

import scala.collection.mutable
import scala.concurrent.Future
import scala.util.{Failure, Success}

object BlockPoolActor {
Expand All @@ -33,7 +32,7 @@ class BlockPoolActor extends ActorSupport {
val txPoolActor: ActorSelection = context.actorSelection(PARENT_UP + TX_POOL_ACTOR_NAME)

// TODO (Chang): need persistence
var blocksPool = mutable.Map.empty[String, Block]
var blocksPool: mutable.Map[String, Block] = mutable.Map.empty[String, Block]

def receive: Receive = {
case GetBlocks => onGetBlocks()
Expand All @@ -49,12 +48,10 @@ class BlockPoolActor extends ActorSupport {
/**
* Handlers for each of the Messages.
*/

private[this] def onGetBlocks(): Unit = sender() ! blocksPool.values.toSeq

private[this] def onGetBlocks(hashes: Set[String]): Unit = sender() ! blocksPool.filterKeys(
k => hashes.contains(k)
).values.toSeq
private[this] def onGetBlocks(hashes: Set[String]): Unit =
sender() ! blocksPool.filterKeys(hashes.contains).values.toSeq

private[this] def onAddBlock(block: Block): Unit = {
if (blocksPool.contains(block.hash))
Expand All @@ -79,18 +76,18 @@ class BlockPoolActor extends ActorSupport {
}

private def onMineAndAddNextBlock(data: String, ids: Seq[String]): Unit = {
val maybeBlock: Future[Option[Block]] =
(blockchainActor ? BlockchainActor.MineNextBlock(data, ids)).mapTo[Option[Block]]
val theSender: ActorRef = sender()
maybeBlock onComplete {
case Success(blockOpt) => blockOpt match {
case Some(block) =>
blocksPool += (block.hash -> block)
theSender ! Some(block)
case None => theSender ! None
(blockchainActor ? BlockchainActor.MineNextBlock(data, ids))
.mapTo[Option[Block]]
.onComplete {
case Success(blockOpt) => blockOpt match {
case Some(block) =>
blocksPool += (block.hash -> block)
theSender ! Some(block)
case None => theSender ! None
}
case Failure(_) => theSender ! None
}
case Failure(_) => theSender ! None
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ class BlockchainActor extends ActorSupport {
/**
* Handlers for each of the BlockchainMsg.
*/

private def onGetBlockchain(): Unit = sender() ! blockchainOpt

private def onCreateBlockchain(): Unit = blockchainOpt match {
Expand All @@ -97,9 +96,7 @@ class BlockchainActor extends ActorSupport {
}

private def onCheckBlockchainValidity(): Unit = blockchainOpt match {
case Some(blockchain) => sender() ! {
if (blockchain.isValid) SuccessMsg("true") else SuccessMsg("false")
}
case Some(blockchain) => sender() ! SuccessMsg(blockchain.isValid.toString)
case None =>
clearMappingOnNoBlockchain()
sender() ! FailureMsg("Blockchain does not exist.")
Expand All @@ -108,7 +105,6 @@ class BlockchainActor extends ActorSupport {
/**
* Handlers for each of the BlockMsg.
*/

private def onGetBlockByHash(hash: String): Unit = sender() ! getBlockByHash(hash)

private def onGetBlocksByHashesAndIndices(hashes: Set[String], indices: Set[Int]): Unit =
Expand Down Expand Up @@ -144,7 +140,7 @@ class BlockchainActor extends ActorSupport {
case Success(blockOpt) => blockOpt match {
case Some(block) =>
appendBlock(block, blockchain)
sender ! SuccessMsg(s"New Block ${block.hash} added on the chain.")
theSender ! SuccessMsg(s"New Block ${block.hash} added on the chain.")
case None => theSender ! FailureMsg(s"Did not find Block $hash in the poll.")
}
case Failure(e) =>
Expand Down Expand Up @@ -186,7 +182,9 @@ class BlockchainActor extends ActorSupport {
private def onGetBlockFromPool(hash: String): Unit =
blockPoolActor forward BlockPoolActor.GetBlock(hash)


/**
* Private helper methods.
*/
private def getBlockByHash(hash: String): Option[Block] = hashIndexMapping.get(hash) match {
case Some(index) => blockchainOpt match {
case Some(blockchain) => Some(blockchain.chain(index))
Expand All @@ -211,5 +209,4 @@ class BlockchainActor extends ActorSupport {
hashIndexMapping.clear()
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -52,37 +52,42 @@ class NetworkActor extends ActorSupport {
/**
* Handlers for each of the Messages.
*/

private def onGetNetwork(): Unit = sender() ! context.children.map(_.path.name).toSet

private def onGetPeers(): Unit = Future.sequence(context.children.map(p => {
(p ? GetPublicKeys).mapTo[Set[String]].map(keys => p.path.name -> keys)
})).map(_.toMap).pipeTo(sender())
private def onGetPeers(): Unit = {
val peers = context.children.map { p =>
(p ? GetPublicKeys).mapTo[Set[String]].map(keys => p.path.name -> keys)
}
Future.sequence(peers).map(_.toMap).pipeTo(sender())
}

private def onGetPeers(names: Set[String]): Unit = Future.sequence(context.children
.filter(p => names.contains(p.path.name))
.map(p => { (p ? GetPublicKeys).mapTo[Set[String]].map(keys => p.path.name -> keys) })
).map(_.toMap).pipeTo(sender())
private def onGetPeers(names: Set[String]): Unit = {
val peers = context.children
.filter { p => names.contains(p.path.name) }
.map { p => (p ? GetPublicKeys).mapTo[Set[String]].map(keys => p.path.name -> keys) }
Future.sequence(peers).map(_.toMap).pipeTo(sender())
}

private def onCreatePeer(name: String): Unit =
if (context.child(name).isDefined) sender() ! FailureMsg(s"Peer $name has been created.")
else {
private def onCreatePeer(name: String): Unit = context.child(name) match {
case Some(_) => sender() ! FailureMsg(s"Peer $name has been created.")
case None =>
val _ = context.actorOf(Props[PeerActor], name)
sender() ! SuccessMsg(s"Peer $name created.")
}
}

private def onGetPeer(name: String): Unit =
if (context.child(name).isDefined) {
(context.child(name).get ? GetPublicKeys)
.mapTo[Set[String]]
.map(keys => Some(Peer(name, keys)))
.pipeTo(sender())
} else sender() ! None
private def onGetPeer(name: String): Unit = context.child(name) match {
case Some(_) => (context.child(name).get ? GetPublicKeys)
.mapTo[Set[String]]
.map { keys => Some(Peer(name, keys)) }
.pipeTo(sender())
case None => sender() ! None
}

private def onDeletePeer(name: String): Unit =
if (context.child(name).isDefined) {
private def onDeletePeer(name: String): Unit = context.child(name) match {
case Some(_) =>
context stop context.child(name).get
sender() ! SuccessMsg(s"Peer $name deleted.")
} else sender() ! FailureMsg(s"Peer $name does not exist.")
case None => sender() ! FailureMsg(s"Peer $name does not exist.")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class PeerActor extends ActorSupport {
override def postStop(): Unit = log.info("{} stopped!", this.getClass.getSimpleName)

// TODO (Chang): need persistence
val wallet = mutable.Map.empty[String, KeyContainer]
val others = mutable.Map.empty[String, Peer]
val wallet: mutable.Map[String, KeyContainer] = mutable.Map.empty[String, KeyContainer]
val others: mutable.Map[String, Peer] = mutable.Map.empty[String, Peer]

/**
* TODO (Chang):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class TxPoolActor extends ActorSupport {
/**
* Handlers for each of the Messages.
*/

private def onGetTransactions(): Unit = sender() ! transPool.values.toSeq

private def onGetTransactions(ids: Seq[String]): Unit =
Expand Down

0 comments on commit eb9eda0

Please sign in to comment.