Browse Source

server: Store the transaction index while synchronizing the blocks

prometheus-integration
Alexis Hernandez 6 years ago
parent
commit
a9aa182091
  1. 5
      server/app/com/xsn/explorer/data/anorm/LedgerPostgresDataHandler.scala
  2. 16
      server/app/com/xsn/explorer/data/anorm/dao/TransactionPostgresDAO.scala
  3. 2
      server/test/com/xsn/explorer/data/TransactionPostgresDataHandlerSpec.scala

5
server/app/com/xsn/explorer/data/anorm/LedgerPostgresDataHandler.scala

@ -71,7 +71,10 @@ class LedgerPostgresDataHandler @Inject() (
_ <- blockPostgresDAO.insert(block)
// transactions
_ <- transactions.map(tx => transactionPostgresDAO.upsert(tx)).everything
_ <- transactions
.zipWithIndex
.map { case (tx, index) => transactionPostgresDAO.upsert(index, tx) }
.everything
// balances
balanceList = balances(transactions)

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

@ -17,9 +17,9 @@ class TransactionPostgresDAO @Inject() (fieldOrderingSQLInterpreter: FieldOrderi
/**
* NOTE: Ensure the connection has an open transaction.
*/
def upsert(transaction: Transaction)(implicit conn: Connection): Option[Transaction] = {
def upsert(index: Int, transaction: Transaction)(implicit conn: Connection): Option[Transaction] = {
for {
partialTx <- upsertTransaction(transaction)
partialTx <- upsertTransaction(index, transaction)
inputs <- upsertInputs(transaction.id, transaction.inputs)
outputs <- upsertOutputs(transaction.id, transaction.outputs)
_ <- spend(transaction.id, inputs)
@ -307,24 +307,26 @@ class TransactionPostgresDAO @Inject() (fieldOrderingSQLInterpreter: FieldOrderi
result.toMap
}
private def upsertTransaction(transaction: Transaction)(implicit conn: Connection): Option[Transaction] = {
private def upsertTransaction(index: Int, transaction: Transaction)(implicit conn: Connection): Option[Transaction] = {
SQL(
"""
|INSERT INTO transactions
| (txid, blockhash, time, size)
| (txid, blockhash, time, size, index)
|VALUES
| ({txid}, {blockhash}, {time}, {size})
| ({txid}, {blockhash}, {time}, {size}, {index})
|ON CONFLICT (txid) DO UPDATE
| SET blockhash = EXCLUDED.blockhash,
| time = EXCLUDED.time,
| size = EXCLUDED.size
| size = EXCLUDED.size,
| index = EXCLUDED.index
|RETURNING txid, blockhash, time, size
""".stripMargin
).on(
'txid -> transaction.id.string,
'blockhash -> transaction.blockhash.string,
'time -> transaction.time,
'size -> transaction.size.int
'size -> transaction.size.int,
'index -> index
).as(parseTransaction.singleOpt).flatten
}

2
server/test/com/xsn/explorer/data/TransactionPostgresDataHandlerSpec.scala

@ -115,7 +115,7 @@ class TransactionPostgresDataHandlerSpec extends PostgresDataHandlerSpec with Be
private def upsertTransaction(transaction: Transaction) = {
val dao = new TransactionPostgresDAO(new FieldOrderingSQLInterpreter)
database.withConnection { implicit conn =>
val maybe = dao.upsert(transaction)
val maybe = dao.upsert(1, transaction)
Or.from(maybe, One(TransactionNotFoundError))
}
}

Loading…
Cancel
Save