فهرست منبع

Extract standard keyset amounts generation to reusable function

Consolidates repeated pattern of `(0..32).map(|n| 2u64.pow(n)).collect()` into
a single reusable helper function. This reduces code duplication and makes the
keyset amount generation pattern more maintainable.
Cesar Rodas 2 ماه پیش
والد
کامیت
529b4f9a95

+ 7 - 1
crates/cdk-common/src/database/mint/test/mod.rs

@@ -22,6 +22,12 @@ mod proofs;
 pub use self::mint::*;
 pub use self::proofs::*;
 
+/// 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
@@ -37,7 +43,7 @@ where
         derivation_path: DerivationPath::from_str("m/0'/0'/0'").unwrap(),
         derivation_path_index: Some(0),
         input_fee_ppk: 0,
-        amounts: (0..32).map(|n| 2u64.pow(n)).collect(),
+        amounts: standard_keyset_amounts(32),
     };
     let mut writer = db.begin_transaction().await.expect("db.begin()");
     writer.add_keyset_info(keyset_info).await.unwrap();

+ 11 - 0
crates/cdk-integration-tests/src/lib.rs

@@ -36,6 +36,17 @@ pub mod init_pure_tests;
 pub mod init_regtest;
 pub mod shared;
 
+/// Generate standard keyset amounts as powers of 2
+///
+/// Returns a vector of amounts: [1, 2, 4, 8, 16, 32, ..., 2^(n-1)]
+/// where n is the number of amounts to generate.
+///
+/// # Arguments
+/// * `max_order` - The maximum power of 2 (exclusive). For example, max_order=32 generates amounts up to 2^31
+pub fn standard_keyset_amounts(max_order: u32) -> Vec<u64> {
+    (0..max_order).map(|n| 2u64.pow(n)).collect()
+}
+
 pub async fn fund_wallet(wallet: Arc<Wallet>, amount: Amount) {
     let quote = wallet
         .mint_quote(amount, None)

+ 15 - 3
crates/cdk-integration-tests/tests/integration_tests_pure.rs

@@ -565,7 +565,11 @@ async fn test_swap_overpay_underpay_fee() {
         .expect("Failed to create test mint");
 
     mint_bob
-        .rotate_keyset(CurrencyUnit::Sat, (0..32).map(|n| 2u64.pow(n)).collect(), 1)
+        .rotate_keyset(
+            CurrencyUnit::Sat,
+            cdk_integration_tests::standard_keyset_amounts(32),
+            1,
+        )
         .await
         .unwrap();
 
@@ -640,7 +644,11 @@ async fn test_mint_enforce_fee() {
         .expect("Failed to create test mint");
 
     mint_bob
-        .rotate_keyset(CurrencyUnit::Sat, (0..32).map(|n| 2u64.pow(n)).collect(), 1)
+        .rotate_keyset(
+            CurrencyUnit::Sat,
+            cdk_integration_tests::standard_keyset_amounts(32),
+            1,
+        )
         .await
         .unwrap();
 
@@ -749,7 +757,11 @@ async fn test_mint_change_with_fee_melt() {
         .expect("Failed to create test mint");
 
     mint_bob
-        .rotate_keyset(CurrencyUnit::Sat, (0..32).map(|n| 2u64.pow(n)).collect(), 1)
+        .rotate_keyset(
+            CurrencyUnit::Sat,
+            cdk_integration_tests::standard_keyset_amounts(32),
+            1,
+        )
         .await
         .unwrap();
 

+ 14 - 6
crates/cdk-integration-tests/tests/mint.rs

@@ -74,9 +74,13 @@ async fn test_correct_keyset() {
         .expect("There is a keyset for unit");
     let old_keyset_info = mint.get_keyset_info(active).expect("There is keyset");
 
-    mint.rotate_keyset(CurrencyUnit::Sat, (0..32).map(|n| 2u64.pow(n)).collect(), 0)
-        .await
-        .unwrap();
+    mint.rotate_keyset(
+        CurrencyUnit::Sat,
+        cdk_integration_tests::standard_keyset_amounts(32),
+        0,
+    )
+    .await
+    .unwrap();
 
     let active = mint.get_active_keysets();
 
@@ -88,9 +92,13 @@ async fn test_correct_keyset() {
 
     assert_ne!(keyset_info.id, old_keyset_info.id);
 
-    mint.rotate_keyset(CurrencyUnit::Sat, (0..32).map(|n| 2u64.pow(n)).collect(), 0)
-        .await
-        .unwrap();
+    mint.rotate_keyset(
+        CurrencyUnit::Sat,
+        cdk_integration_tests::standard_keyset_amounts(32),
+        0,
+    )
+    .await
+    .unwrap();
 
     let active = mint.get_active_keysets();
 

+ 14 - 6
crates/cdk-integration-tests/tests/test_swap_flow.rs

@@ -657,9 +657,13 @@ async fn test_swap_with_fees() {
         .expect("Failed to create test wallet");
 
     // Rotate to keyset with 1 sat per proof fee
-    mint.rotate_keyset(CurrencyUnit::Sat, (0..32).map(|n| 2u64.pow(n)).collect(), 1)
-        .await
-        .expect("Failed to rotate keyset");
+    mint.rotate_keyset(
+        CurrencyUnit::Sat,
+        cdk_integration_tests::standard_keyset_amounts(32),
+        1,
+    )
+    .await
+    .expect("Failed to rotate keyset");
 
     // Fund with 1000 sats as individual 1-sat proofs using the fee-based keyset
     // Wait a bit for keyset to be available
@@ -973,9 +977,13 @@ async fn test_wallet_multi_keyset_counter_updates() {
     let first_keyset_id = get_keyset_id(&mint).await;
 
     // Rotate to a second keyset
-    mint.rotate_keyset(CurrencyUnit::Sat, (0..32).map(|n| 2u64.pow(n)).collect(), 0)
-        .await
-        .expect("Failed to rotate keyset");
+    mint.rotate_keyset(
+        CurrencyUnit::Sat,
+        cdk_integration_tests::standard_keyset_amounts(32),
+        0,
+    )
+    .await
+    .expect("Failed to rotate keyset");
 
     // Wait for keyset rotation to propagate
     tokio::time::sleep(std::time::Duration::from_millis(100)).await;

+ 1 - 1
crates/cdk-mint-rpc/src/proto/server.rs

@@ -731,7 +731,7 @@ impl CdkMint for MintRPCServer {
             .map_err(|_| Status::invalid_argument("Invalid unit".to_string()))?;
 
         let amounts = if request.amounts.is_empty() {
-            (0..32).map(|n| 2u64.pow(n)).collect()
+            return Err(Status::invalid_argument("amounts cannot be empty"));
         } else {
             request.amounts
         };