Browse Source

server: Use the validators in the service package

bitcoin
Alexis Hernandez 6 years ago
parent
commit
277abf922b
  1. 16
      server/app/com/xsn/explorer/services/AddressService.scala
  2. 16
      server/app/com/xsn/explorer/services/TransactionRPCService.scala
  3. 11
      server/app/com/xsn/explorer/services/TransactionService.scala
  4. 3
      server/test/com/xsn/explorer/services/LedgerSynchronizerServiceSpec.scala

16
server/app/com/xsn/explorer/services/AddressService.scala

@ -1,24 +1,23 @@
package com.xsn.explorer.services
import com.alexitc.playsonify.core.FutureApplicationResult
import com.alexitc.playsonify.core.FutureOr.Implicits.{FutureOps, OrOps}
import com.alexitc.playsonify.core.{ApplicationResult, FutureApplicationResult}
import com.xsn.explorer.data.async.{BalanceFutureDataHandler, TransactionFutureDataHandler}
import com.xsn.explorer.errors.AddressFormatError
import com.xsn.explorer.models.persisted.{Balance, Transaction}
import com.xsn.explorer.models.values.Address
import com.xsn.explorer.services.validators.AddressValidator
import javax.inject.Inject
import org.scalactic.{One, Or}
import scala.concurrent.ExecutionContext
class AddressService @Inject() (
addressValidator: AddressValidator,
balanceFutureDataHandler: BalanceFutureDataHandler,
transactionFutureDataHandler: TransactionFutureDataHandler)(
implicit ec: ExecutionContext) {
def getBy(addressString: String): FutureApplicationResult[Balance] = {
val result = for {
address <- getAddress(addressString).toFutureOr
address <- addressValidator.validate(addressString).toFutureOr
balance <- balanceFutureDataHandler.getBy(address).toFutureOr
} yield balance
@ -27,15 +26,10 @@ class AddressService @Inject() (
def getUnspentOutputs(addressString: String): FutureApplicationResult[List[Transaction.Output]] = {
val result = for {
address <- getAddress(addressString).toFutureOr
address <- addressValidator.validate(addressString).toFutureOr
outputs <- transactionFutureDataHandler.getUnspentOutputs(address).toFutureOr
} yield outputs
result.toFuture
}
private def getAddress(addressString: String): ApplicationResult[Address] = {
val maybe = Address.from(addressString)
Or.from(maybe, One(AddressFormatError))
}
}

16
server/app/com/xsn/explorer/services/TransactionRPCService.scala

@ -2,11 +2,12 @@ package com.xsn.explorer.services
import com.alexitc.playsonify.core.FutureOr.Implicits.{FutureListOps, FutureOps, OrOps}
import com.alexitc.playsonify.core.{FutureApplicationResult, FutureOr}
import com.xsn.explorer.errors.{InvalidRawTransactionError, TransactionFormatError, TransactionNotFoundError, XSNWorkQueueDepthExceeded}
import com.xsn.explorer.errors.{InvalidRawTransactionError, TransactionNotFoundError, XSNWorkQueueDepthExceeded}
import com.xsn.explorer.models.persisted.Transaction
import com.xsn.explorer.models.rpc.TransactionVIN
import com.xsn.explorer.models.values._
import com.xsn.explorer.models.{TPoSContract, TransactionDetails, TransactionValue}
import com.xsn.explorer.services.validators.TransactionIdValidator
import com.xsn.explorer.util.Extensions.FutureOrExt
import javax.inject.Inject
import org.scalactic.{Bad, Good, One, Or}
@ -17,6 +18,7 @@ import scala.concurrent.{ExecutionContext, Future}
import scala.util.control.NonFatal
class TransactionRPCService @Inject() (
transactionIdValidator: TransactionIdValidator,
xsnService: XSNService)(
implicit ec: ExecutionContext) {
@ -24,11 +26,7 @@ class TransactionRPCService @Inject() (
def getRawTransaction(txidString: String): FutureApplicationResult[JsValue] = {
val result = for {
txid <- {
val maybe = TransactionId.from(txidString)
Or.from(maybe, One(TransactionFormatError)).toFutureOr
}
txid <- transactionIdValidator.validate(txidString).toFutureOr
transaction <- xsnService.getRawTransaction(txid).toFutureOr
} yield transaction
@ -37,11 +35,7 @@ class TransactionRPCService @Inject() (
def getTransactionDetails(txidString: String): FutureApplicationResult[TransactionDetails] = {
val result = for {
txid <- {
val maybe = TransactionId.from(txidString)
Or.from(maybe, One(TransactionFormatError)).toFutureOr
}
txid <- transactionIdValidator.validate(txidString).toFutureOr
transaction <- xsnService.getTransaction(txid).toFutureOr
vin <- getTransactionVIN(transaction.vin).toFutureOr
} yield TransactionDetails.from(transaction.copy(vin = vin))

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

@ -6,12 +6,10 @@ import com.alexitc.playsonify.models.ordering.OrderingQuery
import com.alexitc.playsonify.models.pagination.{Limit, Offset, PaginatedQuery}
import com.alexitc.playsonify.validators.PaginatedQueryValidator
import com.xsn.explorer.data.async.TransactionFutureDataHandler
import com.xsn.explorer.errors._
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, TransactionIdValidator}
import com.xsn.explorer.services.validators.{AddressValidator, BlockhashValidator, TransactionIdValidator}
import javax.inject.Inject
import org.scalactic._
import org.slf4j.LoggerFactory
@ -24,6 +22,7 @@ class TransactionService @Inject() (
transactionOrderingParser: TransactionOrderingParser,
addressValidator: AddressValidator,
transactionIdValidator: TransactionIdValidator,
blockhashValidator: BlockhashValidator,
transactionFutureDataHandler: TransactionFutureDataHandler)(
implicit ec: ExecutionContext) {
@ -75,7 +74,7 @@ class TransactionService @Inject() (
def getByBlockhash(blockhashString: String, paginatedQuery: PaginatedQuery, orderingQuery: OrderingQuery): FuturePaginatedResult[TransactionWithValues] = {
val result = for {
blockhash <- Or.from(Blockhash.from(blockhashString), One(BlockhashFormatError)).toFutureOr
blockhash <- blockhashValidator.validate(blockhashString).toFutureOr
validatedQuery <- paginatedQueryValidator.validate(paginatedQuery, maxTransactionsPerQuery).toFutureOr
order <- transactionOrderingParser.from(orderingQuery).toFutureOr
r <- transactionFutureDataHandler.getByBlockhash(blockhash, validatedQuery, order).toFutureOr
@ -86,7 +85,7 @@ class TransactionService @Inject() (
def getByBlockhash(blockhashString: String, limit: Limit, lastSeenTxidString: Option[String]): FutureApplicationResult[WrappedResult[List[TransactionWithValues]]] = {
val result = for {
blockhash <- Or.from(Blockhash.from(blockhashString), One(BlockhashFormatError)).toFutureOr
blockhash <- blockhashValidator.validate(blockhashString).toFutureOr
_ <- paginatedQueryValidator.validate(PaginatedQuery(Offset(0), limit), maxTransactionsPerQuery).toFutureOr
lastSeenTxid <- lastSeenTxidString
@ -106,7 +105,7 @@ class TransactionService @Inject() (
lastSeenTxidString: Option[String]): FutureApplicationResult[WrappedResult[List[LightWalletTransaction]]] = {
val result = for {
blockhash <- Or.from(Blockhash.from(blockhashString), One(BlockhashFormatError)).toFutureOr
blockhash <- blockhashValidator.validate(blockhashString).toFutureOr
_ <- paginatedQueryValidator.validate(PaginatedQuery(Offset(0), limit), maxTransactionsPerQuery).toFutureOr
lastSeenTxid <- lastSeenTxidString

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

@ -218,6 +218,7 @@ class LedgerSynchronizerServiceSpec extends PostgresDataHandlerSpec with BeforeA
new TransactionOrderingParser,
new AddressValidator,
new TransactionIdValidator,
new BlockhashValidator,
new TransactionFutureDataHandler(transactionDataHandler)(Executors.databaseEC))
val blockService = new BlockService(
@ -230,7 +231,7 @@ class LedgerSynchronizerServiceSpec extends PostgresDataHandlerSpec with BeforeA
new OrderingConditionParser,
BlockHeaderCache.default
)
val transactionRPCService = new TransactionRPCService(xsnService)
val transactionRPCService = new TransactionRPCService(new TransactionIdValidator, xsnService)
new LedgerSynchronizerService(
xsnService,
transactionService,

Loading…
Cancel
Save