diff --git a/src/main/kotlin/io/emeraldpay/dshackle/rpc/TrackBitcoinAddress.kt b/src/main/kotlin/io/emeraldpay/dshackle/rpc/TrackBitcoinAddress.kt index d4b80e34..9321ee1d 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/rpc/TrackBitcoinAddress.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/rpc/TrackBitcoinAddress.kt @@ -140,6 +140,9 @@ class TrackBitcoinAddress( .map { unspent -> totalUnspent(address, includeUtxo, unspent) } + .switchIfEmpty(Mono.just(0).map { + AddressBalance(address, BigInteger.ZERO) + }) .onErrorResume { t -> log.error("Failed to get unspent", t) Mono.empty() diff --git a/src/test/groovy/io/emeraldpay/dshackle/rpc/TrackBitcoinAddressSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/rpc/TrackBitcoinAddressSpec.groovy index 8b04a6bf..cb858bf5 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/rpc/TrackBitcoinAddressSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/rpc/TrackBitcoinAddressSpec.groovy @@ -83,6 +83,40 @@ class TrackBitcoinAddressSpec extends Specification { total.utxo.isEmpty() } + def "Correct sum for none"() { + setup: + def unspents = [] + TrackBitcoinAddress track = new TrackBitcoinAddress(Stub(MultistreamHolder)) + def address = new TrackBitcoinAddress.Address( + Chain.BITCOIN, "1K7xkspJg7DDKNwzXgoRSDCUxiFsRegsSK" + ) + when: + def total = track.totalUnspent(address, false, unspents) + + then: + total.balance == 0 + total.utxo.isEmpty() + } + + def "Correct sum for none read"() { + setup: + TrackBitcoinAddress track = new TrackBitcoinAddress(Stub(MultistreamHolder)) + def address = new TrackBitcoinAddress.Address( + Chain.BITCOIN, "1K7xkspJg7DDKNwzXgoRSDCUxiFsRegsSK" + ) + def api = Mock(BitcoinMultistream) { + 1 * getReader() >> Mock(BitcoinReader) { + 1 * listUnspent(_) >> Mono.empty() + } + } + when: + def total = track.balanceForAddress(api, address, false).block() + + then: + total.balance == 0 + total.utxo.isEmpty() + } + def "Correct sum for few with utxo"() { setup: def unspents = [