-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathshared.rs
More file actions
366 lines (332 loc) · 12 KB
/
Copy pathshared.rs
File metadata and controls
366 lines (332 loc) · 12 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//! Shared utilities for mint integration tests
//!
//! This module provides common functionality used across different
//! integration test binaries to reduce code duplication.
use std::fs;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;
use anyhow::Result;
use cdk_axum::cache;
use cdk_mintd::config::{Database, DatabaseEngine};
use tokio::signal;
use tokio::sync::Notify;
use tokio_util::sync::CancellationToken;
use crate::cli::{init_logging, CommonArgs};
/// Default minimum mint amount for test mints
const DEFAULT_MIN_MINT: u64 = 1;
/// Default maximum mint amount for test mints
const DEFAULT_MAX_MINT: u64 = 500_000;
/// Default minimum melt amount for test mints
const DEFAULT_MIN_MELT: u64 = 1;
/// Default maximum melt amount for test mints
const DEFAULT_MAX_MELT: u64 = 500_000;
/// Wait for mint to be ready by checking its info endpoint, with optional shutdown signal
pub async fn wait_for_mint_ready_with_shutdown(
port: u16,
timeout_secs: u64,
shutdown_notify: Arc<CancellationToken>,
) -> Result<()> {
let url = format!("http://127.0.0.1:{port}/v1/info");
let start_time = std::time::Instant::now();
println!("Waiting for mint on port {port} to be ready...");
loop {
// Check if timeout has been reached
if start_time.elapsed().as_secs() > timeout_secs {
return Err(anyhow::anyhow!("Timeout waiting for mint on port {}", port));
}
if shutdown_notify.is_cancelled() {
return Err(anyhow::anyhow!("Canceled waiting for {}", port));
}
tokio::select! {
// Try to make a request to the mint info endpoint
result = reqwest::get(&url) => {
match result {
Ok(response) => {
if response.status().is_success() {
println!("Mint on port {port} is ready");
return Ok(());
} else {
println!(
"Mint on port {} returned status: {}",
port,
response.status()
);
}
}
Err(e) => {
println!("Error connecting to mint on port {port}: {e}");
}
}
}
// Check for shutdown signal
_ = shutdown_notify.cancelled() => {
return Err(anyhow::anyhow!(
"Shutdown requested while waiting for mint on port {}",
port
));
}
}
}
}
/// Initialize working directory
pub fn init_working_directory(work_dir: &str) -> Result<PathBuf> {
let temp_dir = PathBuf::from_str(work_dir)?;
// Create the temp directory if it doesn't exist
fs::create_dir_all(&temp_dir)?;
Ok(temp_dir)
}
/// Write environment variables to .env file
pub fn write_env_file(temp_dir: &Path, env_vars: &[(&str, &str)]) -> Result<()> {
let mut env_content = String::new();
for (key, value) in env_vars {
env_content.push_str(&format!("{key}={value}\n"));
}
let env_file_path = temp_dir.join(".env");
fs::write(&env_file_path, &env_content)
.map(|_| {
println!(
"Environment variables written to: {}",
env_file_path.display()
);
})
.map_err(|e| anyhow::anyhow!("Could not write .env file: {}", e))
}
/// Wait for .env file to be created
pub async fn wait_for_env_file(temp_dir: &Path, timeout_secs: u64) -> Result<()> {
let env_file_path = temp_dir.join(".env");
let start_time = std::time::Instant::now();
println!(
"Waiting for .env file to be created at: {}",
env_file_path.display()
);
loop {
// Check if timeout has been reached
if start_time.elapsed().as_secs() > timeout_secs {
return Err(anyhow::anyhow!(
"Timeout waiting for .env file at {}",
env_file_path.display()
));
}
// Check if the file exists
if env_file_path.exists() {
println!(".env file found at: {}", env_file_path.display());
return Ok(());
}
tokio::time::sleep(Duration::from_secs(1)).await;
}
}
/// Setup common logging based on CLI arguments
pub fn setup_logging(common_args: &CommonArgs) {
init_logging(common_args.enable_logging, common_args.log_level);
}
/// Create shutdown handler for graceful termination
pub fn create_shutdown_handler() -> Arc<Notify> {
Arc::new(Notify::new())
}
/// Wait for Ctrl+C signal
pub async fn wait_for_shutdown_signal(shutdown: Arc<Notify>) {
signal::ctrl_c()
.await
.expect("failed to install CTRL+C handler");
println!("\nReceived Ctrl+C, shutting down...");
shutdown.notify_waiters();
}
/// Common mint information display
pub fn display_mint_info(port: u16, temp_dir: &Path, database_type: &str) {
println!("Mint started successfully!");
println!("Mint URL: http://127.0.0.1:{port}");
println!("Temp directory: {temp_dir:?}");
println!("Database type: {database_type}");
}
/// Create settings for a fake wallet mint
pub fn create_fake_wallet_settings(
port: u16,
database: &str,
mnemonic: Option<String>,
signatory_config: Option<(String, String)>, // (url, certs_dir)
fake_wallet_config: Option<cdk_mintd::config::FakeWallet>,
) -> cdk_mintd::config::Settings {
cdk_mintd::config::Settings {
info: cdk_mintd::config::Info {
url: format!("http://127.0.0.1:{port}"),
quote_ttl: None,
listen_host: "127.0.0.1".to_string(),
listen_port: port,
seed: None,
mnemonic,
signatory_url: signatory_config.as_ref().map(|(url, _)| url.clone()),
signatory_certs: signatory_config
.as_ref()
.map(|(_, certs_dir)| certs_dir.clone()),
input_fee_ppk: None,
http_cache: cache::Config::default(),
logging: cdk_mintd::config::LoggingConfig {
output: cdk_mintd::config::LoggingOutput::Both,
console_level: Some("debug".to_string()),
file_level: Some("debug".to_string()),
},
enable_swagger_ui: None,
},
mint_info: cdk_mintd::config::MintInfo::default(),
payment_backend: cdk_mintd::config::PaymentBackend {
kind: cdk_mintd::config::PaymentBackendKind::FakeWallet,
ln_backend: cdk_mintd::config::PaymentBackendKind::None,
invoice_description: None,
min_mint: DEFAULT_MIN_MINT.into(),
max_mint: DEFAULT_MAX_MINT.into(),
min_melt: DEFAULT_MIN_MELT.into(),
max_melt: DEFAULT_MAX_MELT.into(),
fake_wallet: fake_wallet_config,
cln: None,
lnd: None,
lnbits: None,
ldk_node: None,
grpc_processor: None,
},
database: Database {
engine: DatabaseEngine::from_str(database).expect("valid database"),
postgres: None,
},
auth_database: None,
mint_management_rpc: None,
auth: None,
prometheus: Some(Default::default()),
cln: None,
lnbits: None,
lnd: None,
ldk_node: None,
fake_wallet: None,
grpc_processor: None,
using_deprecated_config: None,
ln: cdk_mintd::config::PaymentBackend {
ln_backend: cdk_mintd::config::PaymentBackendKind::LdkNode,
invoice_description: None,
min_mint: 1.into(),
max_mint: 500_000.into(),
min_melt: 1.into(),
max_melt: 500_000.into(),
..Default::default()
},
}
}
/// Create settings for a CLN mint
pub fn create_cln_settings(
port: u16,
_cln_rpc_path: PathBuf,
mnemonic: String,
cln_config: cdk_mintd::config::Cln,
) -> cdk_mintd::config::Settings {
cdk_mintd::config::Settings {
info: cdk_mintd::config::Info {
url: format!("http://127.0.0.1:{port}"),
quote_ttl: None,
listen_host: "127.0.0.1".to_string(),
listen_port: port,
seed: None,
mnemonic: Some(mnemonic),
signatory_url: None,
signatory_certs: None,
input_fee_ppk: None,
http_cache: cache::Config::default(),
logging: cdk_mintd::config::LoggingConfig {
output: cdk_mintd::config::LoggingOutput::Both,
console_level: Some("debug".to_string()),
file_level: Some("debug".to_string()),
},
enable_swagger_ui: None,
},
mint_info: cdk_mintd::config::MintInfo::default(),
payment_backend: cdk_mintd::config::PaymentBackend {
kind: cdk_mintd::config::PaymentBackendKind::Cln,
invoice_description: None,
min_mint: DEFAULT_MIN_MINT.into(),
max_mint: DEFAULT_MAX_MINT.into(),
min_melt: DEFAULT_MIN_MELT.into(),
max_melt: DEFAULT_MAX_MELT.into(),
cln: Some(cln_config.clone()),
..Default::default()
},
database: cdk_mintd::config::Database::default(),
auth_database: None,
mint_management_rpc: None,
auth: None,
prometheus: Some(Default::default()),
cln: None,
lnbits: None,
lnd: None,
ldk_node: None,
fake_wallet: None,
grpc_processor: None,
using_deprecated_config: None,
ln: cdk_mintd::config::PaymentBackend {
ln_backend: cdk_mintd::config::PaymentBackendKind::LdkNode,
invoice_description: None,
min_mint: 1.into(),
max_mint: 500_000.into(),
min_melt: 1.into(),
max_melt: 500_000.into(),
..Default::default()
},
}
}
/// Create settings for an LND mint
pub fn create_lnd_settings(
port: u16,
lnd_config: cdk_mintd::config::Lnd,
mnemonic: String,
) -> cdk_mintd::config::Settings {
cdk_mintd::config::Settings {
info: cdk_mintd::config::Info {
quote_ttl: None,
url: format!("http://127.0.0.1:{port}"),
listen_host: "127.0.0.1".to_string(),
listen_port: port,
seed: None,
mnemonic: Some(mnemonic),
signatory_url: None,
signatory_certs: None,
input_fee_ppk: None,
http_cache: cache::Config::default(),
logging: cdk_mintd::config::LoggingConfig {
output: cdk_mintd::config::LoggingOutput::Both,
console_level: Some("debug".to_string()),
file_level: Some("debug".to_string()),
},
enable_swagger_ui: None,
},
mint_info: cdk_mintd::config::MintInfo::default(),
payment_backend: cdk_mintd::config::PaymentBackend {
kind: cdk_mintd::config::PaymentBackendKind::Lnd,
invoice_description: None,
min_mint: DEFAULT_MIN_MINT.into(),
max_mint: DEFAULT_MAX_MINT.into(),
min_melt: DEFAULT_MIN_MELT.into(),
max_melt: DEFAULT_MAX_MELT.into(),
lnd: Some(lnd_config),
..Default::default()
},
database: cdk_mintd::config::Database::default(),
auth_database: None,
mint_management_rpc: None,
auth: None,
prometheus: Some(Default::default()),
cln: None,
lnbits: None,
lnd: None,
ldk_node: None,
fake_wallet: None,
grpc_processor: None,
using_deprecated_config: None,
ln: cdk_mintd::config::PaymentBackend {
ln_backend: cdk_mintd::config::PaymentBackendKind::LdkNode,
invoice_description: None,
min_mint: 1.into(),
max_mint: 500_000.into(),
min_melt: 1.into(),
max_melt: 500_000.into(),
..Default::default()
},
}
}