Skip to content

Commit

Permalink
refactor messages of Blockchian
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed May 5, 2018
1 parent 8dfb20b commit 772fddd
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand All @@ -42,25 +46,31 @@ 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)
case AppendBlock(block) => onAppendBlock(block)
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ trait BlockchainRoutes extends RoutesSupport {

def blockchainActor: ActorRef

/**
* TODO (Chang):
*
*/

lazy val blockchainRoutes: Route =
pathPrefix(BLOCKCHAIN) {
path(VALIDITY) {
Expand Down

0 comments on commit 772fddd

Please sign in to comment.