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 8e16726..ce03d09 100644 --- a/src/main/scala/com/fluency03/blockchain/api/actors/BlocksActor.scala +++ b/src/main/scala/com/fluency03/blockchain/api/actors/BlocksActor.scala @@ -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) @@ -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) @@ -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.") 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 6e36ac1..d3b1351 100644 --- a/src/main/scala/com/fluency03/blockchain/api/routes/BlockRoutes.scala +++ b/src/main/scala/com/fluency03/blockchain/api/routes/BlockRoutes.scala @@ -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._ @@ -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) } } ~ diff --git a/src/test/scala/com/fluency03/blockchain/api/actors/BlocksActorTest.scala b/src/test/scala/com/fluency03/blockchain/api/actors/BlocksActorTest.scala index 958751c..ee51ed5 100644 --- a/src/test/scala/com/fluency03/blockchain/api/actors/BlocksActorTest.scala +++ b/src/test/scala/com/fluency03/blockchain/api/actors/BlocksActorTest.scala @@ -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.")) @@ -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))