nutshell icon indicating copy to clipboard operation
nutshell copied to clipboard

simplify `amount_split` by iterating over amount bits

Open theStack opened this issue 2 years ago • 3 comments

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: https://github.com/cashubtc/cashu-feni/pull/44).

theStack avatar May 31 '23 22:05 theStack

Codecov Report

Patch coverage: 100.00% and project coverage change: -0.02 :warning:

Comparison is base (5c820f9) 55.41% compared to head (71c1b72) 55.40%.

:exclamation: Current head 71c1b72 differs from pull request most recent head bafb6d0. Consider uploading reports for the commit bafb6d0 to get more accurate results

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              
Impacted Files Coverage Δ
cashu/core/split.py 100.00% <100.00%> (ø)

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

codecov[bot] avatar May 31 '23 22:05 codecov[bot]

@AngusP: Thanks for reviewing! Seems like the tests have a code path with negative amount to amount_split which triggers the newly introduced exception and makes the test fail (on master, it would return something wrong instead). Have to investigate a bit how to best fix this.

theStack avatar Jun 05 '23 21:06 theStack

@theStack

 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: AssertionError

The 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 amount

So should be pretty easy to fix, you've changed it to "can't split negative amount" whereas presumably some other bit of code spotted this error later

AngusP avatar Jun 12 '23 10:06 AngusP