Skip to content

Commit

Permalink
style fix for BlockchainActor
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed May 12, 2018
1 parent 019d5f3 commit de9baba
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
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, Blockchain, Transaction}

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

object BlockchainActor {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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.")
}

Expand All @@ -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
}

Expand All @@ -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.")
}

Expand All @@ -167,38 +162,36 @@ 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.")
}

private def onMineNextBlock(data: String, ids: Seq[String]): Unit = blockchainOpt match {
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
Expand All @@ -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()
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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")
}
Expand Down

0 comments on commit de9baba

Please sign in to comment.