@@ -19,15 +19,6 @@ pub(crate) type StorageKey = [u8; KEY_LEN];
1919/// Maximum nesting depth for module paths.
2020pub ( crate ) const MAX_PATH_LEN : usize = 8 ;
2121
22- /// Determines how storage keys are encoded.
23- #[ derive( Clone , Copy , Debug , PartialEq ) ]
24- pub enum KeyEncoding {
25- /// Default: fields 1-15 use legacy 4-bit u32 encoding, fields 16+ use path encoding.
26- Legacy ,
27- /// All fields use path encoding. For new contracts only.
28- V2
29- }
30-
3122/// Trait that needs to be implemented by all contract refs.
3223pub trait ContractRef {
3324 /// Creates a new instance of the Contract Ref.
@@ -51,7 +42,6 @@ pub trait ContractRef {
5142pub struct ContractEnv {
5243 path : [ u8 ; MAX_PATH_LEN ] ,
5344 path_len : u8 ,
54- encoding : KeyEncoding ,
5545 mapping_data : Vec < u8 > ,
5646 backend : Rc < RefCell < dyn ContractContext > >
5747}
@@ -64,11 +54,10 @@ impl Revertible for ContractEnv {
6454
6555impl ContractEnv {
6656 /// Creates a new ContractEnv instance.
67- pub const fn new ( encoding : KeyEncoding , backend : Rc < RefCell < dyn ContractContext > > ) -> Self {
57+ pub const fn new ( backend : Rc < RefCell < dyn ContractContext > > ) -> Self {
6858 Self {
6959 path : [ 0u8 ; MAX_PATH_LEN ] ,
7060 path_len : 0 ,
71- encoding,
7261 mapping_data : Vec :: new ( ) ,
7362 backend
7463 }
@@ -105,22 +94,18 @@ impl ContractEnv {
10594 /// - A → `[0xFF, 2, 3, 5]`, B → `[0xFF, 1, 3] ++ [5]` = `[0xFF, 1, 3, 5]` — **distinct.**
10695 pub ( crate ) fn index_bytes ( & self ) -> Vec < u8 > {
10796 let path = & self . path [ ..self . path_len as usize ] ;
108- match self . encoding {
109- // Legacy: pack indices into u32 via 4-bit shifts (e.g. path [3, 15] → 0x3F).
110- // Only used when all indices fit in a nibble, preserving old storage keys.
111- KeyEncoding :: Legacy if path. iter ( ) . all ( |& idx| idx <= 15 ) => {
112- let index: u32 = path. iter ( ) . fold ( 0u32 , |acc, & idx| ( acc << 4 ) + idx as u32 ) ;
113- index. to_be_bytes ( ) . to_vec ( )
114- }
115- // Path encoding: [0xFF, len, idx_0, idx_1, ...]. Used for fields 16+
116- // in Legacy mode or for all fields in V2 mode.
117- _ => {
118- let mut bytes = Vec :: with_capacity ( 2 + path. len ( ) ) ;
119- bytes. push ( 0xFF ) ;
120- bytes. push ( self . path_len ) ;
121- bytes. extend_from_slice ( path) ;
122- bytes
123- }
97+ // Legacy: pack indices into u32 via 4-bit shifts (e.g. path [3, 15] → 0x3F).
98+ // Only used when all indices fit in a nibble, preserving old storage keys.
99+ if path. iter ( ) . all ( |& idx| idx <= 15 ) {
100+ let index: u32 = path. iter ( ) . fold ( 0u32 , |acc, & idx| ( acc << 4 ) + idx as u32 ) ;
101+ index. to_be_bytes ( ) . to_vec ( )
102+ } else {
103+ // Path encoding: [0xFF, len, idx_0, idx_1, ...]. Used for fields 16+.
104+ let mut bytes = Vec :: with_capacity ( 2 + path. len ( ) ) ;
105+ bytes. push ( 0xFF ) ;
106+ bytes. push ( self . path_len ) ;
107+ bytes. extend_from_slice ( path) ;
108+ bytes
124109 }
125110 }
126111
@@ -153,7 +138,6 @@ impl ContractEnv {
153138 Self {
154139 path : new_path,
155140 path_len : self . path_len + 1 ,
156- encoding : self . encoding ,
157141 mapping_data : self . mapping_data . clone ( ) ,
158142 backend : self . backend . clone ( )
159143 }
@@ -550,7 +534,7 @@ mod tests {
550534 use super :: * ;
551535 use crate :: contract_context:: MockContractContext ;
552536
553- fn make_env ( encoding : KeyEncoding ) -> ContractEnv {
537+ fn make_env ( ) -> ContractEnv {
554538 let mut ctx = MockContractContext :: new ( ) ;
555539 ctx. expect_hash ( ) . returning ( |input| {
556540 let mut result = [ 0u8 ; 32 ] ;
@@ -561,16 +545,16 @@ mod tests {
561545 }
562546 result
563547 } ) ;
564- ContractEnv :: new ( encoding , Rc :: new ( RefCell :: new ( ctx) ) )
548+ ContractEnv :: new ( Rc :: new ( RefCell :: new ( ctx) ) )
565549 }
566550
567551 fn legacy_u32_for_path ( path : & [ u8 ] ) -> u32 {
568552 path. iter ( ) . fold ( 0u32 , |acc, & idx| ( acc << 4 ) + idx as u32 )
569553 }
570554
571555 #[ test]
572- fn legacy_encoding_matches_old_u32_formula ( ) {
573- let env = make_env ( KeyEncoding :: Legacy ) ;
556+ fn encoding_matches_old_u32_formula ( ) {
557+ let env = make_env ( ) ;
574558 let child = env. child ( 3 ) ;
575559 assert_eq ! (
576560 child. index_bytes( ) ,
@@ -592,7 +576,7 @@ mod tests {
592576
593577 #[ test]
594578 fn path_encoding_used_for_indices_above_15 ( ) {
595- let env = make_env ( KeyEncoding :: Legacy ) ;
579+ let env = make_env ( ) ;
596580 let child = env. child ( 3 ) . child ( 16 ) ;
597581 let bytes = child. index_bytes ( ) ;
598582 assert_eq ! ( bytes[ 0 ] , 0xFF ) ;
@@ -601,28 +585,9 @@ mod tests {
601585 assert_eq ! ( bytes[ 3 ] , 16 ) ;
602586 }
603587
604- #[ test]
605- fn v2_encoding_always_uses_path ( ) {
606- let env = make_env ( KeyEncoding :: V2 ) ;
607- let child = env. child ( 3 ) ;
608- let bytes = child. index_bytes ( ) ;
609- assert_eq ! ( bytes[ 0 ] , 0xFF ) ;
610- assert_eq ! ( bytes[ 1 ] , 1 ) ;
611- assert_eq ! ( bytes[ 2 ] , 3 ) ;
612- }
613-
614- #[ test]
615- fn v2_and_legacy_produce_different_keys_for_same_path ( ) {
616- let legacy_env = make_env ( KeyEncoding :: Legacy ) ;
617- let v2_env = make_env ( KeyEncoding :: V2 ) ;
618- let legacy_key = legacy_env. child ( 3 ) . child ( 5 ) . current_key ( ) ;
619- let v2_key = v2_env. child ( 3 ) . child ( 5 ) . current_key ( ) ;
620- assert_ne ! ( legacy_key, v2_key) ;
621- }
622-
623588 #[ test]
624589 fn no_collision_between_var_and_mapping ( ) {
625- let env = make_env ( KeyEncoding :: Legacy ) ;
590+ let env = make_env ( ) ;
626591
627592 let var_key = env. child ( 3 ) . child ( 16 ) . current_key ( ) ;
628593 let mut map_env = env. child ( 3 ) ;
@@ -638,10 +603,10 @@ mod tests {
638603 }
639604
640605 #[ test]
641- fn no_collision_between_legacy_and_path_encoding ( ) {
642- let env = make_env ( KeyEncoding :: Legacy ) ;
643- let legacy_key = env. child ( 1 ) . child ( 2 ) . current_key ( ) ;
606+ fn no_collision_between_small_and_path_encoding ( ) {
607+ let env = make_env ( ) ;
608+ let small_key = env. child ( 1 ) . child ( 2 ) . current_key ( ) ;
644609 let path_key = env. child ( 1 ) . child ( 20 ) . current_key ( ) ;
645- assert_ne ! ( legacy_key , path_key) ;
610+ assert_ne ! ( small_key , path_key) ;
646611 }
647612}
0 commit comments