diff --git a/src/main/scala/com/github/fluency03/blockchain/api/actors/BlockPoolActor.scala b/src/main/scala/com/github/fluency03/blockchain/api/actors/BlockPoolActor.scala index 0776c48..8944a44 100644 --- a/src/main/scala/com/github/fluency03/blockchain/api/actors/BlockPoolActor.scala +++ b/src/main/scala/com/github/fluency03/blockchain/api/actors/BlockPoolActor.scala @@ -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 { @@ -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() @@ -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)) @@ -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 - } } diff --git a/src/main/scala/com/github/fluency03/blockchain/api/actors/BlockchainActor.scala b/src/main/scala/com/github/fluency03/blockchain/api/actors/BlockchainActor.scala index 4bd9c3b..e4f0af6 100644 --- a/src/main/scala/com/github/fluency03/blockchain/api/actors/BlockchainActor.scala +++ b/src/main/scala/com/github/fluency03/blockchain/api/actors/BlockchainActor.scala @@ -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 { @@ -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.") @@ -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 = @@ -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) => @@ -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)) @@ -211,5 +209,4 @@ class BlockchainActor extends ActorSupport { hashIndexMapping.clear() } - } diff --git a/src/main/scala/com/github/fluency03/blockchain/api/actors/NetworkActor.scala b/src/main/scala/com/github/fluency03/blockchain/api/actors/NetworkActor.scala index d5dccec..3fd3a19 100644 --- a/src/main/scala/com/github/fluency03/blockchain/api/actors/NetworkActor.scala +++ b/src/main/scala/com/github/fluency03/blockchain/api/actors/NetworkActor.scala @@ -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.") + } } diff --git a/src/main/scala/com/github/fluency03/blockchain/api/actors/PeerActor.scala b/src/main/scala/com/github/fluency03/blockchain/api/actors/PeerActor.scala index 8a691a5..817d0a7 100644 --- a/src/main/scala/com/github/fluency03/blockchain/api/actors/PeerActor.scala +++ b/src/main/scala/com/github/fluency03/blockchain/api/actors/PeerActor.scala @@ -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): diff --git a/src/main/scala/com/github/fluency03/blockchain/api/actors/TxPoolActor.scala b/src/main/scala/com/github/fluency03/blockchain/api/actors/TxPoolActor.scala index d019212..86aa3be 100644 --- a/src/main/scala/com/github/fluency03/blockchain/api/actors/TxPoolActor.scala +++ b/src/main/scala/com/github/fluency03/blockchain/api/actors/TxPoolActor.scala @@ -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 =