Skip to content

Commit

Permalink
Merge pull request #330 from icon-project/feat/sui-xcall-contracts
Browse files Browse the repository at this point in the history
Feat/sui xcall contracts
  • Loading branch information
gcranju authored Jun 11, 2024
2 parents 9cadda3 + 94d4390 commit ef7d0e9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
2 changes: 1 addition & 1 deletion contracts/evm/lib/openzeppelin-contracts-upgradeable
34 changes: 21 additions & 13 deletions contracts/sui/libs/sui_rlp/sources/utils.move
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,34 @@ module sui_rlp::utils {
let mut i:u8=0;
while(i < 8){
let val =( (number>>(i * 8) & 0xFF) as u8) ;
if (val > 0) {
vector::push_back(&mut bytes,val);
};
i=i+1;
};
bytes.reverse();
truncate_zeros(&bytes)
}

fun truncate_zeros(bytes: &vector<u8>): vector<u8> {
let mut i = 0;
let mut started = false;
let mut result: vector<u8> = vector::empty();
while (i < vector::length(bytes)) {
let val = *vector::borrow(bytes, i) as u8;
if (val > 0 || started) {
started = true;
vector::push_back(&mut result, val);
};

i = i + 1;
};

let mut prefix = vector<u8>[0];
prefix.append(bytes);
prefix.append(result);
prefix
}

// Convert bytes to u64
public fun from_bytes_u64(bytes: &vector<u8>): u64 {
public fun from_bytes_u64(bytes: &vector<u8>): u64 {
let mut result = 0;
let mut multiplier = 1;
let length = vector::length(bytes);
Expand All @@ -64,9 +78,6 @@ module sui_rlp::utils {
//std::debug::print(&result);

if (i > 0) {
//std::debug::print(&b"MULTIPLIER".to_string());
//std::debug::print(&multiplier);
//std::debug::print(&i);
multiplier = multiplier * 256
};

Expand All @@ -82,16 +93,11 @@ module sui_rlp::utils {
let mut i:u8=0;
while(i < 16){
let val = ((number>>(i * 8) & 0xFF) as u8) ;
if (val > 0) {
vector::push_back(&mut bytes,val);
};

i=i+1;
};
bytes.reverse();
let mut prefix = vector<u8>[0];
prefix.append(bytes);
prefix
truncate_zeros(&bytes)
}

// Convert bytes to u128
Expand Down Expand Up @@ -162,7 +168,9 @@ module sui_rlp::utils_test {
fun test_u128_conversion() {
let num= (1222223333 as u128);
let bytes= utils::to_bytes_u128(num);
std::debug::print(&bytes);
let converted=utils::from_bytes_u128(&bytes);
std::debug::print(&converted);
assert!(num==converted,0x01);

}
Expand Down
9 changes: 6 additions & 3 deletions contracts/sui/libs/sui_rlp/tests/rlp_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,23 @@ module sui_rlp::rlp_tests {
#[test]
fun test_encode_u64() {

let val=(12345667 as u64);
let val=(2*100000000000 as u64);
let encoded= encoder::encode_u64(val);
//std::debug::print(&encoded);
std::debug::print(&encoded);
let decoded_val= decoder::decode(&encoded);
let decoded= decoder::decode_u64(&decoded_val);
std::debug::print(&decoded);
assert!(decoded==val,0x01);
}

#[test]
fun test_encode_u128() {
let val=(2245667788 as u128);
let val=(2*100000000000000000 as u128);
let encoded= encoder::encode_u128(val);
std::debug::print(&encoded);
let decoded_val= decoder::decode(&encoded);
let decoded= decoder::decode_u128(&decoded_val);
std::debug::print(&decoded);
assert!(decoded==val,0x01);
}

Expand Down
8 changes: 4 additions & 4 deletions contracts/sui/xcall/sources/types/message_result.move
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ module xcall::message_result_tests {
fun test_message_result_encoding_1(){
let msg= message_result::create(1,message_result::success(),vector::empty());
let encoded= message_result::encode(&msg);
//debug::print(&encoded);
assert!(encoded==x"c3010180",0x01);
std::debug::print(&encoded);
assert!(encoded==x"c58200010180",0x01);
let decoded=message_result::decode(&encoded);
assert!(decoded==msg,0x01);

Expand All @@ -104,8 +104,8 @@ module xcall::message_result_tests {
fun test_message_result_encoding_2(){
let msg= message_result::create(2,message_result::failure(),vector::empty());
let encoded= message_result::encode(&msg);
//debug::print(&encoded);
assert!(encoded==x"c3020080",0x01);
std::debug::print(&encoded);
assert!(encoded==x"c58200020080",0x01);
let decoded=message_result::decode(&encoded);
assert!(decoded==msg,0x01);

Expand Down

0 comments on commit ef7d0e9

Please sign in to comment.