Skip to content

Commit

Permalink
Merge branch 'main' into cleanup_wishlist
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Nov 26, 2023
2 parents afb7019 + fa5193c commit 81c3931
Show file tree
Hide file tree
Showing 15 changed files with 342 additions and 152 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
python-version: ["3.9", "3.10"]
python-version: ["3.10"]
poetry-version: ["1.7.1"]
mint-cache-secrets: ["true", "false"]
# db-url: ["", "postgres://cashu:cashu@localhost:5432/test"] # TODO: Postgres test not working
db-url: [""]
backend-wallet-class: ["FakeWallet"]
uses: ./.github/workflows/tests.yml
with:
python-version: ${{ matrix.python-version }}
poetry-version: ${{ matrix.poetry-version }}
mint-cache-secrets: ${{ matrix.mint-cache-secrets }}
regtest:
uses: ./.github/workflows/regtest.yml
strategy:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ on:
os:
default: "ubuntu-latest"
type: string
mint-cache-secrets:
default: "false"
type: string

jobs:
poetry:
Expand Down Expand Up @@ -47,6 +50,7 @@ jobs:
MINT_HOST: localhost
MINT_PORT: 3337
MINT_DATABASE: ${{ inputs.db-url }}
MINT_CACHE_SECRETS: ${{ inputs.mint-cache-secrets }}
TOR: false
run: |
make test
Expand Down
3 changes: 2 additions & 1 deletion cashu/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from contextlib import asynccontextmanager
from typing import Optional, Union

from loguru import logger
from sqlalchemy import create_engine
from sqlalchemy_aio.base import AsyncConnection
from sqlalchemy_aio.strategy import ASYNCIO_STRATEGY # type: ignore
Expand Down Expand Up @@ -130,7 +131,7 @@ def _parse_timestamp(value, _):
# )
else:
if not os.path.exists(self.db_location):
print(f"Creating database directory: {self.db_location}")
logger.info(f"Creating database directory: {self.db_location}")
os.makedirs(self.db_location)
self.path = os.path.join(self.db_location, f"{self.name}.sqlite3")
database_uri = f"sqlite:///{self.path}"
Expand Down
3 changes: 3 additions & 0 deletions cashu/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class EnvSettings(CashuSettings):
debug: bool = Field(default=False)
log_level: str = Field(default="INFO")
cashu_dir: str = Field(default=os.path.join(str(Path.home()), ".cashu"))
debug_profiling: bool = Field(default=False)


class MintSettings(CashuSettings):
Expand All @@ -57,6 +58,7 @@ class MintSettings(CashuSettings):
mint_max_peg_in: int = Field(default=None)
mint_max_peg_out: int = Field(default=None)
mint_max_request_length: int = Field(default=1000)
mint_max_balance: int = Field(default=None)

mint_lnbits_endpoint: str = Field(default=None)
mint_lnbits_key: str = Field(default=None)
Expand All @@ -68,6 +70,7 @@ class FakeWalletSettings(MintSettings):
fakewallet_brr: bool = Field(default=True)
fakewallet_delay_payment: bool = Field(default=False)
fakewallet_stochastic_invoice: bool = Field(default=False)
mint_cache_secrets: bool = Field(default=True)


class MintInformation(CashuSettings):
Expand Down
9 changes: 6 additions & 3 deletions cashu/mint/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
)
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse

# from fastapi_profiler import PyInstrumentProfilerMiddleware
from loguru import logger
from starlette.middleware import Middleware
from starlette.middleware.cors import CORSMiddleware
Expand All @@ -21,6 +19,9 @@
from .router_deprecated import router_deprecated
from .startup import start_mint_init

if settings.debug_profiling:
from fastapi_profiler import PyInstrumentProfilerMiddleware

# from starlette_context import context
# from starlette_context.middleware import RawContextMiddleware

Expand Down Expand Up @@ -109,7 +110,9 @@ def emit(self, record):
middleware=middleware,
)

# app.add_middleware(PyInstrumentProfilerMiddleware)
if settings.debug_profiling:
assert PyInstrumentProfilerMiddleware is not None
app.add_middleware(PyInstrumentProfilerMiddleware)

return app

Expand Down
46 changes: 43 additions & 3 deletions cashu/mint/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ async def get_secrets_used(
*,
db: Database,
conn: Optional[Connection] = None,
) -> Optional[List[str]]: ...
) -> List[str]: ...

async def get_proof_used(
self,
db: Database,
proof: Proof,
conn: Optional[Connection] = None,
) -> Optional[Proof]: ...

@abstractmethod
async def invalidate_proof(
Expand Down Expand Up @@ -79,6 +86,13 @@ async def store_keyset(
conn: Optional[Connection] = None,
) -> None: ...

@abstractmethod
async def get_balance(
self,
db: Database,
conn: Optional[Connection] = None,
) -> int: ...

@abstractmethod
async def store_promise(
self,
Expand Down Expand Up @@ -264,11 +278,11 @@ async def get_secrets_used(
*,
db: Database,
conn: Optional[Connection] = None,
) -> Optional[List[str]]:
) -> List[str]:
rows = await (conn or db).fetchall(f"""
SELECT secret from {table_with_schema(db, 'proofs_used')}
""")
return [row[0] for row in rows] if rows else None
return [row[0] for row in rows] if rows else []

async def invalidate_proof(
self,
Expand Down Expand Up @@ -604,6 +618,17 @@ async def store_keyset(
keyset.unit.name,
),
)
async def get_balance(
self,
db: Database,
conn: Optional[Connection] = None,
) -> int:
row = await (conn or db).fetchone(f"""
SELECT * from {table_with_schema(db, 'balance')}
""")
assert row, "Balance not found"
return int(row[0])


async def get_keyset(
self,
Expand Down Expand Up @@ -641,3 +666,18 @@ async def get_keyset(
tuple(values),
)
return [MintKeyset(**row) for row in rows]

async def get_proof_used(
self,
db: Database,
proof: Proof,
conn: Optional[Connection] = None,
) -> Optional[Proof]:
row = await (conn or db).fetchone(
f"""
SELECT 1 from {table_with_schema(db, 'proofs_used')}
WHERE secret = ?
""",
(str(proof.secret),),
)
return Proof(**row) if row else None
Loading

0 comments on commit 81c3931

Please sign in to comment.