Tests: mint restore api test (#473)

* tests: mint restore api test

* skipifs for deprecated tests

* use correct secret counter for tests

* docker only on release
This commit is contained in:
callebtc
2024-03-14 13:17:05 +01:00
committed by GitHub
parent 5ba19a17cf
commit 7ecf14f87b
5 changed files with 80 additions and 13 deletions

View File

@@ -1,7 +1,6 @@
name: Docker Build name: Docker Build
on: on:
push:
release: release:
types: [published] types: [published]

View File

@@ -799,7 +799,7 @@ class Ledger(LedgerVerification, LedgerSpendingConditions):
async def restore( async def restore(
self, outputs: List[BlindedMessage] self, outputs: List[BlindedMessage]
) -> Tuple[List[BlindedMessage], List[BlindedSignature]]: ) -> Tuple[List[BlindedMessage], List[BlindedSignature]]:
promises: List[BlindedSignature] = [] signatures: List[BlindedSignature] = []
return_outputs: List[BlindedMessage] = [] return_outputs: List[BlindedMessage] = []
async with self.db.connect() as conn: async with self.db.connect() as conn:
for output in outputs: for output in outputs:
@@ -814,10 +814,10 @@ class Ledger(LedgerVerification, LedgerSpendingConditions):
if not promise.id and len(self.keysets) == 1: if not promise.id and len(self.keysets) == 1:
promise.id = self.keyset.id promise.id = self.keyset.id
# END backwards compatibility # END backwards compatibility
promises.append(promise) signatures.append(promise)
return_outputs.append(output) return_outputs.append(output)
logger.trace(f"promise found: {promise}") logger.trace(f"promise found: {promise}")
return return_outputs, promises return return_outputs, signatures
# ------- BLIND SIGNATURES ------- # ------- BLIND SIGNATURES -------

View File

@@ -354,5 +354,5 @@ async def check_state(
) )
async def restore(payload: PostMintRequest) -> PostRestoreResponse: async def restore(payload: PostMintRequest) -> PostRestoreResponse:
assert payload.outputs, Exception("no outputs provided.") assert payload.outputs, Exception("no outputs provided.")
outputs, promises = await ledger.restore(payload.outputs) outputs, signatures = await ledger.restore(payload.outputs)
return PostRestoreResponse(outputs=outputs, signatures=promises) return PostRestoreResponse(outputs=outputs, signatures=signatures)

View File

@@ -6,10 +6,13 @@ import pytest_asyncio
from cashu.core.base import ( from cashu.core.base import (
PostCheckStateRequest, PostCheckStateRequest,
PostCheckStateResponse, PostCheckStateResponse,
PostMintRequest,
PostRestoreResponse,
SpentState, SpentState,
) )
from cashu.core.settings import settings from cashu.core.settings import settings
from cashu.mint.ledger import Ledger from cashu.mint.ledger import Ledger
from cashu.wallet.crud import bump_secret_derivation
from cashu.wallet.wallet import Wallet from cashu.wallet.wallet import Wallet
from tests.helpers import get_real_invoice, is_fake, is_regtest, pay_if_regtest from tests.helpers import get_real_invoice, is_fake, is_regtest, pay_if_regtest
@@ -183,11 +186,11 @@ async def test_mint_quote(ledger: Ledger):
assert result["request"] assert result["request"]
invoice = bolt11.decode(result["request"]) invoice = bolt11.decode(result["request"])
assert invoice.amount_msat == 100 * 1000 assert invoice.amount_msat == 100 * 1000
expiry = None expiry = None
if invoice.expiry is not None: if invoice.expiry is not None:
expiry = invoice.date + invoice.expiry expiry = invoice.date + invoice.expiry
assert result["expiry"] == expiry assert result["expiry"] == expiry
# get mint quote again from api # get mint quote again from api
@@ -251,7 +254,7 @@ async def test_melt_quote_internal(ledger: Ledger, wallet: Wallet):
# TODO: internal invoice, fee should be 0 # TODO: internal invoice, fee should be 0
assert result["fee_reserve"] == 0 assert result["fee_reserve"] == 0
invoice_obj = bolt11.decode(request) invoice_obj = bolt11.decode(request)
expiry = None expiry = None
if invoice_obj.expiry is not None: if invoice_obj.expiry is not None:
expiry = invoice_obj.date + invoice_obj.expiry expiry = invoice_obj.date + invoice_obj.expiry
@@ -399,3 +402,38 @@ async def test_api_check_state(ledger: Ledger):
assert response assert response
assert len(response.states) == 2 assert len(response.states) == 2
assert response.states[0].state == SpentState.unspent assert response.states[0].state == SpentState.unspent
@pytest.mark.asyncio
@pytest.mark.skipif(
settings.debug_mint_only_deprecated,
reason="settings.debug_mint_only_deprecated is set",
)
async def test_api_restore(ledger: Ledger, wallet: Wallet):
invoice = await wallet.request_mint(64)
pay_if_regtest(invoice.bolt11)
await wallet.mint(64, id=invoice.id)
assert wallet.balance == 64
secret_counter = await bump_secret_derivation(
db=wallet.db, keyset_id=wallet.keyset_id, by=0, skip=True
)
secrets, rs, derivation_paths = await wallet.generate_secrets_from_to(
secret_counter - 1, secret_counter - 1
)
outputs, rs = wallet._construct_outputs([64], secrets, rs)
payload = PostMintRequest(outputs=outputs, quote="placeholder")
response = httpx.post(
f"{BASE_URL}/v1/restore",
json=payload.dict(),
)
data = response.json()
assert "signatures" in data
assert "outputs" in data
assert response.status_code == 200, f"{response.url} {response.status_code}"
response = PostRestoreResponse.parse_obj(response.json())
assert response
assert response
assert len(response.signatures) == 1
assert len(response.outputs) == 1
assert response.outputs == outputs

