From b0a857eec9068d73a8c70027561c8242a853b0b4 Mon Sep 17 00:00:00 2001 From: Thomas Droxler Date: Mon, 30 Sep 2024 16:41:08 +0200 Subject: [PATCH] Return distinct transaction when querying txs by addresses 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. --- .../queries/TransactionQueries.scala | 2 +- .../queries/TransactionQueriesSpec.scala | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/app/src/main/scala/org/alephium/explorer/persistence/queries/TransactionQueries.scala b/app/src/main/scala/org/alephium/explorer/persistence/queries/TransactionQueries.scala index a7b6d31f..95b9683f 100644 --- a/app/src/main/scala/org/alephium/explorer/persistence/queries/TransactionQueries.scala +++ b/app/src/main/scala/org/alephium/explorer/persistence/queries/TransactionQueries.scala @@ -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 diff --git a/app/src/test/scala/org/alephium/explorer/persistence/queries/TransactionQueriesSpec.scala b/app/src/test/scala/org/alephium/explorer/persistence/queries/TransactionQueriesSpec.scala index 04906b5f..93798177 100644 --- a/app/src/test/scala/org/alephium/explorer/persistence/queries/TransactionQueriesSpec.scala +++ b/app/src/test/scala/org/alephium/explorer/persistence/queries/TransactionQueriesSpec.scala @@ -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 {