-
-
Notifications
You must be signed in to change notification settings - Fork 139
simplify amount_split by iterating over amount bits
#245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## main #245 +/- ##
==========================================
- Coverage 55.41% 55.40% -0.02%
==========================================
Files 42 42
Lines 3315 3314 -1
==========================================
- Hits 1837 1836 -1
Misses 1478 1478
☔ View full report in Codecov by Sentry. |
cashu/core/split.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO should also add a check that amount >= 0 and raise an Exception if not, as this behaves incorrectly if the amount is negative:
In [1]: amount = -12
In [2]: rv = []
...: for i in range(amount.bit_length()):
...: if amount & (1 << i): # if bit i is set, add 2**i to list
...: rv.append(1 << i)
...:
In [3]: rv
Out[3]: [4] # 4 != -12 or 12 ⁉️
In [4]: amount = 12
In [5]: rv = []
...: for i in range(amount.bit_length()):
...: if amount & (1 << i): # if bit i is set, add 2**i to list
...: rv.append(1 << i)
...:
In [6]: rv
Out[6]: [4, 8] # 8 + 4 == 12 ✅(Though the old version would also mess up with negative amounts as bin(-12) == "-0b1100" 🤣 so the [:-2] cut is wrong)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, that makes sense. Added an assert amount >= value at the beginning of the function, i.e. an AssertionError exception is thrown if a negative value was passed.
71c1b72 to
a57bb18
Compare
Small optimization: rather than involving a string data type here (which needs to also be reversed and sliced), we can just directly operate on the amount integer variable by iterating over all the bits and add 2^i to the return list if bit i is set. Also, add type hints to the parameter / return value and assert that the passed value must not be negative.
a57bb18 to
bafb6d0
Compare
|
@AngusP: Thanks for reviewing! Seems like the tests have a code path with negative amount to |
msg = 'Mint Error: invalid split amount: -1'
async def assert_err(f, msg):
"""Compute f() and expect an error message 'msg'."""
try:
await f
except Exception as exc:
> assert exc.args[0] == msg, Exception(
f"Expected error: {msg}, got: {exc.args[0]}"
)
E AssertionError: Exception("Expected error: Mint Error: invalid split amount: -1, got: can't split negative amount")
E assert "can't split negative amount" == 'Mint Error: ...it amount: -1'
E - Mint Error: invalid split amount: -1
E + can't split negative amount
tests/test_wallet.py:23: AssertionErrorThe test is failing just because the error message is different: assert "can't split negative amount" == 'Mint Error: ...it amount: -1'
--- - Mint Error: invalid split amount: -1
+++ + can't split negative amountSo should be pretty easy to fix, you've changed it to |
|
In an effort to close out some of these older pull requests I have opened a new one with the rebased changed #851. If that is merged this one should be closed at the same time. |
Small optimization: rather than involving a string data type here (which needs to also be reversed and sliced), we can just directly operate on the amount integer variable by iterating over all the bits and add 2^i to the return list if bit i is set.
(A similar change was done a few weeks ago also for cashu-feni: cashubtc/cashu-feni#44).