Skip to content

Commit

Permalink
feat!: port rust code to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
mpolitzer committed May 24, 2024
1 parent ce96c95 commit a26da7d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 49 deletions.
4 changes: 2 additions & 2 deletions rollup-http/rollup-http-server/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
87 changes: 40 additions & 47 deletions rollup-http/rollup-http-server/src/rollup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ pub struct AdvanceMetadata {
impl From<cmt_rollup_advance_t> 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 {
Expand Down Expand Up @@ -254,16 +254,22 @@ pub fn rollup_read_advance_state_request(
) -> Result<AdvanceRequest, Box<dyn std::error::Error>> {
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 {
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()) };
Expand All @@ -275,19 +281,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<u8> = Vec::with_capacity(advance_request.payload_length as usize);
if advance_request.payload_length > 0 {
let mut payload: Vec<u8> = 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);
}
}

Expand Down Expand Up @@ -358,21 +364,16 @@ pub fn rollup_write_notice(
}
};

let mut buffer: Vec<u8> = Vec::with_capacity(binary_payload.len());
let length = binary_payload.len() as u64;
let mut notice_index: std::os::raw::c_ulong = 0;
let payload = cmt_abi_bytes_t {
data: binary_payload.as_mut_ptr() as *mut c_void,
length: binary_payload.len()
};

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,
&payload,
&mut notice_index,
)
};
Expand Down Expand Up @@ -429,13 +430,13 @@ pub fn rollup_write_voucher(
}
};

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(),
Expand All @@ -448,8 +449,7 @@ pub fn rollup_write_voucher(
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,
&payload,
&mut voucher_index,
)
};
Expand Down Expand Up @@ -483,16 +483,13 @@ pub fn rollup_write_report(

let mut buffer: Vec<u8> = Vec::with_capacity(binary_payload.len());

let data = buffer.as_mut_ptr() as *mut c_void;
let length = binary_payload.len();
let payload = cmt_abi_bytes_t {
data: binary_payload.as_mut_ptr() as *mut c_void,
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)
cmt_rollup_emit_report(fd.0, &payload)
};

if res != 0 {
Expand Down Expand Up @@ -587,17 +584,13 @@ pub fn rollup_throw_exception(
}
};

let mut buffer: Vec<u8> = Vec::with_capacity(binary_payload.len());
let length = binary_payload.len();
let data = buffer.as_mut_ptr() as *mut c_void;
let payload = cmt_abi_bytes_t {
data: binary_payload.as_mut_ptr() as *mut c_void,
length: binary_payload.len(),
};

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)
cmt_rollup_emit_exception(fd.0, &payload)
};
if res != 0 {
return Err(Box::new(RollupError::new(&format!(
Expand Down

0 comments on commit a26da7d

Please sign in to comment.