Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop the in-memory database #613

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/cdk-integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ rand = "0.8.5"
bip39 = { version = "2.0", features = ["rand"] }
anyhow = "1"
cashu = { path = "../cashu", features = ["mint", "wallet"] }
cdk = { path = "../cdk", features = ["mint", "wallet"] }
cdk = { path = "../cdk", features = ["mint", "wallet", "memory_database"] }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it makes sense to add a memory db feature? Or should we just keep it as you have to bring in one of the cdk-sqlite or redb crates. And whether the sqlite is in mem or not can be handled by that crate. The feature maybe is a little similar for the end user but it creates two para dimes to accomplish the same thing one enabling the feature one bringing in the create.

Related: #511

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR has already undergone several iterations, but in its final version, which has just two functions per DB implementation, I believe that moving those functions from cdk to the SQLite crate makes sense.

Regarding #511, that does make sense.

cdk-cln = { path = "../cdk-cln" }
cdk-lnd = { path = "../cdk-lnd" }
cdk-axum = { path = "../cdk-axum" }
Expand Down
9 changes: 7 additions & 2 deletions crates/cdk-integration-tests/src/bin/fake_wallet.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::env;

use anyhow::Result;
use cdk::cdk_database::mint_memory::MintMemoryDatabase;
use cdk::cdk_database::mint_memory;
use cdk_integration_tests::init_fake_wallet::start_fake_mint;
use cdk_integration_tests::init_regtest::get_temp_dir;
use cdk_redb::MintRedbDatabase;
Expand All @@ -16,7 +16,12 @@ async fn main() -> Result<()> {

match mint_db_kind.as_str() {
"MEMORY" => {
start_fake_mint(addr, port, MintMemoryDatabase::default()).await?;
start_fake_mint(
addr,
port,
mint_memory::empty().await.expect("valid db instance"),
)
.await?;
}
"SQLITE" => {
let sqlite_db = MintSqliteDatabase::new(&get_temp_dir().join("mint")).await?;
Expand Down
6 changes: 3 additions & 3 deletions crates/cdk-integration-tests/src/bin/regtest_mint.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::env;

use anyhow::Result;
use cdk::cdk_database::mint_memory::MintMemoryDatabase;
use cdk::cdk_database::mint_memory;
use cdk_integration_tests::init_regtest::{
create_cln_backend, create_lnd_backend, create_mint, fund_ln, generate_block, get_bitcoin_dir,
get_cln_dir, get_lnd_cert_file_path, get_lnd_dir, get_lnd_macaroon_path, get_temp_dir,
Expand Down Expand Up @@ -161,7 +161,7 @@ async fn main() -> Result<()> {
create_mint(
mint_addr,
cln_mint_port,
MintMemoryDatabase::default(),
mint_memory::empty().await.expect("valid db instance"),
cln_backend,
)
.await
Expand All @@ -171,7 +171,7 @@ async fn main() -> Result<()> {
create_mint(
mint_addr,
lnd_mint_port,
MintMemoryDatabase::default(),
mint_memory::empty().await.expect("valid db instance"),
lnd_backend,
)
.await?;
Expand Down
9 changes: 4 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::{mint_memory, wallet_memory, MintDatabase};
use cdk::mint::{FeeReserve, MintBuilder, MintMeltLimits};
use cdk::nuts::nut00::ProofsMethods;
use cdk::nuts::{
Expand Down Expand Up @@ -158,7 +157,7 @@ pub async fn create_and_start_test_mint() -> anyhow::Result<Arc<Mint>> {

let mut mint_builder = MintBuilder::new();

let database = MintMemoryDatabase::default();
let database = 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 +208,13 @@ 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 = 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,7 @@ use std::sync::Arc;
use anyhow::{bail, Result};
use bip39::Mnemonic;
use cdk::amount::SplitTarget;
use cdk::cdk_database::WalletMemoryDatabase;
use cdk::cdk_database::wallet_memory;
use cdk::nuts::nut00::ProofsMethods;
use cdk::nuts::{
CurrencyUnit, MeltBolt11Request, MeltQuoteState, MintBolt11Request, PreMintSecrets, Proofs,
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(wallet_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(wallet_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(wallet_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(wallet_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(wallet_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(wallet_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(wallet_memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -382,7 +382,7 @@ async fn test_fake_mint_with_witness() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(wallet_memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand All @@ -406,7 +406,7 @@ async fn test_fake_mint_without_witness() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(wallet_memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -442,7 +442,7 @@ async fn test_fake_mint_with_wrong_witness() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(wallet_memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -482,7 +482,7 @@ async fn test_fake_mint_inflated() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(wallet_memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -534,7 +534,7 @@ async fn test_fake_mint_multiple_units() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(wallet_memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand All @@ -550,7 +550,7 @@ async fn test_fake_mint_multiple_units() -> Result<()> {
let wallet_usd = Wallet::new(
MINT_URL,
CurrencyUnit::Usd,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(wallet_memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -604,7 +604,7 @@ async fn test_fake_mint_multiple_unit_swap() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(wallet_memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand All @@ -618,7 +618,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(wallet_memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -708,7 +708,7 @@ async fn test_fake_mint_multiple_unit_melt() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(wallet_memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand All @@ -727,7 +727,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(wallet_memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -827,7 +827,7 @@ async fn test_fake_mint_input_output_mismatch() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(wallet_memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand All @@ -841,7 +841,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(wallet_memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -883,7 +883,7 @@ async fn test_fake_mint_swap_inflated() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(wallet_memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -925,7 +925,7 @@ async fn test_fake_mint_duplicate_proofs_swap() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(wallet_memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
Expand Down Expand Up @@ -999,7 +999,7 @@ async fn test_fake_mint_duplicate_proofs_melt() -> Result<()> {
let wallet = Wallet::new(
MINT_URL,
CurrencyUnit::Sat,
Arc::new(WalletMemoryDatabase::default()),
Arc::new(wallet_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
7 changes: 3 additions & 4 deletions crates/cdk-integration-tests/tests/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ 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::cdk_database::{mint_memory, MintDatabase};
use cdk::dhke::construct_proofs;
use cdk::mint::{FeeReserve, MintBuilder, MintMeltLimits, MintQuote};
use cdk::nuts::nut00::ProofsMethods;
Expand Down Expand Up @@ -43,7 +42,7 @@ async fn new_mint(fee: u64) -> Mint {

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

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

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

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

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

Expand Down
Loading
Loading