From 194a7df37150182543b9fc3ca98b2fe182a8c230 Mon Sep 17 00:00:00 2001 From: Marcelo Politzer <251334+mpolitzer@users.noreply.github.com> Date: Mon, 20 May 2024 10:26:10 -0300 Subject: [PATCH] feat!: Add prevrandao field to EvmAdvance rework libcmt to use stronger types rename CMT_ADDRESS_LENGTH to CMT_ABI_ADDRESS_LENGTH rename CMT_ABI_WORD_LENGTH to CMT_ABI_U256_LENGTH port rust code to new API port rollup code to the new API port ioctl-echo-loop code to the new API split header files into a separate folder. This allows users to build and link without a installing by preserving the header files relative path and `-I/include` when building. --- rollup-http/rollup-http-server/build.rs | 4 +- .../rollup-http-server/src/http_service.rs | 3 +- .../rollup-http-server/src/rollup/mod.rs | 233 +++++++++--------- .../tests/rollup-http-server-tests.rs | 15 +- sys-utils/ioctl-echo-loop/ioctl-echo-loop.c | 34 +-- sys-utils/libcmt/Makefile | 9 +- .../libcmt/doc/examples/abi_decode_000.c | 2 +- .../libcmt/doc/examples/abi_decode_001.c | 10 +- .../libcmt/doc/examples/abi_decode_002.c | 17 +- .../libcmt/doc/examples/abi_encode_000.c | 3 +- .../libcmt/doc/examples/abi_encode_001.c | 7 +- .../libcmt/doc/examples/abi_encode_002.c | 11 +- sys-utils/libcmt/doc/examples/io.c | 2 +- sys-utils/libcmt/doc/examples/rollup.c | 4 +- .../libcmt/{src => include/libcmt}/abi.h | 104 ++++++-- .../libcmt/{src => include/libcmt}/buf.h | 0 sys-utils/libcmt/{src => include/libcmt}/io.h | 0 .../libcmt/{src => include/libcmt}/keccak.h | 0 .../libcmt/{src => include/libcmt}/merkle.h | 0 .../libcmt/{src => include/libcmt}/rollup.h | 24 +- .../libcmt/{src => include/libcmt}/util.h | 0 sys-utils/libcmt/src/abi.c | 112 +++++---- sys-utils/libcmt/src/buf.c | 2 +- sys-utils/libcmt/src/io-mock.c | 4 +- sys-utils/libcmt/src/io.c | 4 +- sys-utils/libcmt/src/keccak.c | 4 +- sys-utils/libcmt/src/merkle.c | 4 +- sys-utils/libcmt/src/rollup.c | 82 +++--- sys-utils/libcmt/tests/abi-multi.c | 20 +- sys-utils/libcmt/tests/abi-single.c | 144 ++++++----- sys-utils/libcmt/tests/buf.c | 2 +- sys-utils/libcmt/tests/create-data.sh | 3 +- sys-utils/libcmt/tests/data.h | 12 +- sys-utils/libcmt/tests/gio.c | 4 +- sys-utils/libcmt/tests/keccak.c | 4 +- sys-utils/libcmt/tests/merkle.c | 2 +- sys-utils/libcmt/tests/progress.c | 2 +- sys-utils/libcmt/tests/rollup.c | 82 +++--- sys-utils/libcmt/tools/funsel.c | 2 +- sys-utils/rollup/rollup.cpp | 56 +++-- 40 files changed, 579 insertions(+), 448 deletions(-) rename sys-utils/libcmt/{src => include/libcmt}/abi.h (84%) rename sys-utils/libcmt/{src => include/libcmt}/buf.h (100%) rename sys-utils/libcmt/{src => include/libcmt}/io.h (100%) rename sys-utils/libcmt/{src => include/libcmt}/keccak.h (100%) rename sys-utils/libcmt/{src => include/libcmt}/merkle.h (100%) rename sys-utils/libcmt/{src => include/libcmt}/rollup.h (87%) rename sys-utils/libcmt/{src => include/libcmt}/util.h (100%) 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/http_service.rs b/rollup-http/rollup-http-server/src/http_service.rs index 8afc64c0..80f88a43 100644 --- a/rollup-http/rollup-http-server/src/http_service.rs +++ b/rollup-http/rollup-http-server/src/http_service.rs @@ -213,7 +213,8 @@ async fn finish(finish: Json, data: Data>) -> Http let context = data.lock().await; let rollup_fd = context.rollup_fd.lock().await; // Write finish request, read indicator for next request - let new_rollup_request = match rollup::perform_rollup_finish_request(&*rollup_fd, accept).await { + let new_rollup_request = match rollup::perform_rollup_finish_request(&*rollup_fd, accept).await + { Ok(finish_request) => { // Received new request, process it log::info!( diff --git a/rollup-http/rollup-http-server/src/rollup/mod.rs b/rollup-http/rollup-http-server/src/rollup/mod.rs index 646be3e3..ffb79ce4 100644 --- a/rollup-http/rollup-http-server/src/rollup/mod.rs +++ b/rollup-http/rollup-http-server/src/rollup/mod.rs @@ -114,6 +114,40 @@ 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 value = cmt_abi_u256_t::from_hex(hex)?; + let mut address: cmt_abi_address_t = unsafe { std::mem::zeroed() }; + unsafe { + let offset = (CMT_ABI_U256_LENGTH - CMT_ABI_ADDRESS_LENGTH) as usize; + std::ptr::copy( + value.data.as_ptr().wrapping_add(offset), + address.data.as_mut_ptr(), + CMT_ABI_ADDRESS_LENGTH as usize, + ); + }; + return Ok(address); + } +} + #[derive(Debug, Clone, Serialize, Deserialize, Validate)] pub struct GIORequest { #[validate(range(min = 0x10))] // avoid overlapping with our HTIF_YIELD_MANUAL_REASON_* @@ -127,23 +161,36 @@ pub struct GIOResponse { pub response: String, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Validate)] pub struct AdvanceMetadata { + pub chain_id: u64, + #[validate(regex = "ETH_ADDR_REGEXP")] + pub app_contract: String, + #[validate(regex = "ETH_ADDR_REGEXP")] pub msg_sender: String, - pub input_index: u64, pub block_number: u64, pub block_timestamp: u64, + #[validate(regex = "ETH_U256_REGEXP")] + pub prev_randao: String, + pub input_index: u64, } impl From for AdvanceMetadata { fn from(other: cmt_rollup_advance_t) -> Self { - let mut address = "0x".to_string(); - address.push_str(&hex::encode(&other.msg_sender)); + let mut msg_sender = "0x".to_string(); + 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.data)); + let mut prev_randao = "0x".to_string(); + prev_randao.push_str(&hex::encode(&other.prev_randao.data)); AdvanceMetadata { - input_index: other.index, + chain_id: other.chain_id, + app_contract: app_contract, + msg_sender: msg_sender, block_timestamp: other.block_timestamp, block_number: other.block_number, - msg_sender: address, + input_index: other.index, + prev_randao: prev_randao, } } } @@ -241,13 +288,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_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()) }; @@ -259,19 +323,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); } } @@ -287,9 +351,10 @@ pub fn rollup_read_inspect_state_request( fd: &RollupFd, ) -> Result> { let mut inspect_request = Box::new(cmt_rollup_inspect_t { - 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_inspect_state(fd.0, inspect_request.as_mut()) }; @@ -300,23 +365,23 @@ pub fn rollup_read_inspect_state_request( )))); } - if inspect_request.payload_length == 0 { + if inspect_request.payload.length == 0 { log::info!("read zero size payload from inspect state request"); } println!( "inspect_request.payload_length: {}", - inspect_request.payload_length + inspect_request.payload.length ); - let mut payload: Vec = Vec::with_capacity(inspect_request.payload_length as usize); - if inspect_request.payload_length > 0 { + let mut payload: Vec = Vec::with_capacity(inspect_request.payload.length as usize); + if inspect_request.payload.length > 0 { unsafe { std::ptr::copy( - inspect_request.payload, + inspect_request.payload.data, payload.as_mut_ptr() as *mut c_void, - inspect_request.payload_length as usize, + inspect_request.payload.length as usize, ); - payload.set_len(inspect_request.payload_length as usize); + payload.set_len(inspect_request.payload.length as usize); } } @@ -333,7 +398,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!( @@ -342,25 +407,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 {}", @@ -379,7 +433,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!( @@ -387,56 +441,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!( @@ -456,7 +470,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!( @@ -465,20 +479,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 {}", @@ -562,7 +569,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!( @@ -571,18 +578,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 31ebf8c7..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 @@ -120,24 +120,26 @@ async fn test_finish_request( context_future: impl Future, ) -> Result<(), Box> { /* - * cast calldata "EvmAdvance(uint256,address,address,uint256,uint256,uint256,bytes)" \ + * cast calldata "EvmAdvance(uint256,address,address,uint256,uint256,uint256,uint256,bytes)" \ * 0x0000000000000000000000000000000000000001 \ * 0x0000000000000000000000000000000000000002 \ * 0x0000000000000000000000000000000000000003 \ * 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 = "cc7dee1f\ + let advance_payload_data = "415bf363\ 0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000002\ 0000000000000000000000000000000000000000000000000000000000000003\ 0000000000000000000000000000000000000000000000000000000000000004\ 0000000000000000000000000000000000000000000000000000000000000005\ 0000000000000000000000000000000000000000000000000000000000000006\ - 00000000000000000000000000000000000000000000000000000000000000e0\ + 0000000000000000000000000000000000000000000000000000000000000007\ + 0000000000000000000000000000000000000000000000000000000000000100\ 000000000000000000000000000000000000000000000000000000000000000a\ 616476616e63652d300a00000000000000000000000000000000000000000000"; @@ -378,10 +380,7 @@ async fn test_exception_throw( //Read text file with results let exception = std::fs::read("none.exception-0.bin").expect("error reading test exception file"); - assert_eq!( - String::from_utf8(exception).unwrap(), - contents, - ); + assert_eq!(String::from_utf8(exception).unwrap(), contents,); println!("Removing exception text file"); std::fs::remove_file("none.exception-0.bin")?; Ok(()) diff --git a/sys-utils/ioctl-echo-loop/ioctl-echo-loop.c b/sys-utils/ioctl-echo-loop/ioctl-echo-loop.c index 07f5ba51..59694a2f 100644 --- a/sys-utils/ioctl-echo-loop/ioctl-echo-loop.c +++ b/sys-utils/ioctl-echo-loop/ioctl-echo-loop.c @@ -90,29 +90,31 @@ static int finish_request(cmt_rollup_t *me, cmt_rollup_finish_t *finish, bool ac return cmt_rollup_finish(me, finish); } -static int write_notices(cmt_rollup_t *me, unsigned count, uint32_t length, const void *data) { +static int write_notices(cmt_rollup_t *me, unsigned count, cmt_abi_bytes_t *payload) { for (unsigned i = 0; i < count; i++) { - int rc = cmt_rollup_emit_notice(me, length, data, NULL); + int rc = cmt_rollup_emit_notice(me, payload, NULL); if (rc) return rc; } return 0; } -static int write_vouchers(cmt_rollup_t *me, unsigned count, uint8_t destination[CMT_ADDRESS_LENGTH], uint32_t length, - const void *data) { - uint8_t value[] = {0xde, 0xad, 0xbe, 0xef}; +static int write_vouchers(cmt_rollup_t *me, unsigned count, cmt_abi_address_t *destination, cmt_abi_bytes_t *payload) { + cmt_abi_u256_t value = {{ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xde, 0xad, 0xbe, 0xef, + }}; for (unsigned i = 0; i < count; i++) { - int rc = cmt_rollup_emit_voucher(me, CMT_ADDRESS_LENGTH, destination, sizeof(value), value, length, data, NULL); + int rc = cmt_rollup_emit_voucher(me, destination, &value, payload, NULL); if (rc) return rc; } return 0; } -static int write_reports(cmt_rollup_t *me, unsigned count, uint32_t length, const void *data) { +static int write_reports(cmt_rollup_t *me, unsigned count, cmt_abi_bytes_t *payload) { for (unsigned i = 0; i < count; i++) { - int rc = cmt_rollup_emit_report(me, length, data); + int rc = cmt_rollup_emit_report(me, payload); if (rc) return rc; } @@ -126,13 +128,13 @@ static int handle_advance_state_request(cmt_rollup_t *me, struct parsed_args *ar return rc; *index = advance.index; fprintf(stderr, "advance with index %d\n", (int) advance.index); - if (write_vouchers(me, args->voucher_count, advance.msg_sender, advance.payload_length, advance.payload) != 0) { + if (write_vouchers(me, args->voucher_count, &advance.msg_sender, &advance.payload) != 0) { return -1; } - if (write_notices(me, args->notice_count, advance.payload_length, advance.payload) != 0) { + if (write_notices(me, args->notice_count, &advance.payload) != 0) { return -1; } - if (write_reports(me, args->report_count, advance.payload_length, advance.payload) != 0) { + if (write_reports(me, args->report_count, &advance.payload) != 0) { return -1; } return 0; @@ -144,7 +146,7 @@ static int handle_inspect_state_request(cmt_rollup_t *me, struct parsed_args *ar if (rc) return rc; - if (write_reports(me, args->report_count, inspect.payload_length, inspect.payload) != 0) { + if (write_reports(me, args->report_count, &inspect.payload) != 0) { return -1; } return 0; @@ -193,8 +195,12 @@ int main(int argc, char *argv[]) { reject_inspect = (finish.next_request_type == HTIF_YIELD_REASON_INSPECT) && args.reject_inspects; throw_exception = (finish.next_request_type == HTIF_YIELD_REASON_ADVANCE) && (args.exception == advance_index); if (throw_exception) { - const char message[] = "exception"; - cmt_rollup_emit_exception(&rollup, sizeof message - 1, message); + char message[] = "exception"; + const cmt_abi_bytes_t payload = { + .data = message, + .length = sizeof message - 1, + }; + cmt_rollup_emit_exception(&rollup, &payload); } if (finish_request(&rollup, &finish, !(reject_advance || reject_inspect)) != 0) { break; diff --git a/sys-utils/libcmt/Makefile b/sys-utils/libcmt/Makefile index e322fa40..b45a9c95 100644 --- a/sys-utils/libcmt/Makefile +++ b/sys-utils/libcmt/Makefile @@ -26,7 +26,7 @@ TARGET_PREFIX ?= $(PREFIX) TOOLCHAIN_PREFIX ?= riscv64-linux-gnu- TARGET_CC := $(TOOLCHAIN_PREFIX)gcc TARGET_AR := $(TOOLCHAIN_PREFIX)ar -COMMON_CFLAGS := -Wvla -O2 -g -Wall -pedantic -Wextra -Isrc \ +COMMON_CFLAGS := -Wvla -O2 -g -Wall -pedantic -Wextra -Iinclude \ -fno-strict-aliasing -fno-strict-overflow -fPIC TARGET_CFLAGS := $(COMMON_CFLAGS) -ftrivial-auto-var-init=zero -Wstrict-aliasing=3 CFLAGS := $(COMMON_CFLAGS) @@ -85,7 +85,7 @@ install: $(libcmt_LIB) $(libcmt_SO) build/ffi.h mkdir -p $(TARGET_DESTDIR)$(TARGET_PREFIX)/lib cp -f $(libcmt_LIB) $(libcmt_SO) $(TARGET_DESTDIR)$(TARGET_PREFIX)/lib mkdir -p $(TARGET_DESTDIR)$(TARGET_PREFIX)/include/libcmt/ - cp -f src/*.h $(TARGET_DESTDIR)$(TARGET_PREFIX)/include/libcmt/ + cp -f include/libcmt/*.h $(TARGET_DESTDIR)$(TARGET_PREFIX)/include/libcmt/ cp -f build/ffi.h $(TARGET_DESTDIR)$(TARGET_PREFIX)/include/libcmt/ mkdir -p $(TARGET_DESTDIR)$(TARGET_PREFIX)/lib/pkgconfig sed -e 's|@ARG_PREFIX@|$(TARGET_PREFIX)|g' src/libcmt.pc > $(TARGET_DESTDIR)$(TARGET_PREFIX)/lib/pkgconfig/libcmt.pc @@ -126,7 +126,7 @@ install-mock: $(mock_LIB) $(mock_SO) build/ffi.h mkdir -p $(DESTDIR)$(PREFIX)/lib cp -f $(mock_LIB) $(mock_SO) $(DESTDIR)$(PREFIX)/lib mkdir -p $(DESTDIR)$(PREFIX)/include/libcmt/ - cp -f src/*.h $(DESTDIR)$(PREFIX)/include/libcmt/ + cp -f include/libcmt/*.h $(DESTDIR)$(PREFIX)/include/libcmt/ cp -f build/ffi.h $(DESTDIR)$(PREFIX)/include/libcmt/ mkdir -p $(DESTDIR)$(PREFIX)/lib/pkgconfig sed -e 's|@ARG_PREFIX@|$(PREFIX)|g' src/libcmt_mock.pc > $(DESTDIR)$(PREFIX)/lib/pkgconfig/libcmt.pc @@ -183,7 +183,8 @@ $(tools_OBJDIR)/funsel: tools/funsel.c $(mock_LIB) tools: $(tools_BINS) -build/ffi.h: src/buf.h src/abi.h src/keccak.h src/merkle.h src/io.h src/rollup.h +HDRS := $(patsubst %,include/libcmt/%, buf.h abi.h keccak.h merkle.h io.h rollup.h) +build/ffi.h: $(HDRS) cat $^ | sh tools/prepare-ffi.sh > $@ #------------------------------------------------------------------------------- LINTER_IGNORE_SOURCES=src/io.c diff --git a/sys-utils/libcmt/doc/examples/abi_decode_000.c b/sys-utils/libcmt/doc/examples/abi_decode_000.c index 28324d3c..f38933b5 100644 --- a/sys-utils/libcmt/doc/examples/abi_decode_000.c +++ b/sys-utils/libcmt/doc/examples/abi_decode_000.c @@ -1,7 +1,7 @@ #include "libcmt/abi.h" #include "libcmt/buf.h" -int decode_address(cmt_buf_t *tx, uint32_t expected_funsel, uint8_t address[CMT_ADDRESS_LENGTH]) { +int decode_address(cmt_buf_t *tx, uint32_t expected_funsel, cmt_abi_address_t *address) { cmt_buf_t wr = *tx; return cmt_abi_check_funsel(&wr, expected_funsel) || cmt_abi_get_address(&wr, address); diff --git a/sys-utils/libcmt/doc/examples/abi_decode_001.c b/sys-utils/libcmt/doc/examples/abi_decode_001.c index 4b56b70a..59f37b59 100644 --- a/sys-utils/libcmt/doc/examples/abi_decode_001.c +++ b/sys-utils/libcmt/doc/examples/abi_decode_001.c @@ -1,9 +1,11 @@ #include "libcmt/abi.h" #include "libcmt/buf.h" -int decode_address(cmt_buf_t *tx, uint32_t expected_funsel, uint8_t address[CMT_ADDRESS_LENGTH]) { - cmt_buf_t wr = *tx; - return cmt_abi_check_funsel(&wr, expected_funsel) - || cmt_abi_get_address(&wr, address); +int decode(cmt_buf_t *rx, uint32_t expected_funsel, cmt_abi_address_t *address, cmt_abi_u256_t *value) { + cmt_buf_t rd = *rx; + return cmt_abi_check_funsel(&rd, expected_funsel) + || cmt_abi_get_address(&rd, address) + || cmt_abi_get_uint256(&rd, value) + ; } diff --git a/sys-utils/libcmt/doc/examples/abi_decode_002.c b/sys-utils/libcmt/doc/examples/abi_decode_002.c index 4b56b70a..daa96014 100644 --- a/sys-utils/libcmt/doc/examples/abi_decode_002.c +++ b/sys-utils/libcmt/doc/examples/abi_decode_002.c @@ -1,9 +1,18 @@ #include "libcmt/abi.h" #include "libcmt/buf.h" -int decode_address(cmt_buf_t *tx, uint32_t expected_funsel, uint8_t address[CMT_ADDRESS_LENGTH]) { - cmt_buf_t wr = *tx; - return cmt_abi_check_funsel(&wr, expected_funsel) - || cmt_abi_get_address(&wr, address); +int decode(cmt_buf_t *rx, uint32_t expected_funsel, cmt_abi_address_t *address, cmt_abi_bytes_t *payload) { + cmt_buf_t rd = *rx; + cmt_buf_t frame; + cmt_buf_t offset; + + /* static section */ + return cmt_abi_check_funsel(&rd, expected_funsel) + || cmt_abi_mark_frame(&rd, &frame) + || cmt_abi_get_address(&rd, address) + || cmt_abi_get_bytes_s(&rd, &offset) + /* dynamic section */ + || cmt_abi_get_bytes_d(&rd, &offset, &payload->length, &payload->data) + ; } diff --git a/sys-utils/libcmt/doc/examples/abi_encode_000.c b/sys-utils/libcmt/doc/examples/abi_encode_000.c index 64bf6af1..896a6ae7 100644 --- a/sys-utils/libcmt/doc/examples/abi_encode_000.c +++ b/sys-utils/libcmt/doc/examples/abi_encode_000.c @@ -1,8 +1,9 @@ #include "libcmt/abi.h" #include "libcmt/buf.h" -int encode_address(cmt_buf_t *tx, uint32_t funsel, uint8_t address[CMT_ADDRESS_LENGTH]) { +int encode_address(cmt_buf_t *tx, uint32_t funsel, cmt_abi_address_t *address) { cmt_buf_t wr = *tx; + /* static section */ return cmt_abi_put_funsel(&wr, funsel) || cmt_abi_put_address(&wr, address); } diff --git a/sys-utils/libcmt/doc/examples/abi_encode_001.c b/sys-utils/libcmt/doc/examples/abi_encode_001.c index 405ed250..3943a89f 100644 --- a/sys-utils/libcmt/doc/examples/abi_encode_001.c +++ b/sys-utils/libcmt/doc/examples/abi_encode_001.c @@ -1,14 +1,15 @@ #include "libcmt/abi.h" #include "libcmt/buf.h" -int encode_bytes(cmt_buf_t *tx, uint32_t funsel, size_t bytes_length, void *bytes_data) { +int encode_bytes(cmt_buf_t *tx, uint32_t funsel, cmt_abi_bytes_t *payload) { cmt_buf_t wr = *tx; cmt_buf_t of; - void *params_base = wr.begin + 4; // after funsel + cmt_buf_t frame; // static section return cmt_abi_put_funsel(&wr, funsel) + || cmt_abi_mark_frame(&wr, &frame) || cmt_abi_put_bytes_s(&wr, &of) // dynamic section - || cmt_abi_put_bytes_d(&wr, &of, bytes_length, bytes_data, params_base); + || cmt_abi_put_bytes_d(&wr, &of, &frame, payload); } diff --git a/sys-utils/libcmt/doc/examples/abi_encode_002.c b/sys-utils/libcmt/doc/examples/abi_encode_002.c index abe627fb..c0fce8fd 100644 --- a/sys-utils/libcmt/doc/examples/abi_encode_002.c +++ b/sys-utils/libcmt/doc/examples/abi_encode_002.c @@ -1,20 +1,19 @@ #include "libcmt/abi.h" #include "libcmt/buf.h" -int encode_bytes(cmt_buf_t *tx, uint32_t funsel, - size_t bytes0_length, void *bytes0_data, - size_t bytes1_length, void *bytes1_data) { +int encode_bytes(cmt_buf_t *tx, uint32_t funsel, cmt_abi_bytes_t *payload0, cmt_abi_bytes_t *payload1) { cmt_buf_t wr = *tx; cmt_buf_t of[2]; - void *params_base = wr.begin + 4; // after funsel + cmt_buf_t frame; // static section return cmt_abi_put_funsel(&wr, funsel) + || cmt_abi_mark_frame(&wr, &frame) || cmt_abi_put_bytes_s(&wr, &of[0]) || cmt_abi_put_bytes_s(&wr, &of[1]) // dynamic section - || cmt_abi_put_bytes_d(&wr, &of[0], bytes0_length, bytes0_data, params_base) - || cmt_abi_put_bytes_d(&wr, &of[1], bytes1_length, bytes1_data, params_base) + || cmt_abi_put_bytes_d(&wr, &frame, &of[0], payload0) + || cmt_abi_put_bytes_d(&wr, &frame, &of[1], payload1) ; } diff --git a/sys-utils/libcmt/doc/examples/io.c b/sys-utils/libcmt/doc/examples/io.c index 154dc1a6..00188a93 100644 --- a/sys-utils/libcmt/doc/examples/io.c +++ b/sys-utils/libcmt/doc/examples/io.c @@ -21,7 +21,7 @@ int exception(union cmt_io_driver *io) { /* exception -------------------------------------------------------- */ struct cmt_io_yield req[1] = {{ .dev = HTIF_DEVICE_YIELD, - .cmd = HTIF_YIELD_MANUAL, + .cmd = HTIF_YIELD_CMD_MANUAL, .reason = HTIF_YIELD_MANUAL_REASON_TX_EXCEPTION, .data = n, }}; diff --git a/sys-utils/libcmt/doc/examples/rollup.c b/sys-utils/libcmt/doc/examples/rollup.c index 13caa009..a198896a 100644 --- a/sys-utils/libcmt/doc/examples/rollup.c +++ b/sys-utils/libcmt/doc/examples/rollup.c @@ -24,13 +24,13 @@ int main(void) { break; } - rc = cmt_rollup_emit_voucher(&rollup, sizeof advance.sender, advance.sender, 0, NULL, advance.length, advance.data, NULL); + rc = cmt_rollup_emit_voucher(&rollup, &advance.msg_sender, NULL, &advance.payload, NULL); if (rc < 0) { fprintf(stderr, "%s:%d Error on voucher %s (%d)\n", __FILE__, __LINE__, strerror(-rc), (-rc)); break; } - rc = cmt_rollup_emit_notice(&rollup, advance.length, advance.data, NULL); + rc = cmt_rollup_emit_notice(&rollup, &advance.payload, NULL); if (rc < 0) { fprintf(stderr, "%s:%d Error on voucher %s (%d)\n", __FILE__, __LINE__, strerror(-rc), (-rc)); break; diff --git a/sys-utils/libcmt/src/abi.h b/sys-utils/libcmt/include/libcmt/abi.h similarity index 84% rename from sys-utils/libcmt/src/abi.h rename to sys-utils/libcmt/include/libcmt/abi.h index 936c680d..85153173 100644 --- a/sys-utils/libcmt/src/abi.h +++ b/sys-utils/libcmt/include/libcmt/abi.h @@ -115,8 +115,8 @@ #include enum { - CMT_WORD_LENGTH = 32, /**< length of a evm word in bytes */ - CMT_ADDRESS_LENGTH = 20, /**< length of a evm address in bytes */ + CMT_ABI_U256_LENGTH = 32, /**< length of a evm word in bytes */ + CMT_ABI_ADDRESS_LENGTH = 20, /**< length of a evm address in bytes */ }; /** Compile time equivalent to @ref cmt_abi_funsel @@ -129,7 +129,20 @@ enum { (((uint32_t) (A) << 000) | ((uint32_t) (B) << 010) | ((uint32_t) (C) << 020) | ((uint32_t) (D) << 030)) #endif -// put section --------------------------------------------------------------- +/** EVM address */ +typedef struct cmt_abi_address { + uint8_t data[CMT_ABI_ADDRESS_LENGTH]; +} cmt_abi_address_t; + +/** EVM u256 in big endian format */ +typedef struct cmt_abi_u256 { + uint8_t data[CMT_ABI_U256_LENGTH]; +} cmt_abi_u256_t; + +typedef struct cmt_abi_bytes { + size_t length; + void *data; +} cmt_abi_bytes_t; /** Create a function selector from an array of bytes * @param [in] funsel function selector bytes @@ -137,6 +150,20 @@ enum { * - function selector converted to big endian (as expected by EVM) */ uint32_t cmt_abi_funsel(uint8_t a, uint8_t b, uint8_t c, uint8_t d); +/** Create a frame for the dynamic section. Read the EVM ABI for the details + * @param [in] me reader or writer buffer + * @param [out] frame start of the parameters frame + * + * @return + * | | | + * |--:|-----------------------------| + * | 0| success | + * |< 0| failure with a -errno value | + */ +int cmt_abi_mark_frame(const cmt_buf_t *me, cmt_buf_t *frame); + +// put section --------------------------------------------------------------- + /** Encode a function selector into the buffer @p me * * @param [in,out] me a initialized buffer working as iterator @@ -152,7 +179,8 @@ uint32_t cmt_abi_funsel(uint8_t a, uint8_t b, uint8_t c, uint8_t d); * It is always represented in big endian. */ int cmt_abi_put_funsel(cmt_buf_t *me, uint32_t funsel); -/** Encode a unsigned integer of up to 32bytes of data into the buffer +/** Encode a native endianness unsigned integer of up to 32bytes of data into + * the buffer * * @param [in,out] me a initialized buffer working as iterator * @param [in] n size of @p data in bytes @@ -172,11 +200,11 @@ int cmt_abi_put_funsel(cmt_buf_t *me, uint32_t funsel); * uint64_t x = UINT64_C(0xdeadbeef); * cmt_abi_put_uint(&it, sizeof x, &x); * ... - * @endcode - * @note This function takes care of endianness conversions */ + * @endcode */ int cmt_abi_put_uint(cmt_buf_t *me, size_t data_length, const void *data); -/** Encode a big-endian value of up to 32bytes of data into the buffer +/** Encode a big endian unsigned integer of up to 32bytes of data into the + * buffer * * @param [in,out] me a initialized buffer working as iterator * @param [in] length size of @p data in bytes @@ -211,6 +239,18 @@ int cmt_abi_put_uint(cmt_buf_t *me, size_t data_length, const void *data); * @note This function takes care of endianness conversions */ int cmt_abi_put_uint_be(cmt_buf_t *me, size_t data_length, const void *data); +/** Encode a @ref cmt_abi_u256_t into the buffer + * + * @param [in,out] me a initialized buffer working as iterator + * @param [in] data pointer to a @ref cmt_abi_u256_t + * + * @return + * | | | + * |-------:|---------------------------------------------------| + * | 0| success | + * |-ENOBUFS| no space left in @p me | */ +int cmt_abi_put_uint256(cmt_buf_t *me, const cmt_abi_u256_t *value); + /** Encode a bool into the buffer * * @param [in,out] me a initialized buffer working as iterator @@ -231,17 +271,17 @@ int cmt_abi_put_uint_be(cmt_buf_t *me, size_t data_length, const void *data); * @note This function takes care of endianness conversions */ int cmt_abi_put_bool(cmt_buf_t *me, bool value); -/** Encode @p address (exactly @ref CMT_ADDRESS_LENGTH bytes) into the buffer +/** Encode @p address (exactly @ref CMT_ABI_ADDRESS_LENGTH bytes) into the buffer * * @param [in,out] me initialized buffer - * @param [in] address exactly @ref CMT_ADDRESS_LENGTH bytes + * @param [in] address a value of type @ref cmt_abi_address_t * * @return * | | | * |-------:|------------------------| * | 0| success | * |-ENOBUFS| no space left in @p me | */ -int cmt_abi_put_address(cmt_buf_t *me, const uint8_t address[CMT_ADDRESS_LENGTH]); +int cmt_abi_put_address(cmt_buf_t *me, const cmt_abi_address_t *address); /** Encode the static part of @b bytes into the message, * used in conjunction with @ref cmt_abi_put_bytes_d @@ -270,7 +310,8 @@ int cmt_abi_put_bytes_s(cmt_buf_t *me, cmt_buf_t *offset); * |-------:|------------------------| * | 0| success | * |-ENOBUFS| no space left in @p me | */ -int cmt_abi_put_bytes_d(cmt_buf_t *me, cmt_buf_t *offset, size_t n, const void *data, const void *start); +//int cmt_abi_put_bytes_d(cmt_buf_t *me, cmt_buf_t *offset, size_t n, const void *data, const void *start); +int cmt_abi_put_bytes_d(cmt_buf_t *me, cmt_buf_t *offset, const cmt_buf_t *frame, const cmt_abi_bytes_t *payload); /** Reserve @b n bytes of data from the buffer into @b res to be filled by the * caller @@ -327,7 +368,19 @@ uint32_t cmt_abi_peek_funsel(cmt_buf_t *me); * |-EBADMSG| funsel mismatch | */ int cmt_abi_check_funsel(cmt_buf_t *me, uint32_t expected); -/** Decode a unsigned integer of up to 32bytes from the buffer +/** Decode a @ref cmt_abi_u256_t from the buffer + * + * @param [in,out] me initialized buffer + * @param [out] data value of type @ref cmt_abi_u256_t + * + * @return + * | | | + * |-------:|---------------------------------------------------| + * | 0| success | + * |-ENOBUFS| no space left in @p me | */ +int cmt_abi_get_uint256(cmt_buf_t *me, cmt_abi_u256_t *value); + +/** Decode a unsigned integer of up to 32bytes, in native endianness, from the buffer * * @param [in,out] me initialized buffer * @param [in] n size of @p data in bytes @@ -380,14 +433,25 @@ int cmt_abi_get_bool(cmt_buf_t *me, bool *value); /** Consume and decode @b address from the buffer * * @param [in,out] me initialized buffer - * @param [out] address exactly 20 bytes + * @param [out] address value of type @ref cmt_abi_address_t + * + * @return + * | | | + * |-------:|------------------------| + * | 0| success | + * |-ENOBUFS| no space left in @p me | */ +int cmt_abi_get_address(cmt_buf_t *me, cmt_abi_address_t *value); + +/** Create a frame of reference for the dynamic section * + * @param [in,out] me initialized buffer + * @param [out] frame used when encoding dynamic values * @return * | | | * |-------:|------------------------| * | 0| success | * |-ENOBUFS| no space left in @p me | */ -int cmt_abi_get_address(cmt_buf_t *me, uint8_t address[CMT_ADDRESS_LENGTH]); +int cmt_abi_start_frame(cmt_buf_t *me, void *frame); /** Consume and decode the offset @p of * @@ -446,7 +510,7 @@ int cmt_abi_peek_bytes_d(const cmt_buf_t *start, cmt_buf_t of[1], cmt_buf_t *byt * |-------:|---------------------------------------------------| * | 0| success | * | -EDOM| integer not representable in @p data_length bytes | */ -int cmt_abi_encode_uint(size_t n, const void *data, uint8_t out[CMT_WORD_LENGTH]); +int cmt_abi_encode_uint(size_t n, const void *data, uint8_t out[CMT_ABI_U256_LENGTH]); /** Encode @p n bytes of @p data into @p out (up to 32) in reverse order. * @@ -461,7 +525,7 @@ int cmt_abi_encode_uint(size_t n, const void *data, uint8_t out[CMT_WORD_LENGTH] * | -EDOM| integer not representable in @p data_length bytes | * * @note use @ref cmt_abi_encode_uint instead */ -int cmt_abi_encode_uint_nr(size_t n, const uint8_t *data, uint8_t out[CMT_WORD_LENGTH]); +int cmt_abi_encode_uint_nr(size_t n, const uint8_t *data, uint8_t out[CMT_ABI_U256_LENGTH]); /** Encode @p n bytes of @p data into @p out (up to 32) in normal order. * @@ -476,7 +540,7 @@ int cmt_abi_encode_uint_nr(size_t n, const uint8_t *data, uint8_t out[CMT_WORD_L * | -EDOM| integer not representable in @p data_length bytes | * * @note use @ref cmt_abi_encode_uint instead */ -int cmt_abi_encode_uint_nn(size_t n, const uint8_t *data, uint8_t out[CMT_WORD_LENGTH]); +int cmt_abi_encode_uint_nn(size_t n, const uint8_t *data, uint8_t out[CMT_ABI_U256_LENGTH]); /** Decode @p n bytes of @p data into @p out (up to 32). * @@ -489,7 +553,7 @@ int cmt_abi_encode_uint_nn(size_t n, const uint8_t *data, uint8_t out[CMT_WORD_L * |-------:|---------------------------------------------------| * | 0| success | * | -EDOM| integer not representable in @p data_length bytes | */ -int cmt_abi_decode_uint(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t *out); +int cmt_abi_decode_uint(const uint8_t data[CMT_ABI_U256_LENGTH], size_t n, uint8_t *out); /** Decode @p n bytes of @p data into @p out (up to 32) in reverse order. * @@ -504,7 +568,7 @@ int cmt_abi_decode_uint(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t * * | -EDOM| integer not representable in @p data_length bytes | * * @note if in doubt, use @ref cmt_abi_decode_uint */ -int cmt_abi_decode_uint_nr(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t *out); +int cmt_abi_decode_uint_nr(const uint8_t data[CMT_ABI_U256_LENGTH], size_t n, uint8_t *out); /** Decode @p n bytes of @p data into @p out (up to 32) in normal order. * @@ -519,7 +583,7 @@ int cmt_abi_decode_uint_nr(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_ * | -EDOM| integer not representable in @p data_length bytes | * * @note if in doubt, use @ref cmt_abi_decode_uint */ -int cmt_abi_decode_uint_nn(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t *out); +int cmt_abi_decode_uint_nn(const uint8_t data[CMT_ABI_U256_LENGTH], size_t n, uint8_t *out); #endif /* CMT_ABI_H */ /** @} */ diff --git a/sys-utils/libcmt/src/buf.h b/sys-utils/libcmt/include/libcmt/buf.h similarity index 100% rename from sys-utils/libcmt/src/buf.h rename to sys-utils/libcmt/include/libcmt/buf.h diff --git a/sys-utils/libcmt/src/io.h b/sys-utils/libcmt/include/libcmt/io.h similarity index 100% rename from sys-utils/libcmt/src/io.h rename to sys-utils/libcmt/include/libcmt/io.h diff --git a/sys-utils/libcmt/src/keccak.h b/sys-utils/libcmt/include/libcmt/keccak.h similarity index 100% rename from sys-utils/libcmt/src/keccak.h rename to sys-utils/libcmt/include/libcmt/keccak.h diff --git a/sys-utils/libcmt/src/merkle.h b/sys-utils/libcmt/include/libcmt/merkle.h similarity index 100% rename from sys-utils/libcmt/src/merkle.h rename to sys-utils/libcmt/include/libcmt/merkle.h diff --git a/sys-utils/libcmt/src/rollup.h b/sys-utils/libcmt/include/libcmt/rollup.h similarity index 87% rename from sys-utils/libcmt/src/rollup.h rename to sys-utils/libcmt/include/libcmt/rollup.h index 80c46ffb..d50e7266 100644 --- a/sys-utils/libcmt/src/rollup.h +++ b/sys-utils/libcmt/include/libcmt/rollup.h @@ -44,19 +44,18 @@ typedef struct cmt_rollup { /** Public struct with the advance state contents */ typedef struct cmt_rollup_advance { uint64_t chain_id; /**< network */ - uint8_t app_contract[CMT_ADDRESS_LENGTH]; /**< application contract address */ - uint8_t msg_sender[CMT_ADDRESS_LENGTH]; /**< input sender address */ + cmt_abi_address_t app_contract; /**< application contract address */ + cmt_abi_address_t msg_sender; /**< input sender address */ uint64_t block_number; /**< block number of this input */ uint64_t block_timestamp; /**< block timestamp of this input UNIX epoch format) */ + cmt_abi_u256_t prev_randao; /**< The latest RANDAO mix of the post beacon state of the previous block */ uint64_t index; /**< input index (in relation to all inputs ever sent to the DApp) */ - uint32_t payload_length; /**< length in bytes of the payload field */ - void *payload; /**< payload for this input */ + cmt_abi_bytes_t payload; /**< payload for this input */ } cmt_rollup_advance_t; /** Public struct with the inspect state contents */ typedef struct cmt_rollup_inspect { - uint32_t payload_length; /**< length in bytes of the payload field */ - void *payload; /**< payload for this query */ + cmt_abi_bytes_t payload; /**< payload for this input */ } cmt_rollup_inspect_t; /** Public struct with the finish state contents */ @@ -100,11 +99,8 @@ void cmt_rollup_fini(cmt_rollup_t *me); * Equivalent to the `Voucher(address,uint256,bytes)` solidity call. * * @param [in,out] me initialized @ref cmt_rollup_t instance - * @param [in] address_length destination length in bytes * @param [in] address destination data - * @param [in] value_length value length in bytes * @param [in] value value data - * @param [in] data_length data length in bytes * @param [in] data message contents * @param [out] index index of emitted voucher, if successful * @@ -113,13 +109,11 @@ void cmt_rollup_fini(cmt_rollup_t *me); * |--:|-----------------------------| * | 0| success | * |< 0| failure with a -errno value | */ -int cmt_rollup_emit_voucher(cmt_rollup_t *me, uint32_t address_length, const void *address_data, uint32_t value_length, - const void *value_data, uint32_t length, const void *data, uint64_t *index); +int cmt_rollup_emit_voucher(cmt_rollup_t *me, const cmt_abi_address_t *address, const cmt_abi_u256_t *value, const cmt_abi_bytes_t *data, uint64_t *index); /** Emit a notice * * @param [in,out] me initialized cmt_rollup_t instance - * @param [in] data_length data length in bytes * @param [in] data message contents * @param [out] index index of emitted notice, if successful * @@ -128,7 +122,7 @@ int cmt_rollup_emit_voucher(cmt_rollup_t *me, uint32_t address_length, const voi * |--:|-----------------------------| * | 0| success | * |< 0| failure with a -errno value | */ -int cmt_rollup_emit_notice(cmt_rollup_t *me, uint32_t data_length, const void *data, uint64_t *index); +int cmt_rollup_emit_notice(cmt_rollup_t *me, const cmt_abi_bytes_t *payload, uint64_t *index); /** Emit a report * @param [in,out] me initialized cmt_rollup_t instance @@ -140,7 +134,7 @@ int cmt_rollup_emit_notice(cmt_rollup_t *me, uint32_t data_length, const void *d * |--:|-----------------------------| * | 0| success | * |< 0| failure with a -errno value | */ -int cmt_rollup_emit_report(cmt_rollup_t *me, uint32_t data_length, const void *data); +int cmt_rollup_emit_report(cmt_rollup_t *me, const cmt_abi_bytes_t *payload); /** Emit a exception * @param [in,out] me initialized cmt_rollup_t instance @@ -152,7 +146,7 @@ int cmt_rollup_emit_report(cmt_rollup_t *me, uint32_t data_length, const void *d * |--:|-----------------------------| * | 0| success | * |< 0| failure with a -errno value | */ -int cmt_rollup_emit_exception(cmt_rollup_t *me, uint32_t data_length, const void *data); +int cmt_rollup_emit_exception(cmt_rollup_t *me, const cmt_abi_bytes_t *payload); /** Report progress * diff --git a/sys-utils/libcmt/src/util.h b/sys-utils/libcmt/include/libcmt/util.h similarity index 100% rename from sys-utils/libcmt/src/util.h rename to sys-utils/libcmt/include/libcmt/util.h diff --git a/sys-utils/libcmt/src/abi.c b/sys-utils/libcmt/src/abi.c index ac33963c..bfa1d971 100644 --- a/sys-utils/libcmt/src/abi.c +++ b/sys-utils/libcmt/src/abi.c @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "abi.h" +#include "libcmt/abi.h" #include #include @@ -26,6 +26,14 @@ uint32_t cmt_abi_funsel(uint8_t a, uint8_t b, uint8_t c, uint8_t d) { return CMT_ABI_FUNSEL(a, b, c, d); } +int cmt_abi_mark_frame(const cmt_buf_t *me, cmt_buf_t *frame) { + if (!me || !frame) { + return -EINVAL; + } + *frame = *me; + return 0; +} + int cmt_abi_put_funsel(cmt_buf_t *me, uint32_t funsel) { cmt_buf_t x[1]; int rc = cmt_buf_split(me, sizeof(funsel), x, me); @@ -37,33 +45,33 @@ int cmt_abi_put_funsel(cmt_buf_t *me, uint32_t funsel) { return 0; } -int cmt_abi_encode_uint_nr(size_t n, const uint8_t *data, uint8_t out[CMT_WORD_LENGTH]) { - if (n > CMT_WORD_LENGTH) { +int cmt_abi_encode_uint_nr(size_t n, const uint8_t *data, uint8_t out[CMT_ABI_U256_LENGTH]) { + if (n > CMT_ABI_U256_LENGTH) { return -EDOM; } for (size_t i = 0; i < n; ++i) { - out[CMT_WORD_LENGTH - 1 - i] = data[i]; + out[CMT_ABI_U256_LENGTH - 1 - i] = data[i]; } - for (size_t i = n; i < CMT_WORD_LENGTH; ++i) { - out[CMT_WORD_LENGTH - 1 - i] = 0; + for (size_t i = n; i < CMT_ABI_U256_LENGTH; ++i) { + out[CMT_ABI_U256_LENGTH - 1 - i] = 0; } return 0; } -int cmt_abi_encode_uint_nn(size_t n, const uint8_t *data, uint8_t out[CMT_WORD_LENGTH]) { - if (n > CMT_WORD_LENGTH) { +int cmt_abi_encode_uint_nn(size_t n, const uint8_t *data, uint8_t out[CMT_ABI_U256_LENGTH]) { + if (n > CMT_ABI_U256_LENGTH) { return -EDOM; } - for (size_t i = 0; i < CMT_WORD_LENGTH - n; ++i) { + for (size_t i = 0; i < CMT_ABI_U256_LENGTH - n; ++i) { out[i] = 0; } - for (size_t i = CMT_WORD_LENGTH - n; i < CMT_WORD_LENGTH; ++i) { - out[i] = data[i - CMT_WORD_LENGTH + n]; + for (size_t i = CMT_ABI_U256_LENGTH - n; i < CMT_ABI_U256_LENGTH; ++i) { + out[i] = data[i - CMT_ABI_U256_LENGTH + n]; } return 0; } -int cmt_abi_encode_uint(size_t n, const void *data, uint8_t out[CMT_WORD_LENGTH]) { +int cmt_abi_encode_uint(size_t n, const void *data, uint8_t out[CMT_ABI_U256_LENGTH]) { #if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__ return cmt_abi_encode_uint_nn(n, data, out); #else @@ -71,37 +79,37 @@ int cmt_abi_encode_uint(size_t n, const void *data, uint8_t out[CMT_WORD_LENGTH] #endif } -int cmt_abi_decode_uint_nr(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t *out) { - if (n > CMT_WORD_LENGTH) { +int cmt_abi_decode_uint_nr(const uint8_t data[CMT_ABI_U256_LENGTH], size_t n, uint8_t *out) { + if (n > CMT_ABI_U256_LENGTH) { return -EDOM; } - for (size_t i = 0; i < CMT_WORD_LENGTH - n; ++i) { + for (size_t i = 0; i < CMT_ABI_U256_LENGTH - n; ++i) { if (data[i]) { return -EDOM; } } - for (size_t i = CMT_WORD_LENGTH - n; i < CMT_WORD_LENGTH; ++i) { - out[CMT_WORD_LENGTH - 1 - i] = data[i]; + for (size_t i = CMT_ABI_U256_LENGTH - n; i < CMT_ABI_U256_LENGTH; ++i) { + out[CMT_ABI_U256_LENGTH - 1 - i] = data[i]; } return 0; } -int cmt_abi_decode_uint_nn(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t *out) { - if (n > CMT_WORD_LENGTH) { +int cmt_abi_decode_uint_nn(const uint8_t data[CMT_ABI_U256_LENGTH], size_t n, uint8_t *out) { + if (n > CMT_ABI_U256_LENGTH) { return -EDOM; } - for (size_t i = 0; i < CMT_WORD_LENGTH - n; ++i) { + for (size_t i = 0; i < CMT_ABI_U256_LENGTH - n; ++i) { if (data[i]) { return -EDOM; } } - for (size_t i = CMT_WORD_LENGTH - n; i < CMT_WORD_LENGTH; ++i) { - out[i - CMT_WORD_LENGTH + n] = data[i]; + for (size_t i = CMT_ABI_U256_LENGTH - n; i < CMT_ABI_U256_LENGTH; ++i) { + out[i - CMT_ABI_U256_LENGTH + n] = data[i]; } return 0; } -int cmt_abi_decode_uint(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t *out) { +int cmt_abi_decode_uint(const uint8_t data[CMT_ABI_U256_LENGTH], size_t n, uint8_t *out) { #if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__ return cmt_abi_decode_uint_nn(data, n, out); #else @@ -111,10 +119,10 @@ int cmt_abi_decode_uint(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t * int cmt_abi_put_uint(cmt_buf_t *me, size_t data_length, const void *data) { cmt_buf_t x[1]; - if (data_length > CMT_WORD_LENGTH) { + if (data_length > CMT_ABI_U256_LENGTH) { return -EDOM; } - if (cmt_buf_split(me, CMT_WORD_LENGTH, x, me)) { + if (cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me)) { return -ENOBUFS; } return cmt_abi_encode_uint(data_length, data, x->begin); @@ -122,39 +130,46 @@ int cmt_abi_put_uint(cmt_buf_t *me, size_t data_length, const void *data) { int cmt_abi_put_uint_be(cmt_buf_t *me, size_t data_length, const void *data) { cmt_buf_t x[1]; - if (data_length > CMT_WORD_LENGTH) { + if (data_length > CMT_ABI_U256_LENGTH) { return -EDOM; } - if (cmt_buf_split(me, CMT_WORD_LENGTH, x, me)) { + if (cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me)) { return -ENOBUFS; } return cmt_abi_encode_uint_nn(data_length, data, x->begin); } +int cmt_abi_put_uint256(cmt_buf_t *me, const cmt_abi_u256_t *value) { + cmt_buf_t x[1]; + if (cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me)) { + return -ENOBUFS; + } + return cmt_abi_encode_uint_nn(sizeof(*value), value->data, x->begin); +} int cmt_abi_put_bool(cmt_buf_t *me, bool value) { uint8_t boolean = !!value; return cmt_abi_put_uint(me, sizeof(boolean), &boolean); } -int cmt_abi_put_address(cmt_buf_t *me, const uint8_t address[20]) { +int cmt_abi_put_address(cmt_buf_t *me, const cmt_abi_address_t *address) { cmt_buf_t x[1]; - if (cmt_buf_split(me, CMT_WORD_LENGTH, x, me)) { + if (cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me)) { return -ENOBUFS; } - return cmt_abi_encode_uint_nn(CMT_ADDRESS_LENGTH, address, x->begin); + return cmt_abi_encode_uint_nn(sizeof(*address), address->data, x->begin); } int cmt_abi_put_bytes_s(cmt_buf_t *me, cmt_buf_t *offset) { - return cmt_buf_split(me, CMT_WORD_LENGTH, offset, me); + return cmt_buf_split(me, CMT_ABI_U256_LENGTH, offset, me); } int cmt_abi_reserve_bytes_d(cmt_buf_t *me, cmt_buf_t *of, size_t n, cmt_buf_t *out, const void *start) { int rc = 0; cmt_buf_t tmp[1]; cmt_buf_t sz[1]; - size_t n32 = align_forward(n, CMT_WORD_LENGTH); + size_t n32 = align_forward(n, CMT_ABI_U256_LENGTH); - rc = cmt_buf_split(me, CMT_WORD_LENGTH, sz, tmp); + rc = cmt_buf_split(me, CMT_ABI_U256_LENGTH, sz, tmp); if (rc) { return rc; } @@ -179,14 +194,14 @@ int cmt_abi_reserve_bytes_d(cmt_buf_t *me, cmt_buf_t *of, size_t n, cmt_buf_t *o return 0; } -int cmt_abi_put_bytes_d(cmt_buf_t *me, cmt_buf_t *offset, size_t n, const void *data, const void *start) { +int cmt_abi_put_bytes_d(cmt_buf_t *me, cmt_buf_t *offset, const cmt_buf_t *frame, const cmt_abi_bytes_t *payload) { cmt_buf_t res[1]; - int rc = cmt_abi_reserve_bytes_d(me, offset, n, res, start); + int rc = cmt_abi_reserve_bytes_d(me, offset, payload->length, res, frame->begin); if (rc) { return rc; } // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling) - memcpy(res->begin, data, n); + memcpy(res->begin, payload->data, payload->length); return 0; } @@ -213,10 +228,10 @@ int cmt_abi_check_funsel(cmt_buf_t *me, uint32_t expected) { int cmt_abi_get_uint(cmt_buf_t *me, size_t n, void *data) { cmt_buf_t x[1]; - if (n > CMT_WORD_LENGTH) { + if (n > CMT_ABI_U256_LENGTH) { return -EDOM; } - int rc = cmt_buf_split(me, CMT_WORD_LENGTH, x, me); + int rc = cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me); if (rc) { return rc; } @@ -227,10 +242,10 @@ int cmt_abi_get_uint(cmt_buf_t *me, size_t n, void *data) { int cmt_abi_get_uint_be(cmt_buf_t *me, size_t n, void *data) { cmt_buf_t x[1]; - if (n > CMT_WORD_LENGTH) { + if (n > CMT_ABI_U256_LENGTH) { return -EDOM; } - int rc = cmt_buf_split(me, CMT_WORD_LENGTH, x, me); + int rc = cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me); if (rc) { return rc; } @@ -238,6 +253,14 @@ int cmt_abi_get_uint_be(cmt_buf_t *me, size_t n, void *data) { return cmt_abi_decode_uint_nn(x->begin, n, data); } +int cmt_abi_get_uint256(cmt_buf_t *me, cmt_abi_u256_t *value) { + cmt_buf_t x[1]; + if (cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me)) { + return -ENOBUFS; + } + return cmt_abi_decode_uint_nn(x->begin, sizeof(*value), value->data); +} + int cmt_abi_get_bool(cmt_buf_t *me, bool *value) { bool boolean = 0; int rc = cmt_abi_get_uint(me, sizeof(boolean), &boolean); @@ -248,19 +271,18 @@ int cmt_abi_get_bool(cmt_buf_t *me, bool *value) { return 0; } -int cmt_abi_get_address(cmt_buf_t *me, uint8_t address[CMT_ADDRESS_LENGTH]) { +int cmt_abi_get_address(cmt_buf_t *me, cmt_abi_address_t *address) { cmt_buf_t x[1]; - int rc = cmt_buf_split(me, CMT_WORD_LENGTH, x, me); + int rc = cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me); if (rc) { return rc; } - - return cmt_abi_decode_uint_nn(x->begin, CMT_ADDRESS_LENGTH, address); + return cmt_abi_decode_uint_nn(x->begin, sizeof(*address), address->data); } int cmt_abi_get_bytes_s(cmt_buf_t *me, cmt_buf_t of[1]) { - return cmt_buf_split(me, CMT_WORD_LENGTH, of, me); + return cmt_buf_split(me, CMT_ABI_U256_LENGTH, of, me); } int cmt_abi_peek_bytes_d(const cmt_buf_t *start, cmt_buf_t of[1], cmt_buf_t *bytes) { diff --git a/sys-utils/libcmt/src/buf.c b/sys-utils/libcmt/src/buf.c index 63c828dc..e2cbf5ae 100644 --- a/sys-utils/libcmt/src/buf.c +++ b/sys-utils/libcmt/src/buf.c @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "buf.h" +#include "libcmt/buf.h" #include #include diff --git a/sys-utils/libcmt/src/io-mock.c b/sys-utils/libcmt/src/io-mock.c index e40b2439..83380080 100644 --- a/sys-utils/libcmt/src/io-mock.c +++ b/sys-utils/libcmt/src/io-mock.c @@ -13,8 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "io.h" -#include "util.h" +#include "libcmt/io.h" +#include "libcmt/util.h" #include #include diff --git a/sys-utils/libcmt/src/io.c b/sys-utils/libcmt/src/io.c index afba0489..f18d4128 100644 --- a/sys-utils/libcmt/src/io.c +++ b/sys-utils/libcmt/src/io.c @@ -13,8 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "io.h" -#include "util.h" +#include "libcmt/io.h" +#include "libcmt/util.h" #include #include diff --git a/sys-utils/libcmt/src/keccak.c b/sys-utils/libcmt/src/keccak.c index a282ba92..6b82e4cb 100644 --- a/sys-utils/libcmt/src/keccak.c +++ b/sys-utils/libcmt/src/keccak.c @@ -13,8 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "keccak.h" -#include "abi.h" +#include "libcmt/keccak.h" +#include "libcmt/abi.h" #include diff --git a/sys-utils/libcmt/src/merkle.c b/sys-utils/libcmt/src/merkle.c index e58007d9..b6dcd2ad 100644 --- a/sys-utils/libcmt/src/merkle.c +++ b/sys-utils/libcmt/src/merkle.c @@ -13,8 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "merkle.h" -#include "util.h" +#include "libcmt/merkle.h" +#include "libcmt/util.h" #include #include diff --git a/sys-utils/libcmt/src/rollup.c b/sys-utils/libcmt/src/rollup.c index 4aa41dfb..bc7f658e 100644 --- a/sys-utils/libcmt/src/rollup.c +++ b/sys-utils/libcmt/src/rollup.c @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "rollup.h" -#include "abi.h" -#include "merkle.h" -#include "util.h" +#include "libcmt/rollup.h" +#include "libcmt/abi.h" +#include "libcmt/merkle.h" +#include "libcmt/util.h" #include #include @@ -28,8 +28,8 @@ // Notice(bytes) #define NOTICE CMT_ABI_FUNSEL(0xc2, 0x58, 0xd6, 0xe5) -// EvmAdvance(uint256,address,address,uint256,uint256,uint256,bytes) -#define EVM_ADVANCE CMT_ABI_FUNSEL(0xcc, 0x7d, 0xee, 0x1f) +// EvmAdvance(uint256,address,address,uint256,uint256,uint256,uint256,bytes) +#define EVM_ADVANCE CMT_ABI_FUNSEL(0x41, 0x5b, 0xf3, 0x63) #define DBG(X) debug(X, #X, __FILE__, __LINE__) static int debug(int rc, const char *expr, const char *file, int line) { @@ -66,29 +66,27 @@ void cmt_rollup_fini(cmt_rollup_t *me) { cmt_merkle_fini(me->merkle); } -int cmt_rollup_emit_voucher(cmt_rollup_t *me, uint32_t address_length, const void *address_data, uint32_t value_length, - const void *value_data, uint32_t length, const void *data, uint64_t *index) { +int cmt_rollup_emit_voucher(cmt_rollup_t *me, const cmt_abi_address_t *address, + const cmt_abi_u256_t *value, const cmt_abi_bytes_t *payload, uint64_t *index) { if (!me) { return -EINVAL; } - if (!data && length) { + if (!payload || (!payload->data && payload->length)) { return -EINVAL; } cmt_buf_t tx[1] = {cmt_io_get_tx(me->io)}; cmt_buf_t wr[1] = {*tx}; cmt_buf_t of[1]; - void *params_base = tx->begin + 4; // after funsel - - if (address_length != CMT_ADDRESS_LENGTH) - return -EINVAL; + cmt_buf_t frame[1]; // clang-format off if (DBG(cmt_abi_put_funsel(wr, VOUCHER)) - || DBG(cmt_abi_put_address(wr, address_data)) - || DBG(cmt_abi_put_uint_be(wr, value_length, value_data)) + || DBG(cmt_abi_mark_frame(wr, frame)) + || DBG(cmt_abi_put_address(wr, address)) + || DBG(cmt_abi_put_uint256(wr, value)) || DBG(cmt_abi_put_bytes_s(wr, of)) - || DBG(cmt_abi_put_bytes_d(wr, of, length, data, params_base))) { + || DBG(cmt_abi_put_bytes_d(wr, of, frame, payload))) { return -ENOBUFS; } // clang-format on @@ -119,23 +117,24 @@ int cmt_rollup_emit_voucher(cmt_rollup_t *me, uint32_t address_length, const voi return 0; } -int cmt_rollup_emit_notice(cmt_rollup_t *me, uint32_t length, const void *data, uint64_t *index) { +int cmt_rollup_emit_notice(cmt_rollup_t *me, const cmt_abi_bytes_t *payload, uint64_t *index) { if (!me) { return -EINVAL; } - if (!data && length) { + if (!payload || (!payload->data && payload->length)) { return -EINVAL; } cmt_buf_t tx[1] = {cmt_io_get_tx(me->io)}; cmt_buf_t wr[1] = {*tx}; cmt_buf_t of[1]; - void *params_base = tx->begin + 4; // after funsel + cmt_buf_t frame[1]; // clang-format off if (DBG(cmt_abi_put_funsel(wr, NOTICE)) + || DBG(cmt_abi_mark_frame(wr, frame)) || DBG(cmt_abi_put_bytes_s(wr, of)) - || DBG(cmt_abi_put_bytes_d(wr, of, length, data, params_base))) { + || DBG(cmt_abi_put_bytes_d(wr, of, frame, payload))) { return -ENOBUFS; } // clang-format on @@ -166,55 +165,55 @@ int cmt_rollup_emit_notice(cmt_rollup_t *me, uint32_t length, const void *data, return 0; } -int cmt_rollup_emit_report(cmt_rollup_t *me, uint32_t length, const void *data) { +int cmt_rollup_emit_report(cmt_rollup_t *me, const cmt_abi_bytes_t *payload) { if (!me) { return -EINVAL; } - if (!data && length) { + if (!payload || (!payload->data && payload->length)) { return -EINVAL; } cmt_buf_t tx[1] = {cmt_io_get_tx(me->io)}; cmt_buf_t wr[1] = {*tx}; - if (cmt_buf_split(tx, length, wr, tx)) { + if (cmt_buf_split(tx, payload->length, wr, tx)) { return -ENOBUFS; } - if (data) { - memcpy(wr->begin, data, length); + if (payload->data) { + memcpy(wr->begin, payload->data, payload->length); } struct cmt_io_yield req[1] = {{ .dev = HTIF_DEVICE_YIELD, .cmd = HTIF_YIELD_CMD_AUTOMATIC, .reason = HTIF_YIELD_AUTOMATIC_REASON_TX_REPORT, - .data = length, + .data = payload->length, }}; return DBG(cmt_io_yield(me->io, req)); } -int cmt_rollup_emit_exception(cmt_rollup_t *me, uint32_t length, const void *data) { +int cmt_rollup_emit_exception(cmt_rollup_t *me, const cmt_abi_bytes_t *payload) { if (!me) { return -EINVAL; } - if (!data && length) { + if (!payload || (!payload->data && payload->length)) { return -EINVAL; } cmt_buf_t tx[1] = {cmt_io_get_tx(me->io)}; cmt_buf_t wr[1] = {*tx}; cmt_buf_t _[1]; - if (cmt_buf_split(tx, length, wr, _)) { + if (cmt_buf_split(tx, payload->length, wr, _)) { return -ENOBUFS; } - if (data) { - memcpy(wr->begin, data, length); + if (payload->data) { + memcpy(wr->begin, payload->data, payload->length); } struct cmt_io_yield req[1] = {{ .dev = HTIF_DEVICE_YIELD, .cmd = HTIF_YIELD_CMD_MANUAL, .reason = HTIF_YIELD_MANUAL_REASON_TX_EXCEPTION, - .data = length, + .data = payload->length, }}; return DBG(cmt_io_yield(me->io, req)); } @@ -237,26 +236,25 @@ int cmt_rollup_read_advance_state(cmt_rollup_t *me, cmt_rollup_advance_t *advanc if (cmt_rollup_get_rx(me, rd)) { return -ENOBUFS; } - cmt_buf_t anchor[1] = {{rd->begin + 4, rd->end}}; + cmt_buf_t frame[1]; cmt_buf_t of[1]; - size_t payload_length = 0; - // clang-format off if (DBG(cmt_abi_check_funsel(rd, EVM_ADVANCE)) + || DBG(cmt_abi_mark_frame(rd, frame)) || DBG(cmt_abi_get_uint(rd, sizeof(advance->chain_id), &advance->chain_id)) - || DBG(cmt_abi_get_address(rd, advance->app_contract)) - || DBG(cmt_abi_get_address(rd, advance->msg_sender)) + || DBG(cmt_abi_get_address(rd, &advance->app_contract)) + || DBG(cmt_abi_get_address(rd, &advance->msg_sender)) || DBG(cmt_abi_get_uint(rd, sizeof(advance->block_number), &advance->block_number)) || DBG(cmt_abi_get_uint(rd, sizeof(advance->block_timestamp), &advance->block_timestamp)) + || DBG(cmt_abi_get_uint256(rd, &advance->prev_randao)) || DBG(cmt_abi_get_uint(rd, sizeof(advance->index), &advance->index)) || DBG(cmt_abi_get_bytes_s(rd, of)) - || DBG(cmt_abi_get_bytes_d(anchor, of, &payload_length, &advance->payload))) { + || DBG(cmt_abi_get_bytes_d(frame, of, &advance->payload.length, &advance->payload.data))) { return -ENOBUFS; } // clang-format on - advance->payload_length = payload_length; return 0; } @@ -273,8 +271,8 @@ int cmt_rollup_read_inspect_state(cmt_rollup_t *me, cmt_rollup_inspect_t *inspec return -ENOBUFS; } - inspect->payload_length = cmt_buf_length(rd); - inspect->payload = rd->begin; + inspect->payload.length = cmt_buf_length(rd); + inspect->payload.data = rd->begin; return 0; } @@ -317,7 +315,7 @@ int cmt_rollup_finish(cmt_rollup_t *me, cmt_rollup_finish_t *finish) { } cmt_merkle_get_root_hash(me->merkle, cmt_io_get_tx(me->io).begin); - me->fromhost_data = CMT_WORD_LENGTH; + me->fromhost_data = CMT_ABI_U256_LENGTH; int reason = accepted(me->io, &me->fromhost_data); if (reason < 0) { return reason; diff --git a/sys-utils/libcmt/tests/abi-multi.c b/sys-utils/libcmt/tests/abi-multi.c index 1c5fc88a..0ec39055 100644 --- a/sys-utils/libcmt/tests/abi-multi.c +++ b/sys-utils/libcmt/tests/abi-multi.c @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "abi.h" +#include "libcmt/abi.h" #include #include @@ -62,18 +62,19 @@ static int test_request(void) { cmt_buf_t bb[1] = {{mem, mem + sizeof mem}}; cmt_buf_t wr[1] = {*bb}; cmt_buf_t of[1]; + cmt_buf_t frame[1]; - uint8_t address[20] = {0}; + cmt_abi_address_t address = {0}; uint8_t bytes[] = {0xde, 0xad, 0xbe, 0xef}; cmt_abi_put_funsel(wr, REQUEST); - uint8_t *frame = wr->begin; // dynamic frame begins after funsel - cmt_abi_put_address(wr, address); + cmt_abi_mark_frame(wr, frame); + cmt_abi_put_address(wr, &address); cmt_abi_put_uint(wr, sizeof(int), &(int[]){1}); cmt_abi_put_uint(wr, sizeof(int), &(int[]){2}); cmt_abi_put_uint(wr, sizeof(int), &(int[]){3}); cmt_abi_put_bytes_s(wr, of); - cmt_abi_put_bytes_d(wr, of, sizeof bytes, bytes, frame); + cmt_abi_put_bytes_d(wr, of, frame, &(cmt_abi_bytes_t){sizeof bytes, bytes}); return sizeof request != (wr->begin - bb->begin) || memeq(request, bb->begin, sizeof request, __FILE__, __LINE__); } @@ -98,15 +99,16 @@ static int test_reply(void) { cmt_buf_t bb[1] = {{mem, mem + sizeof mem}}; cmt_buf_t wr[1] = {*bb}; cmt_buf_t of[1]; + cmt_buf_t frame[1]; - uint8_t address[20] = {0}; + cmt_abi_address_t address = {0}; uint8_t bytes[] = {0xde, 0xad, 0xbe, 0xef}; cmt_abi_put_funsel(wr, REPLY); - uint8_t *frame = wr->begin; // dynamic frame begins after funsel - cmt_abi_put_address(wr, address); + cmt_abi_mark_frame(wr, frame); + cmt_abi_put_address(wr, &address); cmt_abi_put_bytes_s(wr, of); - cmt_abi_put_bytes_d(wr, of, sizeof bytes, bytes, frame); + cmt_abi_put_bytes_d(wr, of, frame, &(cmt_abi_bytes_t){sizeof bytes, bytes}); return sizeof reply != (wr->begin - bb->begin) || memeq(reply, bb->begin, sizeof reply, __FILE__, __LINE__); } diff --git a/sys-utils/libcmt/tests/abi-single.c b/sys-utils/libcmt/tests/abi-single.c index cf63e63e..93ad9843 100644 --- a/sys-utils/libcmt/tests/abi-single.c +++ b/sys-utils/libcmt/tests/abi-single.c @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "abi.h" +#include "libcmt/abi.h" #include #include @@ -38,8 +38,8 @@ static void abi_funsel(void) { static void encode_u8(void) { uint8_t x = 0x01; - uint8_t en[CMT_WORD_LENGTH]; - uint8_t be[CMT_WORD_LENGTH] = { + uint8_t en[CMT_ABI_U256_LENGTH]; + uint8_t be[CMT_ABI_U256_LENGTH] = { // clang-format off 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, @@ -51,8 +51,8 @@ static void encode_u8(void) { static void encode_u16(void) { uint16_t x = UINT16_C(0x0123); - uint8_t en[CMT_WORD_LENGTH]; - uint8_t be[CMT_WORD_LENGTH] = { + uint8_t en[CMT_ABI_U256_LENGTH]; + uint8_t be[CMT_ABI_U256_LENGTH] = { // clang-format off 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, @@ -64,8 +64,8 @@ static void encode_u16(void) { static void encode_u32(void) { uint32_t x = UINT32_C(0x01234567); - uint8_t en[CMT_WORD_LENGTH]; - uint8_t be[CMT_WORD_LENGTH] = { + uint8_t en[CMT_ABI_U256_LENGTH]; + uint8_t be[CMT_ABI_U256_LENGTH] = { // clang-format off 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, 0x45, 0x67, @@ -77,8 +77,8 @@ static void encode_u32(void) { static void encode_u64(void) { uint64_t x = UINT64_C(0x0123456789abcdef); - uint8_t en[CMT_WORD_LENGTH]; - uint8_t be[CMT_WORD_LENGTH] = { + uint8_t en[CMT_ABI_U256_LENGTH]; + uint8_t be[CMT_ABI_U256_LENGTH] = { // clang-format off 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, @@ -89,14 +89,14 @@ static void encode_u64(void) { } static void encode_u256(void) { - uint8_t x[CMT_WORD_LENGTH] = { + uint8_t x[CMT_ABI_U256_LENGTH] = { // clang-format off 0x1f, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, // clang-format on }; - uint8_t en[CMT_WORD_LENGTH]; - uint8_t be[CMT_WORD_LENGTH] = { + uint8_t en[CMT_ABI_U256_LENGTH]; + uint8_t be[CMT_ABI_U256_LENGTH] = { // clang-format off 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, @@ -107,14 +107,14 @@ static void encode_u256(void) { } static void encode_edom(void) { - uint8_t x[CMT_WORD_LENGTH + 1] = { + uint8_t x[CMT_ABI_U256_LENGTH + 1] = { // clang-format off 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, // clang-format on }; - uint8_t en[CMT_WORD_LENGTH]; + uint8_t en[CMT_ABI_U256_LENGTH]; assert(cmt_abi_encode_uint_nr(sizeof(x), x, en) == -EDOM); assert(cmt_abi_encode_uint_nn(sizeof(x), x, en) == -EDOM); } @@ -122,7 +122,7 @@ static void encode_edom(void) { static void decode_u8(void) { uint8_t x = 0; uint8_t ex = 0x01; - uint8_t be[CMT_WORD_LENGTH] = { + uint8_t be[CMT_ABI_U256_LENGTH] = { // clang-format off 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, @@ -135,7 +135,7 @@ static void decode_u8(void) { static void decode_u16(void) { uint16_t x = 0; uint16_t ex = UINT16_C(0x0123); - uint8_t be[CMT_WORD_LENGTH] = { + uint8_t be[CMT_ABI_U256_LENGTH] = { // clang-format off 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, @@ -148,7 +148,7 @@ static void decode_u16(void) { static void decode_u32(void) { uint32_t x = 0; uint32_t ex = UINT32_C(0x01234567); - uint8_t be[CMT_WORD_LENGTH] = { + uint8_t be[CMT_ABI_U256_LENGTH] = { // clang-format off 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, 0x45, 0x67, @@ -161,7 +161,7 @@ static void decode_u32(void) { static void decode_u64(void) { uint64_t x = 0; uint64_t ex = UINT64_C(0x0123456789abcdef); - uint8_t be[CMT_WORD_LENGTH] = { + uint8_t be[CMT_ABI_U256_LENGTH] = { // clang-format off 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, @@ -172,14 +172,14 @@ static void decode_u64(void) { } static void decode_u256(void) { - uint8_t x[CMT_WORD_LENGTH]; - uint8_t ex[CMT_WORD_LENGTH] = { + uint8_t x[CMT_ABI_U256_LENGTH]; + uint8_t ex[CMT_ABI_U256_LENGTH] = { // clang-format off 0x1f, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, // clang-format on }; - uint8_t be[CMT_WORD_LENGTH] = { + uint8_t be[CMT_ABI_U256_LENGTH] = { // clang-format off 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, @@ -190,7 +190,7 @@ static void decode_u256(void) { } static void decode_uint_edom(void) { - uint8_t be[CMT_WORD_LENGTH] = { + uint8_t be[CMT_ABI_U256_LENGTH] = { // clang-format off 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, @@ -207,12 +207,12 @@ static void decode_uint_edom(void) { } { - uint8_t x[CMT_WORD_LENGTH + 1] = {0}; + uint8_t x[CMT_ABI_U256_LENGTH + 1] = {0}; assert(cmt_abi_decode_uint_nr(be, sizeof(x), x) == -EDOM); } { - uint8_t x[CMT_WORD_LENGTH + 1] = {0}; + uint8_t x[CMT_ABI_U256_LENGTH + 1] = {0}; assert(cmt_abi_decode_uint_nn(be, sizeof(x), x) == -EDOM); } } @@ -238,7 +238,7 @@ static void put_funsel_enobufs(void) { static void put_uint(void) { uint64_t x = UINT64_C(0x0123456789abcdef); - uint8_t be[CMT_WORD_LENGTH] = { + uint8_t be[CMT_ABI_U256_LENGTH] = { // clang-format off 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, @@ -261,8 +261,8 @@ static void put_uint_enobufs(void) { } static void put_uint_edom(void) { - uint64_t x[CMT_WORD_LENGTH + 1] = {0}; - CMT_BUF_DECL(b, CMT_WORD_LENGTH); + uint64_t x[CMT_ABI_U256_LENGTH + 1] = {0}; + CMT_BUF_DECL(b, CMT_ABI_U256_LENGTH); cmt_buf_t it[1] = {*b}; assert(cmt_abi_put_uint(it, sizeof(x), &x) == -EDOM); @@ -270,7 +270,7 @@ static void put_uint_edom(void) { } static void put_bool(void) { - uint8_t be[CMT_WORD_LENGTH] = { + uint8_t be[CMT_ABI_U256_LENGTH] = { // clang-format off 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, @@ -284,31 +284,29 @@ static void put_bool(void) { } static void put_address(void) { - uint8_t x[CMT_ADDRESS_LENGTH] = { - // clang-format off + // clang-format off + cmt_abi_address_t x = {{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, - // clang-format on - }; - uint8_t be[CMT_WORD_LENGTH] = { - // clang-format off + }}; + cmt_abi_address_t be = {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, - // clang-format on - }; + }}; + // clang-format on CMT_BUF_DECL(b, 64); cmt_buf_t it[1] = {*b}; - assert(cmt_abi_put_address(it, x) == 0); - assert(memcmp(b->begin, be, sizeof(be)) == 0); + assert(cmt_abi_put_address(it, &x) == 0); + assert(memcmp(b->begin, be.data, sizeof(be)) == 0); } static void put_address_enobufs(void) { - uint8_t x[CMT_ADDRESS_LENGTH] = {0}; - CMT_BUF_DECL(b, CMT_WORD_LENGTH - 1); + cmt_abi_address_t x = {{0}}; + CMT_BUF_DECL(b, CMT_ABI_U256_LENGTH - 1); cmt_buf_t it[1] = {*b}; - assert(cmt_abi_put_address(it, x) == -ENOBUFS); + assert(cmt_abi_put_address(it, &x) == -ENOBUFS); } static void put_bytes(void) { @@ -325,22 +323,24 @@ static void put_bytes(void) { CMT_BUF_DECL(b, 128); cmt_buf_t it[1] = {*b}; cmt_buf_t of[1]; + cmt_buf_t frame[1]; + assert(cmt_abi_mark_frame(it, frame) == 0); assert(cmt_abi_put_bytes_s(it, of) == 0); - assert(cmt_abi_put_bytes_d(it, of, sizeof(x), &x, b->begin) == 0); + assert(cmt_abi_put_bytes_d(it, of, frame, &(cmt_abi_bytes_t){sizeof(x), &x}) == 0); assert(memcmp(b->begin, be, sizeof(be)) == 0); } static void put_bytes_enobufs(void) { uint64_t x = UINT64_C(0x0123456789abcdef); cmt_buf_t of[1]; + cmt_buf_t frame[1]; - { - CMT_BUF_DECL(b, 3 * 32 - 1); - cmt_buf_t it[1] = {*b}; - assert(cmt_abi_put_bytes_s(it, of) == 0); - assert(cmt_abi_put_bytes_d(it, of, sizeof(x), &x, b->begin) == -ENOBUFS); - } + CMT_BUF_DECL(b, 3 * 32 - 1); + cmt_buf_t it[1] = {*b}; + assert(cmt_abi_mark_frame(it, frame) == 0); + assert(cmt_abi_put_bytes_s(it, of) == 0); + assert(cmt_abi_put_bytes_d(it, of, frame, &(cmt_abi_bytes_t){sizeof(x), &x}) == -ENOBUFS); } static void get_funsel(void) { @@ -368,7 +368,7 @@ static void peek_funsel_error(void) { static void get_uint(void) { uint64_t x = 0; uint64_t ex = UINT64_C(0x0123456789abcdef); - uint8_t be[CMT_WORD_LENGTH] = { + uint8_t be[CMT_ABI_U256_LENGTH] = { // clang-format off 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, @@ -391,8 +391,8 @@ static void get_uint_enobufs(void) { } static void get_uint_edom(void) { - uint64_t x[CMT_WORD_LENGTH + 1] = {0}; - CMT_BUF_DECL(b, CMT_WORD_LENGTH); + uint64_t x[CMT_ABI_U256_LENGTH + 1] = {0}; + CMT_BUF_DECL(b, CMT_ABI_U256_LENGTH); cmt_buf_t it[1] = {*b}; assert(cmt_abi_get_uint(it, sizeof(x), &x) == -EDOM); @@ -402,7 +402,7 @@ static void get_uint_edom(void) { static void get_uint_be(void) { uint64_t x = 0; uint64_t ex = UINT64_C(0x0123456789abcdef); - uint8_t be[CMT_WORD_LENGTH] = { + uint8_t be[CMT_ABI_U256_LENGTH] = { // clang-format off 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01, @@ -418,7 +418,7 @@ static void get_uint_be(void) { static void get_bool(void) { bool x = false; bool ex = true; - uint8_t be[CMT_WORD_LENGTH] = { + uint8_t be[CMT_ABI_U256_LENGTH] = { // clang-format off 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, @@ -433,7 +433,7 @@ static void get_bool(void) { static void get_bool_enobufs(void) { bool x = false; - uint8_t be[CMT_WORD_LENGTH - 1] = { + uint8_t be[CMT_ABI_U256_LENGTH - 1] = { // clang-format off 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -446,37 +446,35 @@ static void get_bool_enobufs(void) { } static void get_address(void) { - uint8_t x[CMT_ADDRESS_LENGTH]; - uint8_t ex[CMT_ADDRESS_LENGTH] = { - // clang-format off + // clang-format off + cmt_abi_address_t x; + uint8_t ex[CMT_ABI_ADDRESS_LENGTH] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67 - // clang-format on }; - uint8_t be[CMT_WORD_LENGTH] = { - // clang-format off + uint8_t be[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, - // clang-format on }; + // clang-format on CMT_BUF_DECL3(b, sizeof(be), be); cmt_buf_t it[1] = {*b}; - assert(cmt_abi_get_address(it, x) == 0); - assert(memcmp(x, ex, sizeof(ex)) == 0); + assert(cmt_abi_get_address(it, &x) == 0); + assert(memcmp(x.data, ex, sizeof(ex)) == 0); } static void get_address_enobufs(void) { - uint8_t x[CMT_ADDRESS_LENGTH]; - uint8_t be[CMT_WORD_LENGTH - 1] = { - // clang-format off + cmt_abi_address_t x; + // clang-format off + uint8_t be[CMT_ABI_U256_LENGTH - 1] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, - // clang-format on }; + // clang-format on CMT_BUF_DECL3(b, sizeof(be), be); cmt_buf_t it[1] = {*b}; - assert(cmt_abi_get_address(it, x) == -ENOBUFS); + assert(cmt_abi_get_address(it, &x) == -ENOBUFS); } static void get_bytes(void) { @@ -501,18 +499,18 @@ static void get_bytes(void) { } static void get_bytes_enobufs(void) { + // clang-format off uint8_t be[] = { - // clang-format off 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - // clang-format on }; + // clang-format on { // when offset of dynamic reagion failed - CMT_BUF_DECL3(b, 1 * CMT_WORD_LENGTH - 1, be); + CMT_BUF_DECL3(b, 1 * CMT_ABI_U256_LENGTH - 1, be); cmt_buf_t it[1] = {*b}; cmt_buf_t of[1] = {0}; cmt_buf_t bytes[1]; @@ -521,7 +519,7 @@ static void get_bytes_enobufs(void) { } { // dynamic reagion is too small to peek bytes - CMT_BUF_DECL3(b, 3 * CMT_WORD_LENGTH - 1, be); + CMT_BUF_DECL3(b, 3 * CMT_ABI_U256_LENGTH - 1, be); cmt_buf_t it[1] = {*b}; cmt_buf_t of[1] = {0}; cmt_buf_t bytes[1]; @@ -531,7 +529,7 @@ static void get_bytes_enobufs(void) { } { // dynamic reagion is too small to copy bytes - CMT_BUF_DECL3(b, 3 * CMT_WORD_LENGTH - 1, be); + CMT_BUF_DECL3(b, 3 * CMT_ABI_U256_LENGTH - 1, be); cmt_buf_t it[1] = {*b}; cmt_buf_t of[1] = {0}; diff --git a/sys-utils/libcmt/tests/buf.c b/sys-utils/libcmt/tests/buf.c index 2a435970..459159fa 100644 --- a/sys-utils/libcmt/tests/buf.c +++ b/sys-utils/libcmt/tests/buf.c @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "buf.h" +#include "libcmt/buf.h" #include #include #include diff --git a/sys-utils/libcmt/tests/create-data.sh b/sys-utils/libcmt/tests/create-data.sh index 82747dda..d98096c5 100755 --- a/sys-utils/libcmt/tests/create-data.sh +++ b/sys-utils/libcmt/tests/create-data.sh @@ -6,13 +6,14 @@ echo "#ifndef DATA_H" echo "#define DATA_H" echo "#include " echo "uint8_t valid_advance_0[] = {" -cast calldata "EvmAdvance(uint256,address,address,uint256,uint256,uint256,bytes)" \ +cast calldata "EvmAdvance(uint256,address,address,uint256,uint256,uint256,uint256,bytes)" \ 0x0000000000000000000000000000000000000001 \ 0x0000000000000000000000000000000000000002 \ 0x0000000000000000000000000000000000000003 \ 0x0000000000000000000000000000000000000004 \ 0x0000000000000000000000000000000000000005 \ 0x0000000000000000000000000000000000000006 \ + 0x0000000000000000000000000000000000000007 \ 0x`echo -en "advance-0" | xxd -p -c0` | xxd -r -p | xxd -i echo "};" diff --git a/sys-utils/libcmt/tests/data.h b/sys-utils/libcmt/tests/data.h index 4cf554b7..eb7d44b3 100644 --- a/sys-utils/libcmt/tests/data.h +++ b/sys-utils/libcmt/tests/data.h @@ -2,7 +2,7 @@ #define DATA_H #include uint8_t valid_advance_0[] = { - 0xcc, 0x7d, 0xee, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x41, 0x5b, 0xf3, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -20,13 +20,15 @@ uint8_t valid_advance_0[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x61, 0x64, 0x76, 0x61, - 0x6e, 0x63, 0x65, 0x2d, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x09, 0x61, 0x64, 0x76, 0x61, 0x6e, 0x63, 0x65, 0x2d, + 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; uint8_t valid_inspect_0[] = { 0x69, 0x6e, 0x73, 0x70, 0x65, 0x63, 0x74, 0x2d, 0x30 diff --git a/sys-utils/libcmt/tests/gio.c b/sys-utils/libcmt/tests/gio.c index 2bd4d8c9..bb8f833d 100644 --- a/sys-utils/libcmt/tests/gio.c +++ b/sys-utils/libcmt/tests/gio.c @@ -1,6 +1,6 @@ #include "data.h" -#include "rollup.h" -#include "util.h" +#include "libcmt/rollup.h" +#include "libcmt/util.h" #include #include #include diff --git a/sys-utils/libcmt/tests/keccak.c b/sys-utils/libcmt/tests/keccak.c index dbb7e806..027aa011 100644 --- a/sys-utils/libcmt/tests/keccak.c +++ b/sys-utils/libcmt/tests/keccak.c @@ -13,8 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "keccak.h" -#include "abi.h" +#include "libcmt/keccak.h" +#include "libcmt/abi.h" #include #include diff --git a/sys-utils/libcmt/tests/merkle.c b/sys-utils/libcmt/tests/merkle.c index 882b282d..f552720f 100644 --- a/sys-utils/libcmt/tests/merkle.c +++ b/sys-utils/libcmt/tests/merkle.c @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "merkle.h" +#include "libcmt/merkle.h" #include #include diff --git a/sys-utils/libcmt/tests/progress.c b/sys-utils/libcmt/tests/progress.c index 9c5a9dc3..d214b677 100644 --- a/sys-utils/libcmt/tests/progress.c +++ b/sys-utils/libcmt/tests/progress.c @@ -1,4 +1,4 @@ -#include "rollup.h" +#include "libcmt/rollup.h" #include #include #include diff --git a/sys-utils/libcmt/tests/rollup.c b/sys-utils/libcmt/tests/rollup.c index 1071621a..ec34b687 100644 --- a/sys-utils/libcmt/tests/rollup.c +++ b/sys-utils/libcmt/tests/rollup.c @@ -13,9 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "rollup.h" +#include "libcmt/rollup.h" +#include "libcmt/util.h" #include "data.h" -#include "util.h" #include #include #include @@ -50,32 +50,39 @@ static void check_first_input(cmt_rollup_t *rollup) { assert(cmt_rollup_read_advance_state(rollup, &advance) == 0); // clang-format off - uint8_t expected_app_contract[CMT_ADDRESS_LENGTH] = { + uint8_t expected_app_contract[CMT_ABI_ADDRESS_LENGTH] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, }; - uint8_t expected_msg_sender[CMT_ADDRESS_LENGTH] = { + uint8_t expected_msg_sender[CMT_ABI_ADDRESS_LENGTH] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, }; + cmt_abi_u256_t expected_prev_randao = {{ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, + }}; char expected_payload[] = "advance-0"; // clang-format on // verify the parsed data assert(advance.chain_id == 1); - assert(memcmp(advance.app_contract, expected_app_contract, CMT_ADDRESS_LENGTH) == 0); - assert(memcmp(advance.msg_sender, expected_msg_sender, CMT_ADDRESS_LENGTH) == 0); + assert(memcmp(advance.app_contract.data, expected_app_contract, CMT_ABI_ADDRESS_LENGTH) == 0); + assert(memcmp(advance.msg_sender.data, expected_msg_sender, CMT_ABI_ADDRESS_LENGTH) == 0); assert(advance.block_number == 4); assert(advance.block_timestamp == 5); - assert(advance.index == 6); - assert(advance.payload_length == strlen(expected_payload)); - assert(memcmp(advance.payload, expected_payload, strlen(expected_payload)) == 0); + assert(memcmp(&advance.prev_randao.data, expected_prev_randao.data, CMT_ABI_U256_LENGTH) == 0); + assert(advance.index == 7); + assert(advance.payload.length == strlen(expected_payload)); + assert(memcmp(advance.payload.data, expected_payload, strlen(expected_payload)) == 0); } static void check_second_input(cmt_rollup_t *rollup) { @@ -83,8 +90,8 @@ static void check_second_input(cmt_rollup_t *rollup) { char expected_payload[] = "inspect-0"; assert(cmt_rollup_read_inspect_state(rollup, &inspect) == 0); - assert(inspect.payload_length == strlen(expected_payload)); - assert(memcmp(inspect.payload, expected_payload, strlen(expected_payload)) == 0); + assert(inspect.payload.length == strlen(expected_payload)); + assert(memcmp(inspect.payload.data, expected_payload, strlen(expected_payload)) == 0); } void test_rollup_parse_inputs(void) { @@ -125,73 +132,78 @@ void test_rollup_outputs_reports_and_exceptions(void) { size_t read_size = 0; // clang-format off - uint8_t address[CMT_ADDRESS_LENGTH] = { + cmt_abi_address_t address = {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 - }; + }}; // clang-format on assert(cmt_rollup_init(&rollup) == 0); // voucher - uint8_t value[] = {0xde, 0xad, 0xbe, 0xef}; + cmt_abi_u256_t value = {{ + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0xde, 0xad, 0xbe, 0xef + }}; char voucher_data[] = "voucher-0"; - assert(cmt_rollup_emit_voucher(&rollup, sizeof address, address, sizeof value, value, strlen(voucher_data), - voucher_data, &index) == 0); + assert(cmt_rollup_emit_voucher(&rollup, &address, &value, &(cmt_abi_bytes_t){strlen(voucher_data), voucher_data}, &index) == 0); assert(index == 0); assert(cmt_util_read_whole_file("none.output-0.bin", sizeof buffer, buffer, &read_size) == 0); assert(sizeof valid_voucher_0 == read_size); assert(memcmp(valid_voucher_0, buffer, sizeof valid_voucher_0) == 0); // voucher (invalid) - assert(cmt_rollup_emit_voucher(NULL, sizeof address, address, sizeof value, value, strlen(voucher_data), - voucher_data, &index) == -EINVAL); - assert(cmt_rollup_emit_voucher(&rollup, sizeof address - 1, address, sizeof value, value, strlen(voucher_data), - NULL, &index) == -EINVAL); - assert(cmt_rollup_emit_voucher(&rollup, sizeof address - 1, address, sizeof value, value, strlen(voucher_data), - voucher_data, &index) == -EINVAL); - assert(cmt_rollup_emit_voucher(&rollup, sizeof address, address, sizeof value, value, UINT32_MAX, voucher_data, + assert(cmt_rollup_emit_voucher(NULL, &address, &value, &(cmt_abi_bytes_t){strlen(voucher_data), voucher_data}, &index) == -EINVAL); + assert(cmt_rollup_emit_voucher(&rollup, &address, &value, &(cmt_abi_bytes_t){strlen(voucher_data), + NULL}, &index) == -EINVAL); + assert(cmt_rollup_emit_voucher(&rollup, &address, &value, &(cmt_abi_bytes_t){UINT32_MAX, voucher_data}, &index) == -ENOBUFS); // notice char notice_data[] = "notice-0"; - assert(cmt_rollup_emit_notice(&rollup, strlen(notice_data), notice_data, &index) == 0); + assert(cmt_rollup_emit_notice(&rollup, &(cmt_abi_bytes_t){strlen(notice_data), notice_data}, &index) == 0); assert(cmt_util_read_whole_file("none.output-1.bin", sizeof buffer, buffer, &read_size) == 0); assert(sizeof valid_notice_0 == read_size); assert(memcmp(valid_notice_0, buffer, sizeof valid_notice_0) == 0); assert(index == 1); // notice (invalid) - assert(cmt_rollup_emit_notice(NULL, strlen(notice_data), notice_data, &index) == -EINVAL); - assert(cmt_rollup_emit_notice(&rollup, strlen(notice_data), NULL, &index) == -EINVAL); - assert(cmt_rollup_emit_notice(&rollup, UINT32_MAX, notice_data, &index) == -ENOBUFS); + assert(cmt_rollup_emit_notice(NULL, &(cmt_abi_bytes_t){strlen(notice_data), notice_data}, &index) == -EINVAL); + assert(cmt_rollup_emit_notice(&rollup, &(cmt_abi_bytes_t){strlen(notice_data), NULL}, &index) == -EINVAL); + assert(cmt_rollup_emit_notice(&rollup, &(cmt_abi_bytes_t){UINT32_MAX, notice_data}, &index) == -ENOBUFS); // report char report_data[] = "report-0"; - assert(cmt_rollup_emit_report(&rollup, strlen(report_data), report_data) == 0); + assert(cmt_rollup_emit_report(&rollup, &(cmt_abi_bytes_t){strlen(report_data), report_data}) == 0); assert(cmt_util_read_whole_file("none.report-0.bin", sizeof buffer, buffer, &read_size) == 0); assert(sizeof valid_report_0 == read_size); assert(memcmp(valid_report_0, buffer, sizeof valid_report_0) == 0); // report (invalid) - assert(cmt_rollup_emit_report(NULL, strlen(report_data), report_data) == -EINVAL); - assert(cmt_rollup_emit_report(&rollup, strlen(report_data), NULL) == -EINVAL); - assert(cmt_rollup_emit_report(&rollup, UINT32_MAX, report_data) == -ENOBUFS); + assert(cmt_rollup_emit_report(NULL, &(cmt_abi_bytes_t){strlen(report_data), report_data}) == -EINVAL); + assert(cmt_rollup_emit_report(&rollup, &(cmt_abi_bytes_t){strlen(report_data), NULL}) == -EINVAL); + assert(cmt_rollup_emit_report(&rollup, &(cmt_abi_bytes_t){UINT32_MAX, report_data}) == -ENOBUFS); // exception char exception_data[] = "exception-0"; - assert(cmt_rollup_emit_exception(&rollup, strlen(exception_data), exception_data) == 0); + assert(cmt_rollup_emit_exception(&rollup, &(cmt_abi_bytes_t){strlen(exception_data), exception_data}) == 0); assert(cmt_util_read_whole_file("none.exception-0.bin", sizeof buffer, buffer, &read_size) == 0); assert(sizeof valid_exception_0 == read_size); assert(memcmp(valid_exception_0, buffer, sizeof valid_exception_0) == 0); // exception (invalid) - assert(cmt_rollup_emit_exception(NULL, strlen(exception_data), exception_data) == -EINVAL); - assert(cmt_rollup_emit_exception(&rollup, strlen(exception_data), NULL) == -EINVAL); - assert(cmt_rollup_emit_exception(&rollup, UINT32_MAX, exception_data) == -ENOBUFS); + assert(cmt_rollup_emit_exception(NULL, &(cmt_abi_bytes_t){strlen(exception_data), exception_data}) == -EINVAL); + assert(cmt_rollup_emit_exception(&rollup, &(cmt_abi_bytes_t){strlen(exception_data), NULL}) == -EINVAL); + assert(cmt_rollup_emit_exception(&rollup, &(cmt_abi_bytes_t){UINT32_MAX, exception_data}) == -ENOBUFS); cmt_rollup_fini(&rollup); printf("test_rollup_outputs_reports_and_exceptions passed!\n"); diff --git a/sys-utils/libcmt/tools/funsel.c b/sys-utils/libcmt/tools/funsel.c index 2fe43f39..7673226d 100644 --- a/sys-utils/libcmt/tools/funsel.c +++ b/sys-utils/libcmt/tools/funsel.c @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "keccak.h" +#include "libcmt/keccak.h" #include #include diff --git a/sys-utils/rollup/rollup.cpp b/sys-utils/rollup/rollup.cpp index b7b66337..79aad99b 100644 --- a/sys-utils/rollup/rollup.cpp +++ b/sys-utils/rollup/rollup.cpp @@ -103,14 +103,15 @@ static void print_help(void) { "chain_id": , "app_contract":
, "msg_sender":
, - "epoch_index": , - "input_index": , "block_number": , "block_timestamp": + "prev_randao": , + "index": , "payload": }, where -
contains a 20-byte EVM address in hex, and +
contains a 20-byte EVM address in hex, + contains a big-endian 32-byte unsigned integer in hex, and contains arbitrary data in hex when field "request_type" contains "inspect_state", @@ -217,13 +218,20 @@ static std::string hex(const uint8_t *data, uint64_t length) { static int write_voucher(void) try { rollup r; auto ji = nlohmann::json::parse(read_input()); - auto payload = unhex(ji["payload"].get()); - auto destination = unhex20(ji["destination"].get()); - auto value = unhex32(ji["value"].get()); + auto payload_bytes = unhex(ji["payload"].get()); + auto destination_bytes = unhex20(ji["destination"].get()); + auto value_bytes = unhex32(ji["value"].get()); uint64_t index = 0; - int ret = cmt_rollup_emit_voucher(r, destination.length(), reinterpret_cast(destination.data()), - value.length(), reinterpret_cast(value.data()), payload.size(), - reinterpret_cast(payload.data()), &index); + cmt_abi_address_t destination; + cmt_abi_u256_t value; + cmt_abi_bytes_t payload; + payload.data = reinterpret_cast(payload_bytes.data()); + payload.length = payload_bytes.size(); + + memcpy(destination.data, reinterpret_cast(destination_bytes.data()), destination_bytes.size()); + memcpy(value.data, reinterpret_cast(value_bytes.data()), value_bytes.size()); + + int ret = cmt_rollup_emit_voucher(r, &destination, &value, &payload, &index); if (ret) return ret; @@ -240,9 +248,12 @@ static int write_voucher(void) try { static int write_notice(void) try { rollup r; auto ji = nlohmann::json::parse(read_input()); - auto payload = unhex(ji["payload"].get()); + auto payload_bytes = unhex(ji["payload"].get()); + cmt_abi_bytes_t payload; + payload.data = reinterpret_cast(payload_bytes.data()); + payload.length = payload_bytes.size(); uint64_t index = 0; - int ret = cmt_rollup_emit_notice(r, payload.size(), reinterpret_cast(payload.data()), &index); + int ret = cmt_rollup_emit_notice(r, &payload, &index); if (ret) return ret; @@ -260,8 +271,11 @@ static int write_notice(void) try { static int write_report(void) try { rollup r; auto ji = nlohmann::json::parse(read_input()); - auto payload = unhex(ji["payload"].get()); - return cmt_rollup_emit_report(r, payload.size(), reinterpret_cast(payload.data())); + auto payload_bytes = unhex(ji["payload"].get()); + cmt_abi_bytes_t payload; + payload.data = reinterpret_cast(payload_bytes.data()); + payload.length = payload_bytes.size(); + return cmt_rollup_emit_report(r, &payload); } catch (std::exception &x) { std::cerr << x.what() << '\n'; return 1; @@ -271,8 +285,11 @@ static int write_report(void) try { static int throw_exception(void) try { rollup r; auto ji = nlohmann::json::parse(read_input()); - auto payload = unhex(ji["payload"].get()); - return cmt_rollup_emit_exception(r, payload.size(), reinterpret_cast(payload.data())); + auto payload_bytes = unhex(ji["payload"].get()); + cmt_abi_bytes_t payload; + payload.data = reinterpret_cast(payload_bytes.data()); + payload.length = payload_bytes.size(); + return cmt_rollup_emit_exception(r, &payload); } catch (std::exception &x) { std::cerr << x.what() << '\n'; return 1; @@ -292,12 +309,13 @@ static int write_advance_state(rollup &r, const cmt_rollup_finish_t *f) { {"data", { {"chain_id", advance.chain_id}, - {"payload", hex(reinterpret_cast(advance.payload), advance.payload_length)}, - {"app_contract", hex(advance.app_contract, sizeof(advance.app_contract))}, - {"msg_sender", hex(advance.msg_sender, sizeof(advance.msg_sender))}, + {"app_contract", hex(advance.app_contract.data, std::size(advance.app_contract.data))}, + {"msg_sender", hex(advance.msg_sender.data, std::size(advance.msg_sender.data))}, {"block_number", advance.block_number}, {"block_timestamp", advance.block_timestamp}, + {"prev_randao", hex(advance.prev_randao.data, std::size(advance.prev_randao.data))}, {"index", advance.index}, + {"payload", hex(reinterpret_cast(advance.payload.data), advance.payload.length)}, }}}; std::cout << j.dump(2) << '\n'; return 0; @@ -316,7 +334,7 @@ static int write_inspect_state(rollup &r, const cmt_rollup_finish_t *f) { nlohmann::json j = {{"request_type", "inspect_state"}, {"data", { - {"payload", hex(reinterpret_cast(inspect.payload), inspect.payload_length)}, + {"payload", hex(reinterpret_cast(inspect.payload.data), inspect.payload.length)}, }}}; std::cout << j.dump(2) << '\n'; return 0;