Skip to content

test: Replace satoshi_round with int() or Decimal() - #23210

Merged
maflcko merged 1 commit into
bitcoin:masterfrom
maflcko:2110-testRound
Oct 7, 2021
Merged

maflcko merged 1 commit into
bitcoin:masterfrom
maflcko:2110-testRound

Conversation

@maflcko

@maflcko maflcko commented Oct 6, 2021

Copy link
Copy Markdown
Member

satoshi_round will round down. To make the code easier to parse use
Decimal() where possible, which does not round. Or use int(), which
explicitly rounds down.

satoshi_round will round down. To make the code easier to parse use
Decimal() where possible, which does not round. Or use int(), which
explicitly rounds down.
@DrahtBot DrahtBot added the Tests label Oct 6, 2021
@katesalazar

katesalazar commented Oct 6, 2021 •

Copy link
Copy Markdown
Contributor

If the problem is satoshi_round defaulting to rounding down,
why not request it to round up using the optional argument?
It is there for that.

Decimal is very nice, no surprise satoshi_round is just a Decimal
syntactic sugar, but satoshi_round has the advantage of
labeling the number with what it is, sats.

@katesalazar

Copy link
Copy Markdown
Contributor

Um sorry I thought satoshi_round had an optional argument
and it doesn't (but could be added)

@katesalazar

Copy link
Copy Markdown
Contributor

Decimal() where possible, which does not round

You know this, but it is "Decimal(str) where possible, which
does not round, unlike Decimal(float)"

When Decimal(1) is fed a float, machine-specific witchcraft is
happening before the decimal module receives the data.

@maflcko

maflcko commented Oct 6, 2021

Copy link
Copy Markdown
Member Author

Um sorry I thought satoshi_round had an optional argument and it doesn't (but could be added)

I don't think the argument should be optional, but required, so that the caller explicitly specifies whether to round down or up. Though, it seems unrelated to the changes here. I am happy to review a follow-up pr.

@katesalazar

Copy link
Copy Markdown
Contributor

Um sorry I thought satoshi_round had an optional argument and it doesn't (but could be added)

I don't think the argument should be optional, but required, so that the caller explicitly specifies whether to round down or up. Though, it seems unrelated to the changes here. I am happy to review a follow-up pr.

If I were to do it right now, I would actually raise TypeError immediately
from satoshi_round if data entry is type float, for security reasons.
Having it accepting only int or str, no environment-particular quirking.
Floating point arithmetic for money is heretic, moreso at tests code.

@maflcko

maflcko commented Oct 6, 2021

Copy link
Copy Markdown
Member Author

I agree, but I think it is unrelated to the changes here.

@lsilva01 lsilva01 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tested ACK fa2ac58 on Ubuntu 20.04.

@maflcko
maflcko merged commit b4437d7 into bitcoin:master Oct 7, 2021
@maflcko
maflcko deleted the 2110-testRound branch October 7, 2021 07:12
@meshcollider

Copy link
Copy Markdown
Contributor

I don't think the argument should be optional, but required, so that the caller explicitly specifies whether to round down or up. Though, it seems unrelated to the changes here. I am happy to review a follow-up pr.

I think satoshi_round should only round down, that is its defining behaviour. If you have 1.5*10^-8 BTC, you have 1 satoshi, you never have 2. Perhaps it should just be renamed to satoshi_truncate though.

@maflcko

maflcko commented Oct 7, 2021

Copy link
Copy Markdown
Member Author

I'd say it depends on the value you pass. If it is a fee, you want to round up (See #22949). If it is a value, you want to round down.

@bitcoin bitcoin locked and limited conversation to collaborators Oct 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants