Skip to content

Commit

Permalink
Remove the shared Mint instance between tests
Browse files Browse the repository at this point in the history
Since the SignatoryManager spawns a worker (with `tokio::spawn`) to execute the
Signatory (which implements the `Signatory` trait), a mpsc channel is used to
exchange messages.

In side Tokio tests, each test has its own Tokio runtime. When the first test
is done, since `instantiate` shares all the Mint instances, their Tokio runtime
is dropped, and the tokio::spawn's task gets killed, breaking the channel.

Since the SignatoryManager has no way to respawn, understandably so, since the
task runs in an infinite loop, receiving messages and spawning new tasks to
non-blocking. There are two choices: either create one Mint per test (which is
what this PR does) or compile a modified version of the SignatoryManager for
tests, which performs the calls to the Signatory trait in place instead of
converting it to a message and passing to the signatory Tokio task.

The first option is more straightforward and fixes other instances of the code
where `tokio::spawn` is used, such as subscriptions.

A third option could be explored, which would imply spawning a Tokio runtime in
another OS-level thread, creating the Mint in that context, and sharing it with
everything else; perhaps this should explored further if the idea is to test
Mint this way.
  • Loading branch information
crodas committed Feb 5, 2025
1 parent 3e16038 commit 1fe6b06
Showing 1 changed file with 5 additions and 17 deletions.
22 changes: 5 additions & 17 deletions crates/cdk-integration-tests/tests/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ use cdk::subscription::{IndexableParams, Params};
use cdk::util::unix_time;
use cdk::Mint;
use cdk_fake_wallet::FakeWallet;
use tokio::sync::OnceCell;
use tokio::time::sleep;

pub const MINT_URL: &str = "http://127.0.0.1:8088";

static INSTANCE: OnceCell<Arc<Mint>> = OnceCell::const_new();

async fn new_mint(fee: u64) -> Mint {
let mut supported_units = HashMap::new();
supported_units.insert(CurrencyUnit::Sat, (fee, 32));
Expand Down Expand Up @@ -70,13 +67,6 @@ async fn new_mint(fee: u64) -> Mint {
.unwrap()
}

async fn initialize() -> Arc<Mint> {
INSTANCE
.get_or_init(|| async { Arc::new(new_mint(0).await) })
.await
.clone()
}

async fn mint_proofs(
mint: &Mint,
amount: Amount,
Expand Down Expand Up @@ -121,7 +111,7 @@ async fn mint_proofs(

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_mint_double_spend() -> Result<()> {
let mint = initialize().await;
let mint = new_mint(0).await;

let keys = mint.pubkeys().await?.keysets.first().unwrap().clone().keys;
let keyset_id = Id::from(&keys);
Expand All @@ -132,9 +122,7 @@ async fn test_mint_double_spend() -> Result<()> {

let swap_request = SwapRequest::new(proofs.clone(), preswap.blinded_messages());

let swap = mint.process_swap_request(swap_request).await;

assert!(swap.is_ok());
mint.process_swap_request(swap_request).await?;

let preswap_two = PreMintSecrets::random(keyset_id, 100.into(), &SplitTarget::default())?;

Expand All @@ -155,7 +143,7 @@ async fn test_mint_double_spend() -> Result<()> {
/// This will work if the mint does not check for outputs amounts overflowing
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_attempt_to_swap_by_overflowing() -> Result<()> {
let mint = initialize().await;
let mint = new_mint(0).await;

let keys = mint.pubkeys().await?.keysets.first().unwrap().clone().keys;
let keyset_id = Id::from(&keys);
Expand Down Expand Up @@ -192,7 +180,7 @@ async fn test_attempt_to_swap_by_overflowing() -> Result<()> {

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
pub async fn test_p2pk_swap() -> Result<()> {
let mint = initialize().await;
let mint = new_mint(0).await;

let keys = mint.pubkeys().await?.keysets.first().unwrap().clone().keys;
let keyset_id = Id::from(&keys);
Expand Down Expand Up @@ -310,7 +298,7 @@ pub async fn test_p2pk_swap() -> Result<()> {

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_swap_unbalanced() -> Result<()> {
let mint = initialize().await;
let mint = new_mint(0).await;

let keys = mint.pubkeys().await?.keysets.first().unwrap().clone().keys;
let keyset_id = Id::from(&keys);
Expand Down

0 comments on commit 1fe6b06

Please sign in to comment.