From df8bc2e9ec9d167de14ab6386d89064ff16c4a73 Mon Sep 17 00:00:00 2001 From: Alexis Hernandez Date: Sat, 29 Dec 2018 19:30:51 -0700 Subject: [PATCH] server: Add method to get scrolled transactions by blockhash --- .../data/TransactionDataHandler.scala | 5 +++ .../TransactionPostgresDataHandler.scala | 12 +++++++ .../anorm/dao/TransactionPostgresDAO.scala | 36 +++++++++++++++++++ .../async/TransactionFutureDataHandler.scala | 8 +++++ 4 files changed, 61 insertions(+) diff --git a/server/app/com/xsn/explorer/data/TransactionDataHandler.scala b/server/app/com/xsn/explorer/data/TransactionDataHandler.scala index 7564235..6f8186d 100644 --- a/server/app/com/xsn/explorer/data/TransactionDataHandler.scala +++ b/server/app/com/xsn/explorer/data/TransactionDataHandler.scala @@ -25,6 +25,11 @@ trait TransactionDataHandler[F[_]] { paginatedQuery: PaginatedQuery, ordering: FieldOrdering[TransactionField]): F[PaginatedResult[TransactionWithValues]] + def getByBlockhash( + blockhash: Blockhash, + limit: Limit, + lastSeenTxid: Option[TransactionId]): F[List[TransactionWithValues]] + def getLatestTransactionBy(addresses: Every[Address]): F[Map[String, String]] } diff --git a/server/app/com/xsn/explorer/data/anorm/TransactionPostgresDataHandler.scala b/server/app/com/xsn/explorer/data/anorm/TransactionPostgresDataHandler.scala index 6deea04..e6f8ffb 100644 --- a/server/app/com/xsn/explorer/data/anorm/TransactionPostgresDataHandler.scala +++ b/server/app/com/xsn/explorer/data/anorm/TransactionPostgresDataHandler.scala @@ -59,6 +59,18 @@ class TransactionPostgresDataHandler @Inject() ( Good(result) } + override def getByBlockhash( + blockhash: Blockhash, + limit: Limit, + lastSeenTxid: Option[TransactionId]): ApplicationResult[List[TransactionWithValues]] = withConnection { implicit conn => + + val transactions = lastSeenTxid + .map { transactionPostgresDAO.getByBlockhash(blockhash, _, limit) } + .getOrElse { transactionPostgresDAO.getByBlockhash(blockhash, limit) } + + Good(transactions) + } + def getLatestTransactionBy(addresses: Every[Address]): ApplicationResult[Map[String, String]] = withConnection { implicit conn => val result = transactionPostgresDAO.getLatestTransactionBy(addresses) diff --git a/server/app/com/xsn/explorer/data/anorm/dao/TransactionPostgresDAO.scala b/server/app/com/xsn/explorer/data/anorm/dao/TransactionPostgresDAO.scala index 2aaeb6e..8c51551 100644 --- a/server/app/com/xsn/explorer/data/anorm/dao/TransactionPostgresDAO.scala +++ b/server/app/com/xsn/explorer/data/anorm/dao/TransactionPostgresDAO.scala @@ -233,6 +233,42 @@ class TransactionPostgresDAO @Inject() (fieldOrderingSQLInterpreter: FieldOrderi Count(result) } + def getByBlockhash(blockhash: Blockhash, limit: Limit)(implicit conn: Connection): List[TransactionWithValues] = { + SQL( + """ + |SELECT t.txid, t.blockhash, t.time, t.size, + | (SELECT COALESCE(SUM(value), 0) FROM transaction_inputs WHERE txid = t.txid) AS sent, + | (SELECT COALESCE(SUM(value), 0) FROM transaction_outputs WHERE txid = t.txid) AS received + |FROM transactions t JOIN blocks USING (blockhash) + |WHERE blockhash = {blockhash} + |ORDER BY t.txid ASC + |LIMIT {limit} + """.stripMargin + ).on( + 'limit -> limit.int, + 'blockhash -> blockhash.string + ).as(parseTransactionWithValues.*).flatten + } + + def getByBlockhash(blockhash: Blockhash, lastSeenTxid: TransactionId, limit: Limit)(implicit conn: Connection): List[TransactionWithValues] = { + SQL( + """ + |SELECT t.txid, t.blockhash, t.time, t.size, + | (SELECT COALESCE(SUM(value), 0) FROM transaction_inputs WHERE txid = t.txid) AS sent, + | (SELECT COALESCE(SUM(value), 0) FROM transaction_outputs WHERE txid = t.txid) AS received + |FROM transactions t JOIN blocks USING (blockhash) + |WHERE blockhash = {blockhash} AND + | t.txid > {lastSeenTxid} + |ORDER BY t.txid ASC + |LIMIT {limit} + """.stripMargin + ).on( + 'limit -> limit.int, + 'blockhash -> blockhash.string, + 'lastSeenTxid -> lastSeenTxid.string + ).as(parseTransactionWithValues.*).flatten + } + def getUnspentOutputs(address: Address)(implicit conn: Connection): List[Transaction.Output] = { SQL( """ diff --git a/server/app/com/xsn/explorer/data/async/TransactionFutureDataHandler.scala b/server/app/com/xsn/explorer/data/async/TransactionFutureDataHandler.scala index 58a8c2a..b535a55 100644 --- a/server/app/com/xsn/explorer/data/async/TransactionFutureDataHandler.scala +++ b/server/app/com/xsn/explorer/data/async/TransactionFutureDataHandler.scala @@ -46,6 +46,14 @@ class TransactionFutureDataHandler @Inject() ( blockingDataHandler.getByBlockhash(blockhash, paginatedQuery, ordering) } + override def getByBlockhash( + blockhash: Blockhash, + limit: Limit, + lastSeenTxid: Option[TransactionId]): FutureApplicationResult[List[TransactionWithValues]] = Future { + + blockingDataHandler.getByBlockhash(blockhash, limit, lastSeenTxid) + } + override def getLatestTransactionBy(addresses: Every[Address]): FutureApplicationResult[Map[String, String]] = Future { blockingDataHandler.getLatestTransactionBy(addresses) }