Skip to content

Commit

Permalink
add some todos and onGetNetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed Apr 29, 2018
1 parent 2e3883b commit 983993f
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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



}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -55,5 +64,4 @@ class BlocksActor extends ActorSupport {
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -29,14 +32,19 @@ 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)
case DeletePeer(name) => onDeletePeer(name)
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.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package object routes {
val BLOCK = "block"

// network
val NETWORK = "network"
val PEERS = "peers"
val PEER = "peer"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,28 @@ 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."))

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
Expand Down

0 comments on commit 983993f

Please sign in to comment.