Skip to content

Commit

Permalink
add GetBlocks based on hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed Apr 29, 2018
1 parent 12f47c5 commit e4db873
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import scala.collection.mutable

object BlocksActor {
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)
Expand All @@ -29,6 +30,7 @@ class BlocksActor extends ActorSupport {

def receive: Receive = {
case GetBlocks => onGetBlocks()
case GetBlocks(hashes) => onGetBlocks(hashes)
case CreateBlock(block) => onCreateBlock(block)
case GetBlock(hash) => onGetBlock(hash)
case DeleteBlock(hash) => onDeleteBlock(hash)
Expand All @@ -45,6 +47,9 @@ class BlocksActor extends ActorSupport {
*/

private[this] def onGetBlocks(): Unit = sender() ! blocks.values.toSeq
private[this] def onGetBlocks(hashes: Set[String]): Unit = sender() ! blocks.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.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import akka.http.scaladsl.server.Route
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.http.scaladsl.unmarshalling.PredefinedFromStringUnmarshallers.CsvSeq
import akka.pattern.ask
import com.fluency03.blockchain.api.{Blocks, Message}
import com.fluency03.blockchain.api.actors.BlocksActor._
Expand All @@ -30,8 +31,10 @@ trait BlockRoutes extends RoutesSupport {

lazy val blockRoutes: Route =
path(BLOCKS) {
get {
val blocks: Future[Blocks] = (blocksActor ? GetBlocks).mapTo[Blocks]
parameters( 'hashes.as(CsvSeq[String]) ? ) { hashes =>
val blocks: Future[Blocks] =
if (hashes.isDefined) (blocksActor ? GetBlocks(hashes.get.toSet)).mapTo[Blocks]
else (blocksActor ? GetBlocks).mapTo[Blocks]
complete(blocks)
}
} ~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class BlocksActorTest extends TestKit(ActorSystem("BlocksActorTest")) with Impli
blocksActor ! GetBlocks
expectMsg(Seq.empty[Block])

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

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

Expand All @@ -32,6 +35,12 @@ class BlocksActorTest extends TestKit(ActorSystem("BlocksActorTest")) with Impli
blocksActor ! GetBlocks
expectMsg(Seq(Block.genesisBlock))

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

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

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

Expand Down

0 comments on commit e4db873

Please sign in to comment.