@@ -33,6 +33,7 @@ use crate::v3::{
33
33
WildFungibility as OldWildFungibility , WildMultiAsset as OldWildAsset ,
34
34
} ;
35
35
use alloc:: { vec, vec:: Vec } ;
36
+ use bounded_collections:: { BoundedVec , ConstU32 } ;
36
37
use core:: {
37
38
cmp:: Ordering ,
38
39
convert:: { TryFrom , TryInto } ,
@@ -562,7 +563,9 @@ impl MaxEncodedLen for Assets {
562
563
563
564
impl Decode for Assets {
564
565
fn decode < I : codec:: Input > ( input : & mut I ) -> Result < Self , parity_scale_codec:: Error > {
565
- Self :: from_sorted_and_deduplicated ( Vec :: < Asset > :: decode ( input) ?)
566
+ let bounded_instructions =
567
+ BoundedVec :: < Asset , ConstU32 < { MAX_ITEMS_IN_ASSETS as u32 } > > :: decode ( input) ?;
568
+ Self :: from_sorted_and_deduplicated ( bounded_instructions. into_inner ( ) )
566
569
. map_err ( |( ) | "Out of order" . into ( ) )
567
570
}
568
571
}
@@ -1002,4 +1005,64 @@ mod tests {
1002
1005
let r = Assets :: from_sorted_and_deduplicated ( mixed_bad) ;
1003
1006
assert ! ( r. is_err( ) ) ;
1004
1007
}
1008
+
1009
+ #[ test]
1010
+ fn reanchor_preserves_sorting ( ) {
1011
+ use super :: * ;
1012
+ use alloc:: vec;
1013
+
1014
+ let reanchor_context: Junctions = Parachain ( 2000 ) . into ( ) ;
1015
+ let dest = Location :: new ( 1 , [ ] ) ;
1016
+
1017
+ let asset_1: Asset = ( Location :: new ( 0 , [ PalletInstance ( 50 ) , GeneralIndex ( 1 ) ] ) , 10 ) . into ( ) ;
1018
+ let mut asset_1_reanchored = asset_1. clone ( ) ;
1019
+ assert ! ( asset_1_reanchored. reanchor( & dest, & reanchor_context) . is_ok( ) ) ;
1020
+ assert_eq ! (
1021
+ asset_1_reanchored,
1022
+ ( Location :: new( 0 , [ Parachain ( 2000 ) , PalletInstance ( 50 ) , GeneralIndex ( 1 ) ] ) , 10 ) . into( )
1023
+ ) ;
1024
+
1025
+ let asset_2: Asset = ( Location :: new ( 1 , [ ] ) , 10 ) . into ( ) ;
1026
+ let mut asset_2_reanchored = asset_2. clone ( ) ;
1027
+ assert ! ( asset_2_reanchored. reanchor( & dest, & reanchor_context) . is_ok( ) ) ;
1028
+ assert_eq ! ( asset_2_reanchored, ( Location :: new( 0 , [ ] ) , 10 ) . into( ) ) ;
1029
+
1030
+ let asset_3: Asset = ( Location :: new ( 1 , [ Parachain ( 1000 ) ] ) , 10 ) . into ( ) ;
1031
+ let mut asset_3_reanchored = asset_3. clone ( ) ;
1032
+ assert ! ( asset_3_reanchored. reanchor( & dest, & reanchor_context) . is_ok( ) ) ;
1033
+ assert_eq ! ( asset_3_reanchored, ( Location :: new( 0 , [ Parachain ( 1000 ) ] ) , 10 ) . into( ) ) ;
1034
+
1035
+ let mut assets: Assets = vec ! [ asset_1. clone( ) , asset_2. clone( ) , asset_3. clone( ) ] . into ( ) ;
1036
+ assert_eq ! ( assets. clone( ) , vec![ asset_1. clone( ) , asset_2. clone( ) , asset_3. clone( ) ] . into( ) ) ;
1037
+
1038
+ assert ! ( assets. reanchor( & dest, & reanchor_context) . is_ok( ) ) ;
1039
+ assert_eq ! ( assets, vec![ asset_2_reanchored, asset_3_reanchored, asset_1_reanchored] . into( ) ) ;
1040
+ }
1041
+
1042
+ #[ test]
1043
+ fn decoding_respects_limit ( ) {
1044
+ use super :: * ;
1045
+
1046
+ // Having lots of one asset will work since they are deduplicated
1047
+ let lots_of_one_asset: Assets =
1048
+ vec ! [ ( GeneralIndex ( 1 ) , 1u128 ) . into( ) ; MAX_ITEMS_IN_ASSETS + 1 ] . into ( ) ;
1049
+ let encoded = lots_of_one_asset. encode ( ) ;
1050
+ assert ! ( Assets :: decode( & mut & encoded[ ..] ) . is_ok( ) ) ;
1051
+
1052
+ // Fewer assets than the limit works
1053
+ let mut few_assets: Assets = Vec :: new ( ) . into ( ) ;
1054
+ for i in 0 ..MAX_ITEMS_IN_ASSETS {
1055
+ few_assets. push ( ( GeneralIndex ( i as u128 ) , 1u128 ) . into ( ) ) ;
1056
+ }
1057
+ let encoded = few_assets. encode ( ) ;
1058
+ assert ! ( Assets :: decode( & mut & encoded[ ..] ) . is_ok( ) ) ;
1059
+
1060
+ // Having lots of different assets will not work
1061
+ let mut too_many_different_assets: Assets = Vec :: new ( ) . into ( ) ;
1062
+ for i in 0 ..MAX_ITEMS_IN_ASSETS + 1 {
1063
+ too_many_different_assets. push ( ( GeneralIndex ( i as u128 ) , 1u128 ) . into ( ) ) ;
1064
+ }
1065
+ let encoded = too_many_different_assets. encode ( ) ;
1066
+ assert ! ( Assets :: decode( & mut & encoded[ ..] ) . is_err( ) ) ;
1067
+ }
1005
1068
}
0 commit comments