Skip to content

Commit

Permalink
Update Error Codes (#702)
Browse files Browse the repository at this point in the history
* add new error codes

* use the new errors in the code

* fix unsorted import

* fix test wallet
  • Loading branch information
lollerfirst authored Feb 13, 2025
1 parent a0ef44d commit ec3db81
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
32 changes: 32 additions & 0 deletions cashu/core/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,38 @@ def __init__(self, detail):
super().__init__(detail, code=self.code)


class TransactionDuplicateInputsError(TransactionError):
detail = "Duplicate inputs provided"
code = 11007

def __init__(self, detail: Optional[str] = None):
super().__init__(detail, code=self.code)


class TransactionDuplicateOutputsError(TransactionError):
detail = "Duplicate outputs provided"
code = 11008

def __init__(self, detail: Optional[str] = None):
super().__init__(detail, code=self.code)


class TransactionMultipleUnitsError(TransactionError):
detail = "Inputs/Outputs of multiple units"
code = 11009

def __init__(self, detail: Optional[str] = None):
super().__init__(detail, code=self.code)


class TransactionUnitMismatchError(TransactionError):
detail = "Inputs and outputs not of same unit"
code = 11010

def __init__(self, detail: Optional[str] = None):
super().__init__(detail, code=self.code)


class KeysetError(CashuError):
detail = "keyset error"
code = 12000
Expand Down
14 changes: 9 additions & 5 deletions cashu/mint/verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
NotAllowedError,
OutputsAlreadySignedError,
SecretTooLongError,
TransactionDuplicateInputsError,
TransactionDuplicateOutputsError,
TransactionError,
TransactionMultipleUnitsError,
TransactionUnitError,
TransactionUnitMismatchError,
)
from ..core.nuts import nut20
from ..core.settings import settings
Expand Down Expand Up @@ -67,7 +71,7 @@ async def verify_inputs_and_outputs(
raise TransactionError("secrets do not match criteria.")
# verify that only unique proofs were used
if not self._verify_no_duplicate_proofs(proofs):
raise TransactionError("duplicate proofs.")
raise TransactionDuplicateInputsError()
# Verify ecash signatures
if not all([self._verify_proof_bdhke(p) for p in proofs]):
raise InvalidProofsError()
Expand Down Expand Up @@ -128,7 +132,7 @@ async def _verify_outputs(
raise TransactionError("invalid amount.")
# verify that only unique outputs were used
if not self._verify_no_duplicate_outputs(outputs):
raise TransactionError("duplicate outputs.")
raise TransactionDuplicateOutputsError()
# verify that outputs have not been signed previously
signed_before = await self._check_outputs_issued_before(outputs, conn)
if any(signed_before):
Expand Down Expand Up @@ -219,11 +223,11 @@ def _verify_units_match(
units_proofs = [self.keysets[p.id].unit for p in proofs]
units_outputs = [self.keysets[o.id].unit for o in outs if o.id]
if not len(set(units_proofs)) == 1:
raise TransactionUnitError("inputs have different units.")
raise TransactionMultipleUnitsError("inputs have different units.")
if not len(set(units_outputs)) == 1:
raise TransactionUnitError("outputs have different units.")
raise TransactionMultipleUnitsError("outputs have different units.")
if not units_proofs[0] == units_outputs[0]:
raise TransactionUnitError("input and output keysets have different units.")
raise TransactionUnitMismatchError()
return units_proofs[0]

def get_fees_for_proofs(self, proofs: List[Proof]) -> int:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ async def test_duplicate_proofs_double_spent(wallet1: Wallet):
doublespend = await wallet1.mint(64, quote_id=mint_quote.quote)
await assert_err(
wallet1.split(wallet1.proofs + doublespend, 20),
"Mint Error: duplicate proofs.",
"Mint Error: Duplicate inputs provided",
)
assert wallet1.balance == 64
assert wallet1.available_balance == 64
Expand Down

0 comments on commit ec3db81

Please sign in to comment.