Skip to content

Commit

Permalink
fix: add zero prefix for numeric types
Browse files Browse the repository at this point in the history
  • Loading branch information
bishalbikram committed Sep 19, 2024
1 parent b3552ee commit cbd3825
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 33 deletions.
53 changes: 27 additions & 26 deletions contracts/soroban/libs/soroban-rlp/src/test/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@ fn test_encode_u32() {
let env = Env::default();

let encoded = encode_u32(&env, 2000022458);
assert_eq!(encoded, bytes!(&env, 0x847735EBBA));

let encoded = encode_u128(&env, 128);
assert_eq!(encoded, bytes!(&env, 0x8180))
assert_eq!(encoded, bytes!(&env, 0x85007735EBBA));
}

#[test]
fn test_encode_u64() {
let env = Env::default();

let encoded = encode_u64(&env, 1999999999999999999);
assert_eq!(encoded, bytes!(&env, 0x881BC16D674EC7FFFF));
assert_eq!(encoded, bytes!(&env, 0x89001BC16D674EC7FFFF));

let encoded = encode_u64(&env, 199999999);
assert_eq!(encoded, bytes!(&env, 0x840BEBC1FF))
assert_eq!(encoded, bytes!(&env, 0x85000BEBC1FF))
}

#[test]
fn test_u128_u128() {
fn test_encode_u128() {
let env = Env::default();

let encoded = encode_u128(&env, 199999999999999999999999999999999999999);
assert_eq!(encoded, bytes!(&env, 0x9096769950B50D88F41314447FFFFFFFFF))
assert_eq!(
encoded,
bytes!(&env, 0x910096769950B50D88F41314447FFFFFFFFF)
)
}

#[test]
Expand All @@ -63,10 +63,11 @@ fn test_encode_string_with_larger_bytes_length() {

let encoded = encode_string(&env, String::from_str(&env, "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s"));

let expected_rlp_byte = 184;
let expected_rlp_byte = 185;
let str_bytes_slice = b"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s";
let mut expected_bytes = Bytes::new(&env);
expected_bytes.push_back(expected_rlp_byte);
expected_bytes.push_back(0);
expected_bytes.push_back(0x97);
expected_bytes.extend_from_slice(str_bytes_slice);

Expand Down Expand Up @@ -107,29 +108,29 @@ fn test_encode_strings_with_longer_bytes() {

let encoded = encode_strings(&env, strings);

let rlp_byte = 0xf7 + 2;
let rlp_byte = 0xf7 + 3;
let mut expected_encode = Bytes::new(&env);

// rlp byte and data length bytes
expected_encode.push_back(rlp_byte);
expected_encode.extend_from_array(&[0x01, 0x71]);
expected_encode.extend_from_array(&[0x00, 0x01, 0x74]);

// strings
let string_rlp_byte = 0xb7 + 1;
let string_rlp_byte = 0xb7 + 2;
let string_len_byte = 0x7c;
expected_encode.extend_from_array(&[string_rlp_byte, string_len_byte]);
expected_encode.extend_from_array(&[string_rlp_byte, 0, string_len_byte]);
expected_encode.extend_from_slice(b"It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.");

let string_len_byte = 0x7b;
expected_encode.extend_from_array(&[string_rlp_byte, string_len_byte]);
expected_encode.extend_from_array(&[string_rlp_byte, 0, string_len_byte]);
expected_encode.extend_from_slice(b"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.");

let string_len_byte = 0x74;
expected_encode.extend_from_array(&[string_rlp_byte, string_len_byte]);
expected_encode.extend_from_array(&[string_rlp_byte, 0, string_len_byte]);
expected_encode.extend_from_slice(b"Egestas maecenas pharetra convallis posuere morbi. Velit laoreet id donec ultrices tincidunt arcu non sodales neque.");

assert_eq!(encoded, expected_encode);
assert_eq!(encoded.len(), 372);
assert_eq!(encoded.len(), 376);
}

#[test]
Expand All @@ -153,7 +154,7 @@ fn test_encode_list_with_smaller_bytes() {

let encoded = encode_list(&env, list, true);

let expected_rlp_byte = 0xc0 + 17;
let expected_rlp_byte = 0xc0 + 18;
let mut expected_bytes = Bytes::new(&env);
expected_bytes.push_back(expected_rlp_byte);
expected_bytes.append(&encode_u32(&env, 4294967295));
Expand Down Expand Up @@ -183,30 +184,30 @@ fn test_encode_list_with_longer_bytes() {
let mut expected_bytes = Bytes::new(&env);

// rlp and data len bytes
let rlp_byte = 0xf7 + 1;
let data_len_byte = 0xAA;
expected_bytes.extend_from_array(&[rlp_byte, data_len_byte]);
let rlp_byte = 0xf7 + 2;
let data_len_byte = 0xAA + 4;
expected_bytes.extend_from_array(&[rlp_byte, 0, data_len_byte]);

// u8
expected_bytes.extend_from_array(&[0x81, 0xF5]);

// u32
expected_bytes.extend_from_array(&[0x84, 0x01, 0x71, 0x34, 0x67]);
expected_bytes.extend_from_array(&[0x85, 0x00, 0x01, 0x71, 0x34, 0x67]);

// u64
expected_bytes.extend_from_array(&[0x88, 0x01, 0x71, 0x34, 0x67, 0xff, 0xff, 0xff, 0xff]);
expected_bytes.extend_from_array(&[0x89, 0x00, 0x01, 0x71, 0x34, 0x67, 0xff, 0xff, 0xff, 0xff]);

// u128
expected_bytes.extend_from_array(&[
0x90, 0x87, 0xdc, 0xfa, 0xcd, 0x87, 0x98, 0x27, 0x36, 0xcd, 0xef, 0xcd, 0xef, 0xff, 0xff,
0xff, 0xff,
0x91, 0x00, 0x87, 0xdc, 0xfa, 0xcd, 0x87, 0x98, 0x27, 0x36, 0xcd, 0xef, 0xcd, 0xef, 0xff,
0xff, 0xff, 0xff,
]);

// strings
let array_rlp_byte = 0xf7 + 1;
let array_rlp_byte = 0xf7 + 2;
let total_rlp_bytes_in_array = 2;
let strings_len_byte = 0x56 + total_rlp_bytes_in_array;
expected_bytes.extend_from_array(&[array_rlp_byte, strings_len_byte]);
expected_bytes.extend_from_array(&[array_rlp_byte, 0, strings_len_byte]);

let rlp_byte = 0x80 + 46;
expected_bytes.push_back(rlp_byte);
Expand Down
2 changes: 1 addition & 1 deletion contracts/soroban/libs/soroban-rlp/src/test/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ fn test_slice_vector() {
let bytes = u128_to_bytes(&env, 1844674407);
let slice = slice_vector(&env, bytes.clone(), 1, 2);

assert_eq!(slice, bytes!(&env, 0xF37F));
assert_eq!(slice, bytes!(&env, 0x6DF3));
}
19 changes: 13 additions & 6 deletions contracts/soroban/libs/soroban-rlp/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use soroban_sdk::{
bytes,
xdr::{FromXdr, ToXdr},
Bytes, Env, String,
};

pub fn u32_to_bytes(env: &Env, number: u32) -> Bytes {
let mut bytes = Bytes::new(&env);
let mut bytes = bytes!(&env, 0x00);
let mut i = 3;
let mut leading_zero = true;
while i >= 0 {
let val = (number >> (i * 8) & 0xff) as u8;
if val > 0 {
if val > 0 || !leading_zero {
leading_zero = false;
bytes.push_back(val);
}

Expand All @@ -26,11 +29,13 @@ pub fn bytes_to_u32(bytes: Bytes) -> u32 {
}

pub fn u64_to_bytes(env: &Env, number: u64) -> Bytes {
let mut bytes = Bytes::new(&env);
let mut bytes = bytes!(&env, 0x00);
let mut i = 7;
let mut leading_zero = true;
while i >= 0 {
let val = (number >> (i * 8) & 0xff) as u8;
if val > 0 {
if val > 0 || !leading_zero {
leading_zero = false;
bytes.push_back(val);
}

Expand All @@ -48,11 +53,13 @@ pub fn bytes_to_u64(bytes: Bytes) -> u64 {
}

pub fn u128_to_bytes(env: &Env, number: u128) -> Bytes {
let mut bytes = Bytes::new(&env);
let mut bytes = bytes!(&env, 0x00);
let mut i = 15;
let mut leading_zero = true;
while i >= 0 {
let val = (number >> (i * 8) & 0xff) as u8;
if val > 0 {
if val > 0 || !leading_zero {
leading_zero = false;
bytes.push_back(val);
}

Expand Down

0 comments on commit cbd3825

Please sign in to comment.