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) _ <- blockPostgresDAO.insert(block)
// transactions // transactions
_ <- transactions.map(tx => transactionPostgresDAO.upsert(tx)).everything _ <- transactions
.zipWithIndex
.map { case (tx, index) => transactionPostgresDAO.upsert(index, tx) }
.everything
// balances // balances
balanceList = balances(transactions) 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. * 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 { for {
partialTx <- upsertTransaction(transaction) partialTx <- upsertTransaction(index, transaction)
inputs <- upsertInputs(transaction.id, transaction.inputs) inputs <- upsertInputs(transaction.id, transaction.inputs)
outputs <- upsertOutputs(transaction.id, transaction.outputs) outputs <- upsertOutputs(transaction.id, transaction.outputs)
_ <- spend(transaction.id, inputs) _ <- spend(transaction.id, inputs)
@ -307,24 +307,26 @@ class TransactionPostgresDAO @Inject() (fieldOrderingSQLInterpreter: FieldOrderi
result.toMap 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( SQL(
""" """
|INSERT INTO transactions |INSERT INTO transactions
| (txid, blockhash, time, size) | (txid, blockhash, time, size, index)
|VALUES |VALUES
| ({txid}, {blockhash}, {time}, {size}) | ({txid}, {blockhash}, {time}, {size}, {index})
|ON CONFLICT (txid) DO UPDATE |ON CONFLICT (txid) DO UPDATE
| SET blockhash = EXCLUDED.blockhash, | SET blockhash = EXCLUDED.blockhash,
| time = EXCLUDED.time, | time = EXCLUDED.time,
| size = EXCLUDED.size | size = EXCLUDED.size,
| index = EXCLUDED.index
|RETURNING txid, blockhash, time, size |RETURNING txid, blockhash, time, size
""".stripMargin """.stripMargin
).on( ).on(
'txid -> transaction.id.string, 'txid -> transaction.id.string,
'blockhash -> transaction.blockhash.string, 'blockhash -> transaction.blockhash.string,
'time -> transaction.time, 'time -> transaction.time,
'size -> transaction.size.int 'size -> transaction.size.int,
'index -> index
).as(parseTransaction.singleOpt).flatten ).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) = { private def upsertTransaction(transaction: Transaction) = {
val dao = new TransactionPostgresDAO(new FieldOrderingSQLInterpreter) val dao = new TransactionPostgresDAO(new FieldOrderingSQLInterpreter)
database.withConnection { implicit conn => database.withConnection { implicit conn =>
val maybe = dao.upsert(transaction) val maybe = dao.upsert(1, transaction)
Or.from(maybe, One(TransactionNotFoundError)) Or.from(maybe, One(TransactionNotFoundError))
} }
} }

Loading…
Cancel
Save