From 8188e845dab9cd5149c7556fd6995cdbe1373240 Mon Sep 17 00:00:00 2001 From: Marcelo Politzer <251334+mpolitzer@users.noreply.github.com> Date: Fri, 24 May 2024 08:07:59 -0300 Subject: [PATCH] feat!: port rust code to new API --- rollup-http/rollup-http-server/build.rs | 4 +- .../rollup-http-server/src/rollup/mod.rs | 196 ++++++++---------- .../tests/rollup-http-server-tests.rs | 3 +- 3 files changed, 95 insertions(+), 108 deletions(-) diff --git a/rollup-http/rollup-http-server/build.rs b/rollup-http/rollup-http-server/build.rs index 08dffeaf..31886b19 100644 --- a/rollup-http/rollup-http-server/build.rs +++ b/rollup-http/rollup-http-server/build.rs @@ -39,14 +39,14 @@ fn main() { }; let header_path = if mock_build { - "../../sys-utils/libcmt/src/rollup.h".into() + "../../sys-utils/libcmt/include/libcmt/rollup.h".into() } else { pkg_config::get_variable("libcmt", "includedir").expect("Could not find include directory") + "/libcmt/rollup.h" }; let include_path = if mock_build { - "-I../../sys-utils/libcmt/src".into() + "-I../../sys-utils/libcmt/include/libcmt".into() } else { "-I".to_string() + &pkg_config::get_variable("libcmt", "includedir") diff --git a/rollup-http/rollup-http-server/src/rollup/mod.rs b/rollup-http/rollup-http-server/src/rollup/mod.rs index 288f943b..a984482d 100644 --- a/rollup-http/rollup-http-server/src/rollup/mod.rs +++ b/rollup-http/rollup-http-server/src/rollup/mod.rs @@ -114,6 +114,42 @@ impl From<&mut RollupFinish> for cmt_rollup_finish_t { } } +impl cmt_abi_u256_t { + fn from_hex(hex: &str) -> Result { + let mut value: cmt_abi_u256_t = unsafe { std::mem::zeroed() }; + let mut binary = hex::decode(hex)?; + let rc = unsafe { + cmt_abi_encode_uint_nn( + binary.len(), + binary.as_mut_ptr() as *const u8, + value.data.as_mut_ptr() + ) + }; + if rc != 0 { + return Err(hex::FromHexError::InvalidStringLength); + } + return Ok(value); + } +} + +impl cmt_abi_address_t { + fn from_hex(hex: &str) -> Result { + let mut value: cmt_abi_address_t = unsafe { std::mem::zeroed() }; + let mut binary = hex::decode(hex)?; + let rc = unsafe { + cmt_abi_encode_uint_nn( + binary.len(), + binary.as_mut_ptr() as *const u8, + value.data.as_mut_ptr() + ) + }; + if rc != 0 { + return Err(hex::FromHexError::InvalidStringLength); + } + return Ok(value); + } +} + #[derive(Debug, Clone, Serialize, Deserialize, Validate)] pub struct GIORequest { #[validate(range(min = 0x10))] // avoid overlapping with our HTIF_YIELD_MANUAL_REASON_* @@ -144,9 +180,9 @@ pub struct AdvanceMetadata { impl From for AdvanceMetadata { fn from(other: cmt_rollup_advance_t) -> Self { let mut msg_sender = "0x".to_string(); - msg_sender.push_str(&hex::encode(&other.msg_sender)); + msg_sender.push_str(&hex::encode(&other.msg_sender.data)); let mut app_contract = "0x".to_string(); - app_contract.push_str(&hex::encode(&other.app_contract)); + app_contract.push_str(&hex::encode(&other.app_contract.data)); let mut prev_randao = "0x".to_string(); prev_randao.push_str(&hex::encode(&other.prev_randao.data)); AdvanceMetadata { @@ -254,16 +290,30 @@ pub fn rollup_read_advance_state_request( ) -> Result> { let mut advance_request = Box::new(cmt_rollup_advance_t { chain_id: 0, - msg_sender: Default::default(), - app_contract: Default::default(), + msg_sender: { + cmt_abi_address_t { + data: Default::default(), + } + }, + app_contract: { + cmt_abi_address_t { + data: Default::default(), + } + }, block_number: 0, block_timestamp: 0, - prev_randao: { cmt_u256_t { - data: Default::default(), - }}, + prev_randao: { + cmt_abi_u256_t { + data: Default::default(), + } + }, index: 0, - payload_length: 0, - payload: std::ptr::null::<::std::os::raw::c_uchar>() as *mut c_void, + payload: { + cmt_abi_bytes_t { + length: 0, + data: std::ptr::null::<::std::os::raw::c_uchar>() as *mut c_void, + } + }, }); let res = unsafe { cmt_rollup_read_advance_state(fd.0, advance_request.as_mut()) }; @@ -275,19 +325,19 @@ pub fn rollup_read_advance_state_request( )))); } - if advance_request.payload_length == 0 { + if advance_request.payload.length == 0 { log::info!("read zero size payload from advance state request"); } - let mut payload: Vec = Vec::with_capacity(advance_request.payload_length as usize); - if advance_request.payload_length > 0 { + let mut payload: Vec = Vec::with_capacity(advance_request.payload.length as usize); + if advance_request.payload.length > 0 { unsafe { std::ptr::copy( - advance_request.payload, + advance_request.payload.data, payload.as_mut_ptr() as *mut c_void, - advance_request.payload_length as usize, + advance_request.payload.length as usize, ); - payload.set_len(advance_request.payload_length as usize); + payload.set_len(advance_request.payload.length as usize); } } @@ -349,7 +399,7 @@ pub fn rollup_write_notice( ) -> Result> { print_notice(notice); - let binary_payload = match hex::decode(¬ice.payload[2..]) { + let mut binary_payload = match hex::decode(¬ice.payload[2..]) { Ok(payload) => payload, Err(_err) => { return Err(Box::new(RollupError::new(&format!( @@ -358,25 +408,14 @@ pub fn rollup_write_notice( } }; - let mut buffer: Vec = Vec::with_capacity(binary_payload.len()); - let length = binary_payload.len() as u64; let mut notice_index: std::os::raw::c_ulong = 0; - - let res = unsafe { - std::ptr::copy( - binary_payload.as_ptr(), - buffer.as_mut_ptr(), - binary_payload.len(), - ); - - cmt_rollup_emit_notice( - fd.0, - length as u32, - buffer.as_mut_ptr() as *mut c_void, - &mut notice_index, - ) + let payload = cmt_abi_bytes_t { + data: binary_payload.as_mut_ptr() as *mut c_void, + length: binary_payload.len(), }; + let res = unsafe { cmt_rollup_emit_notice(fd.0, &payload, &mut notice_index) }; + if res != 0 { return Err(Box::new(RollupError::new(&format!( "IOCTL_ROLLUP_WRITE_NOTICE returned error {}", @@ -395,7 +434,7 @@ pub fn rollup_write_voucher( ) -> Result> { print_voucher(voucher); - let binary_payload = match hex::decode(&voucher.payload[2..]) { + let mut binary_payload = match hex::decode(&voucher.payload[2..]) { Ok(payload) => payload, Err(_err) => { return Err(Box::new(RollupError::new(&format!( @@ -403,56 +442,16 @@ pub fn rollup_write_voucher( )))); } }; - let mut payload_buffer: Vec = Vec::with_capacity(binary_payload.len()); - let payload_data = payload_buffer.as_mut_ptr(); - let payload_length = binary_payload.len(); - - let binary_value = match hex::decode(&voucher.value[2..]) { - Ok(data) => data, - Err(_err) => { - return Err(Box::new(RollupError::new(&format!( - "Error decoding voucher value, it must be in Ethereum hex binary format" - )))); - } - }; - let mut value_buffer: Vec = Vec::with_capacity(binary_value.len()); - let value_data = value_buffer.as_mut_ptr(); - let value_length = binary_value.len(); - - let address_c = match hex::decode(&voucher.destination[2..]) { - Ok(res) => res, - Err(e) => { - return Err(Box::new(RollupError::new(&format!( - "address not valid: {}", - e - )))); - } + let value = cmt_abi_u256_t::from_hex(&voucher.value[2..])?; + let address = cmt_abi_address_t::from_hex(&voucher.destination[2..])?; + let payload = cmt_abi_bytes_t { + data: binary_payload.as_mut_ptr() as *mut c_void, + length: binary_payload.len(), }; let mut voucher_index: std::os::raw::c_ulong = 0; - let res = unsafe { - std::ptr::copy( - binary_payload.as_ptr(), - payload_buffer.as_mut_ptr(), - binary_payload.len(), - ); - std::ptr::copy( - binary_value.as_ptr(), - value_buffer.as_mut_ptr(), - binary_value.len(), - ); - - cmt_rollup_emit_voucher( - fd.0, - address_c.len() as u32, - address_c.as_ptr() as *const c_void, - value_length as u32, - value_data as *mut c_void, - payload_length as u32, - payload_data as *mut c_void, - &mut voucher_index, - ) - }; + let res = + unsafe { cmt_rollup_emit_voucher(fd.0, &address, &value, &payload, &mut voucher_index) }; if res != 0 { return Err(Box::new(RollupError::new(&format!( @@ -472,7 +471,7 @@ pub fn rollup_write_report( ) -> Result<(), Box> { print_report(report); - let binary_payload = match hex::decode(&report.payload[2..]) { + let mut binary_payload = match hex::decode(&report.payload[2..]) { Ok(payload) => payload, Err(_err) => { return Err(Box::new(RollupError::new(&format!( @@ -481,20 +480,13 @@ pub fn rollup_write_report( } }; - let mut buffer: Vec = Vec::with_capacity(binary_payload.len()); - - let data = buffer.as_mut_ptr() as *mut c_void; - let length = binary_payload.len(); - - let res = unsafe { - std::ptr::copy( - binary_payload.as_ptr(), - buffer.as_mut_ptr(), - binary_payload.len(), - ); - cmt_rollup_emit_report(fd.0, length as u32, data) + let payload = cmt_abi_bytes_t { + data: binary_payload.as_mut_ptr() as *mut c_void, + length: binary_payload.len(), }; + let res = unsafe { cmt_rollup_emit_report(fd.0, &payload) }; + if res != 0 { return Err(Box::new(RollupError::new(&format!( "IOCTL_ROLLUP_WRITE_REPORT returned error {}", @@ -578,7 +570,7 @@ pub fn rollup_throw_exception( ) -> Result<(), Box> { print_exception(exception); - let binary_payload = match hex::decode(&exception.payload[2..]) { + let mut binary_payload = match hex::decode(&exception.payload[2..]) { Ok(payload) => payload, Err(_err) => { return Err(Box::new(RollupError::new(&format!( @@ -587,18 +579,12 @@ pub fn rollup_throw_exception( } }; - let mut buffer: Vec = Vec::with_capacity(binary_payload.len()); - let length = binary_payload.len(); - let data = buffer.as_mut_ptr() as *mut c_void; - - let res = unsafe { - std::ptr::copy( - binary_payload.as_ptr(), - buffer.as_mut_ptr(), - binary_payload.len(), - ); - cmt_rollup_emit_exception(fd.0, length as u32, data) + let payload = cmt_abi_bytes_t { + data: binary_payload.as_mut_ptr() as *mut c_void, + length: binary_payload.len(), }; + + let res = unsafe { cmt_rollup_emit_exception(fd.0, &payload) }; if res != 0 { return Err(Box::new(RollupError::new(&format!( "IOCTL_ROLLUP_THROW_EXCEPTION returned error {}", diff --git a/rollup-http/rollup-http-server/tests/rollup-http-server-tests.rs b/rollup-http/rollup-http-server/tests/rollup-http-server-tests.rs index 440f23d5..aab663e3 100644 --- a/rollup-http/rollup-http-server/tests/rollup-http-server-tests.rs +++ b/rollup-http/rollup-http-server/tests/rollup-http-server-tests.rs @@ -127,7 +127,8 @@ async fn test_finish_request( * 0x0000000000000000000000000000000000000004 \ * 0x0000000000000000000000000000000000000005 \ * 0x0000000000000000000000000000000000000006 \ - * 0x`echo "advance-0" | xxd -p -c0` + * 0x0000000000000000000000000000000000000007 \ + * 0x`echo -e "advance-0" | xxd -p -c0` */ let advance_payload_field = "advance-0\n"; // must match `cast` invocation! let advance_payload_data = "415bf363\