Newer
Older
let mixed_good = vec![(Here, 10).into(), (Here, *b"good").into()];
let r = Assets::from_sorted_and_deduplicated(mixed_good.clone());
assert_eq!(r, Ok(Assets(mixed_good)));
let mixed_bad = vec![(Here, *b"bad!").into(), (Here, 10).into()];
let r = Assets::from_sorted_and_deduplicated(mixed_bad);
assert!(r.is_err());
}
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
#[test]
fn reanchor_preserves_sorting() {
use super::*;
use alloc::vec;
let reanchor_context: Junctions = Parachain(2000).into();
let dest = Location::new(1, []);
let asset_1: Asset = (Location::new(0, [PalletInstance(50), GeneralIndex(1)]), 10).into();
let mut asset_1_reanchored = asset_1.clone();
assert!(asset_1_reanchored.reanchor(&dest, &reanchor_context).is_ok());
assert_eq!(
asset_1_reanchored,
(Location::new(0, [Parachain(2000), PalletInstance(50), GeneralIndex(1)]), 10).into()
);
let asset_2: Asset = (Location::new(1, []), 10).into();
let mut asset_2_reanchored = asset_2.clone();
assert!(asset_2_reanchored.reanchor(&dest, &reanchor_context).is_ok());
assert_eq!(asset_2_reanchored, (Location::new(0, []), 10).into());
let asset_3: Asset = (Location::new(1, [Parachain(1000)]), 10).into();
let mut asset_3_reanchored = asset_3.clone();
assert!(asset_3_reanchored.reanchor(&dest, &reanchor_context).is_ok());
assert_eq!(asset_3_reanchored, (Location::new(0, [Parachain(1000)]), 10).into());
let mut assets: Assets = vec![asset_1.clone(), asset_2.clone(), asset_3.clone()].into();
assert_eq!(assets.clone(), vec![asset_1.clone(), asset_2.clone(), asset_3.clone()].into());
// decoding respects limits and sorting
assert!(assets.using_encoded(|mut enc| Assets::decode(&mut enc).map(|_| ())).is_ok());
assert!(assets.reanchor(&dest, &reanchor_context).is_ok());
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
assert_eq!(assets.0, vec![asset_2_reanchored, asset_3_reanchored, asset_1_reanchored]);
// decoding respects limits and sorting
assert!(assets.using_encoded(|mut enc| Assets::decode(&mut enc).map(|_| ())).is_ok());
}
#[test]
fn prepend_preserves_sorting() {
use super::*;
use alloc::vec;
let prefix = Location::new(0, [Parachain(1000)]);
let asset_1: Asset = (Location::new(0, [PalletInstance(50), GeneralIndex(1)]), 10).into();
let mut asset_1_prepended = asset_1.clone();
assert!(asset_1_prepended.prepend_with(&prefix).is_ok());
// changes interior X2->X3
assert_eq!(
asset_1_prepended,
(Location::new(0, [Parachain(1000), PalletInstance(50), GeneralIndex(1)]), 10).into()
);
let asset_2: Asset = (Location::new(1, [PalletInstance(50), GeneralIndex(1)]), 10).into();
let mut asset_2_prepended = asset_2.clone();
assert!(asset_2_prepended.prepend_with(&prefix).is_ok());
// changes parent
assert_eq!(
asset_2_prepended,
(Location::new(0, [PalletInstance(50), GeneralIndex(1)]), 10).into()
);
let asset_3: Asset = (Location::new(2, [PalletInstance(50), GeneralIndex(1)]), 10).into();
let mut asset_3_prepended = asset_3.clone();
assert!(asset_3_prepended.prepend_with(&prefix).is_ok());
// changes parent
assert_eq!(
asset_3_prepended,
(Location::new(1, [PalletInstance(50), GeneralIndex(1)]), 10).into()
);
// `From` impl does sorting.
let mut assets: Assets = vec![asset_1, asset_2, asset_3].into();
// decoding respects limits and sorting
assert!(assets.using_encoded(|mut enc| Assets::decode(&mut enc).map(|_| ())).is_ok());
// let's do `prepend_with`
assert!(assets.prepend_with(&prefix).is_ok());
assert_eq!(assets.0, vec![asset_2_prepended, asset_1_prepended, asset_3_prepended]);
// decoding respects limits and sorting
assert!(assets.using_encoded(|mut enc| Assets::decode(&mut enc).map(|_| ())).is_ok());
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
}
#[test]
fn decoding_respects_limit() {
use super::*;
// Having lots of one asset will work since they are deduplicated
let lots_of_one_asset: Assets =
vec![(GeneralIndex(1), 1u128).into(); MAX_ITEMS_IN_ASSETS + 1].into();
let encoded = lots_of_one_asset.encode();
assert!(Assets::decode(&mut &encoded[..]).is_ok());
// Fewer assets than the limit works
let mut few_assets: Assets = Vec::new().into();
for i in 0..MAX_ITEMS_IN_ASSETS {
few_assets.push((GeneralIndex(i as u128), 1u128).into());
}
let encoded = few_assets.encode();
assert!(Assets::decode(&mut &encoded[..]).is_ok());
// Having lots of different assets will not work
let mut too_many_different_assets: Assets = Vec::new().into();
for i in 0..MAX_ITEMS_IN_ASSETS + 1 {
too_many_different_assets.push((GeneralIndex(i as u128), 1u128).into());
}
let encoded = too_many_different_assets.encode();
assert!(Assets::decode(&mut &encoded[..]).is_err());
}