Skip to content

Commit

Permalink
Drop the in-memory database
Browse files Browse the repository at this point in the history
Fixes cashubtc#607

This PR drops the implementation of in-memory database traits.

They are useful for testing purposes since the tests should test our codebase
and assume the database works as expected (although a follow-up PR should write
a sanity test suite for all database trait implementors).

As complexity is worth with database requirements to simplify complexity and
add more robustness, for instance, with the following plans to add support for
transactions or buffered writes, it would become more complex and
time-consuming to support a correct database trait. This PR drops the
implementation and replaces it with a SQLite memory instance
  • Loading branch information
crodas committed Feb 28, 2025
1 parent 6339305 commit aa2fb12
Show file tree
Hide file tree
Showing 25 changed files with 165 additions and 948 deletions.
13 changes: 8 additions & 5 deletions crates/cdk-integration-tests/src/init_pure_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use std::sync::Arc;
use async_trait::async_trait;
use bip39::Mnemonic;
use cdk::amount::SplitTarget;
use cdk::cdk_database::mint_memory::MintMemoryDatabase;
use cdk::cdk_database::{MintDatabase, WalletMemoryDatabase};
use cdk::cdk_database::MintDatabase;
use cdk::mint::{FeeReserve, MintBuilder, MintMeltLimits};
use cdk::nuts::nut00::ProofsMethods;
use cdk::nuts::{
Expand Down Expand Up @@ -158,7 +157,9 @@ pub async fn create_and_start_test_mint() -> anyhow::Result<Arc<Mint>> {

let mut mint_builder = MintBuilder::new();

let database = MintMemoryDatabase::default();
let database = cdk_sqlite::mint::memory::empty()
.await
.expect("valid db instance");

let localstore = Arc::new(database);
mint_builder = mint_builder.with_localstore(localstore.clone());
Expand Down Expand Up @@ -209,13 +210,15 @@ pub async fn create_and_start_test_mint() -> anyhow::Result<Arc<Mint>> {
Ok(mint_arc)
}

pub fn create_test_wallet_for_mint(mint: Arc<Mint>) -> anyhow::Result<Arc<Wallet>> {
pub async fn create_test_wallet_for_mint(mint: Arc<Mint>) -> anyhow::Result<Arc<Wallet>> {
let connector = DirectMintConnection::new(mint);

let seed = Mnemonic::generate(12)?.to_seed_normalized("");
let mint_url = "http://aa".to_string();
let unit = CurrencyUnit::Sat;
let localstore = WalletMemoryDatabase::default();
let localstore = cdk_sqlite::wallet::memory::empty()
.await
.expect("valid db instance");
let mut wallet = Wallet::new(&mint_url, unit, Arc::new(localstore), &seed, None)?;

wallet.set_client(connector);
Expand Down
46 changes: 23 additions & 23 deletions crates/cdk-integration-tests/tests/fake_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::sync::Arc;
use anyhow::{bail, Result};
use bip39::Mnemonic;
use cdk::amount::SplitTarget;
use cdk::cdk_database::WalletMemoryDatabase;
use cdk::nuts::nut00::ProofsMethods;
use cdk::nuts::{
CurrencyUnit, MeltBolt11Request, MeltQuoteState, MintBolt11Request, PreMintSecrets, Proofs,
Expand All @@ -13,6 +12,7 @@ use cdk::wallet::client::{HttpClient, MintConnector};
use cdk::wallet::Wallet;
use cdk_fake_wallet::{create_fake_invoice, FakeInvoiceDescription};
use cdk_integration_tests::{attempt_to_swap_pending, wait_for_mint_to_be_paid};
use cdk_sqlite::wallet::memory;

const MINT_URL: &str = "http://127.0.0.1:8086";

Expand All @@ -22,7 +22,7 @@ async fn test_fake_tokens_pending() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -62,7 +62,7 @@ async fn test_fake_melt_payment_fail() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -125,7 +125,7 @@ async fn test_fake_melt_payment_fail_and_check() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -170,7 +170,7 @@ async fn test_fake_melt_payment_return_fail_status() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -230,7 +230,7 @@ async fn test_fake_melt_payment_error_unknown() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -291,7 +291,7 @@ async fn test_fake_melt_payment_err_paid() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -329,7 +329,7 @@ async fn test_fake_melt_change_in_quote() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -417,7 +417,7 @@ async fn test_fake_mint_with_witness() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand All @@ -441,7 +441,7 @@ async fn test_fake_mint_without_witness() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -477,7 +477,7 @@ async fn test_fake_mint_with_wrong_witness() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -517,7 +517,7 @@ async fn test_fake_mint_inflated() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -569,7 +569,7 @@ async fn test_fake_mint_multiple_units() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand All @@ -585,7 +585,7 @@ async fn test_fake_mint_multiple_units() -> Result<()> {
let wallet_usd = Wallet::new(
MINT_URL,
CurrencyUnit::Usd,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -639,7 +639,7 @@ async fn test_fake_mint_multiple_unit_swap() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand All @@ -653,7 +653,7 @@ async fn test_fake_mint_multiple_unit_swap() -> Result<()> {
let wallet_usd = Wallet::new(
MINT_URL,
CurrencyUnit::Usd,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -743,7 +743,7 @@ async fn test_fake_mint_multiple_unit_melt() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand All @@ -762,7 +762,7 @@ async fn test_fake_mint_multiple_unit_melt() -> Result<()> {
let wallet_usd = Wallet::new(
MINT_URL,
CurrencyUnit::Usd,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -863,7 +863,7 @@ async fn test_fake_mint_input_output_mismatch() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand All @@ -877,7 +877,7 @@ async fn test_fake_mint_input_output_mismatch() -> Result<()> {
let wallet_usd = Wallet::new(
MINT_URL,
CurrencyUnit::Usd,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -919,7 +919,7 @@ async fn test_fake_mint_swap_inflated() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -961,7 +961,7 @@ async fn test_fake_mint_duplicate_proofs_swap() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -1035,7 +1035,7 @@ async fn test_fake_mint_duplicate_proofs_melt() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down
4 changes: 2 additions & 2 deletions crates/cdk-integration-tests/tests/integration_tests_pure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use cdk_integration_tests::init_pure_tests::{
#[tokio::test]
async fn test_swap_to_send() -> anyhow::Result<()> {
let mint_bob = create_and_start_test_mint().await?;
let wallet_alice = create_test_wallet_for_mint(mint_bob.clone())?;
let wallet_alice = create_test_wallet_for_mint(mint_bob.clone()).await?;

// Alice gets 64 sats
fund_wallet(wallet_alice.clone(), 64).await?;
Expand All @@ -33,7 +33,7 @@ async fn test_swap_to_send() -> anyhow::Result<()> {
assert_eq!(Amount::from(24), wallet_alice.total_balance().await?);

// Alice sends cashu, Carol receives
let wallet_carol = create_test_wallet_for_mint(mint_bob.clone())?;
let wallet_carol = create_test_wallet_for_mint(mint_bob.clone()).await?;
let received_amount = wallet_carol
.receive_proofs(token.proofs(), SplitTarget::None, &[], &[])
.await?;
Expand Down
6 changes: 3 additions & 3 deletions crates/cdk-integration-tests/tests/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::time::Duration;
use anyhow::{bail, Result};
use bip39::Mnemonic;
use cdk::amount::{Amount, SplitTarget};
use cdk::cdk_database::mint_memory::MintMemoryDatabase;
use cdk::cdk_database::MintDatabase;
use cdk::dhke::construct_proofs;
use cdk::mint::{FeeReserve, MintBuilder, MintMeltLimits, MintQuote};
Expand All @@ -21,6 +20,7 @@ use cdk::types::QuoteTTL;
use cdk::util::unix_time;
use cdk::Mint;
use cdk_fake_wallet::FakeWallet;
use cdk_sqlite::mint::memory;
use tokio::sync::OnceCell;
use tokio::time::sleep;

Expand All @@ -43,7 +43,7 @@ async fn new_mint(fee: u64) -> Mint {

let mint_info = MintInfo::new().nuts(nuts);

let localstore = MintMemoryDatabase::default();
let localstore = memory::empty().await.expect("valid db instance");

localstore
.set_mint_info(mint_info)
Expand Down Expand Up @@ -450,7 +450,7 @@ async fn test_correct_keyset() -> Result<()> {
percent_fee_reserve: 1.0,
};

let database = MintMemoryDatabase::default();
let database = memory::empty().await.expect("valid db instance");

let fake_wallet = FakeWallet::new(fee_reserve, HashMap::default(), HashSet::default(), 0);

Expand Down
Loading

0 comments on commit aa2fb12

Please sign in to comment.