Skip to content

Commit

Permalink
Fix f64 serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibo-lg committed Jul 10, 2024
1 parent 214ff35 commit c7ad23b
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions dlc-messages/src/ser_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,25 +174,22 @@ pub fn write_f64<W: lightning::util::ser::Writer>(
input: f64,
writer: &mut W,
) -> Result<(), ::lightning::io::Error> {
let sign = input >= 0.0;
sign.write(writer)?;
let input_abs = f64::abs(input);
let no_precision = f64::floor(input_abs);
(no_precision as u64).write(writer)?;
let extra_precision = f64::floor((input_abs - no_precision) * ((1 << 16) as f64)) as u16;
extra_precision.write(writer)
for b in input.to_be_bytes() {
b.write(writer)?;
}

Ok(())
}

/// Reads an `f64` value from the given reader.
pub fn read_f64<R: ::lightning::io::Read>(
reader: &mut R,
) -> Result<f64, lightning::ln::msgs::DecodeError> {
let sign: bool = Readable::read(reader)?;
let no_precision: u64 = Readable::read(reader)?;
let extra_precision: u16 = Readable::read(reader)?;
let mul_sign: f64 = if sign { 1.0 } else { -1.0 };

Ok(((no_precision as f64) + ((extra_precision as f64) / ((1 << 16) as f64))) * mul_sign)
let mut buf = [0u8; 8];
for b in &mut buf {
*b = Readable::read(reader)?;
}
Ok(f64::from_be_bytes(buf))
}

/// Writes a [`secp256k1_zkp::schnorrsig::Signature`] value to the given writer.
Expand Down Expand Up @@ -639,3 +636,20 @@ impl_dlc_writeable_external!(PartyParams, party_params, {
(input_amount, writeable),
(collateral, writeable)
});

#[cfg(test)]
mod tests {
use std::io::Cursor;

use super::{read_f64, write_f64};

#[test]
fn f64_serialize_round_trip() {
let original = 2.3;
let mut ser = Vec::new();
write_f64(original, &mut ser).unwrap();
let deser = read_f64(&mut Cursor::new(&mut ser)).unwrap();

assert_eq!(original, deser);
}
}

0 comments on commit c7ad23b

Please sign in to comment.