Implement dynamic amount of tokens for change (#223)

With the recent update to NUT-08, we can ensure that the amount of blank
outputs is always enough to cover any overpaid lightning fees. This
change implements this functionality for both the wallet and the mint.
The mint updateis backwards-compatible with respect to old wallets.
This commit is contained in:
xphade
2023-05-23 17:40:48 +02:00
committed by GitHub
parent 5df0a9aa59
commit 21069fb61a
7 changed files with 130 additions and 23 deletions

View File

@@ -1,4 +1,7 @@
import pytest
from cashu.core.base import TokenV3
from cashu.core.helpers import calculate_number_of_blank_outputs
from cashu.core.split import amount_split
@@ -22,3 +25,26 @@ def test_tokenv3_deserialize_serialize():
token_str = "cashuAeyJ0b2tlbiI6IFt7InByb29mcyI6IFt7ImlkIjogIkplaFpMVTZuQ3BSZCIsICJhbW91bnQiOiAyLCAic2VjcmV0IjogIjBFN2lDazRkVmxSZjVQRjFnNFpWMnciLCAiQyI6ICIwM2FiNTgwYWQ5NTc3OGVkNTI5NmY4YmVlNjU1ZGJkN2Q2NDJmNWQzMmRlOGUyNDg0NzdlMGI0ZDZhYTg2M2ZjZDUifSwgeyJpZCI6ICJKZWhaTFU2bkNwUmQiLCAiYW1vdW50IjogOCwgInNlY3JldCI6ICJzNklwZXh3SGNxcXVLZDZYbW9qTDJnIiwgIkMiOiAiMDIyZDAwNGY5ZWMxNmE1OGFkOTAxNGMyNTliNmQ2MTRlZDM2ODgyOWYwMmMzODc3M2M0NzIyMWY0OTYxY2UzZjIzIn1dLCAibWludCI6ICJodHRwOi8vbG9jYWxob3N0OjMzMzgifV19"
token = TokenV3.deserialize(token_str)
assert token.serialize() == token_str
def test_calculate_number_of_blank_outputs():
# Example from NUT-08 specification.
fee_reserve_sat = 1000
expected_n_blank_outputs = 10
n_blank_outputs = calculate_number_of_blank_outputs(fee_reserve_sat)
assert n_blank_outputs == expected_n_blank_outputs
def test_calculate_number_of_blank_outputs_for_small_fee_reserve():
# There should always be at least one blank output.
fee_reserve_sat = 1
expected_n_blank_outputs = 1
n_blank_outputs = calculate_number_of_blank_outputs(fee_reserve_sat)
assert n_blank_outputs == expected_n_blank_outputs
def test_calculate_number_of_blank_outputs_fails_for_negative_fee_reserve():
# Negative fee reserve is not supported.
fee_reserve_sat = 0
with pytest.raises(AssertionError):
_ = calculate_number_of_blank_outputs(fee_reserve_sat)