@@ -18,6 +18,8 @@ use crate::errors::{CommunicationError, VmError, VmResult};
18
18
#[ cfg( feature = "iterator" ) ]
19
19
use crate :: memory:: maybe_read_region;
20
20
use crate :: memory:: { read_region, write_region} ;
21
+ #[ cfg( target_arch = "wasm32" ) ]
22
+ use crate :: sections:: encode_sections;
21
23
use crate :: serde:: to_vec;
22
24
use crate :: GasInfo ;
23
25
@@ -424,53 +426,6 @@ fn do_next<A: BackendApi, S: Storage, Q: Querier>(
424
426
write_to_contract :: < A , S , Q > ( env, & out_data)
425
427
}
426
428
427
- /// Encodes multiple sections of data into one vector.
428
- ///
429
- /// Each section is suffixed by a section length encoded as big endian uint32.
430
- /// Using suffixes instead of prefixes allows reading sections in reverse order,
431
- /// such that the first element does not need to be re-allocated if the contract's
432
- /// data structure supports truncation (such as a Rust vector).
433
- ///
434
- /// The resulting data looks like this:
435
- ///
436
- /// ```ignore
437
- /// section1 || section1_len || section2 || section2_len || section3 || section3_len || …
438
- /// ```
439
- #[ allow( dead_code) ]
440
- fn encode_sections ( sections : & [ Vec < u8 > ] ) -> VmResult < Vec < u8 > > {
441
- let mut out_len: usize = sections. iter ( ) . map ( |section| section. len ( ) ) . sum ( ) ;
442
- out_len += 4 * sections. len ( ) ;
443
- let mut out_data = Vec :: with_capacity ( out_len) ;
444
- for section in sections {
445
- let section_len = to_u32 ( section. len ( ) ) ?. to_be_bytes ( ) ;
446
- out_data. extend ( section) ;
447
- out_data. extend_from_slice ( & section_len) ;
448
- }
449
- debug_assert_eq ! ( out_data. len( ) , out_len) ;
450
- debug_assert_eq ! ( out_data. capacity( ) , out_len) ;
451
- Ok ( out_data)
452
- }
453
-
454
- /// Returns the data shifted by 32 bits towards the most significant bit.
455
- ///
456
- /// This is independent of endianness. But to get the idea, it would be
457
- /// `data || 0x00000000` in big endian representation.
458
- #[ inline]
459
- fn to_high_half ( data : u32 ) -> u64 {
460
- // See https://stackoverflow.com/a/58956419/2013738 to understand
461
- // why this is endianness agnostic.
462
- ( data as u64 ) << 32
463
- }
464
-
465
- /// Returns the data copied to the 4 least significant bytes.
466
- ///
467
- /// This is independent of endianness. But to get the idea, it would be
468
- /// `0x00000000 || data` in big endian representation.
469
- #[ inline]
470
- fn to_low_half ( data : u32 ) -> u64 {
471
- data. into ( )
472
- }
473
-
474
429
#[ cfg( test) ]
475
430
mod tests {
476
431
use super :: * ;
@@ -1854,72 +1809,4 @@ mod tests {
1854
1809
e => panic ! ( "Unexpected error: {:?}" , e) ,
1855
1810
}
1856
1811
}
1857
-
1858
- #[ test]
1859
- fn encode_sections_works_for_empty_sections ( ) {
1860
- let enc = encode_sections ( & [ ] ) . unwrap ( ) ;
1861
- assert_eq ! ( enc, b"" as & [ u8 ] ) ;
1862
- let enc = encode_sections ( & [ vec ! [ ] ] ) . unwrap ( ) ;
1863
- assert_eq ! ( enc, b"\0 \0 \0 \0 " as & [ u8 ] ) ;
1864
- let enc = encode_sections ( & [ vec ! [ ] , vec ! [ ] ] ) . unwrap ( ) ;
1865
- assert_eq ! ( enc, b"\0 \0 \0 \0 \0 \0 \0 \0 " as & [ u8 ] ) ;
1866
- let enc = encode_sections ( & [ vec ! [ ] , vec ! [ ] , vec ! [ ] ] ) . unwrap ( ) ;
1867
- assert_eq ! ( enc, b"\0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 " as & [ u8 ] ) ;
1868
- }
1869
-
1870
- #[ test]
1871
- fn encode_sections_works_for_one_element ( ) {
1872
- let enc = encode_sections ( & [ ] ) . unwrap ( ) ;
1873
- assert_eq ! ( enc, b"" as & [ u8 ] ) ;
1874
- let enc = encode_sections ( & [ vec ! [ 0xAA ] ] ) . unwrap ( ) ;
1875
- assert_eq ! ( enc, b"\xAA \0 \0 \0 \x01 " as & [ u8 ] ) ;
1876
- let enc = encode_sections ( & [ vec ! [ 0xAA , 0xBB ] ] ) . unwrap ( ) ;
1877
- assert_eq ! ( enc, b"\xAA \xBB \0 \0 \0 \x02 " as & [ u8 ] ) ;
1878
- let enc = encode_sections ( & [ vec ! [ 0x9D ; 277 ] ] ) . unwrap ( ) ;
1879
- assert_eq ! ( enc, b"\x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \x9D \0 \0 \x01 \x15 " as & [ u8 ] ) ;
1880
- }
1881
-
1882
- #[ test]
1883
- fn encode_sections_works_for_multiple_elements ( ) {
1884
- let enc = encode_sections ( & [ vec ! [ 0xAA ] ] ) . unwrap ( ) ;
1885
- assert_eq ! ( enc, b"\xAA \0 \0 \0 \x01 " as & [ u8 ] ) ;
1886
- let enc = encode_sections ( & [ vec ! [ 0xAA ] , vec ! [ 0xDE , 0xDE ] ] ) . unwrap ( ) ;
1887
- assert_eq ! ( enc, b"\xAA \0 \0 \0 \x01 \xDE \xDE \0 \0 \0 \x02 " as & [ u8 ] ) ;
1888
- let enc = encode_sections ( & [ vec ! [ 0xAA ] , vec ! [ 0xDE , 0xDE ] , vec ! [ ] ] ) . unwrap ( ) ;
1889
- assert_eq ! ( enc, b"\xAA \0 \0 \0 \x01 \xDE \xDE \0 \0 \0 \x02 \0 \0 \0 \0 " as & [ u8 ] ) ;
1890
- let enc = encode_sections ( & [ vec ! [ 0xAA ] , vec ! [ 0xDE , 0xDE ] , vec ! [ ] , vec ! [ 0xFF ; 19 ] ] ) . unwrap ( ) ;
1891
- assert_eq ! ( enc, b"\xAA \0 \0 \0 \x01 \xDE \xDE \0 \0 \0 \x02 \0 \0 \0 \0 \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF \0 \0 \0 \x13 " as & [ u8 ] ) ;
1892
- }
1893
-
1894
- #[ test]
1895
- fn to_high_half_works ( ) {
1896
- assert_eq ! (
1897
- to_high_half( 0 ) ,
1898
- u64 :: from_be_bytes( [ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ] )
1899
- ) ;
1900
- assert_eq ! (
1901
- to_high_half( 1 ) ,
1902
- u64 :: from_be_bytes( [ 0x00 , 0x00 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0x00 ] )
1903
- ) ;
1904
- assert_eq ! (
1905
- to_high_half( u32 :: from_be_bytes( [ 0x12 , 0x34 , 0x56 , 0x78 ] ) ) ,
1906
- u64 :: from_be_bytes( [ 0x12 , 0x34 , 0x56 , 0x78 , 0x00 , 0x00 , 0x00 , 0x00 ] )
1907
- ) ;
1908
- }
1909
-
1910
- #[ test]
1911
- fn to_low_half_works ( ) {
1912
- assert_eq ! (
1913
- to_low_half( 0 ) ,
1914
- u64 :: from_be_bytes( [ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ] )
1915
- ) ;
1916
- assert_eq ! (
1917
- to_low_half( 1 ) ,
1918
- u64 :: from_be_bytes( [ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x01 ] )
1919
- ) ;
1920
- assert_eq ! (
1921
- to_low_half( u32 :: from_be_bytes( [ 0x12 , 0x34 , 0x56 , 0x78 ] ) ) ,
1922
- u64 :: from_be_bytes( [ 0x00 , 0x00 , 0x00 , 0x00 , 0x12 , 0x34 , 0x56 , 0x78 ] )
1923
- ) ;
1924
- }
1925
1812
}
0 commit comments