From 772fddd21bf663ad8074ac57556308158bdb31de Mon Sep 17 00:00:00 2001 From: fluency03 Date: Sat, 5 May 2018 17:03:01 +0200 Subject: [PATCH] refactor messages of Blockchian --- .../api/actors/BlockPoolActor.scala | 4 ++ .../api/actors/BlockchainActor.scala | 44 ++++++++++++------- .../blockchain/api/actors/NetworkActor.scala | 4 ++ .../blockchain/api/actors/TxPoolActor.scala | 4 ++ .../api/routes/BlockchainRoutes.scala | 5 --- 5 files changed, 39 insertions(+), 22 deletions(-) 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 cf408d9..b182ede 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 @@ -46,6 +46,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( 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 0fb08fb..5db332e 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 @@ -11,18 +11,22 @@ import scala.concurrent.Future import scala.util.{Failure, Success} object BlockchainActor { - final case object GetBlockchain - final case object CreateBlockchain - final case object DeleteBlockchain - final case class GetBlockFromChain(hash: String) - final case object GetLastBlock - final case class GetTxOfBlock(id: String, hash: String) - final case class AppendBlock(block: Block) - final case class AppendBlockFromPool(hash: String) - final case object RemoveLastBlock - final case class MineNextBlock(data: String, ids: Seq[String]) - final case object CheckBlockchainValidity - final case class GetBlockFromPool(hash: String) + sealed trait BlockchainMsg + sealed trait BlockMsg + + final case object GetBlockchain extends BlockchainMsg + final case object CreateBlockchain extends BlockchainMsg + final case object DeleteBlockchain extends BlockchainMsg + final case object CheckBlockchainValidity extends BlockchainMsg + + final case class GetBlockFromChain(hash: String) extends BlockMsg + final case object GetLastBlock extends BlockMsg + final case class GetTxOfBlock(id: String, hash: String) extends BlockMsg + final case class AppendBlock(block: Block) extends BlockMsg + final case class AppendBlockFromPool(hash: String) extends BlockMsg + final case object RemoveLastBlock extends BlockMsg + final case class MineNextBlock(data: String, ids: Seq[String]) extends BlockMsg + final case class GetBlockFromPool(hash: String) extends BlockMsg def props: Props = Props[BlockchainActor] } @@ -42,9 +46,19 @@ class BlockchainActor extends ActorSupport { val hashIndexMapping = mutable.Map.empty[String, Int] def receive: Receive = { + case msg: BlockchainMsg => inCaseOfBlockchainMsg(msg) + case msg: BlockMsg => inCaseOfBlockMsg(msg) + case _ => unhandled _ + } + + private def inCaseOfBlockchainMsg(msg: BlockchainMsg): Unit = msg match { case GetBlockchain => onGetBlockchain() case CreateBlockchain => onCreateBlockchain() case DeleteBlockchain => onDeleteBlockchain() + case CheckBlockchainValidity => onCheckBlockchainValidity() + } + + private def inCaseOfBlockMsg(msg: BlockMsg): Unit = msg match { case GetBlockFromChain(hash) => onGetBlockFromChain(hash) case GetLastBlock => onGetLastBlock() case GetTxOfBlock(id, hash) => onGetTxOfBlock(id, hash) @@ -52,15 +66,11 @@ class BlockchainActor extends ActorSupport { case AppendBlockFromPool(hash) => onAppendBlockFromPool(hash) case RemoveLastBlock => onRemoveLastBlock() case MineNextBlock(data, ids) => onMineNextBlock(data, ids) - case CheckBlockchainValidity => onCheckBlockchainValidity() case GetBlockFromPool(hash) => onGetBlockFromPool(hash) - case _ => unhandled _ } /** - * TODO (Chang): - * - GetLastBlock - * + * Handlers for each of the Messages. */ private def onGetBlockchain(): Unit = sender() ! blockchainOpt 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 47983c7..d5dccec 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 @@ -49,6 +49,10 @@ class NetworkActor extends ActorSupport { case _ => unhandled _ } + /** + * 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 => { 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 cc6e91a..8d8401a 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 @@ -46,6 +46,10 @@ class TxPoolActor extends ActorSupport { case _ => unhandled _ } + /** + * Handlers for each of the Messages. + */ + private def onGetTransactions(): Unit = sender() ! transPool.values.toSeq private def onGetTransactions(ids: Seq[String]): Unit = diff --git a/src/main/scala/com/github/fluency03/blockchain/api/routes/BlockchainRoutes.scala b/src/main/scala/com/github/fluency03/blockchain/api/routes/BlockchainRoutes.scala index 4eb1cf9..116e4ec 100644 --- a/src/main/scala/com/github/fluency03/blockchain/api/routes/BlockchainRoutes.scala +++ b/src/main/scala/com/github/fluency03/blockchain/api/routes/BlockchainRoutes.scala @@ -20,11 +20,6 @@ trait BlockchainRoutes extends RoutesSupport { def blockchainActor: ActorRef - /** - * TODO (Chang): - * - */ - lazy val blockchainRoutes: Route = pathPrefix(BLOCKCHAIN) { path(VALIDITY) {