@@ -6,7 +6,6 @@ use cosmwasm_std::Record;
66use cosmwasm_std:: { from_slice, StdResult } ;
77
88use crate :: de:: KeyDeserialize ;
9- use crate :: helpers:: encode_length;
109
1110#[ allow( dead_code) ]
1211pub ( crate ) fn deserialize_v < T : DeserializeOwned > ( kv : Record ) -> StdResult < Record < T > > {
@@ -24,16 +23,6 @@ pub(crate) fn deserialize_kv<K: KeyDeserialize, T: DeserializeOwned>(
2423 Ok ( ( kt, vt) )
2524}
2625
27- /// Calculates the raw key prefix for a given namespace as documented
28- /// in https://github.com/webmaster128/key-namespacing#length-prefixed-keys
29- #[ allow( dead_code) ]
30- pub ( crate ) fn to_length_prefixed ( namespace : & [ u8 ] ) -> Vec < u8 > {
31- let mut out = Vec :: with_capacity ( namespace. len ( ) + 2 ) ;
32- out. extend_from_slice ( & encode_length ( namespace) ) ;
33- out. extend_from_slice ( namespace) ;
34- out
35- }
36-
3726// TODO: add a check here that it is the real prefix?
3827#[ inline]
3928pub ( crate ) fn trim ( namespace : & [ u8 ] , key : & [ u8 ] ) -> Vec < u8 > {
@@ -47,60 +36,6 @@ pub(crate) fn concat(namespace: &[u8], key: &[u8]) -> Vec<u8> {
4736 k
4837}
4938
50- #[ cfg( test) ]
51- mod test {
52- use super :: * ;
53-
54- #[ test]
55- fn to_length_prefixed_works ( ) {
56- assert_eq ! ( to_length_prefixed( b"" ) , b"\x00 \x00 " ) ;
57- assert_eq ! ( to_length_prefixed( b"a" ) , b"\x00 \x01 a" ) ;
58- assert_eq ! ( to_length_prefixed( b"ab" ) , b"\x00 \x02 ab" ) ;
59- assert_eq ! ( to_length_prefixed( b"abc" ) , b"\x00 \x03 abc" ) ;
60- }
61-
62- #[ test]
63- fn to_length_prefixed_works_for_long_prefix ( ) {
64- let long_namespace1 = vec ! [ 0 ; 256 ] ;
65- let prefix1 = to_length_prefixed ( & long_namespace1) ;
66- assert_eq ! ( prefix1. len( ) , 256 + 2 ) ;
67- assert_eq ! ( & prefix1[ 0 ..2 ] , b"\x01 \x00 " ) ;
68-
69- let long_namespace2 = vec ! [ 0 ; 30000 ] ;
70- let prefix2 = to_length_prefixed ( & long_namespace2) ;
71- assert_eq ! ( prefix2. len( ) , 30000 + 2 ) ;
72- assert_eq ! ( & prefix2[ 0 ..2 ] , b"\x75 \x30 " ) ;
73-
74- let long_namespace3 = vec ! [ 0 ; 0xFFFF ] ;
75- let prefix3 = to_length_prefixed ( & long_namespace3) ;
76- assert_eq ! ( prefix3. len( ) , 0xFFFF + 2 ) ;
77- assert_eq ! ( & prefix3[ 0 ..2 ] , b"\xFF \xFF " ) ;
78- }
79-
80- #[ test]
81- #[ should_panic( expected = "only supports namespaces up to length 0xFFFF" ) ]
82- fn to_length_prefixed_panics_for_too_long_prefix ( ) {
83- let limit = 0xFFFF ;
84- let long_namespace = vec ! [ 0 ; limit + 1 ] ;
85- to_length_prefixed ( & long_namespace) ;
86- }
87-
88- #[ test]
89- fn to_length_prefixed_calculates_capacity_correctly ( ) {
90- // Those tests cannot guarantee the required capacity was calculated correctly before
91- // the vector allocation but increase the likelyhood of a proper implementation.
92-
93- let key = to_length_prefixed ( b"" ) ;
94- assert_eq ! ( key. capacity( ) , key. len( ) ) ;
95-
96- let key = to_length_prefixed ( b"h" ) ;
97- assert_eq ! ( key. capacity( ) , key. len( ) ) ;
98-
99- let key = to_length_prefixed ( b"hij" ) ;
100- assert_eq ! ( key. capacity( ) , key. len( ) ) ;
101- }
102- }
103-
10439// currently disabled tests as they require a bunch of legacy non-sense
10540// TODO: enable
10641#[ cfg( test) ]
0 commit comments