Browse Source

server: Mark output as spent while creating transactions (#34)

prometheus-integration
Alexis Hernandez 7 years ago
parent
commit
ef0b7ba633
  1. 24
      server/app/com/xsn/explorer/data/anorm/dao/TransactionPostgresDAO.scala

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

@ -20,9 +20,33 @@ class TransactionPostgresDAO @Inject() (fieldOrderingSQLInterpreter: FieldOrderi
partialTx <- upsertTransaction(transaction)
inputs <- upsertInputs(transaction.id, transaction.inputs)
outputs <- upsertOutputs(transaction.id, transaction.outputs)
_ <- spend(transaction.id, inputs)
} yield partialTx.copy(inputs = inputs, outputs = outputs)
}
private def spend(txid: TransactionId, inputs: List[Transaction.Input])(implicit conn: Connection): Option[Unit] = {
val result = inputs.flatMap { input => spend(txid, input) }
Option(result)
.filter(_.size == inputs.size)
.map(_ => ())
}
private def spend(txid: TransactionId, input: Transaction.Input)(implicit conn: Connection): Option[Transaction.Output] = {
SQL(
"""
|UPDATE transaction_outputs
|SET spent_on = {spent_on}
|WHERE txid = {output_txid} AND
| index = {output_index}
|RETURNING txid, index, hex_script, value, address, tpos_owner_address, tpos_merchant_address
""".stripMargin
).on(
'spent_on -> txid.string,
'output_txid -> input.fromTxid.string,
'output_index -> input.fromOutputIndex
).as(parseTransactionOutput.single)
}
/**
* NOTE: Ensure the connection has an open transaction.
*/

Loading…
Cancel
Save