From a43435c4c3856ed49443c1497b5a6e69b3496dff Mon Sep 17 00:00:00 2001 From: Alexis Hernandez Date: Sun, 15 Apr 2018 13:26:49 -0500 Subject: [PATCH] server: Move getTransaction method from BlockEventsProcessor to TransactionService --- .../processors/BlockEventsProcessor.scala | 26 ++--------------- .../services/TransactionService.scala | 28 ++++++++++++++++--- .../controllers/TransactionsController.scala | 2 +- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/server/app/com/xsn/explorer/processors/BlockEventsProcessor.scala b/server/app/com/xsn/explorer/processors/BlockEventsProcessor.scala index ffed775..b0baa93 100644 --- a/server/app/com/xsn/explorer/processors/BlockEventsProcessor.scala +++ b/server/app/com/xsn/explorer/processors/BlockEventsProcessor.scala @@ -8,7 +8,7 @@ import com.xsn.explorer.data.DatabaseSeeder import com.xsn.explorer.data.async.{BlockFutureDataHandler, DatabaseFutureSeeder} import com.xsn.explorer.errors.BlockNotFoundError import com.xsn.explorer.models.rpc.Block -import com.xsn.explorer.models.{Blockhash, Transaction, TransactionId} +import com.xsn.explorer.models.{Blockhash, Transaction} import com.xsn.explorer.services.{TransactionService, XSNService} import com.xsn.explorer.util.Extensions.FutureApplicationResultExt import org.scalactic.{Bad, Good, One} @@ -49,37 +49,17 @@ class BlockEventsProcessor @Inject() ( def newLatestBlock(blockhash: Blockhash): FutureApplicationResult[Result] = { val result = for { block <- xsnService.getBlock(blockhash).toFutureOr - transactions <- block.transactions.map(getTransaction).toFutureOr + transactions <- block.transactions.map(transactionService.getTransaction).toFutureOr r <- newLatestBlock(block, transactions).toFutureOr } yield r result.toFuture } - private def getTransaction(txid: TransactionId): FutureApplicationResult[Transaction] = { - val result = for { - tx <- xsnService.getTransaction(txid).toFutureOr - transactionVIN <- tx.vin.map { vin => - transactionService.getTransactionValue(vin) - .map { - case Good(transactionValue) => - val newVIN = vin.copy(address = Some(transactionValue.address), value = Some(transactionValue.value)) - Good(newVIN) - - case Bad(_) => Good(vin) - } - }.toFutureOr - - rpcTransaction = tx.copy(vin = transactionVIN) - } yield Transaction.fromRPC(rpcTransaction) - - result.toFuture - } - private def newLatestBlock(newBlock: Block, newTransactions: List[Transaction]): FutureApplicationResult[Result] = { def onRechain(orphanBlock: Block): FutureApplicationResult[Result] = { val result = for { - orphanTransactions <- orphanBlock.transactions.map(getTransaction).toFutureOr + orphanTransactions <- orphanBlock.transactions.map(transactionService.getTransaction).toFutureOr command = DatabaseSeeder.ReplaceBlockCommand( orphanBlock = orphanBlock, diff --git a/server/app/com/xsn/explorer/services/TransactionService.scala b/server/app/com/xsn/explorer/services/TransactionService.scala index efc3287..a7952e0 100644 --- a/server/app/com/xsn/explorer/services/TransactionService.scala +++ b/server/app/com/xsn/explorer/services/TransactionService.scala @@ -6,15 +6,15 @@ import com.alexitc.playsonify.core.FutureApplicationResult import com.alexitc.playsonify.core.FutureOr.Implicits.{FutureOps, OrOps} import com.xsn.explorer.errors.{TransactionFormatError, TransactionNotFoundError} import com.xsn.explorer.models.rpc.TransactionVIN -import com.xsn.explorer.models.{TransactionDetails, TransactionId, TransactionValue} +import com.xsn.explorer.models.{Transaction, TransactionDetails, TransactionId, TransactionValue} import com.xsn.explorer.util.Extensions.FutureApplicationResultExt -import org.scalactic.{Good, One, Or} +import org.scalactic.{Bad, Good, One, Or} import scala.concurrent.{ExecutionContext, Future} class TransactionService @Inject() (xsnService: XSNService)(implicit ec: ExecutionContext) { - def getTransaction(txidString: String): FutureApplicationResult[TransactionDetails] = { + def getTransactionDetails(txidString: String): FutureApplicationResult[TransactionDetails] = { val result = for { txid <- { val maybe = TransactionId.from(txidString) @@ -32,7 +32,27 @@ class TransactionService @Inject() (xsnService: XSNService)(implicit ec: Executi result.toFuture } - def getTransactionValue(vin: TransactionVIN): FutureApplicationResult[TransactionValue] = { + def getTransaction(txid: TransactionId): FutureApplicationResult[Transaction] = { + val result = for { + tx <- xsnService.getTransaction(txid).toFutureOr + transactionVIN <- tx.vin.map { vin => + getTransactionValue(vin) + .map { + case Good(transactionValue) => + val newVIN = vin.copy(address = Some(transactionValue.address), value = Some(transactionValue.value)) + Good(newVIN) + + case Bad(_) => Good(vin) + } + }.toFutureOr + + rpcTransaction = tx.copy(vin = transactionVIN) + } yield Transaction.fromRPC(rpcTransaction) + + result.toFuture + } + + private def getTransactionValue(vin: TransactionVIN): FutureApplicationResult[TransactionValue] = { val valueMaybe = for { value <- vin.value address <- vin.address diff --git a/server/app/controllers/TransactionsController.scala b/server/app/controllers/TransactionsController.scala index b6da392..237380f 100644 --- a/server/app/controllers/TransactionsController.scala +++ b/server/app/controllers/TransactionsController.scala @@ -11,6 +11,6 @@ class TransactionsController @Inject() ( extends MyJsonController(cc) { def getTransaction(txid: String) = publicNoInput { _ => - transactionService.getTransaction(txid) + transactionService.getTransactionDetails(txid) } }