Skip to content

Commit

Permalink
Merge pull request #204 from wavesplatform/matcher_redirect_reverse_p…
Browse files Browse the repository at this point in the history
…airs

Matcher returns redirect on inverse asset pairs
  • Loading branch information
alexeykiselev authored Mar 23, 2017
2 parents e98ca45 + 674db06 commit b571a76
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}")
}
}
}

Expand Down
15 changes: 11 additions & 4 deletions src/main/scala/com/wavesplatform/matcher/market/MatcherActor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit b571a76

Please sign in to comment.