Skip to content

Commit

Permalink
refactor: test find lowest unused short id
Browse files Browse the repository at this point in the history
  • Loading branch information
j4m1ef0rd committed Dec 5, 2024
1 parent 051dc72 commit d4af5c8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 27 deletions.
77 changes: 52 additions & 25 deletions api/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ pub trait BrokerApi: SignedExtrinsicApi + StorageApi + Sized + Send + Sync + 'st
affiliate_id: AccountId32,
short_id: Option<AffiliateShortId>,
) -> Result<AffiliateShortId> {
let next_id = if let Some(short_id) = short_id {
let register_as_id = if let Some(short_id) = short_id {
short_id
} else {
let affiliates =
Expand All @@ -534,33 +534,17 @@ pub trait BrokerApi: SignedExtrinsicApi + StorageApi + Sized + Send + Sync + 'st
return Ok(*existing_short_id);
}

// Find the lowest unused short id
let mut used_ids: Vec<AffiliateShortId> =
// Auto assign the lowest unused short id
let used_ids: Vec<AffiliateShortId> =
affiliates.into_iter().map(|(short_id, _)| short_id).collect();
let used_id_len = used_ids.len();
if used_ids.is_empty() {
AffiliateShortId::from(0)
} else if used_id_len > u8::MAX as usize {
bail!("No unused affiliate short IDs available")
} else {
used_ids.sort_unstable();
AffiliateShortId::from(
used_ids
.into_iter()
.enumerate()
.find(|(index, assigned_id)| {
AffiliateShortId::from(*index as u8) != *assigned_id
})
.map(|(index, _)| index)
.unwrap_or(used_id_len) as u8,
)
}
find_lowest_unused_short_id(&used_ids)?
};

let (_, events, ..) =
self.submit_signed_extrinsic_with_dry_run(
pallet_cf_swapping::Call::register_affiliate { affiliate_id, short_id: next_id },
)
let (_, events, ..) = self
.submit_signed_extrinsic_with_dry_run(pallet_cf_swapping::Call::register_affiliate {
affiliate_id,
short_id: register_as_id,
})
.await?
.until_in_block()
.await?;
Expand Down Expand Up @@ -713,6 +697,26 @@ pub fn generate_ethereum_key(
))
}

fn find_lowest_unused_short_id(used_ids: &[AffiliateShortId]) -> Result<AffiliateShortId> {
let used_id_len = used_ids.len();
if used_ids.is_empty() {
Ok(AffiliateShortId::from(0))
} else if used_id_len > u8::MAX as usize {
bail!("No unused affiliate short IDs available")
} else {
let mut used_ids = used_ids.to_vec();
used_ids.sort_unstable();
Ok(AffiliateShortId::from(
used_ids
.iter()
.enumerate()
.find(|(index, assigned_id)| &AffiliateShortId::from(*index as u8) != *assigned_id)
.map(|(index, _)| index)
.unwrap_or(used_id_len) as u8,
))
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -818,4 +822,27 @@ mod tests {
);
}
}

#[test]
fn test_find_lowest_unused_short_id() {
fn test_lowest(used_ids: &mut Vec<AffiliateShortId>, expected: AffiliateShortId) {
assert_eq!(find_lowest_unused_short_id(used_ids).unwrap(), expected);
assert_eq!(
used_ids.iter().find(|id| *id == &expected),
None,
"Should not overwrite existing IDs"
);
used_ids.push(expected);
}

let mut used_ids = vec![AffiliateShortId::from(1), AffiliateShortId::from(3)];
test_lowest(&mut used_ids, AffiliateShortId::from(0));
test_lowest(&mut used_ids, AffiliateShortId::from(2));
test_lowest(&mut used_ids, AffiliateShortId::from(4));
test_lowest(&mut used_ids, AffiliateShortId::from(5));
let mut used_ids: Vec<AffiliateShortId> =
(0..u8::MAX).map(AffiliateShortId::from).collect();
test_lowest(&mut used_ids, AffiliateShortId::from(255));
assert!(find_lowest_unused_short_id(&used_ids).is_err());
}
}
4 changes: 2 additions & 2 deletions bouncer/shared/send_btc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function fundAndSendTransaction(
}

export async function sendVaultTransaction(
nulldata_payload: string,
nulldataPayload: string,
amountBtc: number,
depositAddress: string,
refundAddress: string,
Expand All @@ -47,7 +47,7 @@ export async function sendVaultTransaction(
[depositAddress]: amountBtc,
},
{
data: nulldata_payload.replace('0x', ''),
data: nulldataPayload.replace('0x', ''),
},
],
refundAddress,
Expand Down

0 comments on commit d4af5c8

Please sign in to comment.