Browse Source

server: Add method to get scrolled transactions by blockhash

prometheus-integration
Alexis Hernandez 6 years ago
parent
commit
df8bc2e9ec
  1. 5
      server/app/com/xsn/explorer/data/TransactionDataHandler.scala
  2. 12
      server/app/com/xsn/explorer/data/anorm/TransactionPostgresDataHandler.scala
  3. 36
      server/app/com/xsn/explorer/data/anorm/dao/TransactionPostgresDAO.scala
  4. 8
      server/app/com/xsn/explorer/data/async/TransactionFutureDataHandler.scala

5
server/app/com/xsn/explorer/data/TransactionDataHandler.scala

@ -25,6 +25,11 @@ trait TransactionDataHandler[F[_]] {
paginatedQuery: PaginatedQuery, paginatedQuery: PaginatedQuery,
ordering: FieldOrdering[TransactionField]): F[PaginatedResult[TransactionWithValues]] 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]] def getLatestTransactionBy(addresses: Every[Address]): F[Map[String, String]]
} }

12
server/app/com/xsn/explorer/data/anorm/TransactionPostgresDataHandler.scala

@ -59,6 +59,18 @@ class TransactionPostgresDataHandler @Inject() (
Good(result) 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 => def getLatestTransactionBy(addresses: Every[Address]): ApplicationResult[Map[String, String]] = withConnection { implicit conn =>
val result = transactionPostgresDAO.getLatestTransactionBy(addresses) val result = transactionPostgresDAO.getLatestTransactionBy(addresses)

36
server/app/com/xsn/explorer/data/anorm/dao/TransactionPostgresDAO.scala

@ -233,6 +233,42 @@ class TransactionPostgresDAO @Inject() (fieldOrderingSQLInterpreter: FieldOrderi
Count(result) 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] = { def getUnspentOutputs(address: Address)(implicit conn: Connection): List[Transaction.Output] = {
SQL( SQL(
""" """

8
server/app/com/xsn/explorer/data/async/TransactionFutureDataHandler.scala

@ -46,6 +46,14 @@ class TransactionFutureDataHandler @Inject() (
blockingDataHandler.getByBlockhash(blockhash, paginatedQuery, ordering) 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 { override def getLatestTransactionBy(addresses: Every[Address]): FutureApplicationResult[Map[String, String]] = Future {
blockingDataHandler.getLatestTransactionBy(addresses) blockingDataHandler.getLatestTransactionBy(addresses)
} }

Loading…
Cancel
Save