From a9aa182091b5d8d6037ae700b4c743174b8c5dc7 Mon Sep 17 00:00:00 2001 From: Alexis Hernandez Date: Sun, 30 Dec 2018 16:58:23 -0700 Subject: [PATCH] server: Store the transaction index while synchronizing the blocks --- .../data/anorm/LedgerPostgresDataHandler.scala | 5 ++++- .../data/anorm/dao/TransactionPostgresDAO.scala | 16 +++++++++------- .../TransactionPostgresDataHandlerSpec.scala | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/server/app/com/xsn/explorer/data/anorm/LedgerPostgresDataHandler.scala b/server/app/com/xsn/explorer/data/anorm/LedgerPostgresDataHandler.scala index 9b2fa77..fdaa579 100644 --- a/server/app/com/xsn/explorer/data/anorm/LedgerPostgresDataHandler.scala +++ b/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) 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 8c51551..01b9d85 100644 --- a/server/app/com/xsn/explorer/data/anorm/dao/TransactionPostgresDAO.scala +++ b/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 } diff --git a/server/test/com/xsn/explorer/data/TransactionPostgresDataHandlerSpec.scala b/server/test/com/xsn/explorer/data/TransactionPostgresDataHandlerSpec.scala index 617393d..487a1fe 100644 --- a/server/test/com/xsn/explorer/data/TransactionPostgresDataHandlerSpec.scala +++ b/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)) } }