View File

@@ -5,10 +5,13 @@ import pytest_asyncio
from cashu.core.base import ( from cashu.core.base import (
CheckSpendableRequest_deprecated, CheckSpendableRequest_deprecated,
CheckSpendableResponse_deprecated, CheckSpendableResponse_deprecated,
PostMintRequest,
PostRestoreResponse,
Proof, Proof,
) )
from cashu.core.settings import settings from cashu.core.settings import settings
from cashu.mint.ledger import Ledger from cashu.mint.ledger import Ledger
from cashu.wallet.crud import bump_secret_derivation
from cashu.wallet.wallet import Wallet from cashu.wallet.wallet import Wallet
from tests.helpers import get_real_invoice, is_fake, is_regtest, pay_if_regtest from tests.helpers import get_real_invoice, is_fake, is_regtest, pay_if_regtest
@@ -305,10 +308,6 @@ async def test_checkfees_external(ledger: Ledger, wallet: Wallet):
@pytest.mark.asyncio @pytest.mark.asyncio
@pytest.mark.skipif(
settings.debug_mint_only_deprecated,
reason="settings.debug_mint_only_deprecated is set",
)
async def test_api_check_state(ledger: Ledger): async def test_api_check_state(ledger: Ledger):
proofs = [ proofs = [
Proof(id="1234", amount=0, secret="asdasdasd", C="asdasdasd"), Proof(id="1234", amount=0, secret="asdasdasd", C="asdasdasd"),
@@ -325,3 +324,34 @@ async def test_api_check_state(ledger: Ledger):
assert len(states.spendable) == 2 assert len(states.spendable) == 2
assert states.pending assert states.pending
assert len(states.pending) == 2 assert len(states.pending) == 2
@pytest.mark.asyncio
async def test_api_restore(ledger: Ledger, wallet: Wallet):
invoice = await wallet.request_mint(64)
pay_if_regtest(invoice.bolt11)
await wallet.mint(64, id=invoice.id)
assert wallet.balance == 64
secret_counter = await bump_secret_derivation(
db=wallet.db, keyset_id=wallet.keyset_id, by=0, skip=True
)
secrets, rs, derivation_paths = await wallet.generate_secrets_from_to(
secret_counter - 1, secret_counter - 1
)
outputs, rs = wallet._construct_outputs([64], secrets, rs)
payload = PostMintRequest(outputs=outputs, quote="placeholder")
response = httpx.post(
f"{BASE_URL}/restore",
json=payload.dict(),
)
data = response.json()
assert "promises" in data
assert "outputs" in data
assert response.status_code == 200, f"{response.url} {response.status_code}"
response = PostRestoreResponse.parse_obj(response.json())
assert response
assert response.promises
assert len(response.promises) == 1
assert len(response.outputs) == 1
assert response.outputs == outputs