Skip to content

Commit

Permalink
disallow improve step for large target amount (#10)
Browse files Browse the repository at this point in the history
* no improve step for large target amount

* increment version
  • Loading branch information
vladmelnyk authored Oct 31, 2019
1 parent e3e6c4b commit 32208af
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group = 'com.coinselection'
version = '1.1.0'
version = '1.1.1'
buildscript {
ext.kotlin_version = '1.3.50'
ext.spring_boot_version = '2.0.6.RELEASE'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,16 @@ internal class RandomImproveCoinSelectionProvider(maxNumberOfInputs: Int, transa
) ?: return null

val remainingUtxoList = utxoList.subtract(dataPair.utxoList).toList()
val improvedUtxoList = improve(remainingUtxoList, targetValue, costCalculator, dataPair.cumulativeHolder)

val remainingTotalAmount = remainingUtxoList.sumByBigDecimal { it.amount }

val canPerformImproveStep = remainingTotalAmount > targetValue
val improvedUtxoList =
if (canPerformImproveStep) {
improve(remainingUtxoList, targetValue, costCalculator, dataPair.cumulativeHolder)
} else {
listOf()
}

val utxoResult = dataPair.utxoList.union(improvedUtxoList).toList()

Expand Down
14 changes: 14 additions & 0 deletions src/test/kotlin/com/coinselection/CoinSelectionProviderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ class CoinSelectionProviderTest {
Assertions.assertEquals(totalFeeExpected, totalFee)
}

@Test
fun `should not improve for big target amount`() {
val targetValue = BigDecimal(27)
val rangeMin = 1.0
val rangeMax = 1.01
val utxoList = (1..50).map { rangeMin + (rangeMax - rangeMin) * random.nextDouble() }.map { createUnspentOutput(it) }
val coinSelectionResult = coinSelectionProviderRandomImprove.provide(utxoList, targetValue, smartFeePerByteHigh)
Assertions.assertNotNull(coinSelectionResult!!.selectedUtxos)
Assertions.assertTrue(coinSelectionResult.selectedUtxos!!.sumByBigDecimal { it.amount } < 28.toBigDecimal())
val totalFeeExpected = calculateTransactionFee(coinSelectionResult.selectedUtxos!!.size, 2, smartFeePerByteHigh)
val totalFee = coinSelectionResult.totalFee
Assertions.assertEquals(totalFeeExpected, totalFee)
}


@Test
fun `should account for op return output`() {
Expand Down

0 comments on commit 32208af

Please sign in to comment.