Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BIP21 parser: add tests that cover rounding errors #1073

Merged
merged 6 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ jobs:

- name: Run sdk-common tests
working-directory: libs/sdk-common
run: cargo test
run: cargo test --features liquid

- name: Check git status
env:
Expand Down
28 changes: 28 additions & 0 deletions libs/sdk-common/src/input_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,34 @@ pub(crate) mod tests {
Ok(())
}

/// BIP21 amounts which can lead to rounding errors.
/// The format is: (sat amount, BIP21 BTC amount)
pub(crate) fn get_bip21_rounding_test_vectors() -> Vec<(u64, f64)> {
vec![
(999, 0.0000_0999),
(1_000, 0.0000_1000),
(59_810, 0.0005_9810),
]
}

#[tokio::test]
async fn test_bitcoin_address_bip21_rounding() -> Result<()> {
for (amount_sat, amount_btc) in get_bip21_rounding_test_vectors() {
let addr = format!("bitcoin:1andreas3batLhQa2FawWjeyjCqyBzypd?amount={amount_btc}");

match parse(&addr).await? {
InputType::BitcoinAddress {
address: addr_with_amount_parsed,
} => {
assert_eq!(addr_with_amount_parsed.amount_sat, Some(amount_sat));
}
_ => return Err(anyhow!("Invalid type parsed")),
}
}

Ok(())
}

#[tokio::test]
#[cfg(feature = "liquid")]
async fn test_liquid_address() -> Result<()> {
Expand Down
27 changes: 27 additions & 0 deletions libs/sdk-common/src/liquid/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
pub mod bip21;
pub use bip21::*;

#[cfg(test)]
mod tests {
use anyhow::{anyhow, Result};

use crate::input_parser::tests::get_bip21_rounding_test_vectors;
use crate::input_parser::*;

#[tokio::test]
async fn test_liquid_address_bip21_rounding() -> Result<()> {
let asset_id = elements::issuance::AssetId::LIQUID_BTC.to_string();
for (amount_sat, amount_btc) in get_bip21_rounding_test_vectors() {
let addr = format!("liquidnetwork:tlq1qqw5ur50rnvcx33vmljjtnez3hrtl6n7vs44tdj2c9fmnxrrgzgwnhw6jtpn8cljkmlr8tgfw9hemrr5y8u2nu024hhak3tpdk?amount={amount_btc}&assetid={asset_id}");

match parse(&addr).await? {
InputType::LiquidAddress {
address: addr_with_amount_parsed,
} => {
assert_eq!(addr_with_amount_parsed.amount_sat, Some(amount_sat));
}
_ => return Err(anyhow!("Invalid type parsed")),
}
}

Ok(())
}
}
Loading