From da560dc59acbc16696ab140e7cdf8ac345d9b96f Mon Sep 17 00:00:00 2001 From: fluency03 Date: Sat, 12 May 2018 18:16:53 +0200 Subject: [PATCH] style fix for Routes --- .../blockchain/api/actors/TxPoolActor.scala | 1 - .../api/routes/BlockPoolRoutes.scala | 27 ++++--- .../api/routes/BlockchainRoutes.scala | 70 +++++++++++-------- .../blockchain/api/routes/GenericRoutes.scala | 50 +++++++------ .../blockchain/api/routes/NetworkRoutes.scala | 21 +++--- .../blockchain/api/routes/RoutesSupport.scala | 3 +- .../blockchain/api/routes/TxPoolRoutes.scala | 26 ++++--- 7 files changed, 118 insertions(+), 80 deletions(-) diff --git a/src/main/scala/com/github/fluency03/blockchain/api/actors/TxPoolActor.scala b/src/main/scala/com/github/fluency03/blockchain/api/actors/TxPoolActor.scala index ee57ffc..9442171 100644 --- a/src/main/scala/com/github/fluency03/blockchain/api/actors/TxPoolActor.scala +++ b/src/main/scala/com/github/fluency03/blockchain/api/actors/TxPoolActor.scala @@ -85,5 +85,4 @@ class TxPoolActor extends ActorSupport { } } - } diff --git a/src/main/scala/com/github/fluency03/blockchain/api/routes/BlockPoolRoutes.scala b/src/main/scala/com/github/fluency03/blockchain/api/routes/BlockPoolRoutes.scala index c4fe582..e2ee671 100644 --- a/src/main/scala/com/github/fluency03/blockchain/api/routes/BlockPoolRoutes.scala +++ b/src/main/scala/com/github/fluency03/blockchain/api/routes/BlockPoolRoutes.scala @@ -34,9 +34,12 @@ trait BlockPoolRoutes extends RoutesSupport { post { parameters('ids.as(CsvSeq[String]).?) { idsOpt: Option[Seq[String]] => entity(as[Input]) { in => + val action = MineAndAddNextBlock(in.content, idsOpt.getOrElse(Seq.empty[String])) val maybeNextBlock: Future[Option[Block]] = - (blockPoolActor ? MineAndAddNextBlock(in.content, idsOpt.getOrElse(Seq.empty[String]))).mapTo[Option[Block]] - rejectEmptyResponse { complete(maybeNextBlock) } + (blockPoolActor ? action).mapTo[Option[Block]] + rejectEmptyResponse { + complete(maybeNextBlock) + } } } } @@ -45,26 +48,30 @@ trait BlockPoolRoutes extends RoutesSupport { pathEnd { post { entity(as[Block]) { block => - val blockCreated: Future[Message] = (blockPoolActor ? AddBlock(block)).mapTo[Message] - onSuccess(blockCreated) { respondOnCreation } + onSuccess((blockPoolActor ? AddBlock(block)).mapTo[Message]) { + respondOnCreation + } } } } ~ pathPrefix(Segment) { hash => pathEnd { get { - val maybeBlock: Future[Option[Block]] = (blockPoolActor ? GetBlock(hash)).mapTo[Option[Block]] - rejectEmptyResponse { complete(maybeBlock) } + rejectEmptyResponse { + complete((blockPoolActor ? GetBlock(hash)).mapTo[Option[Block]]) + } } ~ delete { - val blockDeleted: Future[Message] = (blockPoolActor ? DeleteBlock(hash)).mapTo[Message] - onSuccess(blockDeleted) { respondOnDeletion } + onSuccess((blockPoolActor ? DeleteBlock(hash)).mapTo[Message]) { + respondOnDeletion + } } } ~ path(TRANSACTION / Segment) { id => get { - val maybeTx: Future[Option[Transaction]] = (blockPoolActor ? GetTxOfBlock(id, hash)).mapTo[Option[Transaction]] - rejectEmptyResponse { complete(maybeTx) } + rejectEmptyResponse { + complete((blockPoolActor ? GetTxOfBlock(id, hash)).mapTo[Option[Transaction]]) + } } } } diff --git a/src/main/scala/com/github/fluency03/blockchain/api/routes/BlockchainRoutes.scala b/src/main/scala/com/github/fluency03/blockchain/api/routes/BlockchainRoutes.scala index 60109f5..2b12db4 100644 --- a/src/main/scala/com/github/fluency03/blockchain/api/routes/BlockchainRoutes.scala +++ b/src/main/scala/com/github/fluency03/blockchain/api/routes/BlockchainRoutes.scala @@ -23,28 +23,33 @@ trait BlockchainRoutes extends RoutesSupport { pathPrefix(BLOCKCHAIN) { path(VALIDITY) { get { - val validity: Future[Message] = (blockchainActor ? CheckBlockchainValidity).mapTo[Message] - onSuccess(validity) { respondOnUpdate } + onSuccess((blockchainActor ? CheckBlockchainValidity).mapTo[Message]) { + respondOnUpdate + } } } ~ path(LAST_BLOCK) { get { - val maybeLastBlock: Future[Option[Block]] = (blockchainActor ? GetLastBlock).mapTo[Option[Block]] - rejectEmptyResponse { complete(maybeLastBlock) } + rejectEmptyResponse { + complete((blockchainActor ? GetLastBlock).mapTo[Option[Block]]) + } } ~ delete { - val blockchainUpdated: Future[Message] = (blockchainActor ? RemoveLastBlock).mapTo[Message] - onSuccess(blockchainUpdated) { respondOnUpdate } + onSuccess((blockchainActor ? RemoveLastBlock).mapTo[Message]) { + respondOnUpdate + } } } ~ path(NEW_BLOCK) { parameters('hash.?) { case Some(hash) => - val blockchainUpdated: Future[Message] = (blockchainActor ? AppendBlockFromPool(hash)).mapTo[Message] - onSuccess(blockchainUpdated) { respondOnUpdate } + onSuccess((blockchainActor ? AppendBlockFromPool(hash)).mapTo[Message]) { + respondOnUpdate + } case None => entity(as[Block]) { block => - val blockchainUpdated: Future[Message] = (blockchainActor ? AppendBlock(block)).mapTo[Message] - onSuccess(blockchainUpdated) { respondOnUpdate } + onSuccess((blockchainActor ? AppendBlock(block)).mapTo[Message]) { + respondOnUpdate + } } } } ~ @@ -53,50 +58,59 @@ trait BlockchainRoutes extends RoutesSupport { parameters('id.as(CsvSeq[String]).?) { idsOpt: Option[Seq[String]] => entity(as[Input]) { in => val maybeNextBlock: Future[Option[Block]] = - (blockchainActor ? MineNextBlock(in.content, idsOpt.getOrElse(Seq.empty[String]))).mapTo[Option[Block]] - rejectEmptyResponse { complete(maybeNextBlock) } + (blockchainActor ? MineNextBlock(in.content, idsOpt.getOrElse(Seq.empty[String]))) + .mapTo[Option[Block]] + rejectEmptyResponse { + complete(maybeNextBlock) + } } } } } ~ pathEnd { get { - val blockchain: Future[Option[Blockchain]] = (blockchainActor ? GetBlockchain).mapTo[Option[Blockchain]] - rejectEmptyResponse { complete(blockchain) } + rejectEmptyResponse { + complete((blockchainActor ? GetBlockchain).mapTo[Option[Blockchain]]) + } } ~ post { - val blockchainCreated: Future[Message] = (blockchainActor ? CreateBlockchain).mapTo[Message] - onSuccess(blockchainCreated) { respondOnCreation } + onSuccess((blockchainActor ? CreateBlockchain).mapTo[Message]) { + respondOnCreation + } } ~ delete { - val blockchainDeleted: Future[Message] = (blockchainActor ? DeleteBlockchain).mapTo[Message] - onSuccess(blockchainDeleted) { respondOnDeletion } + onSuccess((blockchainActor ? DeleteBlockchain).mapTo[Message]) { + respondOnDeletion + } } } ~ path(BLOCKS) { get { parameters('hashes.as(CsvSeq[String]).?, 'indices.as(CsvSeq[Int]).?) { (hashesOpt: Option[Seq[String]], indicesOpt: Option[Seq[Int]]) => - val blocks: Future[Set[Block]] = (blockchainActor ? - GetBlocksByHashesAndIndices( - hashesOpt.getOrElse(Seq.empty[String]).toSet, - indicesOpt.getOrElse(Seq.empty[Int]).toSet)) - .mapTo[Set[Block]] - rejectEmptyResponse { complete(blocks) } + val (hashes, indices) = (hashesOpt.getOrElse(Seq.empty[String]).toSet, + indicesOpt.getOrElse(Seq.empty[Int]).toSet) + val blocks: Future[Set[Block]] = + (blockchainActor ? GetBlocksByHashesAndIndices(hashes, indices)).mapTo[Set[Block]] + rejectEmptyResponse { + complete(blocks) + } } } } ~ pathPrefix(BLOCK / Segment) { hash => pathEnd { get { - val maybeBlock: Future[Option[Block]] = (blockchainActor ? GetBlockByHash(hash)).mapTo[Option[Block]] - rejectEmptyResponse { complete(maybeBlock) } + rejectEmptyResponse { + complete((blockchainActor ? GetBlockByHash(hash)).mapTo[Option[Block]]) + } } } ~ path(TRANSACTION / Segment) { id => get { - val maybeTx: Future[Option[Transaction]] = (blockchainActor ? GetTxOfBlock(id, hash)).mapTo[Option[Transaction]] - rejectEmptyResponse { complete(maybeTx) } + rejectEmptyResponse { + complete((blockchainActor ? GetTxOfBlock(id, hash)).mapTo[Option[Transaction]]) + } } } } diff --git a/src/main/scala/com/github/fluency03/blockchain/api/routes/GenericRoutes.scala b/src/main/scala/com/github/fluency03/blockchain/api/routes/GenericRoutes.scala index 7b83604..dfea470 100644 --- a/src/main/scala/com/github/fluency03/blockchain/api/routes/GenericRoutes.scala +++ b/src/main/scala/com/github/fluency03/blockchain/api/routes/GenericRoutes.scala @@ -22,29 +22,39 @@ trait GenericRoutes extends RoutesSupport { pathPrefix(GENERIC) { path(TO_SHA256) { post { - entity(as[Input]) { in => complete( failsafeResp { in.content.toSha256 } ) } + entity(as[Input]) { in => + complete(failsafeResp(in.content.toSha256)) + } } } ~ - path(TO_BASE64) { - post { - entity(as[Input]) { in => complete( failsafeResp { in.content.toBase64 } ) } - } - } ~ - path(FROM_BASE64) { - post { - entity(as[Input]) { in => complete( failsafeResp { fromBase64(in.content) } ) } - } - } ~ - path(TO_EPOCH_TIME) { - post { - entity(as[Input]) { in => complete( failsafeResp { epochTimeOf(in.content).toString } ) } - } - } ~ - path(TIME_FROM_EPOCH) { - post { - entity(as[Input]) { in => complete( failsafeResp { Instant.ofEpochSecond(in.content.toLong).toString } ) } + path(TO_BASE64) { + post { + entity(as[Input]) { in => + complete(failsafeResp(in.content.toBase64)) + } + } + } ~ + path(FROM_BASE64) { + post { + entity(as[Input]) { in => + complete(failsafeResp(fromBase64(in.content))) + } + } + } ~ + path(TO_EPOCH_TIME) { + post { + entity(as[Input]) { in => + complete(failsafeResp(epochTimeOf(in.content).toString)) + } + } + } ~ + path(TIME_FROM_EPOCH) { + post { + entity(as[Input]) { in => + complete(failsafeResp(Instant.ofEpochSecond(in.content.toLong).toString)) + } + } } - } } } diff --git a/src/main/scala/com/github/fluency03/blockchain/api/routes/NetworkRoutes.scala b/src/main/scala/com/github/fluency03/blockchain/api/routes/NetworkRoutes.scala index 2cacceb..7f2dfd3 100644 --- a/src/main/scala/com/github/fluency03/blockchain/api/routes/NetworkRoutes.scala +++ b/src/main/scala/com/github/fluency03/blockchain/api/routes/NetworkRoutes.scala @@ -29,15 +29,15 @@ trait NetworkRoutes extends RoutesSupport { lazy val networkRoutes: Route = path(NETWORK) { get { - val network: Future[Set[String]] = (networkActor ? GetNetwork).mapTo[Set[String]] - complete(network) + complete((networkActor ? GetNetwork).mapTo[Set[String]]) } } ~ path(PEERS) { get { parameters( 'names.as(CsvSeq[String]).? ) { namesOpt => val peers: Future[Map[String, Set[String]]] = namesOpt match { - case Some(names) => (networkActor ? GetPeers(names.toSet)).mapTo[Map[String, Set[String]]] + case Some(names) => + (networkActor ? GetPeers(names.toSet)).mapTo[Map[String, Set[String]]] case None => (networkActor ? GetPeers).mapTo[Map[String, Set[String]]] } complete(peers) @@ -48,19 +48,22 @@ trait NetworkRoutes extends RoutesSupport { pathEnd { post { entity(as[PeerSimple]) { peer => - val peerCreated: Future[Message] = (networkActor ? CreatePeer(peer.name)).mapTo[Message] - onSuccess(peerCreated) { respondOnCreation } + onSuccess((networkActor ? CreatePeer(peer.name)).mapTo[Message]) { + respondOnCreation + } } } } ~ path(Segment) { name => get { - val maybePeer: Future[Option[Peer]] = (networkActor ? GetPeer(name)).mapTo[Option[Peer]] - rejectEmptyResponse { complete(maybePeer) } + rejectEmptyResponse { + complete((networkActor ? GetPeer(name)).mapTo[Option[Peer]]) + } } ~ delete { - val peerDeleted: Future[Message] = (networkActor ? DeletePeer(name)).mapTo[Message] - onSuccess(peerDeleted) { respondOnDeletion } + onSuccess((networkActor ? DeletePeer(name)).mapTo[Message]) { + respondOnDeletion + } } } } diff --git a/src/main/scala/com/github/fluency03/blockchain/api/routes/RoutesSupport.scala b/src/main/scala/com/github/fluency03/blockchain/api/routes/RoutesSupport.scala index 1257809..fe66ddf 100644 --- a/src/main/scala/com/github/fluency03/blockchain/api/routes/RoutesSupport.scala +++ b/src/main/scala/com/github/fluency03/blockchain/api/routes/RoutesSupport.scala @@ -15,7 +15,8 @@ trait RoutesSupport extends JsonSupport { implicit def system: ActorSystem // Required by the `ask` (?) method - implicit lazy val timeout: Timeout = Timeout(5.seconds) // usually we'd obtain the timeout from the system's configuration + // usually we'd obtain the timeout from the system's configuration + implicit lazy val timeout: Timeout = Timeout(5.seconds) def respondOnCreation(m: Message): StandardRoute = m match { case s: SuccessMsg => complete((StatusCodes.Created, s)) diff --git a/src/main/scala/com/github/fluency03/blockchain/api/routes/TxPoolRoutes.scala b/src/main/scala/com/github/fluency03/blockchain/api/routes/TxPoolRoutes.scala index 8f7d001..68aa302 100644 --- a/src/main/scala/com/github/fluency03/blockchain/api/routes/TxPoolRoutes.scala +++ b/src/main/scala/com/github/fluency03/blockchain/api/routes/TxPoolRoutes.scala @@ -42,27 +42,31 @@ trait TxPoolRoutes extends RoutesSupport { pathEnd { post { entity(as[Transaction]) { tx => - val msgOnCreate: Future[Message] = (txPoolActor ? AddTransaction(tx)).mapTo[Message] - onSuccess(msgOnCreate) { respondOnCreation } + onSuccess((txPoolActor ? AddTransaction(tx)).mapTo[Message]) { + respondOnCreation + } } } } ~ path(Segment) { id => get { - val maybeTx: Future[Option[Transaction]] = (txPoolActor ? GetTransaction(id)).mapTo[Option[Transaction]] - rejectEmptyResponse { complete(maybeTx) } + rejectEmptyResponse { + complete((txPoolActor ? GetTransaction(id)).mapTo[Option[Transaction]]) + } } ~ delete { - val txDeleted: Future[Message] = (txPoolActor ? DeleteTransaction(id)).mapTo[Message] - onSuccess(txDeleted) { respondOnDeletion } + onSuccess((txPoolActor ? DeleteTransaction(id)).mapTo[Message]) { + respondOnDeletion + } } ~ put { entity(as[Transaction]) { tx => - if (tx.id != id) - complete((StatusCodes.InternalServerError, FailureMsg("Transaction ID in the data does not match ID on the path."))) - else { - val msgOnUpdate: Future[Message] = (txPoolActor ? UpdateTransaction(tx)).mapTo[Message] - onSuccess(msgOnUpdate) { respondOnUpdate } + if (tx.id != id) { + val code = StatusCodes.InternalServerError + val msg = FailureMsg("Transaction ID in the data does not match ID on the path.") + complete((code, msg)) + } else onSuccess((txPoolActor ? UpdateTransaction(tx)).mapTo[Message]) { + respondOnUpdate } } }