diff --git a/src/main/scala/com/fluency03/blockchain/api/actors/BlocksActor.scala b/src/main/scala/com/fluency03/blockchain/api/actors/BlocksActor.scala index ce03d09..a10b252 100644 --- a/src/main/scala/com/fluency03/blockchain/api/actors/BlocksActor.scala +++ b/src/main/scala/com/fluency03/blockchain/api/actors/BlocksActor.scala @@ -26,7 +26,7 @@ class BlocksActor extends ActorSupport { val transActor: ActorSelection = context.actorSelection(PARENT_UP + TRANS_ACTOR_NAME) // TODO (Chang): need persistence - var blocks = mutable.Map.empty[String, Block] + var blocksPool = mutable.Map.empty[String, Block] def receive: Receive = { case GetBlocks => onGetBlocks() @@ -46,24 +46,25 @@ class BlocksActor extends ActorSupport { * */ - private[this] def onGetBlocks(): Unit = sender() ! blocks.values.toSeq - private[this] def onGetBlocks(hashes: Set[String]): Unit = sender() ! blocks.filterKeys( + private[this] def onGetBlocks(): Unit = sender() ! blocksPool.values.toSeq + + private[this] def onGetBlocks(hashes: Set[String]): Unit = sender() ! blocksPool.filterKeys( k => hashes.contains(k) ).values.toSeq private[this] def onCreateBlock(block: Block): Unit = { - if (blocks.contains(block.hash)) sender() ! FailureMsg(s"Block ${block.hash} already exists.") + if (blocksPool.contains(block.hash)) sender() ! FailureMsg(s"Block ${block.hash} already exists.") else { - blocks += (block.hash -> block) + blocksPool += (block.hash -> block) sender() ! SuccessMsg(s"Block ${block.hash} created.") } } - private[this] def onGetBlock(hash: String): Unit = sender() ! blocks.get(hash) + private[this] def onGetBlock(hash: String): Unit = sender() ! blocksPool.get(hash) private[this] def onDeleteBlock(hash: String): Unit = { - if (blocks.contains(hash)) { - blocks -= hash + if (blocksPool.contains(hash)) { + blocksPool -= hash sender() ! SuccessMsg(s"Block $hash deleted.") } else sender() ! FailureMsg(s"Block $hash does not exist.") } diff --git a/src/main/scala/com/fluency03/blockchain/api/actors/NetworkActor.scala b/src/main/scala/com/fluency03/blockchain/api/actors/NetworkActor.scala index bd953f1..c953beb 100644 --- a/src/main/scala/com/fluency03/blockchain/api/actors/NetworkActor.scala +++ b/src/main/scala/com/fluency03/blockchain/api/actors/NetworkActor.scala @@ -32,6 +32,13 @@ class NetworkActor extends ActorSupport { // TODO (Chang): need persistence + /** + * TODO (Chang): + * - Remove wallet + * - Sign transaction + * + */ + def receive: Receive = { case GetNetwork => onGetNetwork() case GetPeers => onGetPeers() diff --git a/src/main/scala/com/fluency03/blockchain/api/actors/PeerActor.scala b/src/main/scala/com/fluency03/blockchain/api/actors/PeerActor.scala index bcde51b..92b1d2d 100644 --- a/src/main/scala/com/fluency03/blockchain/api/actors/PeerActor.scala +++ b/src/main/scala/com/fluency03/blockchain/api/actors/PeerActor.scala @@ -27,6 +27,13 @@ class PeerActor extends ActorSupport { val wallets = mutable.Map.empty[String, Wallet] val others = mutable.Map.empty[String, Peer] + /** + * TODO (Chang): + * - Remove wallet + * - Sign transactions + * + */ + def receive: Receive = { case GetPublicKeys => sender() ! wallets.values.map(_.address).toSet case CreateWallet => sender() ! addWallet() diff --git a/src/main/scala/com/fluency03/blockchain/api/actors/TransactionsActor.scala b/src/main/scala/com/fluency03/blockchain/api/actors/TransactionsActor.scala index b644aa8..1fc0699 100644 --- a/src/main/scala/com/fluency03/blockchain/api/actors/TransactionsActor.scala +++ b/src/main/scala/com/fluency03/blockchain/api/actors/TransactionsActor.scala @@ -22,13 +22,20 @@ class TransactionsActor extends ActorSupport { override def postStop(): Unit = log.info("{} stopped!", this.getClass.getSimpleName) // TODO (Chang): need persistence - val currentTransactions: mutable.Map[String, Transaction] = mutable.Map.empty[String, Transaction] + val transPool: mutable.Map[String, Transaction] = mutable.Map.empty[String, Transaction] 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 networkActor: ActorSelection = context.actorSelection(PARENT_UP + NETWORK_ACTOR_NAME) + /** + * TODO (Chang): + * - Update transaction + * - Sign transaction + * + */ + def receive: Receive = { case GetTransactions => onGetTransactions() case GetTransactions(ids) => onGetTransactions(ids) @@ -38,25 +45,25 @@ class TransactionsActor extends ActorSupport { case _ => unhandled _ } - private def onGetTransactions(): Unit = sender() ! currentTransactions.values.toSeq + private def onGetTransactions(): Unit = sender() ! transPool.values.toSeq - private def onGetTransactions(ids: Set[String]): Unit = sender() ! currentTransactions.filterKeys( + private def onGetTransactions(ids: Set[String]): Unit = sender() ! transPool.filterKeys( k => ids.contains(k) ).values.toSeq private def onCreateTransaction(tx: Transaction): Unit = { - if (currentTransactions.contains(tx.id)) sender() ! FailureMsg(s"Transaction ${tx.id} already exists.") + if (transPool.contains(tx.id)) sender() ! FailureMsg(s"Transaction ${tx.id} already exists.") else { - currentTransactions += (tx.id -> tx) + transPool += (tx.id -> tx) sender() ! SuccessMsg(s"Transaction ${tx.id} created.") } } - private def onGetTransaction(id: String): Unit = sender() ! currentTransactions.get(id) + private def onGetTransaction(id: String): Unit = sender() ! transPool.get(id) private def onDeleteTransaction(id: String): Unit = - if (currentTransactions contains id) { - currentTransactions -= id + if (transPool contains id) { + transPool -= id sender() ! SuccessMsg(s"Transaction $id deleted.") } else sender() ! FailureMsg(s"Transaction $id does not exist.") diff --git a/src/main/scala/com/fluency03/blockchain/api/routes/NetworkRoutes.scala b/src/main/scala/com/fluency03/blockchain/api/routes/NetworkRoutes.scala index 12f4a3e..d4f301a 100644 --- a/src/main/scala/com/fluency03/blockchain/api/routes/NetworkRoutes.scala +++ b/src/main/scala/com/fluency03/blockchain/api/routes/NetworkRoutes.scala @@ -20,6 +20,12 @@ trait NetworkRoutes extends RoutesSupport { def networkActor: ActorRef + /** + * TODO (Chang): + * - Sign transaction + * + */ + lazy val networkRoutes: Route = path(NETWORK) { get { diff --git a/src/main/scala/com/fluency03/blockchain/api/routes/TransactionRoutes.scala b/src/main/scala/com/fluency03/blockchain/api/routes/TransactionRoutes.scala index ae24724..4f63ace 100644 --- a/src/main/scala/com/fluency03/blockchain/api/routes/TransactionRoutes.scala +++ b/src/main/scala/com/fluency03/blockchain/api/routes/TransactionRoutes.scala @@ -20,6 +20,13 @@ trait TransactionRoutes extends RoutesSupport { def transActor: ActorRef + /** + * TODO (Chang): + * - Update transaction + * - Sign transaction + * + */ + lazy val transRoutes: Route = path(TRANSACTIONS) { parameters( 'ids.as(CsvSeq[String]) ? ) { ids =>