-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathmod.rs
More file actions
277 lines (247 loc) · 8.98 KB
/
Copy pathmod.rs
File metadata and controls
277 lines (247 loc) · 8.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//! Macro with default tests
//!
//! This set is generic and checks the default and expected behaviour for a mint database
//! implementation
#![allow(clippy::unwrap_used, clippy::missing_panics_doc)]
use std::str::FromStr;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};
// For derivation path parsing
use bitcoin::bip32::DerivationPath;
use cashu::CurrencyUnit;
use super::*;
use crate::database::KVStoreDatabase;
use crate::mint::MintKeySetInfo;
mod keys;
mod mint;
mod proofs;
mod saga;
mod signatures;
pub use self::keys::*;
pub use self::mint::*;
pub use self::proofs::*;
pub use self::saga::*;
pub use self::signatures::*;
/// Generate standard keyset amounts as powers of 2
#[inline]
fn standard_keyset_amounts(max_order: u32) -> Vec<u64> {
(0..max_order).map(|n| 2u64.pow(n)).collect()
}
#[inline]
async fn setup_keyset<DB>(db: &DB) -> Id
where
DB: KeysDatabase<Err = crate::database::Error>,
{
let keyset_id = Id::from_str("00916bbf7ef91a36").unwrap();
let keyset_info = MintKeySetInfo {
id: keyset_id,
unit: CurrencyUnit::Sat,
active: true,
valid_from: 0,
final_expiry: None,
derivation_path: DerivationPath::from_str("m/0'/0'/0'").unwrap(),
derivation_path_index: Some(0),
input_fee_ppk: 0,
amounts: standard_keyset_amounts(32),
};
let mut writer = db.begin_transaction().await.expect("db.begin()");
writer.add_keyset_info(keyset_info).await.unwrap();
writer.commit().await.expect("commit()");
keyset_id
}
/// Test KV store functionality including write, read, list, update, and remove operations
pub async fn kvstore_functionality<DB>(db: DB)
where
DB: Database<crate::database::Error> + KVStoreDatabase<Err = crate::database::Error>,
{
// Test basic read/write operations in transaction
{
let mut tx = Database::begin_transaction(&db).await.unwrap();
// Write some test data
tx.kv_write("test_namespace", "sub_namespace", "key1", b"value1")
.await
.unwrap();
tx.kv_write("test_namespace", "sub_namespace", "key2", b"value2")
.await
.unwrap();
tx.kv_write("test_namespace", "other_sub", "key3", b"value3")
.await
.unwrap();
// Read back the data in the transaction
let value1 = tx
.kv_read("test_namespace", "sub_namespace", "key1")
.await
.unwrap();
assert_eq!(value1, Some(b"value1".to_vec()));
// List keys in namespace
let keys = tx.kv_list("test_namespace", "sub_namespace").await.unwrap();
assert_eq!(keys, vec!["key1", "key2"]);
// Commit transaction
tx.commit().await.unwrap();
}
// Test read operations after commit
{
let value1 = db
.kv_read("test_namespace", "sub_namespace", "key1")
.await
.unwrap();
assert_eq!(value1, Some(b"value1".to_vec()));
let keys = db.kv_list("test_namespace", "sub_namespace").await.unwrap();
assert_eq!(keys, vec!["key1", "key2"]);
let other_keys = db.kv_list("test_namespace", "other_sub").await.unwrap();
assert_eq!(other_keys, vec!["key3"]);
}
// Test update and remove operations
{
let mut tx = Database::begin_transaction(&db).await.unwrap();
// Update existing key
tx.kv_write("test_namespace", "sub_namespace", "key1", b"updated_value1")
.await
.unwrap();
// Remove a key
tx.kv_remove("test_namespace", "sub_namespace", "key2")
.await
.unwrap();
tx.commit().await.unwrap();
}
// Verify updates
{
let value1 = db
.kv_read("test_namespace", "sub_namespace", "key1")
.await
.unwrap();
assert_eq!(value1, Some(b"updated_value1".to_vec()));
let value2 = db
.kv_read("test_namespace", "sub_namespace", "key2")
.await
.unwrap();
assert_eq!(value2, None);
let keys = db.kv_list("test_namespace", "sub_namespace").await.unwrap();
assert_eq!(keys, vec!["key1"]);
}
}
static COUNTER: AtomicU64 = AtomicU64::new(0);
/// Returns a unique, random-looking Base62 string (no external crates).
/// Not cryptographically secure, but great for ids, keys, temp names, etc.
fn unique_string() -> String {
// 1) high-res timestamp (nanos since epoch)
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos();
// 2) per-process monotonic counter to avoid collisions in the same instant
let n = COUNTER.fetch_add(1, Ordering::Relaxed) as u128;
// 3) process id to reduce collision chance across processes
let pid = std::process::id() as u128;
// Mix the components (simple XOR/shift mix; good enough for "random-looking")
let mixed = now ^ (pid << 64) ^ (n << 32);
base62_encode(mixed)
}
fn base62_encode(mut x: u128) -> String {
const ALPHABET: &[u8; 62] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
if x == 0 {
return "0".to_string();
}
let mut buf = [0u8; 26]; // enough for base62(u128)
let mut i = buf.len();
while x > 0 {
let rem = (x % 62) as usize;
x /= 62;
i -= 1;
buf[i] = ALPHABET[rem];
}
String::from_utf8_lossy(&buf[i..]).into_owned()
}
/// Unit test that is expected to be passed for a correct database implementation
#[macro_export]
macro_rules! mint_db_test {
($make_db_fn:ident) => {
mint_db_test!(
$make_db_fn,
add_and_find_proofs,
add_duplicate_proofs,
kvstore_functionality,
add_mint_quote,
add_mint_quote_only_once,
register_payments,
read_mint_from_db_and_tx,
get_proofs_by_keyset_id,
reject_duplicate_payments_same_tx,
reject_duplicate_payments_diff_tx,
reject_over_issue_same_tx,
reject_over_issue_different_tx,
reject_over_issue_with_payment,
reject_over_issue_with_payment_different_tx,
add_melt_request_unique_blinded_messages,
reject_melt_duplicate_blinded_signature,
reject_duplicate_blinded_message_db_constraint,
cleanup_melt_request_after_processing,
add_and_get_melt_quote,
add_melt_quote_only_once,
update_melt_quote_state_transition,
update_melt_quote_request_lookup_id,
get_all_mint_quotes,
get_all_melt_quotes,
get_mint_quote_by_request,
get_mint_quote_by_request_lookup_id,
delete_blinded_messages,
add_and_get_blind_signatures,
get_blind_signatures_for_keyset,
get_blind_signatures_for_quote,
get_total_issued,
get_total_fees_collected,
get_nonexistent_blind_signatures,
add_duplicate_blind_signatures,
add_and_get_keyset_info,
add_duplicate_keyset_info,
get_all_keyset_infos,
set_and_get_active_keyset,
get_all_active_keysets,
update_active_keyset,
get_nonexistent_keyset_info,
get_active_keyset_when_none_set,
get_proofs_states,
get_nonexistent_proof_states,
get_proofs_by_nonexistent_ys,
proof_transaction_isolation,
proof_rollback,
multiple_proofs_same_keyset,
add_and_get_saga,
add_duplicate_saga,
update_saga_state,
delete_saga,
get_incomplete_swap_sagas,
get_incomplete_melt_sagas,
get_nonexistent_saga,
update_nonexistent_saga,
delete_nonexistent_saga,
saga_with_quote_id,
saga_transaction_rollback,
multiple_sagas_different_states,
increment_mint_quote_amount_paid,
increment_mint_quote_amount_issued,
get_mint_quote_in_transaction,
get_melt_quote_in_transaction,
get_mint_quote_by_request_in_transaction,
get_mint_quote_by_request_lookup_id_in_transaction,
get_blind_signatures_in_transaction,
reject_duplicate_payment_ids,
remove_spent_proofs_should_fail,
get_proofs_with_inconsistent_states_fails,
get_proofs_fails_when_some_not_found,
update_proofs_state_updates_proofs_with_state,
);
};
($make_db_fn:ident, $($name:ident),+ $(,)?) => {
$(
#[tokio::test]
async fn $name() {
use std::time::{SystemTime, UNIX_EPOCH};
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Time went backwards");
cdk_common::database::mint::test::$name($make_db_fn(format!("test_{}_{}", now.as_nanos(), stringify!($name))).await).await;
}
)+
};
}