From 55ea71f960354bd8c3bbe1fad5bacaeb90b9dddc Mon Sep 17 00:00:00 2001 From: Alexey Koloskov Date: Thu, 23 Mar 2017 15:51:49 +0300 Subject: [PATCH 1/2] Matcher returns redirect on inverse asset pairs --- .../matcher/api/MatcherApiRoute.scala | 12 ++++++++---- .../matcher/market/MatcherActor.scala | 15 +++++++++++---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main/scala/com/wavesplatform/matcher/api/MatcherApiRoute.scala b/src/main/scala/com/wavesplatform/matcher/api/MatcherApiRoute.scala index aab697b51c6..c2b83f31a0b 100644 --- a/src/main/scala/com/wavesplatform/matcher/api/MatcherApiRoute.scala +++ b/src/main/scala/com/wavesplatform/matcher/api/MatcherApiRoute.scala @@ -3,7 +3,7 @@ package com.wavesplatform.matcher.api import javax.ws.rs.Path import akka.actor.ActorRef -import akka.http.scaladsl.model.StatusCodes +import akka.http.scaladsl.model.{StatusCodes, Uri} import akka.http.scaladsl.server.{Directive1, Route} import akka.pattern.ask import akka.util.Timeout @@ -63,9 +63,13 @@ case class MatcherApiRoute(application: Application, matcher: ActorRef, settings )) def orderBook: Route = (path("orderbook" / Segment / Segment) & get) { (a1, a2) => withAssetPair(a1, a2) { pair => - complete((matcher ? GetOrderBookRequest(pair, None)) - .mapTo[MatcherResponse] - .map(r => r.code -> r.json)) + onComplete((matcher ? GetOrderBookRequest(pair, None)).mapTo[MatcherResponse]) { + case Success(resp) => resp.code match { + case StatusCodes.Found => redirect(Uri(s"/matcher/orderbook/$a2/$a1"), StatusCodes.Found) + case code => complete(code -> resp.json) + } + case Failure(ex) => complete(StatusCodes.InternalServerError -> s"An error occurred: ${ex.getMessage}") + } } } diff --git a/src/main/scala/com/wavesplatform/matcher/market/MatcherActor.scala b/src/main/scala/com/wavesplatform/matcher/market/MatcherActor.scala index b478bfed849..c9d93c41a55 100644 --- a/src/main/scala/com/wavesplatform/matcher/market/MatcherActor.scala +++ b/src/main/scala/com/wavesplatform/matcher/market/MatcherActor.scala @@ -42,8 +42,7 @@ class MatcherActor(storedState: StoredState, wallet: Wallet, settings: MatcherSe msg.assetPair.priceAsset.map(storedState.assetsExtension.getAssetQuantity).forall(_ > 0) :| s"Unknown Asset ID: ${msg.assetPair.priceAssetStr}" && msg.assetPair.amountAsset.map(storedState.assetsExtension.getAssetQuantity).forall(_ > 0) :| - s"Unknown Asset ID: ${msg.assetPair.amountAssetStr}" && - checkPairOrdering(msg.assetPair) + s"Unknown Asset ID: ${msg.assetPair.amountAssetStr}" } def checkPairOrdering(aPair: AssetPair): Validation = { @@ -73,8 +72,16 @@ class MatcherActor(storedState: StoredState, wallet: Wallet, settings: MatcherSe def checkAssetPair[A <: {def assetPair : AssetPair}](msg: A)(f: => Unit): Unit = { val v = basicValidation(msg) - if (v) f - else sender() ! StatusCodeMatcherResponse(StatusCodes.NotFound, v.messages()) + if (!v) { + sender() ! StatusCodeMatcherResponse(StatusCodes.NotFound, v.messages()) + } else { + val ov = checkPairOrdering(msg.assetPair) + if (!ov) { + sender() ! StatusCodeMatcherResponse(StatusCodes.Found, ov.messages()) + } else { + f + } + } } def getMatcherPublicKey: Array[Byte] = { From 674db060a260ae8414690460aa8e74bcc0bd7ceb Mon Sep 17 00:00:00 2001 From: Alexey Koloskov Date: Thu, 23 Mar 2017 16:26:58 +0300 Subject: [PATCH 2/2] test fix --- .../matcher/market/MatcherActorSpecification.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/scala/com/wavesplatform/matcher/market/MatcherActorSpecification.scala b/src/test/scala/com/wavesplatform/matcher/market/MatcherActorSpecification.scala index 46c7c5a5443..44c269697a8 100644 --- a/src/test/scala/com/wavesplatform/matcher/market/MatcherActorSpecification.scala +++ b/src/test/scala/com/wavesplatform/matcher/market/MatcherActorSpecification.scala @@ -72,7 +72,7 @@ class MatcherActorSpecification extends TestKit(ActorSystem.apply("MatcherTest") def reversePredefinedPair = AssetPair(Base58.decode("BASE1").toOption, Base58.decode("BASE2").toOption) actor ! GetOrderBookRequest(reversePredefinedPair, None) - expectMsg(StatusCodeMatcherResponse(StatusCodes.NotFound, "Invalid AssetPair ordering, should be reversed: BASE2-BASE1")) + expectMsg(StatusCodeMatcherResponse(StatusCodes.Found, "Invalid AssetPair ordering, should be reversed: BASE2-BASE1")) } "AssetPair with predefined price assets" in { @@ -82,7 +82,7 @@ class MatcherActorSpecification extends TestKit(ActorSystem.apply("MatcherTest") def wrongPriceAsset = AssetPair(Base58.decode("BASE2").toOption, Base58.decode("Some").toOption) actor ! GetOrderBookRequest(wrongPriceAsset, None) - expectMsg(StatusCodeMatcherResponse(StatusCodes.NotFound, "Invalid AssetPair ordering, should be reversed: Some-BASE2")) + expectMsg(StatusCodeMatcherResponse(StatusCodes.Found, "Invalid AssetPair ordering, should be reversed: Some-BASE2")) } "AssetPair with unknown assets" in { @@ -92,7 +92,7 @@ class MatcherActorSpecification extends TestKit(ActorSystem.apply("MatcherTest") def wrongUnknownAssets = AssetPair(Base58.decode("Some1").toOption, Base58.decode("Some2").toOption) actor ! GetOrderBookRequest(wrongUnknownAssets, None) - expectMsg(StatusCodeMatcherResponse(StatusCodes.NotFound, "Invalid AssetPair ordering, should be reversed: Some2-Some1")) + expectMsg(StatusCodeMatcherResponse(StatusCodes.Found, "Invalid AssetPair ordering, should be reversed: Some2-Some1")) } "accept orders with AssetPair with same assets" in {