Browse Source

server: Use the TransactionIdValidator on the TransactionService

bitcoin
Alexis Hernandez 6 years ago
parent
commit
8a9ce8179b
  1. 36
      server/app/com/xsn/explorer/services/TransactionService.scala
  2. 12
      server/app/com/xsn/explorer/services/validators/TransactionIdValidator.scala
  3. 3
      server/test/com/xsn/explorer/services/LedgerSynchronizerServiceSpec.scala

36
server/app/com/xsn/explorer/services/TransactionService.scala

@ -11,7 +11,7 @@ import com.xsn.explorer.models._
import com.xsn.explorer.models.transformers._
import com.xsn.explorer.models.values._
import com.xsn.explorer.parsers.{OrderingConditionParser, TransactionOrderingParser}
import com.xsn.explorer.services.validators.AddressValidator
import com.xsn.explorer.services.validators.{AddressValidator, TransactionIdValidator}
import javax.inject.Inject
import org.scalactic._
import org.slf4j.LoggerFactory
@ -23,6 +23,7 @@ class TransactionService @Inject() (
orderingConditionParser: OrderingConditionParser,
transactionOrderingParser: TransactionOrderingParser,
addressValidator: AddressValidator,
transactionIdValidator: TransactionIdValidator,
transactionFutureDataHandler: TransactionFutureDataHandler)(
implicit ec: ExecutionContext) {
@ -57,13 +58,10 @@ class TransactionService @Inject() (
_ <- paginatedQueryValidator.validate(PaginatedQuery(Offset(0), limit), maxTransactionsPerQuery).toFutureOr
orderingCondition <- orderingConditionParser.parseReuslt(orderingConditionString).toFutureOr
lastSeenTxid <- {
lastSeenTxidString
.map(TransactionId.from)
.map { txid => Or.from(txid, One(TransactionFormatError)).map(Option.apply) }
.getOrElse(Good(Option.empty))
.toFutureOr
}
lastSeenTxid <- lastSeenTxidString
.map { string => transactionIdValidator.validate(string).map(Option.apply) }
.getOrElse(Good(None))
.toFutureOr
transactions <- transactionFutureDataHandler.getBy(address, limit, lastSeenTxid, orderingCondition).toFutureOr
} yield {
@ -91,13 +89,10 @@ class TransactionService @Inject() (
blockhash <- Or.from(Blockhash.from(blockhashString), One(BlockhashFormatError)).toFutureOr
_ <- paginatedQueryValidator.validate(PaginatedQuery(Offset(0), limit), maxTransactionsPerQuery).toFutureOr
lastSeenTxid <- {
lastSeenTxidString
.map(TransactionId.from)
.map { txid => Or.from(txid, One(TransactionFormatError)).map(Option.apply) }
.getOrElse(Good(Option.empty))
.toFutureOr
}
lastSeenTxid <- lastSeenTxidString
.map { string => transactionIdValidator.validate(string).map(Option.apply) }
.getOrElse(Good(None))
.toFutureOr
r <- transactionFutureDataHandler.getByBlockhash(blockhash, limit, lastSeenTxid).toFutureOr
} yield WrappedResult(r)
@ -114,13 +109,10 @@ class TransactionService @Inject() (
blockhash <- Or.from(Blockhash.from(blockhashString), One(BlockhashFormatError)).toFutureOr
_ <- paginatedQueryValidator.validate(PaginatedQuery(Offset(0), limit), maxTransactionsPerQuery).toFutureOr
lastSeenTxid <- {
lastSeenTxidString
.map(TransactionId.from)
.map { txid => Or.from(txid, One(TransactionFormatError)).map(Option.apply) }
.getOrElse(Good(Option.empty))
.toFutureOr
}
lastSeenTxid <- lastSeenTxidString
.map { string => transactionIdValidator.validate(string).map(Option.apply) }
.getOrElse(Good(None))
.toFutureOr
transactions <- transactionFutureDataHandler.getTransactionsWithIOBy(blockhash, limit, lastSeenTxid).toFutureOr
} yield {

12
server/app/com/xsn/explorer/services/validators/TransactionIdValidator.scala

@ -0,0 +1,12 @@
package com.xsn.explorer.services.validators
import com.alexitc.playsonify.core.ApplicationResult
import com.xsn.explorer.errors.TransactionFormatError
import com.xsn.explorer.models.values.TransactionId
class TransactionIdValidator {
def validate(string: String): ApplicationResult[TransactionId] = {
optional(string, TransactionFormatError)(TransactionId.from)
}
}

3
server/test/com/xsn/explorer/services/LedgerSynchronizerServiceSpec.scala

@ -13,7 +13,7 @@ import com.xsn.explorer.models.rpc.Block
import com.xsn.explorer.models.values.{Blockhash, Height}
import com.xsn.explorer.parsers.{OrderingConditionParser, TransactionOrderingParser}
import com.xsn.explorer.services.logic.{BlockLogic, TransactionLogic}
import com.xsn.explorer.services.validators.{AddressValidator, BlockhashValidator}
import com.xsn.explorer.services.validators.{AddressValidator, BlockhashValidator, TransactionIdValidator}
import org.scalactic.{Bad, Good, One, Or}
import org.scalatest.BeforeAndAfter
import org.scalatest.concurrent.ScalaFutures
@ -217,6 +217,7 @@ class LedgerSynchronizerServiceSpec extends PostgresDataHandlerSpec with BeforeA
new OrderingConditionParser,
new TransactionOrderingParser,
new AddressValidator,
new TransactionIdValidator,
new TransactionFutureDataHandler(transactionDataHandler)(Executors.databaseEC))
val blockService = new BlockService(

Loading…
Cancel
Save