Skip to content

Commit

Permalink
Return distinct transaction when querying txs by addresses
Browse files Browse the repository at this point in the history
If a tx was associated with 2 addresses and we were requesting the txs
of multiple addresses, if the 2 addresses were there, we were returing
twice the same tx.
  • Loading branch information
tdroxler committed Sep 30, 2024
1 parent 3c9665c commit b0a857e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ object TransactionQueries extends StrictLogging {

val query =
s"""
SELECT ${TxByAddressQR.selectFields}
SELECT DISTINCT ${TxByAddressQR.selectFields}
FROM transaction_per_addresses
WHERE main_chain = true
AND address IN $placeholder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,41 @@ class TransactionQueriesSpec extends AlephiumFutureSpec with DatabaseFixtureForE
}
}
}

"return distinct transactions" when {
"a transaction is associated with multiple addresses" in {
forAll(Gen.listOf(genTransactionPerAddressEntity(mainChain = Gen.const(true)))) {
entities =>
val toPersist = entities.flatMap { entity =>
// We copy the entity and change the address
Seq(entity, entity.copy(address = addressGen.sample.get))
}

// clear table and insert entities
run(TransactionPerAddressSchema.table.delete).futureValue
run(TransactionPerAddressSchema.table ++= toPersist).futureValue

val query =
TransactionQueries.getTxHashesByAddressesQuery(
toPersist.map(_.address),
Pagination.unsafe(1, Int.MaxValue)
)

val expectedResult =
entities map { entity =>
TxByAddressQR(
entity.hash,
entity.blockHash,
entity.timestamp,
entity.txOrder,
entity.coinbase
)
}

run(query).futureValue should contain theSameElementsAs expectedResult
}
}
}
}

trait Fixture {
Expand Down

0 comments on commit b0a857e

Please sign in to comment.