Skip to content

Commit 7033e38

Browse files
fix(chain-db): use chain tip for tx confirmations
1 parent 4df9b4e commit 7033e38

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

newm-chain-db/src/main/kotlin/io/newm/chain/database/repository/LedgerRepositoryImpl.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,9 +1576,9 @@ class LedgerRepositoryImpl : LedgerRepository {
15761576

15771577
override fun queryTransactionConfirmationCounts(txIds: List<String>): Map<String, Long> =
15781578
transaction {
1579-
val tipBlockExpression = LedgerUtxosTable.blockCreated.max()
1579+
val tipBlockExpression = ChainTable.blockNumber.max()
15801580
val tipBlock: Long =
1581-
LedgerUtxosTable.select(tipBlockExpression).firstOrNull()?.let {
1581+
ChainTable.select(tipBlockExpression).firstOrNull()?.let {
15821582
it[tipBlockExpression]
15831583
} ?: 0L
15841584

@@ -1590,7 +1590,7 @@ class LedgerRepositoryImpl : LedgerRepository {
15901590
.associate { row ->
15911591
val txId = row[LedgerUtxosTable.txId]
15921592
val blockCreated = row[LedgerUtxosTable.blockCreated]
1593-
txId to max(tipBlock - blockCreated, 0L)
1593+
txId to max(tipBlock - blockCreated + 1L, 0L)
15941594
}
15951595

15961596
txIds.associateWith { txId -> (ledgerMap[txId] ?: 0L) }

newm-chain-db/src/test/kotlin/io/newm/chain/database/repository/QueryTransactionInfoRepositoryTest.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,40 @@ class QueryTransactionInfoRepositoryTest {
214214
}
215215
}
216216

217+
@Test
218+
fun `queryTransactionConfirmationCounts uses chain tip and is inclusive`() {
219+
(100L..106L).forEach { blockNumber ->
220+
insertChainBlock(blockNumber = blockNumber, slotNumber = 1000L + blockNumber)
221+
}
222+
insertLedgerUtxo(
223+
address = "addr_test_dest1",
224+
txId = "target-tx",
225+
txIx = 0,
226+
lovelace = "2500",
227+
blockCreated = 100L,
228+
)
229+
230+
val result = repository.queryTransactionConfirmationCounts(listOf("target-tx", "missing-tx"))
231+
232+
assertThat(result["target-tx"]).isEqualTo(7L)
233+
assertThat(result["missing-tx"]).isEqualTo(0L)
234+
}
235+
236+
@Test
237+
fun `queryTransactionConfirmationCounts returns zero when chain tip is missing`() {
238+
insertLedgerUtxo(
239+
address = "addr_test_dest1",
240+
txId = "target-tx",
241+
txIx = 0,
242+
lovelace = "2500",
243+
blockCreated = 100L,
244+
)
245+
246+
val result = repository.queryTransactionConfirmationCounts(listOf("target-tx"))
247+
248+
assertThat(result["target-tx"]).isEqualTo(0L)
249+
}
250+
217251
private fun insertChainBlock(
218252
blockNumber: Long,
219253
slotNumber: Long,

0 commit comments

Comments
 (0)