From 983993ff8da3adff2bf45fd875c3a83cf178ce0c Mon Sep 17 00:00:00 2001 From: fluency03 Date: Sun, 29 Apr 2018 14:45:00 +0200 Subject: [PATCH] add some todos and onGetNetwork --- .../api/actors/BlockchainActor.scala | 13 ++++++++++--- .../blockchain/api/actors/BlocksActor.scala | 10 +++++++++- .../blockchain/api/actors/NetworkActor.scala | 10 +++++++++- .../blockchain/api/routes/BlockRoutes.scala | 9 +++++++++ .../api/routes/BlockchainRoutes.scala | 11 +++++++++++ .../blockchain/api/routes/NetworkRoutes.scala | 18 ++++++++++++------ .../blockchain/api/routes/package.scala | 1 + .../api/actors/NetworkActorTest.scala | 13 +++++++++++-- 8 files changed, 72 insertions(+), 13 deletions(-) diff --git a/src/main/scala/com/fluency03/blockchain/api/actors/BlockchainActor.scala b/src/main/scala/com/fluency03/blockchain/api/actors/BlockchainActor.scala index 2c0f293..e0620df 100644 --- a/src/main/scala/com/fluency03/blockchain/api/actors/BlockchainActor.scala +++ b/src/main/scala/com/fluency03/blockchain/api/actors/BlockchainActor.scala @@ -31,6 +31,16 @@ class BlockchainActor extends ActorSupport { case _ => unhandled _ } + /** + * TODO (Chang): new APIS: + * - AddBlockOnBlockchain + * - GetBlockFromBlockchain + * - CheckBlockchainIsValid + * - GetTransactionOfABlock + * - MineNextBlock + * + */ + private def onGetBlockchain(): Unit = sender() ! blockchainOpt private def onCreateBlockchain(): Unit = @@ -46,8 +56,5 @@ class BlockchainActor extends ActorSupport { sender() ! SuccessMsg(s"Blockchain deleted.") } else sender() ! FailureMsg(s"Blockchain does not exist.") - // TODO (Chang): APIs for adding new Block on the chain - - } 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 d03a204..8e16726 100644 --- a/src/main/scala/com/fluency03/blockchain/api/actors/BlocksActor.scala +++ b/src/main/scala/com/fluency03/blockchain/api/actors/BlocksActor.scala @@ -35,6 +35,15 @@ class BlocksActor extends ActorSupport { case _ => unhandled _ } + /** + * TODO (Chang): new APIS: + * - CreateBlock + * - GetBlock (onChain or offChain) + * - GetTransactionOfABlock + * - AddBlockOnChain + * + */ + private[this] def onGetBlocks(): Unit = sender() ! blocks.values.toSeq private[this] def onCreateBlock(block: Block): Unit = { @@ -55,5 +64,4 @@ class BlocksActor extends ActorSupport { } - } 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 3b0b838..41b73fe 100644 --- a/src/main/scala/com/fluency03/blockchain/api/actors/NetworkActor.scala +++ b/src/main/scala/com/fluency03/blockchain/api/actors/NetworkActor.scala @@ -7,7 +7,10 @@ import com.fluency03.blockchain.api.actors.NetworkActor._ import com.fluency03.blockchain.api.actors.PeerActor.GetPublicKeys import com.fluency03.blockchain.core.Peer +import scala.concurrent.Future + object NetworkActor { + final case object GetNetwork final case object GetPeers final case class CreatePeer(name: String) final case class GetPeer(name: String) @@ -29,6 +32,7 @@ class NetworkActor extends ActorSupport { // TODO (Chang): need persistence def receive: Receive = { + case GetNetwork => onGetNetwork() case GetPeers => onGetPeers() case CreatePeer(name) => onCreatePeer(name) case GetPeer(name) => onGetPeer(name) @@ -36,7 +40,11 @@ class NetworkActor extends ActorSupport { case _ => unhandled _ } - private def onGetPeers(): Unit = sender() ! context.children.map(_.path.name).toSet + private def onGetNetwork(): Unit = sender() ! context.children.map(_.path.name).toSet + + private def onGetPeers(): Unit = Future.sequence(context.children.map(p => { + (p ? GetPublicKeys).mapTo[Set[String]].map(keys => p.path.name -> keys) + })).map(_.toMap).pipeTo(sender()) private def onCreatePeer(name: String): Unit = if (context.child(name).isDefined) sender() ! FailureMsg(s"Peer $name has been created.") diff --git a/src/main/scala/com/fluency03/blockchain/api/routes/BlockRoutes.scala b/src/main/scala/com/fluency03/blockchain/api/routes/BlockRoutes.scala index 172f9d8..53e1c24 100644 --- a/src/main/scala/com/fluency03/blockchain/api/routes/BlockRoutes.scala +++ b/src/main/scala/com/fluency03/blockchain/api/routes/BlockRoutes.scala @@ -20,6 +20,15 @@ trait BlockRoutes extends RoutesSupport { def blocksActor: ActorRef + /** + * TODO (Chang): new APIS: + * - CreateBlock + * - GetBlock (onChain or offChain) + * - GetTransactionOfABlock + * - AddBlockOnChain + * + */ + lazy val blockRoutes: Route = path(BLOCKS) { get { diff --git a/src/main/scala/com/fluency03/blockchain/api/routes/BlockchainRoutes.scala b/src/main/scala/com/fluency03/blockchain/api/routes/BlockchainRoutes.scala index fb0ef81..816a1b8 100644 --- a/src/main/scala/com/fluency03/blockchain/api/routes/BlockchainRoutes.scala +++ b/src/main/scala/com/fluency03/blockchain/api/routes/BlockchainRoutes.scala @@ -18,6 +18,17 @@ trait BlockchainRoutes extends RoutesSupport { def blockchainActor: ActorRef + /** + * TODO (Chang): new APIS: + * - AddBlockOnBlockchain + * - GetBlockFromBlockchain + * - CheckBlockchainIsValid + * - GetTransactionOfABlock + * - MineNextBlock + * + * + */ + lazy val blockchainRoutes: Route = pathPrefix(BLOCKCHAIN) { pathEnd { 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 64991df..619e680 100644 --- a/src/main/scala/com/fluency03/blockchain/api/routes/NetworkRoutes.scala +++ b/src/main/scala/com/fluency03/blockchain/api/routes/NetworkRoutes.scala @@ -9,7 +9,7 @@ import akka.http.scaladsl.server.directives.MethodDirectives.{delete, get, post} import akka.http.scaladsl.server.directives.PathDirectives.path import akka.http.scaladsl.server.directives.RouteDirectives.complete import akka.pattern.ask -import com.fluency03.blockchain.api.actors.NetworkActor.{CreatePeer, DeletePeer, GetPeer, GetPeers} +import com.fluency03.blockchain.api.actors.NetworkActor._ import com.fluency03.blockchain.api.{FailureMsg, Message, SuccessMsg} import com.fluency03.blockchain.core.{Peer, PeerSimple} @@ -21,9 +21,15 @@ trait NetworkRoutes extends RoutesSupport { def networkActor: ActorRef lazy val networkRoutes: Route = + path(NETWORK) { + get { + val network: Future[Set[String]] = (networkActor ? GetNetwork).mapTo[Set[String]] + complete(network) + } + } ~ path(PEERS) { get { - val peers: Future[Set[String]] = (networkActor ? GetPeers).mapTo[Set[String]] + val peers: Future[Map[String, Set[String]]] = (networkActor ? GetPeers).mapTo[Map[String, Set[String]]] complete(peers) } } ~ @@ -41,10 +47,10 @@ trait NetworkRoutes extends RoutesSupport { val maybePeer: Future[Option[Peer]] = (networkActor ? GetPeer(name)).mapTo[Option[Peer]] rejectEmptyResponse { complete(maybePeer) } } ~ - delete { - val peerDeleted: Future[Message] = (networkActor ? DeletePeer(name)).mapTo[Message] - onSuccess(peerDeleted) { respondOnDeletion } - } + delete { + val peerDeleted: Future[Message] = (networkActor ? DeletePeer(name)).mapTo[Message] + onSuccess(peerDeleted) { respondOnDeletion } + } } } diff --git a/src/main/scala/com/fluency03/blockchain/api/routes/package.scala b/src/main/scala/com/fluency03/blockchain/api/routes/package.scala index 29d2cbd..8990774 100644 --- a/src/main/scala/com/fluency03/blockchain/api/routes/package.scala +++ b/src/main/scala/com/fluency03/blockchain/api/routes/package.scala @@ -21,6 +21,7 @@ package object routes { val BLOCK = "block" // network + val NETWORK = "network" val PEERS = "peers" val PEER = "peer" diff --git a/src/test/scala/com/fluency03/blockchain/api/actors/NetworkActorTest.scala b/src/test/scala/com/fluency03/blockchain/api/actors/NetworkActorTest.scala index 7b4116a..be65646 100644 --- a/src/test/scala/com/fluency03/blockchain/api/actors/NetworkActorTest.scala +++ b/src/test/scala/com/fluency03/blockchain/api/actors/NetworkActorTest.scala @@ -20,9 +20,12 @@ class NetworkActorTest extends TestKit(ActorSystem("NetworkActorTest")) with Imp "Respond with a Set of Peers." in { NetworkActor.props shouldEqual Props[NetworkActor] - networkActor ! GetPeers + networkActor ! GetNetwork expectMsg(Set.empty[String]) + networkActor ! GetPeers + expectMsg(Map.empty[String, Set[String]]) + val name = "peer" networkActor ! CreatePeer(name) expectMsg(SuccessMsg(s"Peer $name created.")) @@ -30,9 +33,15 @@ class NetworkActorTest extends TestKit(ActorSystem("NetworkActorTest")) with Imp networkActor ! CreatePeer(name) expectMsg(FailureMsg(s"Peer $name has been created.")) - networkActor ! GetPeers + networkActor ! GetNetwork expectMsg(Set(name)) + networkActor ! GetPeers + val peers = expectMsgType[Map[String, Set[String]]] + peers.size shouldEqual 1 + peers.contains(name) shouldEqual true + peers(name).size shouldEqual 1 + networkActor ! GetPeer(name) val peerOpt = expectMsgType[Some[Peer]] peerOpt.isDefined shouldEqual true