Skip to content

Commit

Permalink
revert fixed point serialization, remove unused Location code
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Apr 5, 2024
1 parent 2cfdd69 commit 14cb6c5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 44 deletions.
28 changes: 25 additions & 3 deletions libs/types/src/fixed_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//! Copied over from sp_arithmetic
use parity_scale_codec::{CompactAs, Decode, Encode, MaxEncodedLen};
use serde::{Deserialize, Serialize};
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use sp_arithmetic::{
helpers_128bit::multiply_by_rational_with_rounding,
traits::{
Expand Down Expand Up @@ -473,8 +473,6 @@ pub type Quantity = FixedU128<DECIMALS_18>;
Ord,
scale_info::TypeInfo,
MaxEncodedLen,
Serialize,
Deserialize,
)]
pub struct FixedU128<const DIV: u128>(u128);

Expand Down Expand Up @@ -790,6 +788,30 @@ impl<const DIV: u128> sp_std::str::FromStr for FixedU128<DIV> {
}
}

// Manual impl `Serialize` as serde_json does not support i128.
// TODO: remove impl if issue https://github.com/serde-rs/json/issues/548 fixed.
impl<const DIV: u128> Serialize for FixedU128<DIV> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}

// Manual impl `Deserialize` as serde_json does not support i128.
// TODO: remove impl if issue https://github.com/serde-rs/json/issues/548 fixed.
impl<'de, const DIV: u128> Deserialize<'de> for FixedU128<DIV> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
use sp_std::str::FromStr;
let s = String::deserialize(deserializer)?;
FixedU128::from_str(&s).map_err(de::Error::custom)
}
}

#[cfg(test)]
mod test_fixed_u128 {
use super::*;
Expand Down
43 changes: 2 additions & 41 deletions libs/types/src/locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_core::{crypto::AccountId32, H256};
use sp_runtime::traits::{BlakeTwo256, Hash};
use staging_xcm::{v2, v3::MultiLocation, VersionedMultiLocation};
use staging_xcm::VersionedMultiLocation;

use crate::domain_address::DomainAddress;
/// Location types for destinations that can receive restricted transfers
Expand All @@ -39,22 +39,6 @@ impl From<AccountId32> for Location {
}
}

impl From<MultiLocation> for Location {
fn from(ml: MultiLocation) -> Self {
// using hash here as multilocation is significantly larger than any other enum
// type here -- 592 bytes, vs 40 bytes for domain address (next largest)
Self::XCM(BlakeTwo256::hash(&ml.encode()))
}
}

impl From<v2::MultiLocation> for Location {
fn from(ml: v2::MultiLocation) -> Self {
// using hash here as multilocation is significantly larger than any other enum
// type here -- 592 bytes, vs 40 bytes for domain address (next largest)
Self::XCM(BlakeTwo256::hash(&ml.encode()))
}
}

impl From<VersionedMultiLocation> for Location {
fn from(vml: VersionedMultiLocation) -> Self {
// using hash here as multilocation is significantly larger than any other enum
Expand All @@ -73,24 +57,10 @@ impl From<DomainAddress> for Location {
mod test {

use hex::FromHex;
use staging_xcm::v3::MultiLocation;

use super::*;

#[test]
fn from_xcm_v1_address_works() {
let xa = MultiLocation::default();
let l = Location::from(xa.clone());
assert_eq!(
l,
Location::XCM(sp_core::H256(
<[u8; 32]>::from_hex(
"9ee6dfb61a2fb903df487c401663825643bb825d41695e63df8af6162ab145a6"
)
.unwrap()
))
);
}

#[test]
fn from_xcm_versioned_address_works() {
let xa = VersionedMultiLocation::V3(MultiLocation::default());
Expand All @@ -106,15 +76,6 @@ mod test {
);
}

#[test]
fn from_xcm_versioned_address_doesnt_change_if_content_stays_same() {
let xa = staging_xcm::v2::MultiLocation::default();
let xb = staging_xcm::v3::MultiLocation::default();
let l0 = Location::from(xa.clone());
let l1 = Location::from(xb.clone());
assert_eq!(l0, l1);
}

#[test]
fn from_domain_address_works() {
let da = DomainAddress::EVM(
Expand Down

0 comments on commit 14cb6c5

Please sign in to comment.