Skip to content

Commit

Permalink
Dev (#6)
Browse files Browse the repository at this point in the history
* add filter to not include inputs that are smaller than fee
  • Loading branch information
vladmelnyk authored Jun 3, 2019
1 parent 136b9a9 commit fdab1dd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
group = 'com.coinselection'
version = '0.0.5'
version = '0.0.6'
buildscript {
ext.kotlin_version = '1.3.11'
ext.kotlin_version = '1.3.31'
ext.spring_boot_version = '2.0.6.RELEASE'
repositories {
mavenCentral()
Expand Down
18 changes: 9 additions & 9 deletions src/main/kotlin/com.coinselection/BtcCoinSelectionProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ class BtcCoinSelectionProvider(
val cumulativeHolder: CumulativeHolder
)


fun provide(utxoList: List<UnspentOutput>,
targetValue: BigDecimal,
feeRatePerByte: BigDecimal,
numberOfOutputs: Int = 1,
compulsoryUtxoList: List<UnspentOutput> = listOf()
compulsoryUtxoList: List<UnspentOutput>? = null
): CoinSelectionResult? {

val costCalculator = CostCalculator(transactionSize, feeRatePerByte, numberOfOutputs)
Expand Down Expand Up @@ -60,6 +59,7 @@ class BtcCoinSelectionProvider(
.shuffled()
.asSequence()
.take(maxNumberOfInputs)
.filter { it.amount >= costCalculator.getCostPerInput() }
.takeWhile { sumIsLessThanTarget(cumulativeHolder, targetValue = maxTargetValue) }
.onEach { delta.getAndSet((cumulativeHolder.getSum() - (optimalTargetValue + cumulativeHolder.getFee())).abs()) }
.takeWhile { (cumulativeHolder.getSum() + it.amount - (optimalTargetValue + cumulativeHolder.getFee() + costCalculator.getCostPerInput())).abs() < delta.get() }
Expand All @@ -74,16 +74,16 @@ class BtcCoinSelectionProvider(
private fun selectUntilSumIsLessThanTarget(utxoListRearanged: List<UnspentOutput>,
targetValue: BigDecimal,
costCalculator: CostCalculator,
compulsoryUtxoList: List<UnspentOutput>): UtxoSumCalculationData? {
compulsoryUtxoList: List<UnspentOutput>?): UtxoSumCalculationData? {
val cumulativeHolder = CumulativeHolder.defaultInit()
cumulativeHolder.appendFee(costCalculator.getBaseFee())
val selectedCompulsoryUtxoList = compulsoryUtxoList
.asSequence()
.take(maxNumberOfInputs)
.onEach { appendSumAndFee(cumulativeHolder, costCalculator, sum = it.amount) }
.toList()
val selectedCompulsoryUtxoList = compulsoryUtxoList?.asSequence()
?.take(maxNumberOfInputs)
?.onEach { appendSumAndFee(cumulativeHolder, costCalculator, sum = it.amount) }
?.toList() ?: listOf()
val selectedUtxoList = utxoListRearanged
.asSequence()
.filter { it.amount >= costCalculator.getCostPerInput() }
.takeWhile { sumIsLessThanTarget(cumulativeHolder, targetValue) }
.take(maxNumberOfInputs)
.onEach { appendSumAndFee(cumulativeHolder, costCalculator, sum = it.amount) }
Expand All @@ -97,7 +97,7 @@ class BtcCoinSelectionProvider(
private fun fallbackLargestFirstSelection(utxoListRearanged: List<UnspentOutput>,
costCalculator: CostCalculator,
targetValue: BigDecimal,
compulsoryUtxoList: List<UnspentOutput>): UtxoSumCalculationData? {
compulsoryUtxoList: List<UnspentOutput>?): UtxoSumCalculationData? {
val dataPair = selectUntilSumIsLessThanTarget(utxoListRearanged, targetValue, costCalculator, compulsoryUtxoList)
return if (dataPair == null || sumIsLessThanTarget(dataPair.cumulativeHolder, targetValue)) {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.junit.jupiter.api.Test
import java.math.BigDecimal
import java.util.*

private val KB = 1000.toBigDecimal()
val BTC_DUST_AMOUNT: BigDecimal = 546.toBigDecimal().movePointLeft(8)

class BtcCoinSelectionProviderTest {

Expand All @@ -17,6 +17,7 @@ class BtcCoinSelectionProviderTest {

private val coinSelectionProvider: BtcCoinSelectionProvider = BtcCoinSelectionProvider()
private val smartFeePerByte = BigDecimal.ONE.movePointLeft(8)
private val smartFeePerByteHigh = BigDecimal.TEN.movePointLeft(8)
private val random = Random()

@RepeatedTest(100)
Expand Down Expand Up @@ -75,7 +76,24 @@ class BtcCoinSelectionProviderTest {
val totalFeeExpected = calculateTransactionFee(coinSelectionResult.selectedUtxos!!.size, 2, smartFeePerByte)
val totalFee = coinSelectionResult.totalFee
Assertions.assertEquals(totalFeeExpected, totalFee)
}

@Test
fun `should not include small utxos`() {
val targetValue = BigDecimal(5)
val rangeMin = 1.1
val rangeMax = 1.2
val rangeMinDust = BTC_DUST_AMOUNT.toDouble()
val rangeMaxDust = BTC_DUST_AMOUNT.toDouble()
val utxoList = (1..50).map { rangeMinDust + (rangeMaxDust - rangeMinDust) * random.nextDouble() }.map { createUnspentOutput(it) }
val compulsoryUtxoList = (1..20).map { rangeMin + (rangeMax - rangeMin) * random.nextDouble() }.map { createUnspentOutput(it) }
val coinSelectionResult = coinSelectionProvider.provide(utxoList, targetValue, smartFeePerByteHigh, compulsoryUtxoList = compulsoryUtxoList)
Assertions.assertNotNull(coinSelectionResult!!.selectedUtxos)
Assertions.assertTrue(coinSelectionResult.selectedUtxos!!.containsAll(compulsoryUtxoList))
Assertions.assertFalse(coinSelectionResult.selectedUtxos!!.any { it in utxoList })
val totalFeeExpected = calculateTransactionFee(coinSelectionResult.selectedUtxos!!.size, 2, smartFeePerByteHigh)
val totalFee = coinSelectionResult.totalFee
Assertions.assertEquals(totalFeeExpected, totalFee)
}

private fun createUnspentOutput(value: Double): UnspentOutput {
Expand Down

0 comments on commit fdab1dd

Please sign in to comment.