Skip to content

Commit

Permalink
rename Blocks -> BlockPool & Transactions -> TxPool
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed May 4, 2018
1 parent 9b71195 commit 7ae47de
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 81 deletions.
8 changes: 4 additions & 4 deletions src/main/scala/com/fluency03/blockchain/api/Server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import scala.concurrent.{Await, ExecutionContextExecutor, Future}
import scala.concurrent.duration.Duration

object Server extends App
with BlockchainRoutes with BlockRoutes with TransactionRoutes with NetworkRoutes with GenericRoutes {
with BlockchainRoutes with BlockPoolRoutes with TxPoolRoutes with NetworkRoutes with GenericRoutes {
// we leave these abstract, since they will be provided by the App
implicit val system: ActorSystem = ActorSystem("blockchain-http-service")
implicit val materializer: ActorMaterializer = ActorMaterializer()
Expand All @@ -28,11 +28,11 @@ object Server extends App
val (host, port) = (httpConfig.getString("host"), httpConfig.getInt("port"))

val blockchainActor: ActorRef = system.actorOf(BlockchainActor.props, BLOCKCHAIN_ACTOR_NAME)
val blocksActor: ActorRef = system.actorOf(BlocksActor.props, BLOCKS_ACTOR_NAME)
val transActor: ActorRef = system.actorOf(TransactionsActor.props, TRANS_ACTOR_NAME)
val blockPoolActor: ActorRef = system.actorOf(BlockPoolActor.props, BLOCK_POOL_ACTOR_NAME)
val txPoolActor: ActorRef = system.actorOf(TxPoolActor.props, TX_POOL_ACTOR_NAME)
val networkActor: ActorRef = system.actorOf(NetworkActor.props, NETWORK_ACTOR_NAME)

lazy val routes: Route = blockchainRoutes ~ blockRoutes ~ transRoutes ~ networkRoutes ~ genericRoutes
lazy val routes: Route = blockchainRoutes ~ blockPoolRoutes ~ txPoolRoutes ~ networkRoutes ~ genericRoutes

val bindingFuture: Future[ServerBinding] =
Http().bindAndHandle(routes, host, port)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
package com.fluency03.blockchain.api.actors

import akka.actor.{ActorSelection, Props}
import com.fluency03.blockchain.api.actors.BlocksActor._
import com.fluency03.blockchain.api.actors.BlockPoolActor._
import com.fluency03.blockchain.api._
import com.fluency03.blockchain.core.Block

import scala.collection.mutable

object BlocksActor {
object BlockPoolActor {
final case object GetBlocks
final case class GetBlocks(hashes: Set[String])
final case class CreateBlock(block: Block)
final case class GetBlock(hash: String)
final case class DeleteBlock(hash: String)

def props: Props = Props[BlocksActor]
def props: Props = Props[BlockPoolActor]
}

class BlocksActor extends ActorSupport {
class BlockPoolActor extends ActorSupport {
override def preStart(): Unit = log.info("{} started!", this.getClass.getSimpleName)
override def postStop(): Unit = log.info("{} stopped!", this.getClass.getSimpleName)

val blockchainActor: ActorSelection = context.actorSelection(PARENT_UP + BLOCKCHAIN_ACTOR_NAME)
val networkActor: ActorSelection = context.actorSelection(PARENT_UP + NETWORK_ACTOR_NAME)
val transActor: ActorSelection = context.actorSelection(PARENT_UP + TRANS_ACTOR_NAME)
val txPoolActor: ActorSelection = context.actorSelection(PARENT_UP + TX_POOL_ACTOR_NAME)

// TODO (Chang): need persistence
var blocksPool = mutable.Map.empty[String, Block]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class BlockchainActor extends ActorSupport {

import context.dispatcher

val blocksActor: ActorSelection = context.actorSelection(PARENT_UP + BLOCKS_ACTOR_NAME)
val transActor: ActorSelection = context.actorSelection(PARENT_UP + TRANS_ACTOR_NAME)
val blockPoolActor: ActorSelection = context.actorSelection(PARENT_UP + BLOCK_POOL_ACTOR_NAME)
val txPoolActor: ActorSelection = context.actorSelection(PARENT_UP + TX_POOL_ACTOR_NAME)
val networkActor: ActorSelection = context.actorSelection(PARENT_UP + NETWORK_ACTOR_NAME)

// TODO (Chang): need persistence
Expand Down Expand Up @@ -101,7 +101,7 @@ class BlockchainActor extends ActorSupport {

private def onAddBlockFromPool(hash: String): Unit = blockchainOpt match {
case Some(blockchain) =>
val maybeBlock: Future[Option[Block]] = (blocksActor ? BlocksActor.GetBlock(hash)).mapTo[Option[Block]]
val maybeBlock: Future[Option[Block]] = (blockPoolActor ? BlockPoolActor.GetBlock(hash)).mapTo[Option[Block]]
maybeBlock onComplete {
case Success(blockOpt) => blockOpt match {
case Some(block) =>
Expand Down Expand Up @@ -135,7 +135,7 @@ class BlockchainActor extends ActorSupport {
if (ids.isEmpty) sender() ! Some(blockchain.mineNextBlock(data, Seq.empty[Transaction]))
else {
val maybeTrans: Future[Seq[Transaction]] =
(transActor ? TransactionsActor.GetTransactions(ids.toSet)).mapTo[Seq[Transaction]]
(txPoolActor ? TxPoolActor.GetTransactions(ids.toSet)).mapTo[Seq[Transaction]]
maybeTrans onComplete {
case Success(trans) => sender() ! Some(blockchain.mineNextBlock(data, trans))
case Failure(_) => sender() ! None
Expand All @@ -157,7 +157,7 @@ class BlockchainActor extends ActorSupport {
sender() ! FailureMsg("Blockchain does not exist.")
}

private def onGetBlockFromPool(hash: String): Unit = blocksActor forward BlocksActor.GetBlock(hash)
private def onGetBlockFromPool(hash: String): Unit = blockPoolActor forward BlockPoolActor.GetBlock(hash)


private def getBlockFromChain(hash: String): Option[Block] = hashIndexMapping.get(hash) match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class NetworkActor extends ActorSupport {

import context.dispatcher

val blockActor: ActorSelection = context.actorSelection(PARENT_UP + BLOCKS_ACTOR_NAME)
val blockPoolActor: ActorSelection = context.actorSelection(PARENT_UP + BLOCK_POOL_ACTOR_NAME)
val blockchainActor: ActorSelection = context.actorSelection(PARENT_UP + BLOCKCHAIN_ACTOR_NAME)
val transActor: ActorSelection = context.actorSelection(PARENT_UP + TRANS_ACTOR_NAME)
val txPoolActor: ActorSelection = context.actorSelection(PARENT_UP + TX_POOL_ACTOR_NAME)

// TODO (Chang): need persistence

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package com.fluency03.blockchain.api.actors

import akka.actor.{ActorSelection, Props}
import com.fluency03.blockchain.api.actors.TransactionsActor._
import com.fluency03.blockchain.api.actors.TxPoolActor._
import com.fluency03.blockchain.api._
import com.fluency03.blockchain.core.{Outpoint, Transaction, TxOut}
import com.fluency03.blockchain.core.Transaction.hashOfTransaction

import scala.collection.mutable

object TransactionsActor {
object TxPoolActor {
final case object GetTransactions
final case class GetTransactions(ids: Set[String])
final case class CreateTransaction(tx: Transaction)
final case class GetTransaction(id: String)
final case class DeleteTransaction(id: String)
final case class UpdateTransaction(tx: Transaction)
def props: Props = Props[TransactionsActor]
def props: Props = Props[TxPoolActor]
}

class TransactionsActor extends ActorSupport {
class TxPoolActor extends ActorSupport {
override def preStart(): Unit = log.info("{} started!", this.getClass.getSimpleName)
override def postStop(): Unit = log.info("{} stopped!", this.getClass.getSimpleName)

Expand All @@ -27,7 +27,7 @@ class TransactionsActor extends ActorSupport {
val uTxOs: mutable.Map[Outpoint, TxOut] = mutable.Map.empty[Outpoint, TxOut]

val blockchainActor: ActorSelection = context.actorSelection(PARENT_UP + BLOCKCHAIN_ACTOR_NAME)
val blockActor: ActorSelection = context.actorSelection(PARENT_UP + BLOCKS_ACTOR_NAME)
val blockPoolActor: ActorSelection = context.actorSelection(PARENT_UP + BLOCK_POOL_ACTOR_NAME)
val networkActor: ActorSelection = context.actorSelection(PARENT_UP + NETWORK_ACTOR_NAME)

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/com/fluency03/blockchain/api/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ package object api {
type Transactions = Seq[Transaction]
type Peers = Seq[Peer]

val BLOCKS_ACTOR_NAME = "blocksActor"
val BLOCK_POOL_ACTOR_NAME = "blocksPoolActor"
val BLOCKCHAIN_ACTOR_NAME = "blockchainActor"
val NETWORK_ACTOR_NAME = "networkActor"
val PEER_ACTOR_NAME = "peerActor"
val TRANS_ACTOR_NAME = "transActor"
val TX_POOL_ACTOR_NAME = "txPoolActor"

val PARENT_UP = "../"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import akka.http.scaladsl.server.directives.RouteDirectives.complete
import akka.http.scaladsl.unmarshalling.PredefinedFromStringUnmarshallers.CsvSeq
import akka.pattern.ask
import com.fluency03.blockchain.api.{Blocks, Message}
import com.fluency03.blockchain.api.actors.BlocksActor._
import com.fluency03.blockchain.api.actors.BlockPoolActor._
import com.fluency03.blockchain.core.Block

import scala.concurrent.Future

trait BlockRoutes extends RoutesSupport {
lazy val log = Logging(system, classOf[BlockRoutes])
trait BlockPoolRoutes extends RoutesSupport {
lazy val log = Logging(system, classOf[BlockPoolRoutes])

def blocksActor: ActorRef
def blockPoolActor: ActorRef

/**
* TODO (Chang): new APIS:
Expand All @@ -30,12 +30,12 @@ trait BlockRoutes extends RoutesSupport {
*
*/

lazy val blockRoutes: Route =
lazy val blockPoolRoutes: Route =
path(BLOCKS) {
parameters( 'hashes.as(CsvSeq[String]).? ) { hashesOpt =>
val blocks: Future[Blocks] = hashesOpt match {
case Some(hashes) => (blocksActor ? GetBlocks(hashes.toSet)).mapTo[Blocks]
case None => (blocksActor ? GetBlocks).mapTo[Blocks]
case Some(hashes) => (blockPoolActor ? GetBlocks(hashes.toSet)).mapTo[Blocks]
case None => (blockPoolActor ? GetBlocks).mapTo[Blocks]
}
complete(blocks)
}
Expand All @@ -44,18 +44,18 @@ trait BlockRoutes extends RoutesSupport {
pathEnd {
post {
entity(as[Block]) { block =>
val blockCreated: Future[Message] = (blocksActor ? CreateBlock(block)).mapTo[Message]
val blockCreated: Future[Message] = (blockPoolActor ? CreateBlock(block)).mapTo[Message]
onSuccess(blockCreated) { respondOnCreation }
}
}
} ~
path(Segment) { hash =>
get {
val maybeBlock: Future[Option[Block]] = (blocksActor ? GetBlock(hash)).mapTo[Option[Block]]
val maybeBlock: Future[Option[Block]] = (blockPoolActor ? GetBlock(hash)).mapTo[Option[Block]]
rejectEmptyResponse { complete(maybeBlock) }
} ~
delete {
val blockDeleted: Future[Message] = (blocksActor ? DeleteBlock(hash)).mapTo[Message]
val blockDeleted: Future[Message] = (blockPoolActor ? DeleteBlock(hash)).mapTo[Message]
onSuccess(blockDeleted) { respondOnDeletion }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import akka.http.scaladsl.server.directives.PathDirectives.path
import akka.http.scaladsl.server.directives.RouteDirectives.complete
import akka.http.scaladsl.unmarshalling.PredefinedFromStringUnmarshallers.CsvSeq
import akka.pattern.ask
import com.fluency03.blockchain.api.actors.TransactionsActor._
import com.fluency03.blockchain.api.actors.TxPoolActor._
import com.fluency03.blockchain.api.{FailureMsg, Message, Transactions}
import com.fluency03.blockchain.core.Transaction

import scala.concurrent.Future

trait TransactionRoutes extends RoutesSupport {
lazy val log = Logging(system, classOf[TransactionRoutes])
trait TxPoolRoutes extends RoutesSupport {
lazy val log = Logging(system, classOf[TxPoolRoutes])

def transActor: ActorRef
def txPoolActor: ActorRef

/**
* TODO (Chang):
Expand All @@ -28,12 +28,12 @@ trait TransactionRoutes extends RoutesSupport {
*
*/

lazy val transRoutes: Route =
lazy val txPoolRoutes: Route =
path(TRANSACTIONS) {
parameters( 'ids.as(CsvSeq[String]).? ) { idsOpt =>
val transactions: Future[Transactions] = idsOpt match {
case Some(ids) => (transActor ? GetTransactions(ids.toSet)).mapTo[Transactions]
case None => (transActor ? GetTransactions).mapTo[Transactions]
case Some(ids) => (txPoolActor ? GetTransactions(ids.toSet)).mapTo[Transactions]
case None => (txPoolActor ? GetTransactions).mapTo[Transactions]
}
complete(transactions)
}
Expand All @@ -42,26 +42,26 @@ trait TransactionRoutes extends RoutesSupport {
pathEnd {
post {
entity(as[Transaction]) { tx =>
val msgOnCreate: Future[Message] = (transActor ? CreateTransaction(tx)).mapTo[Message]
val msgOnCreate: Future[Message] = (txPoolActor ? CreateTransaction(tx)).mapTo[Message]
onSuccess(msgOnCreate) { respondOnCreation }
}
}
} ~
path(Segment) { id =>
get {
val maybeTx: Future[Option[Transaction]] = (transActor ? GetTransaction(id)).mapTo[Option[Transaction]]
val maybeTx: Future[Option[Transaction]] = (txPoolActor ? GetTransaction(id)).mapTo[Option[Transaction]]
rejectEmptyResponse { complete(maybeTx) }
} ~
delete {
val txDeleted: Future[Message] = (transActor ? DeleteTransaction(id)).mapTo[Message]
val txDeleted: Future[Message] = (txPoolActor ? DeleteTransaction(id)).mapTo[Message]
onSuccess(txDeleted) { respondOnDeletion }
} ~
put {
entity(as[Transaction]) { tx =>
if (tx.id != id)
complete((StatusCodes.InternalServerError, FailureMsg("Transaction ID in the data does not match ID on the path.")))
else {
val msgOnUpdate: Future[Message] = (transActor ? UpdateTransaction(tx)).mapTo[Message]
val msgOnUpdate: Future[Message] = (txPoolActor ? UpdateTransaction(tx)).mapTo[Message]
onSuccess(msgOnUpdate) { respondOnUpdate }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,58 @@ package com.fluency03.blockchain.api.actors

import akka.actor.{ActorRef, ActorSystem, Props}
import akka.testkit.{ImplicitSender, TestKit}
import com.fluency03.blockchain.api.actors.BlocksActor._
import com.fluency03.blockchain.api.actors.BlockPoolActor._
import com.fluency03.blockchain.api.{FailureMsg, SuccessMsg}
import com.fluency03.blockchain.core.Block
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike}

class BlocksActorTest extends TestKit(ActorSystem("BlocksActorTest")) with ImplicitSender
class BlockPoolActorTest extends TestKit(ActorSystem("BlocksActorTest")) with ImplicitSender
with WordSpecLike with Matchers with BeforeAndAfterAll {

override def afterAll: Unit = {
shutdown()
}

val blocksActor: ActorRef = system.actorOf(Props[BlocksActor])
val blockPoolActor: ActorRef = system.actorOf(Props[BlockPoolActor])

"A BlocksActor" should {
"Respond with a Seq of Blocks." in {
BlocksActor.props shouldEqual Props[BlocksActor]
BlockPoolActor.props shouldEqual Props[BlockPoolActor]

blocksActor ! GetBlocks
blockPoolActor ! GetBlocks
expectMsg(Seq.empty[Block])

blocksActor ! GetBlocks(Set("somehash"))
blockPoolActor ! GetBlocks(Set("somehash"))
expectMsg(Seq.empty[Block])

blocksActor ! CreateBlock(Block.genesisBlock)
blockPoolActor ! CreateBlock(Block.genesisBlock)
expectMsg(SuccessMsg(s"Block ${Block.genesisBlock.hash} created."))

blocksActor ! CreateBlock(Block.genesisBlock)
blockPoolActor ! CreateBlock(Block.genesisBlock)
expectMsg(FailureMsg(s"Block ${Block.genesisBlock.hash} already exists."))

blocksActor ! GetBlocks
blockPoolActor ! GetBlocks
expectMsg(Seq(Block.genesisBlock))

blocksActor ! GetBlocks(Set("somehash"))
blockPoolActor ! GetBlocks(Set("somehash"))
expectMsg(Seq.empty[Block])

blocksActor ! GetBlocks(Set(Block.genesisBlock.hash))
blockPoolActor ! GetBlocks(Set(Block.genesisBlock.hash))
expectMsg(Seq(Block.genesisBlock))

blocksActor ! GetBlock(Block.genesisBlock.hash)
blockPoolActor ! GetBlock(Block.genesisBlock.hash)
expectMsg(Some(Block.genesisBlock))

blocksActor ! DeleteBlock(Block.genesisBlock.hash)
blockPoolActor ! DeleteBlock(Block.genesisBlock.hash)
expectMsg(SuccessMsg(s"Block ${Block.genesisBlock.hash} deleted."))

blocksActor ! DeleteBlock(Block.genesisBlock.hash)
blockPoolActor ! DeleteBlock(Block.genesisBlock.hash)
expectMsg(FailureMsg(s"Block ${Block.genesisBlock.hash} does not exist."))

blocksActor ! GetBlock(Block.genesisBlock.hash)
blockPoolActor ! GetBlock(Block.genesisBlock.hash)
expectMsg(None)

blocksActor ! "other"
blockPoolActor ! "other"
expectNoMessage
}
}
Expand Down
Loading

0 comments on commit 7ae47de

Please sign in to comment.