Skip to content

Support for lossless BtcAmount comparison#36

Closed
araspitzu wants to merge 1 commit into
ACINQ:masterfrom
araspitzu:lossless_btcamount_comparison
Closed

Support for lossless BtcAmount comparison#36
araspitzu wants to merge 1 commit into
ACINQ:masterfrom
araspitzu:lossless_btcamount_comparison

Conversation

@araspitzu
Copy link
Copy Markdown

This PR adds support for lossless comparison between different type of BtcAmount, the comparison uses MilliSatoshi (the smallest unit) to provide the most fine grained comparison without losing precision.

@araspitzu araspitzu marked this pull request as ready for review July 25, 2019 13:34
Copy link
Copy Markdown
Member

@sstone sstone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this needs a description because "lossless comparison" is not specific enough. I'm also a bit wary of conversions: what happens if you convert msat to sat and back to msat ?

I'm also beginning to wonder if we should have a type for millisatoshis here or in eclair, because they realy are a special case (i.e in the bitcoin world why would 2001 msat be > than 2000 msat ?)

@araspitzu
Copy link
Copy Markdown
Author

I think that this needs a description because "lossless comparison" is not specific enough. I'm also a bit wary of conversions: what happens if you convert msat to sat and back to msat ?

I'm also beginning to wonder if we should have a type for millisatoshis here or in eclair, because they realy are a special case (i.e in the bitcoin world why would 2001 msat be > than 2000 msat ?)

When I opened this PR I assumed we want to have a lossless comparison, where lossless means without losing precision, and to do that I had to use MilliSatoshi as the base unit for comparisons. However there is the argument that bitcoin-lib have to deal with bitcoin only values and this excludes the MilliSatoshi, in fact when rounding up to Satoshi the comparison 2001 msat > 2000 msat is false. Depending on the outcome of ACINQ/eclair#1088 I will close this PR.

def toBtc: Btc = millibtc2btc(toMilliBtc)
}

case class Satoshi(amount: Long) extends BtcAmount {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit unrelated but I think the amount field should be made private here, and has to be accessed with toLong. It's what we did for the UInt64 class and I find it cleaner.

Suggested change
case class Satoshi(amount: Long) extends BtcAmount {
case class Satoshi(private val amount: Long) extends BtcAmount {

@sstone
Copy link
Copy Markdown
Member

sstone commented Aug 20, 2019

This is not needed anymore.

@sstone sstone closed this Aug 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants