Skip to content

Commit

Permalink
server: Store the transaction index while synchronizing the blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexITC committed Dec 31, 2018
1 parent 585978f commit a9aa182
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Expand Down

0 comments on commit a9aa182

Please sign in to comment.