From b735f366388626d6a2381aa11171dfba13b9bf73 Mon Sep 17 00:00:00 2001 From: Ivan Mashonskii Date: Fri, 18 Mar 2022 14:25:34 +0300 Subject: [PATCH] NODE-2447 Return 404 for block request at invalid height (#3651) Co-authored-by: Ivan Mashonskii --- .../com/wavesplatform/api/http/BlocksApiRoute.scala | 9 +++++++-- .../com/wavesplatform/http/BlocksApiRouteSpec.scala | 13 +++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/node/src/main/scala/com/wavesplatform/api/http/BlocksApiRoute.scala b/node/src/main/scala/com/wavesplatform/api/http/BlocksApiRoute.scala index e87bc88a663..c097d84ad1f 100644 --- a/node/src/main/scala/com/wavesplatform/api/http/BlocksApiRoute.scala +++ b/node/src/main/scala/com/wavesplatform/api/http/BlocksApiRoute.scala @@ -81,8 +81,13 @@ case class BlocksApiRoute(settings: RestAPISettings, commonApi: CommonBlocksApi, } } - private def at(height: Int, includeTransactions: Boolean): StandardRoute = complete { - if (includeTransactions) commonApi.blockAtHeight(height).map(toJson) else commonApi.metaAtHeight(height).map(_.json()) + private def at(height: Int, includeTransactions: Boolean): StandardRoute = { + val result = if (includeTransactions) + commonApi.blockAtHeight(height).map(toJson) + else + commonApi.metaAtHeight(height).map(_.json()) + + complete(result.toRight(BlockDoesNotExist)) } private def seq(start: Int, end: Int, includeTransactions: Boolean): Route = { diff --git a/node/src/test/scala/com/wavesplatform/http/BlocksApiRouteSpec.scala b/node/src/test/scala/com/wavesplatform/http/BlocksApiRouteSpec.scala index 8a0a9022d1e..0349daf4319 100644 --- a/node/src/test/scala/com/wavesplatform/http/BlocksApiRouteSpec.scala +++ b/node/src/test/scala/com/wavesplatform/http/BlocksApiRouteSpec.scala @@ -1,5 +1,6 @@ package com.wavesplatform.http +import akka.http.scaladsl.model.StatusCodes import com.wavesplatform.TestWallet import com.wavesplatform.api.BlockMeta import com.wavesplatform.api.common.CommonBlocksApi @@ -83,6 +84,12 @@ class BlocksApiRouteSpec val response = responseAs[JsObject] response shouldBe testBlock2Json } + + (blocksApi.blockAtHeight _).expects(3).returning(None).once() + Get(routePath("/at/3")) ~> route ~> check { + response.status shouldBe StatusCodes.NotFound + responseAs[String] should include("block does not exist") + } } routePath("/signature/{signature}") in { @@ -175,6 +182,7 @@ class BlocksApiRouteSpec routePath("/headers/at/{height}") in { (blocksApi.metaAtHeight _).expects(1).returning(Some(testBlock1Meta)).once() (blocksApi.metaAtHeight _).expects(2).returning(Some(testBlock2Meta)).once() + (blocksApi.metaAtHeight _).expects(3).returning(None).once() Get(routePath("/headers/at/1")) ~> route ~> check { val response = responseAs[JsObject] @@ -185,6 +193,11 @@ class BlocksApiRouteSpec val response = responseAs[JsObject] response shouldBe testBlock2HeaderJson } + + Get(routePath("/headers/at/3")) ~> route ~> check { + response.status shouldBe StatusCodes.NotFound + responseAs[String] should include("block does not exist") + } } routePath("/headers/seq/{from}/{to}") in {