Skip to content

Commit

Permalink
fix: sui rlp upgrade (#417)
Browse files Browse the repository at this point in the history
* fix: sui-rlp upgrade compatible

* fix: sui-rlp upgrade compatible
  • Loading branch information
gcranju authored Nov 29, 2024
1 parent 9cc11c4 commit 2a357b9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
10 changes: 5 additions & 5 deletions contracts/sui/libs/sui_rlp/sources/encoder.move
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module sui_rlp::encoder {
vector::append(&mut encoded_list,result);

} else {
let length_bytes = utils::to_bytes_u64(len,false);
let length_bytes = utils::to_bytes_u64_sign(len,false);
let prefix = (0xf7 + vector::length(&length_bytes)) as u8;
vector::push_back(&mut encoded_list, prefix);
vector::append(&mut encoded_list, length_bytes);
Expand All @@ -69,7 +69,7 @@ module sui_rlp::encoder {
let len_u8=(len as u8);
vector::push_back(&mut length_info,(offset+len_u8));
}else {
let length_bytes=utils::to_bytes_u64(len,false);
let length_bytes=utils::to_bytes_u64_sign(len,false);
let length_byte_len=vector::length(&length_bytes);
let length_byte_len=offset+(length_byte_len as u8);
vector::push_back(&mut length_info,length_byte_len);
Expand All @@ -86,19 +86,19 @@ module sui_rlp::encoder {
}

public fun encode_u32(num:u32):vector<u8>{
let vec= utils::to_bytes_u32(num,true);
let vec= utils::to_bytes_u32_sign(num,true);
encode(&vec)

}

public fun encode_u64(num:u64):vector<u8>{
let vec= utils::to_bytes_u64(num,true);
let vec= utils::to_bytes_u64_sign(num,true);
encode(&vec)

}

public fun encode_u128(num:u128):vector<u8>{
let vec= utils::to_bytes_u128(num,true);
let vec= utils::to_bytes_u128_sign(num,true);
encode(&vec)
}

Expand Down
29 changes: 23 additions & 6 deletions contracts/sui/libs/sui_rlp/sources/utils.move
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,35 @@ module sui_rlp::utils {

}

public fun to_bytes_u128(number:u128,signed:bool):vector<u8>{
//Deprecated
public fun to_bytes_u128(number:u128):vector<u8>{
let bytes=bcs::to_bytes(&number);
to_signed_bytes(bytes,true)
}

public fun to_bytes_u128_sign(number:u128,signed:bool):vector<u8>{
let bytes=bcs::to_bytes(&number);
to_signed_bytes(bytes,signed)
}

//Deprecated
public fun to_bytes_u64(number:u64):vector<u8>{
let bytes=bcs::to_bytes(&number);
to_signed_bytes(bytes,true)
}

public fun to_bytes_u64(number:u64,signed:bool):vector<u8>{
public fun to_bytes_u64_sign(number:u64,signed:bool):vector<u8>{
let bytes=bcs::to_bytes(&number);
to_signed_bytes(bytes,signed)
}

//Deprecated
public fun to_bytes_u32(number: u32): vector<u8> {
let bytes=bcs::to_bytes(&number);
to_signed_bytes(bytes,true)
}

public fun to_bytes_u32(number: u32,signed:bool): vector<u8> {
public fun to_bytes_u32_sign(number: u32,signed:bool): vector<u8> {
let bytes=bcs::to_bytes(&number);
to_signed_bytes(bytes,signed)
}
Expand Down Expand Up @@ -120,7 +137,7 @@ module sui_rlp::utils_test {
#[test]
fun test_u32_conversion() {
let num= (122 as u32);
let bytes= utils::to_bytes_u32(num,true);
let bytes= utils::to_bytes_u32_sign(num,true);
let converted=utils::from_bytes_u32(&bytes);
assert!(num==converted,0x01);

Expand All @@ -129,7 +146,7 @@ module sui_rlp::utils_test {
#[test]
fun test_u64_conversion() {
let num= (55000 as u64);
let bytes= utils::to_bytes_u64(num,true);
let bytes= utils::to_bytes_u64_sign(num,true);
let converted=utils::from_bytes_u64(&bytes);
std::debug::print(&bytes);
std::debug::print(&converted);
Expand All @@ -140,7 +157,7 @@ module sui_rlp::utils_test {
#[test]
fun test_u128_conversion() {
let num= (1222223333 as u128);
let bytes= utils::to_bytes_u128(num,true);
let bytes= utils::to_bytes_u128_sign(num,true);
std::debug::print(&bytes);
let converted=utils::from_bytes_u128(&bytes);
std::debug::print(&converted);
Expand Down

0 comments on commit 2a357b9

Please sign in to comment.