From de9baba1faefa95bb0af734e95d81f6c6784190a Mon Sep 17 00:00:00 2001 From: fluency03 Date: Sat, 12 May 2018 16:58:48 +0200 Subject: [PATCH] style fix for BlockchainActor --- .../blockchain/api/actors/ActorSupport.scala | 3 +- .../api/actors/BlockPoolActor.scala | 3 +- .../api/actors/BlockchainActor.scala | 105 +++++++++--------- .../blockchain/api/actors/TxPoolActor.scala | 9 +- 4 files changed, 64 insertions(+), 56 deletions(-) diff --git a/src/main/scala/com/github/fluency03/blockchain/api/actors/ActorSupport.scala b/src/main/scala/com/github/fluency03/blockchain/api/actors/ActorSupport.scala index aee94a0..2ff23b1 100644 --- a/src/main/scala/com/github/fluency03/blockchain/api/actors/ActorSupport.scala +++ b/src/main/scala/com/github/fluency03/blockchain/api/actors/ActorSupport.scala @@ -8,6 +8,7 @@ import scala.concurrent.duration._ trait ActorSupport extends Actor with ActorLogging { // Required by the `ask` (?) method - implicit lazy val timeout: Timeout = Timeout(5.seconds) // usually we'd obtain the timeout from the system's configuration + // usually we'd obtain the timeout from the system's configuration + implicit lazy val timeout: Timeout = Timeout(5.seconds) } 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 0a132eb..0776c48 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 @@ -57,7 +57,8 @@ class BlockPoolActor extends ActorSupport { ).values.toSeq private[this] def onAddBlock(block: Block): Unit = { - if (blocksPool.contains(block.hash)) sender() ! FailureMsg(s"Block ${block.hash} already exists in the Pool.") + if (blocksPool.contains(block.hash)) + sender() ! FailureMsg(s"Block ${block.hash} already exists in the Pool.") else { blocksPool += (block.hash -> block) sender() ! SuccessMsg(s"Block ${block.hash} created in the Pool.") 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 0cb0b0a..4bd9c3b 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 @@ -7,7 +7,6 @@ import com.github.fluency03.blockchain.api._ import com.github.fluency03.blockchain.core.{Block, Blockchain, Transaction} import scala.collection.mutable -import scala.concurrent.Future import scala.util.{Failure, Success} object BlockchainActor { @@ -44,7 +43,7 @@ class BlockchainActor extends ActorSupport { // TODO (Chang): need persistence var blockchainOpt: Option[Blockchain] = None - val hashIndexMapping = mutable.Map.empty[String, Int] + val hashIndexMapping: mutable.Map[String, Int] = mutable.Map.empty[String, Int] def receive: Receive = { case msg: BlockchainMsg => inCaseOfBlockchainMsg(msg) @@ -61,7 +60,8 @@ class BlockchainActor extends ActorSupport { private def inCaseOfBlockMsg(msg: BlockMsg): Unit = msg match { case GetBlockByHash(hash) => onGetBlockByHash(hash) - case GetBlocksByHashesAndIndices(hashes, indices) => onGetBlocksByHashesAndIndices(hashes, indices) + case GetBlocksByHashesAndIndices(hashes, indices) => + onGetBlocksByHashesAndIndices(hashes, indices) case GetLastBlock => onGetLastBlock() case GetTxOfBlock(id, hash) => onGetTxOfBlock(id, hash) case AppendBlock(block) => onAppendBlock(block) @@ -77,32 +77,31 @@ class BlockchainActor extends ActorSupport { private def onGetBlockchain(): Unit = sender() ! blockchainOpt - private def onCreateBlockchain(): Unit = - if (blockchainOpt.isDefined) sender() ! FailureMsg("Blockchain already exists.") - else { + private def onCreateBlockchain(): Unit = blockchainOpt match { + case Some(_) => sender() ! FailureMsg("Blockchain already exists.") + case None => + if (hashIndexMapping.nonEmpty) clearMappingOnNoBlockchain() blockchainOpt = Some(Blockchain()) - if (hashIndexMapping.nonEmpty) { - log.error("Hash-to-index mapping is not empty when Blockchain is created! Clear it!") - hashIndexMapping.clear() + blockchainOpt.get.chain.zipWithIndex.foreach { case (b, i) => + hashIndexMapping += (b.hash -> i) } - blockchainOpt.get.chain.zipWithIndex.foreach { case (b, i) => hashIndexMapping += (b.hash -> i) } sender() ! SuccessMsg(s"Blockchain created, with difficulty ${blockchainOpt.get.difficulty}.") - } + } - private def onDeleteBlockchain(): Unit = - if (blockchainOpt.isDefined) { + private def onDeleteBlockchain(): Unit = blockchainOpt match { + case Some(_) => blockchainOpt = None hashIndexMapping.clear() sender() ! SuccessMsg("Blockchain deleted.") - } else sender() ! FailureMsg("Blockchain does not exist.") + case None => sender() ! FailureMsg("Blockchain does not exist.") + } private def onCheckBlockchainValidity(): Unit = blockchainOpt match { - case Some(blockchain) => - if (blockchain.isValid) sender() ! SuccessMsg("true") - else sender() ! SuccessMsg("false") + case Some(blockchain) => sender() ! { + if (blockchain.isValid) SuccessMsg("true") else SuccessMsg("false") + } case None => - log.error("Blockchain does not exist! Clear the hash-to-index mapping!") - hashIndexMapping.clear() + clearMappingOnNoBlockchain() sender() ! FailureMsg("Blockchain does not exist.") } @@ -118,8 +117,7 @@ class BlockchainActor extends ActorSupport { private def onGetLastBlock(): Unit = blockchainOpt match { case Some(blockchain) => sender() ! blockchain.lastBlock() case None => - log.error("Blockchain does not exist! Clear the hash-to-index mapping!") - hashIndexMapping.clear() + clearMappingOnNoBlockchain() sender() ! None } @@ -130,33 +128,30 @@ class BlockchainActor extends ActorSupport { private def onAppendBlock(block: Block): Unit = blockchainOpt match { case Some(blockchain) => - blockchainOpt = Some(blockchain.addBlock(block)) - hashIndexMapping += (block.hash -> blockchain.length) + appendBlock(block, blockchain) sender() ! SuccessMsg(s"New Block ${block.hash} added on the chain.") case None => - log.error("Blockchain does not exist! Clear the hash-to-index mapping!") - hashIndexMapping.clear() + clearMappingOnNoBlockchain() sender() ! FailureMsg("Blockchain does not exist.") } private def onAppendBlockFromPool(hash: String): Unit = blockchainOpt match { case Some(blockchain) => - val maybeBlock: Future[Option[Block]] = (blockPoolActor ? BlockPoolActor.GetBlock(hash)).mapTo[Option[Block]] val theSender: ActorRef = sender() - maybeBlock onComplete { - case Success(blockOpt) => blockOpt match { - case Some(block) => - log.error(theSender.toString()) - blockchainOpt = Some(blockchain.addBlock(block)) - hashIndexMapping += (block.hash -> blockchain.length) - theSender ! SuccessMsg(s"New Block ${block.hash} added on the chain.") - case None => theSender ! FailureMsg(s"Did not find Block $hash in the poll.") + (blockPoolActor ? BlockPoolActor.GetBlock(hash)) + .mapTo[Option[Block]] + .onComplete { + case Success(blockOpt) => blockOpt match { + case Some(block) => + appendBlock(block, blockchain) + sender ! 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) => + theSender ! FailureMsg(s"Cannot get Block $hash from the poll: ${e.getMessage}") } - case Failure(e) => theSender ! FailureMsg(s"Cannot get Block $hash from the poll: ${e.getMessage}") - } case None => - log.error("Blockchain does not exist! Clear the hash-to-index mapping!") - hashIndexMapping.clear() + clearMappingOnNoBlockchain() sender() ! FailureMsg("Blockchain does not exist.") } @@ -167,8 +162,7 @@ class BlockchainActor extends ActorSupport { hashIndexMapping -= toBeRemoved.hash sender() ! SuccessMsg(s"Last Block ${toBeRemoved.hash} removed from the chain.") case None => - log.error("Blockchain does not exist! Clear the hash-to-index mapping!") - hashIndexMapping.clear() + clearMappingOnNoBlockchain() sender() ! FailureMsg("Blockchain does not exist.") } @@ -176,29 +170,28 @@ class BlockchainActor extends ActorSupport { case Some(blockchain) => if (ids.isEmpty) sender() ! Some(blockchain.mineNextBlock(data, Seq.empty[Transaction])) else { - val maybeTrans: Future[Seq[Transaction]] = - (txPoolActor ? TxPoolActor.GetTransactions(ids)).mapTo[Seq[Transaction]] val theSender: ActorRef = sender() - maybeTrans onComplete { - case Success(trans) => theSender ! Some(blockchain.mineNextBlock(data, trans)) - case Failure(_) => theSender ! None - } + (txPoolActor ? TxPoolActor.GetTransactions(ids)) + .mapTo[Seq[Transaction]] + .onComplete { + case Success(trans) => theSender ! Some(blockchain.mineNextBlock(data, trans)) + case Failure(_) => theSender ! None + } } case None => - log.error("Blockchain does not exist! Clear the hash-to-index mapping!") - hashIndexMapping.clear() + clearMappingOnNoBlockchain() sender() ! None } - private def onGetBlockFromPool(hash: String): Unit = blockPoolActor forward BlockPoolActor.GetBlock(hash) + private def onGetBlockFromPool(hash: String): Unit = + blockPoolActor forward BlockPoolActor.GetBlock(hash) private def getBlockByHash(hash: String): Option[Block] = hashIndexMapping.get(hash) match { case Some(index) => blockchainOpt match { case Some(blockchain) => Some(blockchain.chain(index)) case None => - log.error("Blockchain does not exist! Clear the hash-to-index mapping!") - hashIndexMapping.clear() + clearMappingOnNoBlockchain() None } case None => None @@ -208,5 +201,15 @@ class BlockchainActor extends ActorSupport { ??? } + private def appendBlock(block: Block, blockchain: Blockchain): Unit = { + blockchainOpt = Some(blockchain.addBlock(block)) + hashIndexMapping += (block.hash -> blockchain.length) + } + + private def clearMappingOnNoBlockchain(): Unit = { + log.error("Blockchain does not exist! Clear the hash-to-index mapping!") + hashIndexMapping.clear() + } + } 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 8d8401a..d019212 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 @@ -56,7 +56,8 @@ class TxPoolActor extends ActorSupport { sender() ! ids.map(id => transPool.get(id)).filter(_.isDefined).map(_.get) private def onAddTransaction(tx: Transaction): Unit = - if (transPool.contains(tx.id)) sender() ! FailureMsg(s"Transaction ${tx.id} already exists in the Pool.") + if (transPool.contains(tx.id)) + sender() ! FailureMsg(s"Transaction ${tx.id} already exists in the Pool.") else { transPool += (tx.id -> tx) sender() ! SuccessMsg(s"Transaction ${tx.id} created in the Pool.") @@ -77,8 +78,10 @@ class TxPoolActor extends ActorSupport { val notExistBefore = !transPool.contains(actualId) transPool += (actualId -> tx) sender() ! { - if (notExistBefore) SuccessMsg(s"Transaction $actualId does not exist. New transaction created in the Pool.") - else SuccessMsg(s"Transaction $actualId updated in the Pool.") + if (notExistBefore) + SuccessMsg(s"Transaction $actualId does not exist. New transaction created in the Pool.") + else + SuccessMsg(s"Transaction $actualId updated in the Pool.") } } else sender() ! FailureMsg(s"Transaction does not have valid ID. Should be: $expectedId; actually is: $actualId") }