use std::str::FromStr;
use cashu::secret::Secret;
use cashu::{Amount, Id, SecretKey};
use crate::database::mint::test::setup_keyset;
use crate::database::mint::{Database, Error, KeysDatabase, Proof, QuoteId};
use crate::mint::Operation;
use crate::state::check_state_transition;
pub async fn get_proofs_by_keyset_id<DB>(db: DB)
where
DB: Database<Error> + KeysDatabase<Err = Error>,
{
let keyset_id = setup_keyset(&db).await;
let quote_id = QuoteId::new_uuid();
let proofs = vec![
Proof {
amount: Amount::from(100),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
Proof {
amount: Amount::from(200),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
];
let mut tx = Database::begin_transaction(&db).await.unwrap();
tx.add_proofs(
proofs,
Some(quote_id),
&Operation::new_swap(Amount::ZERO, Amount::ZERO, Amount::ZERO),
)
.await
.unwrap();
assert!(tx.commit().await.is_ok());
let (proofs, states) = db.get_proofs_by_keyset_id(&keyset_id).await.unwrap();
assert_eq!(proofs.len(), 2);
assert_eq!(proofs.len(), states.len());
assert_eq!(
states
.into_iter()
.map(|s| s.map(|x| x.to_string()).unwrap_or_default())
.collect::<Vec<_>>(),
vec!["UNSPENT".to_owned(), "UNSPENT".to_owned()]
);
let keyset_id = Id::from_str("00916bbf7ef91a34").unwrap();
let (proofs, states) = db.get_proofs_by_keyset_id(&keyset_id).await.unwrap();
assert_eq!(proofs.len(), 0);
assert_eq!(proofs.len(), states.len());
}
pub async fn add_and_find_proofs<DB>(db: DB)
where
DB: Database<Error> + KeysDatabase<Err = Error>,
{
let keyset_id = setup_keyset(&db).await;
let quote_id = QuoteId::new_uuid();
let proofs = vec![
Proof {
amount: Amount::from(100),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
Proof {
amount: Amount::from(200),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
];
let mut tx = Database::begin_transaction(&db).await.unwrap();
tx.add_proofs(
proofs.clone(),
Some(quote_id.clone()),
&Operation::new_swap(Amount::ZERO, Amount::ZERO, Amount::ZERO),
)
.await
.unwrap();
assert!(tx.commit().await.is_ok());
let proofs_from_db = db.get_proofs_by_ys(&[proofs[0].c, proofs[1].c]).await;
assert!(proofs_from_db.is_ok());
assert_eq!(proofs_from_db.unwrap().len(), 2);
let proofs_from_db = db.get_proof_ys_by_quote_id("e_id).await;
assert!(proofs_from_db.is_ok());
assert_eq!(proofs_from_db.unwrap().len(), 2);
}
pub async fn add_duplicate_proofs<DB>(db: DB)
where
DB: Database<Error> + KeysDatabase<Err = Error>,
{
let keyset_id = setup_keyset(&db).await;
let quote_id = QuoteId::new_uuid();
let proofs = vec![
Proof {
amount: Amount::from(100),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
Proof {
amount: Amount::from(200),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
];
let mut tx = Database::begin_transaction(&db).await.unwrap();
tx.add_proofs(
proofs.clone(),
Some(quote_id.clone()),
&Operation::new_swap(Amount::ZERO, Amount::ZERO, Amount::ZERO),
)
.await
.unwrap();
assert!(tx.commit().await.is_ok());
let mut tx = Database::begin_transaction(&db).await.unwrap();
let result = tx
.add_proofs(
proofs.clone(),
Some(quote_id.clone()),
&Operation::new_swap(Amount::ZERO, Amount::ZERO, Amount::ZERO),
)
.await;
assert!(
matches!(result.unwrap_err(), Error::Duplicate),
"Duplicate entry"
);
}
pub async fn update_proofs_states<DB>(db: DB)
where
DB: Database<Error> + KeysDatabase<Err = Error>,
{
use cashu::State;
let keyset_id = setup_keyset(&db).await;
let quote_id = QuoteId::new_uuid();
let proofs = vec![
Proof {
amount: Amount::from(100),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
Proof {
amount: Amount::from(200),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
];
let ys: Vec<_> = proofs.iter().map(|p| p.c).collect();
let mut tx = Database::begin_transaction(&db).await.unwrap();
tx.add_proofs(
proofs.clone(),
Some(quote_id),
&Operation::new_swap(Amount::ZERO, Amount::ZERO, Amount::ZERO),
)
.await
.unwrap();
tx.commit().await.unwrap();
let states = db.get_proofs_states(&ys).await.unwrap();
assert_eq!(states.len(), 2);
assert!(states[0].is_some());
assert!(states[1].is_some());
let mut tx = Database::begin_transaction(&db).await.unwrap();
let mut proofs = tx.get_proofs(&ys).await.unwrap();
check_state_transition(proofs.state, State::Pending).unwrap();
tx.update_proofs_state(&mut proofs, State::Pending)
.await
.unwrap();
tx.commit().await.unwrap();
let states = db.get_proofs_states(&ys).await.unwrap();
assert_eq!(states[0], Some(State::Pending));
assert_eq!(states[1], Some(State::Pending));
let mut tx = Database::begin_transaction(&db).await.unwrap();
let mut proofs = tx.get_proofs(&ys).await.unwrap();
check_state_transition(proofs.state, State::Spent).unwrap();
tx.update_proofs_state(&mut proofs, State::Spent)
.await
.unwrap();
tx.commit().await.unwrap();
let states = db.get_proofs_states(&ys).await.unwrap();
assert_eq!(states[0], Some(State::Spent));
assert_eq!(states[1], Some(State::Spent));
}
pub async fn update_proofs_state_updates_proofs_with_state<DB>(db: DB)
where
DB: Database<Error> + KeysDatabase<Err = Error>,
{
use cashu::State;
let keyset_id = setup_keyset(&db).await;
let quote_id = QuoteId::new_uuid();
let proofs = vec![Proof {
amount: Amount::from(100),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
}];
let ys: Vec<_> = proofs.iter().map(|p| p.y().unwrap()).collect();
let mut tx = Database::begin_transaction(&db).await.unwrap();
let mut proofs = tx
.add_proofs(
proofs.clone(),
Some(quote_id),
&Operation::new_swap(Amount::ZERO, Amount::ZERO, Amount::ZERO),
)
.await
.unwrap();
assert_eq!(proofs.state, State::Unspent);
tx.update_proofs_state(&mut proofs, State::Pending)
.await
.unwrap();
assert_eq!(
proofs.state,
State::Pending,
"ProofsWithState.state should be updated to Pending after update_proofs_state"
);
tx.commit().await.unwrap();
let mut tx = Database::begin_transaction(&db).await.unwrap();
let mut proofs = tx.get_proofs(&ys).await.unwrap();
assert_eq!(proofs.state, State::Pending);
tx.update_proofs_state(&mut proofs, State::Spent)
.await
.unwrap();
assert_eq!(
proofs.state,
State::Spent,
"ProofsWithState.state should be updated to Spent after update_proofs_state"
);
tx.commit().await.unwrap();
}
pub async fn remove_proofs<DB>(db: DB)
where
DB: Database<Error> + KeysDatabase<Err = Error>,
{
let keyset_id = setup_keyset(&db).await;
let quote_id = QuoteId::new_uuid();
let proofs = vec![
Proof {
amount: Amount::from(100),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
Proof {
amount: Amount::from(200),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
];
let ys: Vec<_> = proofs.iter().map(|p| p.c).collect();
let mut tx = Database::begin_transaction(&db).await.unwrap();
tx.add_proofs(
proofs.clone(),
Some(quote_id.clone()),
&Operation::new_swap(Amount::ZERO, Amount::ZERO, Amount::ZERO),
)
.await
.unwrap();
tx.commit().await.unwrap();
let retrieved = db.get_proofs_by_ys(&ys).await.unwrap();
assert_eq!(retrieved.len(), 2);
let found_count = retrieved.iter().filter(|p| p.is_some()).count();
assert!(found_count >= 1, "At least one proof should exist");
let mut tx = Database::begin_transaction(&db).await.unwrap();
tx.remove_proofs(&[ys[0]], Some(quote_id)).await.unwrap();
tx.commit().await.unwrap();
let retrieved = db.get_proofs_by_ys(&ys).await.unwrap();
assert_eq!(retrieved.len(), 2);
}
pub async fn get_total_redeemed<DB>(db: DB)
where
DB: Database<Error> + KeysDatabase<Err = Error>,
{
use cashu::State;
let keyset_id = setup_keyset(&db).await;
let quote_id = QuoteId::new_uuid();
let proofs = vec![
Proof {
amount: Amount::from(100),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
Proof {
amount: Amount::from(200),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
Proof {
amount: Amount::from(300),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
];
let ys: Vec<_> = proofs.iter().map(|p| p.c).collect();
let mut tx = Database::begin_transaction(&db).await.unwrap();
tx.add_proofs(
proofs.clone(),
Some(quote_id),
&Operation::new_swap(Amount::ZERO, Amount::ZERO, Amount::ZERO),
)
.await
.unwrap();
tx.commit().await.unwrap();
let mut tx = Database::begin_transaction(&db).await.unwrap();
let mut proofs = tx.get_proofs(&[ys[0], ys[1]]).await.unwrap();
check_state_transition(proofs.state, State::Pending).unwrap();
tx.update_proofs_state(&mut proofs, State::Pending)
.await
.unwrap();
tx.commit().await.unwrap();
let mut tx = Database::begin_transaction(&db).await.unwrap();
let mut proofs = tx.get_proofs(&[ys[0], ys[1]]).await.unwrap();
check_state_transition(proofs.state, State::Spent).unwrap();
tx.update_proofs_state(&mut proofs, State::Spent)
.await
.unwrap();
tx.commit().await.unwrap();
let totals = db.get_total_redeemed().await.unwrap();
let total = totals.get(&keyset_id).copied().unwrap_or(Amount::ZERO);
assert!(total >= Amount::from(300));
}
pub async fn get_proof_ys_by_quote_id<DB>(db: DB)
where
DB: Database<Error> + KeysDatabase<Err = Error>,
{
let keyset_id = setup_keyset(&db).await;
let quote_id1 = QuoteId::new_uuid();
let quote_id2 = QuoteId::new_uuid();
let proofs1 = vec![
Proof {
amount: Amount::from(100),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
Proof {
amount: Amount::from(200),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
];
let proofs2 = vec![Proof {
amount: Amount::from(300),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
}];
let expected_ys1: Vec<_> = proofs1.iter().map(|p| p.c).collect();
let expected_ys2: Vec<_> = proofs2.iter().map(|p| p.c).collect();
let mut tx = Database::begin_transaction(&db).await.unwrap();
tx.add_proofs(
proofs1.clone(),
Some(quote_id1.clone()),
&Operation::new_swap(Amount::ZERO, Amount::ZERO, Amount::ZERO),
)
.await
.unwrap();
tx.add_proofs(
proofs2.clone(),
Some(quote_id2.clone()),
&Operation::new_swap(Amount::ZERO, Amount::ZERO, Amount::ZERO),
)
.await
.unwrap();
tx.commit().await.unwrap();
let ys1 = db.get_proof_ys_by_quote_id("e_id1).await.unwrap();
assert_eq!(ys1.len(), 2);
assert!(ys1.contains(&expected_ys1[0]));
assert!(ys1.contains(&expected_ys1[1]));
let ys2 = db.get_proof_ys_by_quote_id("e_id2).await.unwrap();
assert_eq!(ys2.len(), 1);
assert!(ys2.contains(&expected_ys2[0]));
}
pub async fn get_proofs_states<DB>(db: DB)
where
DB: Database<Error> + KeysDatabase<Err = Error>,
{
use cashu::State;
let keyset_id = setup_keyset(&db).await;
let quote_id = QuoteId::new_uuid();
let proofs = vec![
Proof {
amount: Amount::from(100),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
Proof {
amount: Amount::from(200),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
];
let ys: Vec<_> = proofs.iter().map(|p| p.c).collect();
let mut tx = Database::begin_transaction(&db).await.unwrap();
tx.add_proofs(
proofs.clone(),
Some(quote_id),
&Operation::new_swap(Amount::ZERO, Amount::ZERO, Amount::ZERO),
)
.await
.unwrap();
tx.commit().await.unwrap();
let states = db.get_proofs_states(&ys).await.unwrap();
assert_eq!(states.len(), 2);
for s in states.iter().flatten() {
match s {
State::Unspent
| State::Reserved
| State::Pending
| State::Spent
| State::PendingSpent => {}
}
}
}
pub async fn get_nonexistent_proof_states<DB>(db: DB)
where
DB: Database<Error> + KeysDatabase<Err = Error>,
{
let y1 = SecretKey::generate().public_key();
let y2 = SecretKey::generate().public_key();
let states = db.get_proofs_states(&[y1, y2]).await.unwrap();
assert_eq!(states.len(), 2);
assert!(states[0].is_none());
assert!(states[1].is_none());
}
pub async fn get_proofs_by_nonexistent_ys<DB>(db: DB)
where
DB: Database<Error> + KeysDatabase<Err = Error>,
{
let y1 = SecretKey::generate().public_key();
let y2 = SecretKey::generate().public_key();
let proofs = db.get_proofs_by_ys(&[y1, y2]).await.unwrap();
assert_eq!(proofs.len(), 2);
assert!(proofs[0].is_none());
assert!(proofs[1].is_none());
}
pub async fn proof_transaction_isolation<DB>(db: DB)
where
DB: Database<Error> + KeysDatabase<Err = Error>,
{
let keyset_id = setup_keyset(&db).await;
let quote_id = QuoteId::new_uuid();
let proof = Proof {
amount: Amount::from(100),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
};
let y = proof.c;
let proofs_before = db.get_proofs_by_ys(&[y]).await.unwrap();
assert_eq!(proofs_before.len(), 1);
assert!(proofs_before[0].is_none());
let mut tx = Database::begin_transaction(&db).await.unwrap();
tx.add_proofs(
vec![proof.clone()],
Some(quote_id),
&Operation::new_swap(Amount::ZERO, Amount::ZERO, Amount::ZERO),
)
.await
.unwrap();
tx.commit().await.unwrap();
let states = db.get_proofs_states(&[y]).await.unwrap();
assert_eq!(states.len(), 1);
}
pub async fn proof_rollback<DB>(db: DB)
where
DB: Database<Error> + KeysDatabase<Err = Error>,
{
let keyset_id = setup_keyset(&db).await;
let quote_id = QuoteId::new_uuid();
let proof = Proof {
amount: Amount::from(100),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
};
let y = proof.c;
let mut tx = Database::begin_transaction(&db).await.unwrap();
tx.add_proofs(
vec![proof.clone()],
Some(quote_id),
&Operation::new_swap(Amount::ZERO, Amount::ZERO, Amount::ZERO),
)
.await
.unwrap();
tx.rollback().await.unwrap();
let proofs = db.get_proofs_by_ys(&[y]).await.unwrap();
assert_eq!(proofs.len(), 1);
assert!(proofs[0].is_none());
}
pub async fn multiple_proofs_same_keyset<DB>(db: DB)
where
DB: Database<Error> + KeysDatabase<Err = Error>,
{
let keyset_id = setup_keyset(&db).await;
let proofs: Vec<_> = (0..10)
.map(|i| Proof {
amount: Amount::from((i + 1) * 100),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
})
.collect();
let mut tx = Database::begin_transaction(&db).await.unwrap();
tx.add_proofs(
proofs.clone(),
None,
&Operation::new_swap(Amount::ZERO, Amount::ZERO, Amount::ZERO),
)
.await
.unwrap();
tx.commit().await.unwrap();
let (retrieved_proofs, states) = db.get_proofs_by_keyset_id(&keyset_id).await.unwrap();
assert!(retrieved_proofs.len() >= 10);
assert_eq!(retrieved_proofs.len(), states.len());
let total: u64 = retrieved_proofs.iter().map(|p| u64::from(p.amount)).sum();
assert!(total >= 5500); }
pub async fn remove_spent_proofs_should_fail<DB>(db: DB)
where
DB: Database<Error> + KeysDatabase<Err = Error>,
{
use cashu::State;
let keyset_id = setup_keyset(&db).await;
let quote_id = QuoteId::new_uuid();
let proofs = vec![
Proof {
amount: Amount::from(100),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
Proof {
amount: Amount::from(200),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
];
let ys: Vec<_> = proofs.iter().map(|p| p.y().unwrap()).collect();
let mut tx = Database::begin_transaction(&db).await.unwrap();
tx.add_proofs(
proofs.clone(),
Some(quote_id.clone()),
&Operation::new_swap(Amount::ZERO, Amount::ZERO, Amount::ZERO),
)
.await
.unwrap();
tx.commit().await.unwrap();
let states = db.get_proofs_states(&ys).await.unwrap();
assert_eq!(states.len(), 2);
assert_eq!(states[0], Some(State::Unspent));
assert_eq!(states[1], Some(State::Unspent));
let mut tx = Database::begin_transaction(&db).await.unwrap();
let result = tx.remove_proofs(&[ys[0]], Some(quote_id.clone())).await;
assert!(result.is_ok(), "Removing Unspent proof should succeed");
tx.rollback().await.unwrap();
let mut tx = Database::begin_transaction(&db).await.unwrap();
let mut records = tx.get_proofs(&ys).await.expect("valid records");
check_state_transition(records.state, State::Pending).unwrap();
tx.update_proofs_state(&mut records, State::Pending)
.await
.unwrap();
tx.commit().await.unwrap();
let mut tx = Database::begin_transaction(&db).await.unwrap();
let result = tx.remove_proofs(&[ys[0]], Some(quote_id.clone())).await;
assert!(
result.is_ok(),
"Removing Pending proof should succeed: {:?}",
result,
);
tx.rollback().await.unwrap();
let mut tx = Database::begin_transaction(&db).await.unwrap();
let mut records = tx.get_proofs(&ys).await.expect("valid records");
check_state_transition(records.state, State::Spent).unwrap();
tx.update_proofs_state(&mut records, State::Spent)
.await
.unwrap();
tx.commit().await.unwrap();
let states = db.get_proofs_states(&ys).await.unwrap();
assert_eq!(states[0], Some(State::Spent));
assert_eq!(states[1], Some(State::Spent));
let mut tx = Database::begin_transaction(&db).await.unwrap();
let result = tx.remove_proofs(&ys, Some(quote_id.clone())).await;
assert!(
result.is_err(),
"Removing proofs in Spent state should fail"
);
assert!(
matches!(result.unwrap_err(), Error::AttemptRemoveSpentProof),
"Error should be AttemptRemoveSpentProof"
);
tx.rollback().await.unwrap();
let states = db.get_proofs_states(&ys).await.unwrap();
assert_eq!(
states[0],
Some(State::Spent),
"First proof should still exist"
);
assert_eq!(
states[1],
Some(State::Spent),
"Second proof should still exist"
);
}
pub async fn get_proofs_with_inconsistent_states_fails<DB>(db: DB)
where
DB: Database<Error> + KeysDatabase<Err = Error>,
{
use cashu::State;
let keyset_id = setup_keyset(&db).await;
let quote_id = QuoteId::new_uuid();
let proofs = vec![
Proof {
amount: Amount::from(100),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
Proof {
amount: Amount::from(200),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
Proof {
amount: Amount::from(300),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
];
let ys: Vec<_> = proofs.iter().map(|p| p.y().unwrap()).collect();
let mut tx = Database::begin_transaction(&db).await.unwrap();
tx.add_proofs(
proofs,
Some(quote_id),
&Operation::new_swap(Amount::ZERO, Amount::ZERO, Amount::ZERO),
)
.await
.unwrap();
tx.commit().await.unwrap();
let mut tx = Database::begin_transaction(&db).await.unwrap();
let mut first_two_proofs = tx.get_proofs(&ys[0..2]).await.unwrap();
check_state_transition(first_two_proofs.state, State::Pending).unwrap();
tx.update_proofs_state(&mut first_two_proofs, State::Pending)
.await
.unwrap();
tx.commit().await.unwrap();
let states = db.get_proofs_states(&ys).await.unwrap();
assert_eq!(
states[0],
Some(State::Pending),
"First proof should be Pending"
);
assert_eq!(
states[1],
Some(State::Pending),
"Second proof should be Pending"
);
assert_eq!(
states[2],
Some(State::Unspent),
"Third proof should be Unspent"
);
let mut tx = Database::begin_transaction(&db).await.unwrap();
let result = tx.get_proofs(&ys).await;
assert!(
result.is_err(),
"get_proofs should fail when proofs have inconsistent states"
);
tx.rollback().await.unwrap();
}
pub async fn get_proofs_fails_when_some_not_found<DB>(db: DB)
where
DB: Database<Error> + KeysDatabase<Err = Error>,
{
let keyset_id = setup_keyset(&db).await;
let quote_id = QuoteId::new_uuid();
let stored_proofs = vec![
Proof {
amount: Amount::from(100),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
Proof {
amount: Amount::from(200),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
},
];
let non_existent_proof = Proof {
amount: Amount::from(300),
keyset_id,
secret: Secret::generate(),
c: SecretKey::generate().public_key(),
witness: None,
dleq: None,
p2pk_e: None,
};
let stored_ys: Vec<_> = stored_proofs.iter().map(|p| p.y().unwrap()).collect();
let non_existent_y = non_existent_proof.y().unwrap();
let mut tx = Database::begin_transaction(&db).await.unwrap();
tx.add_proofs(
stored_proofs,
Some(quote_id),
&Operation::new_swap(Amount::ZERO, Amount::ZERO, Amount::ZERO),
)
.await
.unwrap();
tx.commit().await.unwrap();
let states = db.get_proofs_states(&stored_ys).await.unwrap();
assert_eq!(states.len(), 2);
assert!(states[0].is_some(), "First proof should exist");
assert!(states[1].is_some(), "Second proof should exist");
let states = db.get_proofs_states(&[non_existent_y]).await.unwrap();
assert_eq!(states[0], None, "Third proof should not exist");
let all_ys = vec![stored_ys[0], stored_ys[1], non_existent_y];
let mut tx = Database::begin_transaction(&db).await.unwrap();
let result = tx.get_proofs(&all_ys).await;
assert!(
result.is_err(),
"get_proofs should fail when some proofs don't exist (got 2 of 3)"
);
tx.rollback().await.unwrap();
}