diff --git a/rollup-http/rollup-http-client/src/rollup.rs b/rollup-http/rollup-http-client/src/rollup.rs index eeef5659..da7fe2fc 100644 --- a/rollup-http/rollup-http-client/src/rollup.rs +++ b/rollup-http/rollup-http-client/src/rollup.rs @@ -51,6 +51,7 @@ pub struct Notice { #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct Voucher { pub destination: String, + pub data: String, pub payload: String, } diff --git a/rollup-http/rollup-http-server/Cargo.toml b/rollup-http/rollup-http-server/Cargo.toml index f0c53827..ab56d152 100644 --- a/rollup-http/rollup-http-server/Cargo.toml +++ b/rollup-http/rollup-http-server/Cargo.toml @@ -36,7 +36,7 @@ tonic-build = "0.5" [dev-dependencies] rollup-http-client = {path = "../rollup-http-client"} - +rand = "0.8.5" [profile.release] diff --git a/rollup-http/rollup-http-server/build.rs b/rollup-http/rollup-http-server/build.rs index 37a540fb..c40a14fe 100644 --- a/rollup-http/rollup-http-server/build.rs +++ b/rollup-http/rollup-http-server/build.rs @@ -17,17 +17,5 @@ extern crate cc; fn main() { - println!("cargo:rerun-if-changed=src/rollup/bindings.c,src/rollup/bindings.h,tests/rollup_test_bindings.c,tests/rollup_test.h"); - let test = std::env::var("USE_ROLLUP_BINDINGS_MOCK").unwrap_or("0".to_string()); - if test == "1" { - cc::Build::new() - .file("tests/rollup_test_bindings.c") - .compile("libtest_bindings.a"); - println!("cargo:rustc-link-lib=test_bindings"); - } else { - cc::Build::new() - .file("src/rollup/bindings.c") - .compile("libbindings.a"); - println!("cargo:rustc-link-lib=bindings"); - } + println!("cargo:rustc-link-lib=cmt"); } diff --git a/rollup-http/rollup-http-server/src/dapp_process.rs b/rollup-http/rollup-http-server/src/dapp_process.rs index 931fce51..8efecd37 100644 --- a/rollup-http/rollup-http-server/src/dapp_process.rs +++ b/rollup-http/rollup-http-server/src/dapp_process.rs @@ -20,10 +20,10 @@ use std::sync::Arc; use async_mutex::Mutex; use tokio::process::Command; -use crate::rollup::{self, Exception}; +use crate::rollup::{self, Exception, RollupFd}; /// Execute the dapp command and throw a rollup exception if it fails or exits -pub async fn run(args: Vec, rollup_fd: Arc>) { +pub async fn run(args: Vec, rollup_fd: Arc>) { log::info!("starting dapp: {}", args.join(" ")); let task = tokio::task::spawn_blocking(move || Command::new(&args[0]).args(&args[1..]).spawn()); let message = match task.await { @@ -40,7 +40,7 @@ pub async fn run(args: Vec, rollup_fd: Arc>) { let exception = Exception { payload: String::from("0x") + &hex::encode(message), }; - match rollup::rollup_throw_exception(*rollup_fd.lock().await, &exception) { + match rollup::rollup_throw_exception(&*rollup_fd.lock().await, &exception) { Ok(_) => { log::debug!("exception successfully thrown {:#?}", exception); } diff --git a/rollup-http/rollup-http-server/src/http_service.rs b/rollup-http/rollup-http-server/src/http_service.rs index c650d68a..ee30a6ad 100644 --- a/rollup-http/rollup-http-server/src/http_service.rs +++ b/rollup-http/rollup-http-server/src/http_service.rs @@ -14,7 +14,6 @@ // limitations under the License. // -use std::os::unix::io::RawFd; use std::sync::Arc; use actix_web::{middleware::Logger, web::Data, web::Json, App, HttpResponse, HttpServer}; @@ -23,7 +22,7 @@ use serde::{Deserialize, Serialize}; use tokio::sync::Notify; use crate::config::Config; -use crate::rollup; +use crate::rollup::{self, RollupFd}; use crate::rollup::{ AdvanceRequest, Exception, InspectRequest, Notice, Report, RollupRequest, Voucher, }; @@ -40,7 +39,7 @@ enum RollupHttpRequest { /// Create new instance of http server pub fn create_server( config: &Config, - rollup_fd: Arc>, + rollup_fd: Arc>, ) -> std::io::Result { let server = HttpServer::new(move || { let data = Data::new(Mutex::new(Context { @@ -64,7 +63,7 @@ pub fn create_server( /// Create and run new instance of http server pub async fn run( config: &Config, - rollup_fd: Arc>, + rollup_fd: Arc>, server_ready: Arc, ) -> std::io::Result<()> { log::info!("starting http dispatcher http service!"); @@ -90,7 +89,7 @@ async fn voucher(mut voucher: Json, data: Data>) -> Http } let context = data.lock().await; // Write voucher to linux rollup device - return match rollup::rollup_write_voucher(*context.rollup_fd.lock().await, &mut voucher.0) { + return match rollup::rollup_write_voucher(&*context.rollup_fd.lock().await, &mut voucher.0) { Ok(voucher_index) => { log::debug!("voucher successfully inserted {:#?}", voucher); HttpResponse::Created().json(IndexResponse { @@ -114,7 +113,7 @@ async fn notice(mut notice: Json, data: Data>) -> HttpRes log::debug!("received notice request"); let context = data.lock().await; // Write notice to linux rollup device - return match rollup::rollup_write_notice(*context.rollup_fd.lock().await, &mut notice.0) { + return match rollup::rollup_write_notice(&*context.rollup_fd.lock().await, &mut notice.0) { Ok(notice_index) => { log::debug!("notice successfully inserted {:#?}", notice); HttpResponse::Created().json(IndexResponse { @@ -135,7 +134,7 @@ async fn report(report: Json, data: Data>) -> HttpRespons log::debug!("received report request"); let context = data.lock().await; // Write report to linux rollup device - return match rollup::rollup_write_report(*context.rollup_fd.lock().await, &report.0) { + return match rollup::rollup_write_report(&*context.rollup_fd.lock().await, &report.0) { Ok(_) => { log::debug!("report successfully inserted {:#?}", report); HttpResponse::Accepted().body("") @@ -157,7 +156,7 @@ async fn exception(exception: Json, data: Data>) -> Ht let context = data.lock().await; // Throw an exception - return match rollup::rollup_throw_exception(*context.rollup_fd.lock().await, &exception.0) { + return match rollup::rollup_throw_exception(&*context.rollup_fd.lock().await, &exception.0) { Ok(_) => { log::debug!("exception successfully thrown {:#?}", exception); HttpResponse::Accepted().body("") @@ -190,7 +189,7 @@ 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).await { Ok(finish_request) => { // Received new request, process it log::info!( @@ -201,7 +200,7 @@ async fn finish(finish: Json, data: Data>) -> Http _ => "UNKNOWN", } ); - match rollup::handle_rollup_requests(*rollup_fd, finish_request).await { + match rollup::handle_rollup_requests(&*rollup_fd, finish_request).await { Ok(rollup_request) => rollup_request, Err(e) => { let error_message = format!( @@ -260,5 +259,5 @@ struct Error { } struct Context { - pub rollup_fd: Arc>, + pub rollup_fd: Arc>, } diff --git a/rollup-http/rollup-http-server/src/main.rs b/rollup-http/rollup-http-server/src/main.rs index c38bacea..cca29199 100644 --- a/rollup-http/rollup-http-server/src/main.rs +++ b/rollup-http/rollup-http-server/src/main.rs @@ -14,15 +14,12 @@ // limitations under the License. // -use std::fs::File; use std::io::ErrorKind; -#[cfg(unix)] -use std::os::unix::io::{IntoRawFd, RawFd}; use std::sync::Arc; use async_mutex::Mutex; use getopts::{Options, ParsingStyle}; -use rollup_http_server::{config::Config, dapp_process, http_service, rollup}; +use rollup_http_server::{config::Config, dapp_process, http_service, rollup::RollupFd}; use tokio::sync::Notify; fn print_usage(program: &str, opts: Options) { @@ -100,16 +97,7 @@ async fn main() -> std::io::Result<()> { .unwrap(); } - // Open rollup device - let rollup_file = match File::open(rollup::ROLLUP_DEVICE_NAME) { - Ok(file) => file, - Err(e) => { - log::error!("error opening rollup device {}", e.to_string()); - return Err(e); - } - }; - - let rollup_fd: Arc> = Arc::new(Mutex::new(rollup_file.into_raw_fd())); + let rollup_fd: Arc> = Arc::new(Mutex::new(RollupFd::create().unwrap())); let server_ready = Arc::new(Notify::new()); // In another thread, wait until the server is ready and then start the dapp diff --git a/rollup-http/rollup-http-server/src/rollup/bindings.c b/rollup-http/rollup-http-server/src/rollup/bindings.c deleted file mode 100644 index 86628c41..00000000 --- a/rollup-http/rollup-http-server/src/rollup/bindings.c +++ /dev/null @@ -1,143 +0,0 @@ -/* Copyright Cartesi and individual authors (see AUTHORS) - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "bindings.h" -#include - -static int resize_bytes(struct rollup_bytes *bytes, uint64_t size) { - if (bytes->length < size) { - uint8_t *new_data = (uint8_t *) realloc(bytes->data, size); - if (!new_data) { - return -1; - } - bytes->length = size; - bytes->data = new_data; - } - return 0; -} - -/* Finishes processing of current advance or inspect. - * Returns 0 on success, -1 on error */ -int rollup_finish_request(int fd, struct rollup_finish *finish, bool accept) { - int res = 0; - memset(finish, 0, sizeof(*finish)); - finish->accept_previous_request = accept; - res = ioctl(fd, IOCTL_ROLLUP_FINISH, (unsigned long) finish); - return res; -} - -/* Obtains arguments to advance state - * Outputs metadata and payload in structs - * Returns 0 on success, -1 on error */ -int rollup_read_advance_state_request(int fd, struct rollup_finish *finish, struct rollup_bytes *bytes, - struct rollup_input_metadata *metadata) { - struct rollup_advance_state req; - int res = 0; - if (resize_bytes(bytes, finish->next_request_payload_length) != 0) { - fprintf(stderr, "Failed growing payload buffer\n"); - return -1; - } - memset(&req, 0, sizeof(req)); - req.payload = *bytes; - res = ioctl(fd, IOCTL_ROLLUP_READ_ADVANCE_STATE, (unsigned long) &req); - if (res != 0) { - fprintf(stderr, "IOCTL_ROLLUP_READ_ADVANCE_STATE returned error (%d), err message: %s\n", res, strerror(errno)); - } - *metadata = req.metadata; - return res; -} - -/* Obtains query of inspect state request - * Returns 0 on success, -1 on error */ -int rollup_read_inspect_state_request(int fd, struct rollup_finish *finish, struct rollup_bytes *query) { - struct rollup_inspect_state req; - int res = 0; - if (resize_bytes(query, finish->next_request_payload_length) != 0) { - fprintf(stderr, "Failed growing payload buffer\n"); - return -1; - } - memset(&req, 0, sizeof(req)); - req.payload = *query; - res = ioctl(fd, IOCTL_ROLLUP_READ_INSPECT_STATE, (unsigned long) &req); - if (res != 0) { - fprintf(stderr, "IOCTL_ROLLUP_READ_INSPECT_STATE returned error (%d), err message: %s\n", res, strerror(errno)); - return res; - } - return 0; -} - -/* Outputs a new voucher. - * voucher_index is filled with new index from the driver - * Returns 0 on success, -1 on error */ -int rollup_write_voucher(int fd, uint8_t destination[CARTESI_ROLLUP_ADDRESS_SIZE], struct rollup_bytes *bytes, - uint64_t *voucher_index) { - struct rollup_voucher v; - memset(&v, 0, sizeof(v)); - memcpy(v.destination, destination, CARTESI_ROLLUP_ADDRESS_SIZE); - v.payload = *bytes; - int res = ioctl(fd, IOCTL_ROLLUP_WRITE_VOUCHER, (unsigned long) &v); - if (res != 0) { - fprintf(stderr, "IOCTL_ROLLUP_WRITE_VOUCHER returned error %d, err message: %s\n", res, strerror(errno)); - return res; - } - *voucher_index = v.index; - return 0; -} - -/* Outputs a new notice. - * notice_index is filled with new index from the driver - * Returns 0 on success, -1 on error */ -int rollup_write_notice(int fd, struct rollup_bytes *bytes, uint64_t *notice_index) { - struct rollup_notice n; - memset(&n, 0, sizeof(n)); - n.payload = *bytes; - int res = ioctl(fd, IOCTL_ROLLUP_WRITE_NOTICE, (unsigned long) &n); - if (res != 0) { - fprintf(stderr, "IOCTL_ROLLUP_WRITE_NOTICE returned error %d, err message: %s\n", res, strerror(errno)); - return res; - } - *notice_index = n.index; - return 0; -} - -/* Outputs a new report. - * Returns 0 on success, -1 on error */ -int rollup_write_report(int fd, struct rollup_bytes *bytes) { - struct rollup_report r; - memset(&r, 0, sizeof(r)); - r.payload = *bytes; - int res = ioctl(fd, IOCTL_ROLLUP_WRITE_REPORT, (unsigned long) &r); - if (res != 0) { - fprintf(stderr, "IOCTL_ROLLUP_WRITE_REPORT returned error %d, err message: %s\n", res, strerror(errno)); - return res; - } - - return 0; -} - -/* Outputs a dapp exception. - * Returns 0 on success, -1 on error */ -int rollup_throw_exception(int fd, struct rollup_bytes *bytes) { - struct rollup_exception e; - memset(&e, 0, sizeof(e)); - e.payload = *bytes; - int res = ioctl(fd, IOCTL_ROLLUP_THROW_EXCEPTION, (unsigned long) &e); - if (res != 0) { - fprintf(stderr, "IOCTL_ROLLUP_THROW_EXCEPTION returned error %d, err message: %s\n", res, strerror(errno)); - return res; - } - return 0; -} diff --git a/rollup-http/rollup-http-server/src/rollup/bindings.h b/rollup-http/rollup-http-server/src/rollup/bindings.h deleted file mode 100644 index 766a14b0..00000000 --- a/rollup-http/rollup-http-server/src/rollup/bindings.h +++ /dev/null @@ -1,46 +0,0 @@ -/* Copyright Cartesi and individual authors (see AUTHORS) - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* C interfaces that are converted to rust using bindgen (https://github.com/rust-lang/rust-bindgen) - * command to generate Rust file from rollup C interface. In case that C rollup bindings are updated, - * `bindings.rs` file needs to be regenerated using following procedure: - * Add toolchain `riscv64-cartesi-linux-gnu` to the shell execution path - * $ cd linux/rollup/http/http-dispatcher/src/rollup - * $ bindgen ./bindings.h -o ./bindings.rs --whitelist-var '^IOCTL.*' --whitelist-var '^CARTESI.*' --whitelist-type - "^rollup_.*" --whitelist-function '^rollup.*'\ - -- --sysroot=/opt/riscv/riscv64-cartesi-linux-gnu/riscv64-cartesi-linux-gnu/sysroot - --target=riscv64-cartesi-linux-gnu -march=rv64gc -mabi=lp64d -*/ - -#include -#include -#include -#include -#include -#include - -#include -#include - -int rollup_finish_request(int fd, struct rollup_finish *finish, bool accept); -int rollup_read_advance_state_request(int fd, struct rollup_finish *finish, struct rollup_bytes *bytes, - struct rollup_input_metadata *metadata); -int rollup_read_inspect_state_request(int fd, struct rollup_finish *finish, struct rollup_bytes *query); -int rollup_write_voucher(int fd, uint8_t destination[CARTESI_ROLLUP_ADDRESS_SIZE], struct rollup_bytes *bytes, - uint64_t *voucher_index); -int rollup_write_notice(int fd, struct rollup_bytes *bytes, uint64_t *notice_index); -int rollup_write_report(int fd, struct rollup_bytes *bytes); -int rollup_throw_exception(int fd, struct rollup_bytes *bytes); diff --git a/rollup-http/rollup-http-server/src/rollup/bindings.rs b/rollup-http/rollup-http-server/src/rollup/bindings.rs index ff4cf8d4..6001a089 100644 --- a/rollup-http/rollup-http-server/src/rollup/bindings.rs +++ b/rollup-http/rollup-http-server/src/rollup/bindings.rs @@ -1,414 +1,1356 @@ -/* automatically generated by rust-bindgen 0.58.1 */ -#![allow(dead_code)] -#![allow(unused_variables)] -#![allow(non_upper_case_globals)] -#![allow(non_camel_case_types)] -#![allow(non_snake_case)] +/* automatically generated by rust-bindgen 0.69.2 */ -pub const CARTESI_ROLLUP_ADVANCE_STATE: u32 = 0; -pub const CARTESI_ROLLUP_INSPECT_STATE: u32 = 1; -pub const CARTESI_ROLLUP_ADDRESS_SIZE: u32 = 20; +pub const __bool_true_false_are_defined: u32 = 1; +pub const true_: u32 = 1; +pub const false_: u32 = 0; +pub const _STDINT_H: u32 = 1; +pub const _FEATURES_H: u32 = 1; +pub const _DEFAULT_SOURCE: u32 = 1; +pub const __GLIBC_USE_ISOC2X: u32 = 0; +pub const __USE_ISOC11: u32 = 1; +pub const __USE_ISOC99: u32 = 1; +pub const __USE_ISOC95: u32 = 1; +pub const __USE_POSIX_IMPLICITLY: u32 = 1; +pub const _POSIX_SOURCE: u32 = 1; +pub const _POSIX_C_SOURCE: u32 = 200809; +pub const __USE_POSIX: u32 = 1; +pub const __USE_POSIX2: u32 = 1; +pub const __USE_POSIX199309: u32 = 1; +pub const __USE_POSIX199506: u32 = 1; +pub const __USE_XOPEN2K: u32 = 1; +pub const __USE_XOPEN2K8: u32 = 1; +pub const _ATFILE_SOURCE: u32 = 1; +pub const __WORDSIZE: u32 = 64; +pub const __WORDSIZE_TIME64_COMPAT32: u32 = 1; +pub const __SYSCALL_WORDSIZE: u32 = 64; +pub const __TIMESIZE: u32 = 64; +pub const __USE_MISC: u32 = 1; +pub const __USE_ATFILE: u32 = 1; +pub const __USE_FORTIFY_LEVEL: u32 = 0; +pub const __GLIBC_USE_DEPRECATED_GETS: u32 = 0; +pub const __GLIBC_USE_DEPRECATED_SCANF: u32 = 0; +pub const _STDC_PREDEF_H: u32 = 1; +pub const __STDC_IEC_559__: u32 = 1; +pub const __STDC_IEC_60559_BFP__: u32 = 201404; +pub const __STDC_IEC_559_COMPLEX__: u32 = 1; +pub const __STDC_IEC_60559_COMPLEX__: u32 = 201404; +pub const __STDC_ISO_10646__: u32 = 201706; +pub const __GNU_LIBRARY__: u32 = 6; +pub const __GLIBC__: u32 = 2; +pub const __GLIBC_MINOR__: u32 = 37; +pub const _SYS_CDEFS_H: u32 = 1; +pub const __glibc_c99_flexarr_available: u32 = 1; +pub const __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI: u32 = 0; +pub const __HAVE_GENERIC_SELECTION: u32 = 1; +pub const __GLIBC_USE_LIB_EXT2: u32 = 0; +pub const __GLIBC_USE_IEC_60559_BFP_EXT: u32 = 0; +pub const __GLIBC_USE_IEC_60559_BFP_EXT_C2X: u32 = 0; +pub const __GLIBC_USE_IEC_60559_EXT: u32 = 0; +pub const __GLIBC_USE_IEC_60559_FUNCS_EXT: u32 = 0; +pub const __GLIBC_USE_IEC_60559_FUNCS_EXT_C2X: u32 = 0; +pub const __GLIBC_USE_IEC_60559_TYPES_EXT: u32 = 0; +pub const _BITS_TYPES_H: u32 = 1; +pub const _BITS_TYPESIZES_H: u32 = 1; +pub const __OFF_T_MATCHES_OFF64_T: u32 = 1; +pub const __INO_T_MATCHES_INO64_T: u32 = 1; +pub const __RLIM_T_MATCHES_RLIM64_T: u32 = 1; +pub const __STATFS_MATCHES_STATFS64: u32 = 1; +pub const __KERNEL_OLD_TIMEVAL_MATCHES_TIMEVAL64: u32 = 1; +pub const __FD_SETSIZE: u32 = 1024; +pub const _BITS_TIME64_H: u32 = 1; +pub const _BITS_WCHAR_H: u32 = 1; +pub const _BITS_STDINT_INTN_H: u32 = 1; +pub const _BITS_STDINT_UINTN_H: u32 = 1; +pub const INT8_MIN: i32 = -128; +pub const INT16_MIN: i32 = -32768; +pub const INT32_MIN: i32 = -2147483648; +pub const INT8_MAX: u32 = 127; +pub const INT16_MAX: u32 = 32767; +pub const INT32_MAX: u32 = 2147483647; +pub const UINT8_MAX: u32 = 255; +pub const UINT16_MAX: u32 = 65535; +pub const UINT32_MAX: u32 = 4294967295; +pub const INT_LEAST8_MIN: i32 = -128; +pub const INT_LEAST16_MIN: i32 = -32768; +pub const INT_LEAST32_MIN: i32 = -2147483648; +pub const INT_LEAST8_MAX: u32 = 127; +pub const INT_LEAST16_MAX: u32 = 32767; +pub const INT_LEAST32_MAX: u32 = 2147483647; +pub const UINT_LEAST8_MAX: u32 = 255; +pub const UINT_LEAST16_MAX: u32 = 65535; +pub const UINT_LEAST32_MAX: u32 = 4294967295; +pub const INT_FAST8_MIN: i32 = -128; +pub const INT_FAST16_MIN: i64 = -9223372036854775808; +pub const INT_FAST32_MIN: i64 = -9223372036854775808; +pub const INT_FAST8_MAX: u32 = 127; +pub const INT_FAST16_MAX: u64 = 9223372036854775807; +pub const INT_FAST32_MAX: u64 = 9223372036854775807; +pub const UINT_FAST8_MAX: u32 = 255; +pub const UINT_FAST16_MAX: i32 = -1; +pub const UINT_FAST32_MAX: i32 = -1; +pub const INTPTR_MIN: i64 = -9223372036854775808; +pub const INTPTR_MAX: u64 = 9223372036854775807; +pub const UINTPTR_MAX: i32 = -1; +pub const PTRDIFF_MIN: i64 = -9223372036854775808; +pub const PTRDIFF_MAX: u64 = 9223372036854775807; +pub const SIG_ATOMIC_MIN: i32 = -2147483648; +pub const SIG_ATOMIC_MAX: u32 = 2147483647; +pub const SIZE_MAX: i32 = -1; +pub const WINT_MIN: u32 = 0; +pub const WINT_MAX: u32 = 4294967295; +pub type wchar_t = ::std::os::raw::c_int; +#[repr(C)] +#[repr(align(16))] +#[derive(Debug, Copy, Clone)] +pub struct max_align_t { + pub __clang_max_align_nonce1: ::std::os::raw::c_longlong, + pub __bindgen_padding_0: u64, + pub __clang_max_align_nonce2: u128, +} +#[test] +fn bindgen_test_layout_max_align_t() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 32usize, + concat!("Size of: ", stringify!(max_align_t)) + ); + assert_eq!( + ::std::mem::align_of::(), + 16usize, + concat!("Alignment of ", stringify!(max_align_t)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).__clang_max_align_nonce1) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(max_align_t), + "::", + stringify!(__clang_max_align_nonce1) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).__clang_max_align_nonce2) as usize - ptr as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(max_align_t), + "::", + stringify!(__clang_max_align_nonce2) + ) + ); +} +pub type __u_char = ::std::os::raw::c_uchar; +pub type __u_short = ::std::os::raw::c_ushort; +pub type __u_int = ::std::os::raw::c_uint; +pub type __u_long = ::std::os::raw::c_ulong; +pub type __int8_t = ::std::os::raw::c_schar; pub type __uint8_t = ::std::os::raw::c_uchar; +pub type __int16_t = ::std::os::raw::c_short; +pub type __uint16_t = ::std::os::raw::c_ushort; +pub type __int32_t = ::std::os::raw::c_int; +pub type __uint32_t = ::std::os::raw::c_uint; +pub type __int64_t = ::std::os::raw::c_long; pub type __uint64_t = ::std::os::raw::c_ulong; +pub type __int_least8_t = __int8_t; +pub type __uint_least8_t = __uint8_t; +pub type __int_least16_t = __int16_t; +pub type __uint_least16_t = __uint16_t; +pub type __int_least32_t = __int32_t; +pub type __uint_least32_t = __uint32_t; +pub type __int_least64_t = __int64_t; +pub type __uint_least64_t = __uint64_t; +pub type __quad_t = ::std::os::raw::c_long; +pub type __u_quad_t = ::std::os::raw::c_ulong; +pub type __intmax_t = ::std::os::raw::c_long; +pub type __uintmax_t = ::std::os::raw::c_ulong; +pub type __dev_t = ::std::os::raw::c_ulong; +pub type __uid_t = ::std::os::raw::c_uint; +pub type __gid_t = ::std::os::raw::c_uint; +pub type __ino_t = ::std::os::raw::c_ulong; +pub type __ino64_t = ::std::os::raw::c_ulong; +pub type __mode_t = ::std::os::raw::c_uint; +pub type __nlink_t = ::std::os::raw::c_ulong; +pub type __off_t = ::std::os::raw::c_long; +pub type __off64_t = ::std::os::raw::c_long; +pub type __pid_t = ::std::os::raw::c_int; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct __fsid_t { + pub __val: [::std::os::raw::c_int; 2usize], +} +#[test] +fn bindgen_test_layout___fsid_t() { + const UNINIT: ::std::mem::MaybeUninit<__fsid_t> = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::<__fsid_t>(), + 8usize, + concat!("Size of: ", stringify!(__fsid_t)) + ); + assert_eq!( + ::std::mem::align_of::<__fsid_t>(), + 4usize, + concat!("Alignment of ", stringify!(__fsid_t)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).__val) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(__fsid_t), + "::", + stringify!(__val) + ) + ); +} +pub type __clock_t = ::std::os::raw::c_long; +pub type __rlim_t = ::std::os::raw::c_ulong; +pub type __rlim64_t = ::std::os::raw::c_ulong; +pub type __id_t = ::std::os::raw::c_uint; +pub type __time_t = ::std::os::raw::c_long; +pub type __useconds_t = ::std::os::raw::c_uint; +pub type __suseconds_t = ::std::os::raw::c_long; +pub type __suseconds64_t = ::std::os::raw::c_long; +pub type __daddr_t = ::std::os::raw::c_int; +pub type __key_t = ::std::os::raw::c_int; +pub type __clockid_t = ::std::os::raw::c_int; +pub type __timer_t = *mut ::std::os::raw::c_void; +pub type __blksize_t = ::std::os::raw::c_long; +pub type __blkcnt_t = ::std::os::raw::c_long; +pub type __blkcnt64_t = ::std::os::raw::c_long; +pub type __fsblkcnt_t = ::std::os::raw::c_ulong; +pub type __fsblkcnt64_t = ::std::os::raw::c_ulong; +pub type __fsfilcnt_t = ::std::os::raw::c_ulong; +pub type __fsfilcnt64_t = ::std::os::raw::c_ulong; +pub type __fsword_t = ::std::os::raw::c_long; +pub type __ssize_t = ::std::os::raw::c_long; +pub type __syscall_slong_t = ::std::os::raw::c_long; +pub type __syscall_ulong_t = ::std::os::raw::c_ulong; +pub type __loff_t = __off64_t; +pub type __caddr_t = *mut ::std::os::raw::c_char; +pub type __intptr_t = ::std::os::raw::c_long; +pub type __socklen_t = ::std::os::raw::c_uint; +pub type __sig_atomic_t = ::std::os::raw::c_int; +pub type int_least8_t = __int_least8_t; +pub type int_least16_t = __int_least16_t; +pub type int_least32_t = __int_least32_t; +pub type int_least64_t = __int_least64_t; +pub type uint_least8_t = __uint_least8_t; +pub type uint_least16_t = __uint_least16_t; +pub type uint_least32_t = __uint_least32_t; +pub type uint_least64_t = __uint_least64_t; +pub type int_fast8_t = ::std::os::raw::c_schar; +pub type int_fast16_t = ::std::os::raw::c_long; +pub type int_fast32_t = ::std::os::raw::c_long; +pub type int_fast64_t = ::std::os::raw::c_long; +pub type uint_fast8_t = ::std::os::raw::c_uchar; +pub type uint_fast16_t = ::std::os::raw::c_ulong; +pub type uint_fast32_t = ::std::os::raw::c_ulong; +pub type uint_fast64_t = ::std::os::raw::c_ulong; +pub type intmax_t = __intmax_t; +pub type uintmax_t = __uintmax_t; +#[doc = " A slice of contiguous memory from @b begin to @b end, as an open range.\n Size can be taken with: `end - begin`.\n\n `begin == end` indicate an empty buffer"] #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct rollup_bytes { - pub data: *mut ::std::os::raw::c_uchar, - pub length: u64, +pub struct cmt_buf_t { + #[doc = "< begin of memory region"] + pub begin: *mut u8, + #[doc = "< end of memory region"] + pub end: *mut u8, } #[test] -fn bindgen_test_layout_rollup_bytes() { +fn bindgen_test_layout_cmt_buf_t() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); assert_eq!( - ::std::mem::size_of::(), + ::std::mem::size_of::(), 16usize, - concat!("Size of: ", stringify!(rollup_bytes)) + concat!("Size of: ", stringify!(cmt_buf_t)) ); assert_eq!( - ::std::mem::align_of::(), + ::std::mem::align_of::(), 8usize, - concat!("Alignment of ", stringify!(rollup_bytes)) + concat!("Alignment of ", stringify!(cmt_buf_t)) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).data as *const _ as usize }, + unsafe { ::std::ptr::addr_of!((*ptr).begin) as usize - ptr as usize }, 0usize, concat!( "Offset of field: ", - stringify!(rollup_bytes), + stringify!(cmt_buf_t), "::", - stringify!(data) + stringify!(begin) ) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).length as *const _ as usize }, + unsafe { ::std::ptr::addr_of!((*ptr).end) as usize - ptr as usize }, 8usize, concat!( "Offset of field: ", - stringify!(rollup_bytes), + stringify!(cmt_buf_t), "::", - stringify!(length) + stringify!(end) ) ); } +extern "C" { + #[doc = " Initialize @p me buffer backed by @p data, @p length bytes in size\n\n @param [out] me a uninitialized instance\n @param [in] length size in bytes of @b data\n @param [in] data the backing memory to be used.\n\n @note @p data memory must outlive @p me.\n user must copy the contents otherwise"] + pub fn cmt_buf_init(me: *mut cmt_buf_t, length: usize, data: *mut ::std::os::raw::c_void); +} +extern "C" { + #[doc = " Split a buffer in two, @b lhs with @b lhs_length bytes and @b rhs with the rest\n\n @param [in,out] me initialized buffer\n @param [in] lhs_length bytes in @b lhs\n @param [out] lhs left hand side\n @param [out] rhs right hand side\n\n @return\n - 0 success\n - negative value on error. -ENOBUFS when length(me) < lhs_length."] + pub fn cmt_buf_split( + me: *const cmt_buf_t, + lhs_length: usize, + lhs: *mut cmt_buf_t, + rhs: *mut cmt_buf_t, + ) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Length in bytes of @p me\n\n @param [in] me initialized buffer\n\n @return\n - size in bytes"] + pub fn cmt_buf_length(me: *const cmt_buf_t) -> usize; +} +extern "C" { + #[doc = " Print the contents of @b me buffer to stdout\n\n @param [in] begin start of memory region\n @param [in] end end of memory region\n @param [in] bytes_per_line bytes per line (must be a power of 2)."] + pub fn cmt_buf_xxd( + begin: *mut ::std::os::raw::c_void, + end: *mut ::std::os::raw::c_void, + bytes_per_line: ::std::os::raw::c_int, + ); +} +extern "C" { + #[doc = " Take the substring @p x from @p xs start to the first @p , (comma).\n @param [out] x substring\n @param [in,out] xs string (interator)\n\n @note @p x points inside @p xs, make a copy if it outlives @p xs."] + pub fn cmt_buf_split_by_comma(x: *mut cmt_buf_t, xs: *mut cmt_buf_t) -> bool; +} +#[doc = "< length of a evm word in bytes"] +pub const CMT_WORD_LENGTH: _bindgen_ty_1 = 32; +#[doc = "< length of a evm address in bytes"] +pub const CMT_ADDRESS_LENGTH: _bindgen_ty_1 = 20; +pub type _bindgen_ty_1 = ::std::os::raw::c_uint; +extern "C" { + #[doc = " Create a function selector from an array of bytes\n @param [in] funsel function selector bytes\n @return\n - function selector converted to big endian (as expected by EVM)"] + pub fn cmt_abi_funsel(a: u8, b: u8, c: u8, d: u8) -> u32; +} +extern "C" { + #[doc = " Encode a function selector into the buffer @p me\n\n @param [in,out] me a initialized buffer working as iterator\n @param [in] funsel function selector\n\n @return\n - 0 success\n - ENOBUFS no space left in @p me\n\n @note A function selector can be compute it with: @ref cmt_keccak_funsel.\n It is always represented in big endian."] + pub fn cmt_abi_put_funsel(me: *mut cmt_buf_t, funsel: u32) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Encode a unsigned integer of up to 32bytes of data into the buffer\n\n @param [in,out] me a initialized buffer working as iterator\n @param [in] n size of @p data in bytes\n @param [in] data poiter to a integer\n\n @return\n - 0 success\n - ENOBUFS no space left in @p me\n - EDOM requested @p n is too large\n\n @code\n ...\n cmt_buf_t it = ...;\n uint64_t x = UINT64_C(0xdeadbeef);\n cmt_abi_put_uint(&it, sizeof x, &x);\n ...\n @endcode\n @note This function takes care of endianess conversions"] + pub fn cmt_abi_put_uint( + me: *mut cmt_buf_t, + n: usize, + data: *const ::std::os::raw::c_void, + ) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Encode a big-endian value of up to 32bytes of data into the buffer\n\n @param [in,out] me a initialized buffer working as iterator\n @param [in] length size of @p data in bytes\n @param [in] data poiter to a integer\n\n @return\n - 0 success\n - ENOBUFS no space left in @p me\n - EDOM requested @p n is too large\n\n @code\n ...\n cmt_buf_t it = ...;\n uint8_t small[] = {\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,\n };\n cmt_abi_put_uint(&it, sizeof small, &small);\n ...\n uint8_t big[] = {\n 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n };\n cmt_abi_put_uint(&it, sizeof big, &big);\n @endcode\n @note This function takes care of endianess conversions"] + pub fn cmt_abi_put_uint_be( + me: *mut cmt_buf_t, + n: usize, + data: *const ::std::os::raw::c_void, + ) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Encode a bool into the buffer\n\n @param [in,out] me a initialized buffer working as iterator\n @param [in] value boolean value\n\n @return\n - 0 success\n - ENOBUFS no space left in @p me\n\n @code\n ...\n cmt_buf_t it = ...;\n cmt_abi_put_bool(&it, true);\n ...\n @endcode\n @note This function takes care of endianess conversions"] + pub fn cmt_abi_put_bool(me: *mut cmt_buf_t, value: bool) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Encode @p address (exacly @ref CMT_ADDRESS_LENGTH bytes) into the buffer\n\n @param [in,out] me initialized buffer\n @param [in] address exactly @ref CMT_ADDRESS_LENGTH bytes\n\n @return\n - 0 success\n - ENOBUFS no space left in @p me"] + pub fn cmt_abi_put_address(me: *mut cmt_buf_t, address: *const u8) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Encode the static part of @b bytes into the message,\n used in conjunction with @ref cmt_abi_put_bytes_d\n\n @param [in,out] me initialized buffer\n @param [out] offset initialize for @ref cmt_abi_put_bytes_d\n @return\n - 0 success\n - ENOBUFS no space left in @p me"] + pub fn cmt_abi_put_bytes_s(me: *mut cmt_buf_t, offset: *mut cmt_buf_t) + -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Encode the dynamic part of @b bytes into the message,\n used in conjunction with @ref cmt_abi_put_bytes_d\n\n @param [in,out] me initialized buffer\n @param [in] offset initialized from @ref cmt_abi_put_bytes_h\n @param [in] n size of @b data\n @param [in] data array of bytes\n @param [in] start starting point for offset calculation (first byte after funsel)\n @return\n - 0 success\n - ENOBUFS no space left in @p me"] + pub fn cmt_abi_put_bytes_d( + me: *mut cmt_buf_t, + offset: *mut cmt_buf_t, + n: usize, + data: *const ::std::os::raw::c_void, + start: *const ::std::os::raw::c_void, + ) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Reserve @b n bytes of data from the buffer into @b res to be filled by the\n caller\n\n @param [in,out] me initialized buffer\n @param [in] n amount of bytes to reserve\n @param [out] res slice of bytes extracted from @p me\n @param [in] start starting point for offset calculation (first byte after funsel)\n @return\n - 0 success\n - ENOBUFS no space left in @p me\n\n @note @p me must outlive @p res.\n Create a duplicate otherwise"] + pub fn cmt_abi_reserve_bytes_d( + me: *mut cmt_buf_t, + of: *mut cmt_buf_t, + n: usize, + out: *mut cmt_buf_t, + start: *const ::std::os::raw::c_void, + ) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Read the funsel without consuming it from the buffer @p me\n\n @param [in] me initialized buffer\n @return\n - The function selector\n\n @code\n ...\n if (cmt_buf_length(it) < 4)\n \treturn EXIT_FAILURE;\n switch (cmt_abi_peek_funsel(it) {\n case CMT_ABI_FUNSEL(...): // known type, try to parse it\n case CMT_ABI_FUNSEL(...): // known type, try to parse it\n default:\n \treturn EXIT_FAILURE;\n }\n @endcode\n\n @note user must ensure there are at least 4 bytes in the buffer.\n This function will fail and return 0 if that is not the case."] + pub fn cmt_abi_peek_funsel(me: *mut cmt_buf_t) -> u32; +} +extern "C" { + #[doc = " Consume funsel from the buffer @p me and ensure it matches @p expected_funsel\n\n @param [in,out] me initialized buffer\n @param [in] expected expected function selector\n\n @return\n - 0 success\n - ENOBUFS no space left in @p me\n - EBADMSG in case of a missmatch"] + pub fn cmt_abi_check_funsel(me: *mut cmt_buf_t, expected: u32) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Decode a unsigned integer of up to 32bytes from the buffer\n\n @param [in,out] me initialized buffer\n @param [in] n size of @p data in bytes\n @param [out] data pointer to a integer\n\n @return\n - 0 success\n - ENOBUFS no space left in @p me\n - EDOM value won't fit into @p n bytes."] + pub fn cmt_abi_get_uint( + me: *mut cmt_buf_t, + n: usize, + data: *mut ::std::os::raw::c_void, + ) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Decode @p length big-endian bytes, up to 32, from the buffer into @p data\n\n @param [in,out] me initialized buffer\n @param [in] length size of @p data in bytes\n @param [out] data pointer to a integer\n\n @return\n - 0 success\n - ENOBUFS no space left in @p me\n - EDOM value won't fit into @p n bytes."] + pub fn cmt_abi_get_uint_be( + me: *mut cmt_buf_t, + n: usize, + data: *mut ::std::os::raw::c_void, + ) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Consume and decode @b address from the buffer\n\n @param [in,out] me initialized buffer\n @param [out] address exactly 20 bytes\n\n @return\n - 0 success\n - ENOBUFS requested size @b n is not available"] + pub fn cmt_abi_get_address(me: *mut cmt_buf_t, address: *mut u8) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Consume and decode the offset @p of\n\n @param [in,out] me initialized buffer\n @param [out] of offset to @p bytes data, for use in conjunction with @ref cmt_abi_get_bytes_d\n @return\n - 0 success\n - ENOBUFS no space left in @p me"] + pub fn cmt_abi_get_bytes_s(me: *mut cmt_buf_t, of: *mut cmt_buf_t) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Decode @b bytes from the buffer by taking a pointer to its contents.\n\n @param [in] start initialized buffer (from the start after funsel)\n @param [out] of offset to @p bytes data\n @param [out] n amount of data available in @b bytes\n @param [out] bytes memory range with contents\n @return\n - 0 success\n - ENOBUFS no space left in @p me\n @note @p of can be initialized by calling @ref cmt_abi_get_bytes_s"] + pub fn cmt_abi_get_bytes_d( + start: *const cmt_buf_t, + of: *mut cmt_buf_t, + n: *mut usize, + bytes: *mut *mut ::std::os::raw::c_void, + ) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Decode @b bytes from the buffer by taking a pointer to its contents.\n\n @param [in] start initialized buffer (from the start after funsel)\n @param [out] of offset to @p bytes data\n @param [out] n amount of data available in @b bytes\n @param [out] bytes memory range with contents\n @return\n - 0 success\n - ENOBUFS no space left in @p me\n @note @p of can be initialized by calling @ref cmt_abi_get_bytes_s"] + pub fn cmt_abi_peek_bytes_d( + start: *const cmt_buf_t, + of: *mut cmt_buf_t, + bytes: *mut cmt_buf_t, + ) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Encode @p n bytes of @p data into @p out (up to 32).\n\n @param [in] n size of @p data in bytes\n @param [in] data integer value to encode into @p out\n @param [out] out encoded result\n @return\n - 0 success\n - EDOM @p n is too large."] + pub fn cmt_abi_encode_uint( + n: usize, + data: *const ::std::os::raw::c_void, + out: *mut u8, + ) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Encode @p n bytes of @p data into @p out (up to 32) in reverse order.\n\n @param [in] n size of @p data in bytes\n @param [in] data integer value to encode into @p out\n @param [out] out encoded result\n @return\n - 0 success\n - EDOM @p n is too large.\n @note use @ref cmt_abi_encode_uint instead"] + pub fn cmt_abi_encode_uint_nr(n: usize, data: *const u8, out: *mut u8) + -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Encode @p n bytes of @p data into @p out (up to 32) in normal order.\n\n @param [in] n size of @p data in bytes\n @param [in] data integer value to encode into @p out\n @param [out] out encoded result\n @return\n - 0 success\n - EDOM @p n is too large.\n @note use @ref cmt_abi_encode_uint instead"] + pub fn cmt_abi_encode_uint_nn(n: usize, data: *const u8, out: *mut u8) + -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Decode @p n bytes of @p data into @p out (up to 32).\n\n @param [in] data integer value to decode into @p out\n @param [in] n size of @p data in bytes\n @param [out] out decoded output"] + pub fn cmt_abi_decode_uint(data: *const u8, n: usize, out: *mut u8) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Decode @p n bytes of @p data into @p out (up to 32) in reverse order.\n\n @param [in] data integer value to decode into @p out\n @param [in] n size of @p data in bytes\n @param [out] out decoded output\n @note if in doubt, use @ref cmt_abi_decode_uint"] + pub fn cmt_abi_decode_uint_nr(data: *const u8, n: usize, out: *mut u8) + -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Decode @p n bytes of @p data into @p out (up to 32) in normal order.\n\n @param [in] data integer value to decode into @p out\n @param [in] n size of @p data in bytes\n @param [out] out decoded output\n @note if in doubt, use @ref cmt_abi_decode_uint"] + pub fn cmt_abi_decode_uint_nn(data: *const u8, n: usize, out: *mut u8) + -> ::std::os::raw::c_int; +} +#[doc = "< IO Device"] +pub const CMT_IO_DEV: _bindgen_ty_2 = 2; +pub type _bindgen_ty_2 = ::std::os::raw::c_uint; +#[doc = "< Automatic"] +pub const CMT_IO_CMD_AUTOMATIC: _bindgen_ty_3 = 0; +#[doc = "< Manual"] +pub const CMT_IO_CMD_MANUAL: _bindgen_ty_3 = 1; +#[doc = " Request"] +pub type _bindgen_ty_3 = ::std::os::raw::c_uint; +#[doc = "< Progress"] +pub const CMT_IO_REASON_PROGRESS: _bindgen_ty_4 = 0; +#[doc = "< Accept and load next input"] +pub const CMT_IO_REASON_RX_ACCEPTED: _bindgen_ty_4 = 1; +#[doc = "< Reject and revert"] +pub const CMT_IO_REASON_RX_REJECTED: _bindgen_ty_4 = 2; +#[doc = "< emit an output"] +pub const CMT_IO_REASON_TX_OUTPUT: _bindgen_ty_4 = 3; +#[doc = "< emit a report"] +pub const CMT_IO_REASON_TX_REPORT: _bindgen_ty_4 = 4; +#[doc = "< emit a exception and halt execution"] +pub const CMT_IO_REASON_TX_EXCEPTION: _bindgen_ty_4 = 5; +#[doc = " Request"] +pub type _bindgen_ty_4 = ::std::os::raw::c_uint; +#[doc = "< Input is advance"] +pub const CMT_IO_REASON_ADVANCE: _bindgen_ty_5 = 0; +#[doc = "< Input is inspect"] +pub const CMT_IO_REASON_INSPECT: _bindgen_ty_5 = 1; +#[doc = " Reply reason when requesting @ref CMT_IO_REASON_RX_ACCEPTED"] +pub type _bindgen_ty_5 = ::std::os::raw::c_uint; #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct rollup_input_metadata { - pub msg_sender: [u8; 20usize], - pub block_number: u64, - pub timestamp: u64, - pub epoch_index: u64, - pub input_index: u64, +pub struct cmt_io_driver_ioctl_t { + pub tx: [cmt_buf_t; 1usize], + pub rx: [cmt_buf_t; 1usize], + pub fd: ::std::os::raw::c_int, } #[test] -fn bindgen_test_layout_rollup_input_metadata() { +fn bindgen_test_layout_cmt_io_driver_ioctl_t() { + const UNINIT: ::std::mem::MaybeUninit = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); assert_eq!( - ::std::mem::size_of::(), - 56usize, - concat!("Size of: ", stringify!(rollup_input_metadata)) + ::std::mem::size_of::(), + 40usize, + concat!("Size of: ", stringify!(cmt_io_driver_ioctl_t)) ); assert_eq!( - ::std::mem::align_of::(), + ::std::mem::align_of::(), 8usize, - concat!("Alignment of ", stringify!(rollup_input_metadata)) + concat!("Alignment of ", stringify!(cmt_io_driver_ioctl_t)) ); assert_eq!( - unsafe { - &(*(::std::ptr::null::())).msg_sender as *const _ as usize - }, + unsafe { ::std::ptr::addr_of!((*ptr).tx) as usize - ptr as usize }, 0usize, concat!( "Offset of field: ", - stringify!(rollup_input_metadata), + stringify!(cmt_io_driver_ioctl_t), "::", - stringify!(msg_sender) + stringify!(tx) ) ); assert_eq!( - unsafe { - &(*(::std::ptr::null::())).block_number as *const _ as usize - }, - 24usize, + unsafe { ::std::ptr::addr_of!((*ptr).rx) as usize - ptr as usize }, + 16usize, concat!( "Offset of field: ", - stringify!(rollup_input_metadata), + stringify!(cmt_io_driver_ioctl_t), "::", - stringify!(block_number) + stringify!(rx) ) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).timestamp as *const _ as usize }, + unsafe { ::std::ptr::addr_of!((*ptr).fd) as usize - ptr as usize }, 32usize, concat!( "Offset of field: ", - stringify!(rollup_input_metadata), + stringify!(cmt_io_driver_ioctl_t), "::", - stringify!(timestamp) + stringify!(fd) ) ); +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct cmt_io_driver_mock_t { + pub tx: [cmt_buf_t; 1usize], + pub rx: [cmt_buf_t; 1usize], + pub inputs_left: cmt_buf_t, + pub input_type: ::std::os::raw::c_int, + pub input_filename: [::std::os::raw::c_char; 128usize], + pub input_fileext: [::std::os::raw::c_char; 16usize], + pub input_seq: ::std::os::raw::c_int, + pub output_seq: ::std::os::raw::c_int, + pub report_seq: ::std::os::raw::c_int, + pub exception_seq: ::std::os::raw::c_int, +} +#[test] +fn bindgen_test_layout_cmt_io_driver_mock_t() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); assert_eq!( - unsafe { - &(*(::std::ptr::null::())).epoch_index as *const _ as usize - }, - 40usize, + ::std::mem::size_of::(), + 216usize, + concat!("Size of: ", stringify!(cmt_io_driver_mock_t)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(cmt_io_driver_mock_t)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).tx) as usize - ptr as usize }, + 0usize, concat!( "Offset of field: ", - stringify!(rollup_input_metadata), + stringify!(cmt_io_driver_mock_t), "::", - stringify!(epoch_index) + stringify!(tx) ) ); assert_eq!( - unsafe { - &(*(::std::ptr::null::())).input_index as *const _ as usize - }, + unsafe { ::std::ptr::addr_of!((*ptr).rx) as usize - ptr as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(cmt_io_driver_mock_t), + "::", + stringify!(rx) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).inputs_left) as usize - ptr as usize }, + 32usize, + concat!( + "Offset of field: ", + stringify!(cmt_io_driver_mock_t), + "::", + stringify!(inputs_left) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).input_type) as usize - ptr as usize }, 48usize, concat!( "Offset of field: ", - stringify!(rollup_input_metadata), + stringify!(cmt_io_driver_mock_t), + "::", + stringify!(input_type) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).input_filename) as usize - ptr as usize }, + 52usize, + concat!( + "Offset of field: ", + stringify!(cmt_io_driver_mock_t), + "::", + stringify!(input_filename) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).input_fileext) as usize - ptr as usize }, + 180usize, + concat!( + "Offset of field: ", + stringify!(cmt_io_driver_mock_t), "::", - stringify!(input_index) + stringify!(input_fileext) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).input_seq) as usize - ptr as usize }, + 196usize, + concat!( + "Offset of field: ", + stringify!(cmt_io_driver_mock_t), + "::", + stringify!(input_seq) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).output_seq) as usize - ptr as usize }, + 200usize, + concat!( + "Offset of field: ", + stringify!(cmt_io_driver_mock_t), + "::", + stringify!(output_seq) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).report_seq) as usize - ptr as usize }, + 204usize, + concat!( + "Offset of field: ", + stringify!(cmt_io_driver_mock_t), + "::", + stringify!(report_seq) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).exception_seq) as usize - ptr as usize }, + 208usize, + concat!( + "Offset of field: ", + stringify!(cmt_io_driver_mock_t), + "::", + stringify!(exception_seq) ) ); } +#[doc = " Implementation specific cmio state."] #[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct rollup_advance_state { - pub metadata: rollup_input_metadata, - pub payload: rollup_bytes, +#[derive(Copy, Clone)] +pub union cmt_io_driver { + pub ioctl: cmt_io_driver_ioctl_t, + pub mock: cmt_io_driver_mock_t, } #[test] -fn bindgen_test_layout_rollup_advance_state() { +fn bindgen_test_layout_cmt_io_driver() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); assert_eq!( - ::std::mem::size_of::(), - 72usize, - concat!("Size of: ", stringify!(rollup_advance_state)) + ::std::mem::size_of::(), + 216usize, + concat!("Size of: ", stringify!(cmt_io_driver)) ); assert_eq!( - ::std::mem::align_of::(), + ::std::mem::align_of::(), 8usize, - concat!("Alignment of ", stringify!(rollup_advance_state)) + concat!("Alignment of ", stringify!(cmt_io_driver)) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).metadata as *const _ as usize }, + unsafe { ::std::ptr::addr_of!((*ptr).ioctl) as usize - ptr as usize }, 0usize, concat!( "Offset of field: ", - stringify!(rollup_advance_state), + stringify!(cmt_io_driver), "::", - stringify!(metadata) + stringify!(ioctl) ) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).payload as *const _ as usize }, - 56usize, + unsafe { ::std::ptr::addr_of!((*ptr).mock) as usize - ptr as usize }, + 0usize, concat!( "Offset of field: ", - stringify!(rollup_advance_state), + stringify!(cmt_io_driver), "::", - stringify!(payload) + stringify!(mock) ) ); } +#[doc = " Implementation specific cmio state."] +pub type cmt_io_driver_t = cmt_io_driver; +#[doc = " yield struct cmt_io_yield"] #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct rollup_inspect_state { - pub payload: rollup_bytes, +pub struct cmt_io_yield { + pub dev: u8, + pub cmd: u8, + pub reason: u16, + pub data: u32, } #[test] -fn bindgen_test_layout_rollup_inspect_state() { +fn bindgen_test_layout_cmt_io_yield() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); assert_eq!( - ::std::mem::size_of::(), - 16usize, - concat!("Size of: ", stringify!(rollup_inspect_state)) + ::std::mem::size_of::(), + 8usize, + concat!("Size of: ", stringify!(cmt_io_yield)) + ); + assert_eq!( + ::std::mem::align_of::(), + 4usize, + concat!("Alignment of ", stringify!(cmt_io_yield)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).dev) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(cmt_io_yield), + "::", + stringify!(dev) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).cmd) as usize - ptr as usize }, + 1usize, + concat!( + "Offset of field: ", + stringify!(cmt_io_yield), + "::", + stringify!(cmd) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).reason) as usize - ptr as usize }, + 2usize, + concat!( + "Offset of field: ", + stringify!(cmt_io_yield), + "::", + stringify!(reason) + ) ); assert_eq!( - ::std::mem::align_of::(), + unsafe { ::std::ptr::addr_of!((*ptr).data) as usize - ptr as usize }, + 4usize, + concat!( + "Offset of field: ", + stringify!(cmt_io_yield), + "::", + stringify!(data) + ) + ); +} +#[doc = " yield struct cmt_io_yield"] +pub type cmt_io_yield_t = cmt_io_yield; +extern "C" { + #[doc = " Open the io device and initialize the driver. Release its resources with @ref cmt_io_fini.\n\n @param [in] me A uninitialized @ref cmt_io_driver state\n @returns\n - 0 on success\n - negative errno code on error"] + pub fn cmt_io_init(me: *mut cmt_io_driver_t) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Release the driver resources and close the io device.\n\n @param [in] me A sucessfuly initialized state by @ref cmt_io_init\n @note usage of @p me after this call is a BUG and will cause undefined behaviour"] + pub fn cmt_io_fini(me: *mut cmt_io_driver_t); +} +extern "C" { + #[doc = " Retrieve the transmit buffer @p tx\n\n @param [in] me A sucessfuly initialized state by @ref cmt_io_init\n @return\n - writable memory region (check @ref cmt_buf_t)\n @note memory is valid until @ref cmt_io_fini is called."] + pub fn cmt_io_get_tx(me: *mut cmt_io_driver_t) -> cmt_buf_t; +} +extern "C" { + #[doc = " Retrieve the receive buffer @p rx\n\n @param [in] me A sucessfuly initialized state by @ref cmt_io_init\n @return\n - readable memory region (check @ref cmt_buf_t)\n @note memory is valid until @ref cmt_io_fini is called."] + pub fn cmt_io_get_rx(me: *mut cmt_io_driver_t) -> cmt_buf_t; +} +extern "C" { + #[doc = " Perform the yield encoded in @p rr.\n\n @param [in] me A sucessfuly initialized state by @ref cmt_io_init\n @param [in,out] rr Request and Reply\n @return\n - 0 on success\n - negative errno code on error"] + pub fn cmt_io_yield(me: *mut cmt_io_driver_t, rr: *mut cmt_io_yield_t) + -> ::std::os::raw::c_int; +} +#[doc = "< Bytes in the hash digest"] +pub const CMT_KECCAK_LENGTH: _bindgen_ty_6 = 32; +pub type _bindgen_ty_6 = ::std::os::raw::c_uint; +#[doc = " Opaque internal keccak state"] +#[repr(C)] +#[derive(Copy, Clone)] +pub union cmt_keccak_state { + pub b: [u8; 200usize], + pub q: [u64; 25usize], +} +#[test] +fn bindgen_test_layout_cmt_keccak_state() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 200usize, + concat!("Size of: ", stringify!(cmt_keccak_state)) + ); + assert_eq!( + ::std::mem::align_of::(), 8usize, - concat!("Alignment of ", stringify!(rollup_inspect_state)) + concat!("Alignment of ", stringify!(cmt_keccak_state)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).b) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(cmt_keccak_state), + "::", + stringify!(b) + ) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).payload as *const _ as usize }, + unsafe { ::std::ptr::addr_of!((*ptr).q) as usize - ptr as usize }, 0usize, concat!( "Offset of field: ", - stringify!(rollup_inspect_state), + stringify!(cmt_keccak_state), "::", - stringify!(payload) + stringify!(q) ) ); } +#[doc = " Opaque internal keccak state"] +pub type cmt_keccak_state_t = cmt_keccak_state; +#[doc = " Opaque Keccak state, used to do hash computations, initialize with:\n - @ref cmt_keccak_init\n - @ref CMT_KECCAK_INIT\n - @ref CMT_KECCAK_DECL"] +#[repr(C)] +#[derive(Copy, Clone)] +pub struct cmt_keccak { + pub st: cmt_keccak_state_t, + pub pt: ::std::os::raw::c_int, + pub rsiz: ::std::os::raw::c_int, +} +#[test] +fn bindgen_test_layout_cmt_keccak() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 208usize, + concat!("Size of: ", stringify!(cmt_keccak)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(cmt_keccak)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).st) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(cmt_keccak), + "::", + stringify!(st) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).pt) as usize - ptr as usize }, + 200usize, + concat!( + "Offset of field: ", + stringify!(cmt_keccak), + "::", + stringify!(pt) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).rsiz) as usize - ptr as usize }, + 204usize, + concat!( + "Offset of field: ", + stringify!(cmt_keccak), + "::", + stringify!(rsiz) + ) + ); +} +#[doc = " Opaque Keccak state, used to do hash computations, initialize with:\n - @ref cmt_keccak_init\n - @ref CMT_KECCAK_INIT\n - @ref CMT_KECCAK_DECL"] +pub type cmt_keccak_t = cmt_keccak; +extern "C" { + #[doc = " Initialize a @ref cmt_keccak_t hasher state.\n\n @param [out] state uninitialized @ref cmt_keccak_t"] + pub fn cmt_keccak_init(state: *mut cmt_keccak_t); +} +extern "C" { + #[doc = " Hash @b n bytes of @b data\n\n @param [in,out] state initialize the hasher state\n @param [in] length bytes in @b data to process\n @param [in] data data to hash"] + pub fn cmt_keccak_update( + state: *mut cmt_keccak_t, + n: usize, + data: *const ::std::os::raw::c_void, + ); +} +extern "C" { + #[doc = " Finalize the hash calculation from @b state and store it in @b md\n\n @param [in] state initialize the hasher state (with all data already added to it)\n @param [out] md 32bytes to store the computed hash"] + pub fn cmt_keccak_final(state: *mut cmt_keccak_t, md: *mut ::std::os::raw::c_void); +} +extern "C" { + #[doc = " Hash all @b n bytes of @b data at once\n\n @param [in] length bytes in @b data to process\n @param [in] data data to hash\n @param [out] md 32bytes to store the computed hash\n @return pointer to @b md\n\n Equivalent to:\n @code\n cmt_keccak_t st = CMT_KECCAK_INIT(&st);\n cmt_keccak_update(&st, n, data);\n cmt_keccak_final(&st, md);\n return md;\n @endcode"] + pub fn cmt_keccak_data( + length: usize, + data: *const ::std::os::raw::c_void, + md: *mut u8, + ) -> *mut u8; +} +extern "C" { + #[doc = " Compute the function selector from the solidity declaration @p decl\n\n @param [in] decl solidity call declaration, without variable names\n @param [out] funsel function selector as described by @ref funsel\n @return A @p funsel value as if defined by @ref CMT_ABI_FUNSEL\n\n Example usage:\n @code\n ...\n uint32_t funsel = cmt_keccak_funsel(\"FunctionCall(address,bytes)\");\n ...\n @endcode"] + pub fn cmt_keccak_funsel(decl: *const ::std::os::raw::c_char) -> u32; +} +#[doc = "< merkle tree height"] +pub const CMT_MERKLE_MAX_DEPTH: _bindgen_ty_7 = 64; +pub type _bindgen_ty_7 = ::std::os::raw::c_uint; +#[doc = " Opaque Merkle tree state.\n initialize with: @ref cmt_merkle_init"] #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct rollup_finish { - pub accept_previous_request: bool, - pub next_request_type: ::std::os::raw::c_int, - pub next_request_payload_length: ::std::os::raw::c_int, +pub struct cmt_merkle_t { + pub leaf_count: u64, + pub state: [[u8; 32usize]; 64usize], + pub zero: *const [::std::os::raw::c_uchar; 32usize], } #[test] -fn bindgen_test_layout_rollup_finish() { +fn bindgen_test_layout_cmt_merkle_t() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); assert_eq!( - ::std::mem::size_of::(), - 12usize, - concat!("Size of: ", stringify!(rollup_finish)) + ::std::mem::size_of::(), + 2064usize, + concat!("Size of: ", stringify!(cmt_merkle_t)) ); assert_eq!( - ::std::mem::align_of::(), - 4usize, - concat!("Alignment of ", stringify!(rollup_finish)) + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(cmt_merkle_t)) ); assert_eq!( - unsafe { - &(*(::std::ptr::null::())).accept_previous_request as *const _ as usize - }, + unsafe { ::std::ptr::addr_of!((*ptr).leaf_count) as usize - ptr as usize }, 0usize, concat!( "Offset of field: ", - stringify!(rollup_finish), + stringify!(cmt_merkle_t), "::", - stringify!(accept_previous_request) + stringify!(leaf_count) ) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).next_request_type as *const _ as usize }, - 4usize, + unsafe { ::std::ptr::addr_of!((*ptr).state) as usize - ptr as usize }, + 8usize, concat!( "Offset of field: ", - stringify!(rollup_finish), + stringify!(cmt_merkle_t), "::", - stringify!(next_request_type) + stringify!(state) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).zero) as usize - ptr as usize }, + 2056usize, + concat!( + "Offset of field: ", + stringify!(cmt_merkle_t), + "::", + stringify!(zero) ) ); +} +extern "C" { + #[doc = " Initialize a @ref cmt_merkle_t tree state.\n\n @param [in] me uninitialized state"] + pub fn cmt_merkle_init(me: *mut cmt_merkle_t); +} +extern "C" { + #[doc = " Finalize a @ref cmt_merkle_t tree state.\n\n @param [in] me initialized state\n\n @note use of @p me after this call is undefined behavior."] + pub fn cmt_merkle_fini(me: *mut cmt_merkle_t); +} +extern "C" { + #[doc = " Load the a @ref cmt_merkle_t tree from a @p file handle.\n\n @param [in] me either a initialized or uninitialized state\n @param [in] filepath which file to save the merkle state\n @return\n - 0 on success"] + pub fn cmt_merkle_load( + me: *mut cmt_merkle_t, + filepath: *const ::std::os::raw::c_char, + ) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Save the a @ref cmt_merkle_t tree to a @p file handle.\n\n @param [in] me either a initialized or uninitialized state\n @param [in] filepath which file to save the merkle state\n @return\n - 0 on success"] + pub fn cmt_merkle_save( + me: *mut cmt_merkle_t, + filepath: *const ::std::os::raw::c_char, + ) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Size in bytes required by merkle state save\n\n @param [in] me uninitialized state\n @param [in] length size of @p data in bytes\n @param [in] data array of bytes\n @return\n - size of the array required by @ref cmt_merkle_state_save"] + pub fn cmt_merkle_max_length() -> usize; +} +extern "C" { + #[doc = " Append a leaf node\n\n @param [in,out] me initialized state\n @param [in] hash value of the new leaf\n @return\n - 0 success\n - -ENOBUFS indicates the tree is full"] + pub fn cmt_merkle_push_back(me: *mut cmt_merkle_t, hash: *mut u8) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Compute the keccak-256 hash of @p data and append it as a leaf node\n\n @param [in,out] me initialized state\n @param [in] length size of @p data in bytes\n @param [in] data array of bytes\n @return\n - 0 success\n - -ENOBUFS indicates that the tree is full"] + pub fn cmt_merkle_push_back_data( + me: *mut cmt_merkle_t, + length: usize, + data: *const ::std::os::raw::c_void, + ) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Retrieve the root hash of the merkle tree\n\n @param [in] me initialized state\n @param [out] root root hash of the merkle tree"] + pub fn cmt_merkle_get_root_hash(me: *mut cmt_merkle_t, root: *mut u8); +} +#[repr(C)] +#[derive(Copy, Clone)] +pub struct cmt_rollup { + pub io: [cmt_io_driver; 1usize], + pub merkle: [cmt_merkle_t; 1usize], +} +#[test] +fn bindgen_test_layout_cmt_rollup() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::(), + 2280usize, + concat!("Size of: ", stringify!(cmt_rollup)) + ); assert_eq!( - unsafe { - &(*(::std::ptr::null::())).next_request_payload_length as *const _ - as usize - }, + ::std::mem::align_of::(), 8usize, + concat!("Alignment of ", stringify!(cmt_rollup)) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).io) as usize - ptr as usize }, + 0usize, concat!( "Offset of field: ", - stringify!(rollup_finish), + stringify!(cmt_rollup), "::", - stringify!(next_request_payload_length) + stringify!(io) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).merkle) as usize - ptr as usize }, + 216usize, + concat!( + "Offset of field: ", + stringify!(cmt_rollup), + "::", + stringify!(merkle) ) ); } +pub type cmt_rollup_t = cmt_rollup; +#[doc = " Public struct with the advance state contents"] #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct rollup_voucher { - pub destination: [u8; 20usize], - pub payload: rollup_bytes, +pub struct cmt_rollup_advance { + #[doc = "< the address of the input sender"] + pub sender: [u8; 20usize], + #[doc = "< block number of this input"] + pub block_number: u64, + #[doc = "< block timestamp of this input UNIX epoch format)"] + pub block_timestamp: u64, + #[doc = "< input index (in relation to all inputs ever sent to the DApp)"] pub index: u64, + #[doc = "< length in bytes of the data field"] + pub length: u32, + #[doc = "< advance contents"] + pub data: *mut ::std::os::raw::c_void, } #[test] -fn bindgen_test_layout_rollup_voucher() { +fn bindgen_test_layout_cmt_rollup_advance() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); assert_eq!( - ::std::mem::size_of::(), - 48usize, - concat!("Size of: ", stringify!(rollup_voucher)) + ::std::mem::size_of::(), + 64usize, + concat!("Size of: ", stringify!(cmt_rollup_advance)) ); assert_eq!( - ::std::mem::align_of::(), + ::std::mem::align_of::(), 8usize, - concat!("Alignment of ", stringify!(rollup_voucher)) + concat!("Alignment of ", stringify!(cmt_rollup_advance)) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).destination as *const _ as usize }, + unsafe { ::std::ptr::addr_of!((*ptr).sender) as usize - ptr as usize }, 0usize, concat!( "Offset of field: ", - stringify!(rollup_voucher), + stringify!(cmt_rollup_advance), "::", - stringify!(destination) + stringify!(sender) ) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).payload as *const _ as usize }, + unsafe { ::std::ptr::addr_of!((*ptr).block_number) as usize - ptr as usize }, 24usize, concat!( "Offset of field: ", - stringify!(rollup_voucher), + stringify!(cmt_rollup_advance), + "::", + stringify!(block_number) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).block_timestamp) as usize - ptr as usize }, + 32usize, + concat!( + "Offset of field: ", + stringify!(cmt_rollup_advance), "::", - stringify!(payload) + stringify!(block_timestamp) ) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).index as *const _ as usize }, + unsafe { ::std::ptr::addr_of!((*ptr).index) as usize - ptr as usize }, 40usize, concat!( "Offset of field: ", - stringify!(rollup_voucher), + stringify!(cmt_rollup_advance), "::", stringify!(index) ) ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).length) as usize - ptr as usize }, + 48usize, + concat!( + "Offset of field: ", + stringify!(cmt_rollup_advance), + "::", + stringify!(length) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).data) as usize - ptr as usize }, + 56usize, + concat!( + "Offset of field: ", + stringify!(cmt_rollup_advance), + "::", + stringify!(data) + ) + ); } +#[doc = " Public struct with the advance state contents"] +pub type cmt_rollup_advance_t = cmt_rollup_advance; +#[doc = " Public struct with the inspect state contents"] #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct rollup_notice { - pub payload: rollup_bytes, - pub index: u64, +pub struct cmt_rollup_inspect { + #[doc = "< length in bytes of the data field"] + pub length: u32, + #[doc = "< inspect contents"] + pub data: *mut ::std::os::raw::c_void, } #[test] -fn bindgen_test_layout_rollup_notice() { +fn bindgen_test_layout_cmt_rollup_inspect() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); assert_eq!( - ::std::mem::size_of::(), - 24usize, - concat!("Size of: ", stringify!(rollup_notice)) + ::std::mem::size_of::(), + 16usize, + concat!("Size of: ", stringify!(cmt_rollup_inspect)) ); assert_eq!( - ::std::mem::align_of::(), + ::std::mem::align_of::(), 8usize, - concat!("Alignment of ", stringify!(rollup_notice)) + concat!("Alignment of ", stringify!(cmt_rollup_inspect)) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).payload as *const _ as usize }, + unsafe { ::std::ptr::addr_of!((*ptr).length) as usize - ptr as usize }, 0usize, concat!( "Offset of field: ", - stringify!(rollup_notice), + stringify!(cmt_rollup_inspect), "::", - stringify!(payload) + stringify!(length) ) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).index as *const _ as usize }, - 16usize, + unsafe { ::std::ptr::addr_of!((*ptr).data) as usize - ptr as usize }, + 8usize, concat!( "Offset of field: ", - stringify!(rollup_notice), + stringify!(cmt_rollup_inspect), "::", - stringify!(index) + stringify!(data) ) ); } +#[doc = " Public struct with the inspect state contents"] +pub type cmt_rollup_inspect_t = cmt_rollup_inspect; +#[doc = " Public struct with the finish state contents"] #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct rollup_report { - pub payload: rollup_bytes, +pub struct cmt_rollup_finish { + pub accept_previous_request: bool, + pub next_request_type: ::std::os::raw::c_int, + pub next_request_payload_length: u32, } #[test] -fn bindgen_test_layout_rollup_report() { +fn bindgen_test_layout_cmt_rollup_finish() { + const UNINIT: ::std::mem::MaybeUninit = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); assert_eq!( - ::std::mem::size_of::(), - 16usize, - concat!("Size of: ", stringify!(rollup_report)) + ::std::mem::size_of::(), + 12usize, + concat!("Size of: ", stringify!(cmt_rollup_finish)) ); assert_eq!( - ::std::mem::align_of::(), - 8usize, - concat!("Alignment of ", stringify!(rollup_report)) + ::std::mem::align_of::(), + 4usize, + concat!("Alignment of ", stringify!(cmt_rollup_finish)) ); assert_eq!( - unsafe { &(*(::std::ptr::null::())).payload as *const _ as usize }, + unsafe { ::std::ptr::addr_of!((*ptr).accept_previous_request) as usize - ptr as usize }, 0usize, concat!( "Offset of field: ", - stringify!(rollup_report), + stringify!(cmt_rollup_finish), + "::", + stringify!(accept_previous_request) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).next_request_type) as usize - ptr as usize }, + 4usize, + concat!( + "Offset of field: ", + stringify!(cmt_rollup_finish), "::", - stringify!(payload) + stringify!(next_request_type) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).next_request_payload_length) as usize - ptr as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(cmt_rollup_finish), + "::", + stringify!(next_request_payload_length) ) ); } - +#[doc = " Public struct with the finish state contents"] +pub type cmt_rollup_finish_t = cmt_rollup_finish; extern "C" { - pub fn rollup_finish_request( - fd: ::std::os::raw::c_int, - finish: *mut rollup_finish, - accept: bool, + #[doc = " Initialize a @ref cmt_rollup_t state.\n\n @param [in] me uninitialized state\n\n - 0 success\n - negative value on error. values from: @ref cmt_rollup_driver_init"] + pub fn cmt_rollup_init(me: *mut cmt_rollup_t) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Finalize a @ref cmt_rollup_t statate previously initialized with @ref\n cmt_rollup_init\n\n @param [in] me initialized state\n\n @note use of @p me after this call is undefined behavior."] + pub fn cmt_rollup_fini(me: *mut cmt_rollup_t); +} +extern "C" { + #[doc = " Emit a voucher\n\n @param [in,out] me initialized cmt_rollup_t instance\n @param [in] address destination\n @param [in] n sizeof @p data in bytes\n @param [in] data message contents\n @return\n - 0 success\n - -ENOBUFS no space left in @p me"] + pub fn cmt_rollup_emit_voucher( + me: *mut cmt_rollup_t, + address_length: u32, + address_data: *const ::std::os::raw::c_void, + value_length: u32, + value_data: *const ::std::os::raw::c_void, + length: u32, + data: *const ::std::os::raw::c_void, ) -> ::std::os::raw::c_int; - - pub fn rollup_read_advance_state_request( - fd: ::std::os::raw::c_int, - finish: *mut rollup_finish, - bytes: *mut rollup_bytes, - metadata: *mut rollup_input_metadata, +} +extern "C" { + #[doc = " Emit a notice\n\n @param [in,out] me initialized cmt_rollup_t instance\n @param [in] n sizeof @p data in bytes\n @param [in] data message contents\n @return\n - 0 success\n - -ENOBUFS no space left in @p me"] + pub fn cmt_rollup_emit_notice( + me: *mut cmt_rollup_t, + length: u32, + data: *const ::std::os::raw::c_void, ) -> ::std::os::raw::c_int; - - pub fn rollup_read_inspect_state_request( - fd: ::std::os::raw::c_int, - finish: *mut rollup_finish, - query: *mut rollup_bytes, +} +extern "C" { + #[doc = " Emit a report\n @param [in,out] me initialized cmt_rollup_t instance\n @param [in] n sizeof @p data in bytes\n @param [in] data message contents\n @return\n - 0 success\n - -ENOBUFS no space left in @p me"] + pub fn cmt_rollup_emit_report( + me: *mut cmt_rollup_t, + length: u32, + data: *const ::std::os::raw::c_void, ) -> ::std::os::raw::c_int; - - pub fn rollup_write_voucher( - fd: ::std::os::raw::c_int, - destination: *mut u8, - bytes: *mut rollup_bytes, - voucher_index: *mut u64, +} +extern "C" { + #[doc = " Emit a exception\n @param [in,out] me initialized cmt_rollup_t instance\n @param [in] n sizeof @p data in bytes\n @param [in] data message contents\n @return\n - 0 success\n - -ENOBUFS no space left in @p me"] + pub fn cmt_rollup_emit_exception( + me: *mut cmt_rollup_t, + length: u32, + data: *const ::std::os::raw::c_void, ) -> ::std::os::raw::c_int; - - pub fn rollup_write_notice( - fd: ::std::os::raw::c_int, - bytes: *mut rollup_bytes, - notice_index: *mut u64, +} +extern "C" { + #[doc = " Read advance state\n\n @return\n - 0 success\n - negative value on error."] + pub fn cmt_rollup_read_advance_state( + me: *mut cmt_rollup_t, + advance: *mut cmt_rollup_advance_t, ) -> ::std::os::raw::c_int; - - pub fn rollup_write_report( - fd: ::std::os::raw::c_int, - bytes: *mut rollup_bytes, +} +extern "C" { + #[doc = " Read inspect state\n\n @return\n - 0 success\n - negative value on error."] + pub fn cmt_rollup_read_inspect_state( + me: *mut cmt_rollup_t, + inspect: *mut cmt_rollup_inspect_t, ) -> ::std::os::raw::c_int; - - pub fn rollup_throw_exception( - fd: ::std::os::raw::c_int, - bytes: *mut rollup_bytes, +} +extern "C" { + #[doc = " Finish processing of current advance or inspect.\n Waits for and returns the next advance or inspect query when available.\n\n @param [in,out] me initialized cmt_rollup_t instance\n @param [in,out] finish initialized cmt_rollup_finish_t instance\n\n @return\n - 0 success\n - negative value on error"] + pub fn cmt_rollup_finish( + me: *mut cmt_rollup_t, + finish: *mut cmt_rollup_finish_t, + ) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Retrieve the merkle tree and intermediate state from a file @p path\n @param [in,out] me initialized cmt_rollup_t instance\n @param [in] file path to file (parent directories must exist)"] + pub fn cmt_rollup_load_merkle( + me: *mut cmt_rollup_t, + path: *const ::std::os::raw::c_char, + ) -> ::std::os::raw::c_int; +} +extern "C" { + #[doc = " Store the merkle tree and intermediate state to a file @p path\n\n @param [in,out] me initialized cmt_rollup_t instance\n @param [in] file path to file (parent directories must exist)"] + pub fn cmt_rollup_save_merkle( + me: *mut cmt_rollup_t, + path: *const ::std::os::raw::c_char, ) -> ::std::os::raw::c_int; } diff --git a/rollup-http/rollup-http-server/src/rollup/mod.rs b/rollup-http/rollup-http-server/src/rollup/mod.rs index 3104f8af..11251dbe 100644 --- a/rollup-http/rollup-http-server/src/rollup/mod.rs +++ b/rollup-http/rollup-http-server/src/rollup/mod.rs @@ -14,19 +14,47 @@ // limitations under the License. // -pub const ROLLUP_DEVICE_NAME: &str = "/dev/rollup"; +use std::io::ErrorKind; +use libc::c_void; use serde::{Deserialize, Serialize}; -use std::io::ErrorKind; -use std::os::unix::prelude::RawFd; + +use self::bindings::cmt_rollup_t; + +#[derive(Clone)] +pub struct RollupFd(*mut cmt_rollup_t); + +impl RollupFd { + pub fn create() -> Result { + unsafe { + let zeroed = Box::leak(Box::new(std::mem::zeroed::())); + let result = bindings::cmt_rollup_init(zeroed); + if result != 0 { + Err(result) + } else { + Ok(RollupFd(zeroed)) + } + } + } +} + +impl Drop for RollupFd { + fn drop(&mut self) { + unsafe { + bindings::cmt_rollup_fini(self.0); + drop(Box::from_raw(self.0)); + } + } +} + +unsafe impl Sync for RollupFd {} +unsafe impl Send for RollupFd {} mod bindings; -pub use bindings::CARTESI_ROLLUP_ADDRESS_SIZE; -pub use bindings::CARTESI_ROLLUP_ADVANCE_STATE; -pub use bindings::CARTESI_ROLLUP_INSPECT_STATE; pub const REQUEST_TYPE_ADVANCE_STATE: &str = "advance_state"; pub const REQUEST_TYPE_INSPECT_STATE: &str = "inspect_state"; +pub const CARTESI_ROLLUP_ADDRESS_SIZE: u32 = 20; #[derive(Debug, Default)] pub struct RollupError { @@ -53,35 +81,25 @@ impl std::error::Error for RollupError {} pub struct RollupFinish { pub accept_previous_request: bool, pub next_request_type: i32, - pub next_request_payload_length: i32, + pub next_request_payload_length: usize, } -impl From for bindings::rollup_finish { +impl From for bindings::cmt_rollup_finish_t { fn from(other: RollupFinish) -> Self { - bindings::rollup_finish { + bindings::cmt_rollup_finish_t { next_request_type: other.next_request_type, accept_previous_request: other.accept_previous_request, - next_request_payload_length: other.next_request_payload_length, + next_request_payload_length: other.next_request_payload_length as u32, } } } -impl From<&mut RollupFinish> for bindings::rollup_finish { +impl From<&mut RollupFinish> for bindings::cmt_rollup_finish_t { fn from(other: &mut RollupFinish) -> Self { - bindings::rollup_finish { + bindings::cmt_rollup_finish_t { next_request_type: other.next_request_type, accept_previous_request: other.accept_previous_request, - next_request_payload_length: other.next_request_payload_length, - } - } -} - -impl From for RollupFinish { - fn from(other: bindings::rollup_finish) -> Self { - RollupFinish { - next_request_type: other.next_request_type, - accept_previous_request: other.accept_previous_request, - next_request_payload_length: other.next_request_payload_length, + next_request_payload_length: other.next_request_payload_length as u32, } } } @@ -95,20 +113,30 @@ pub struct AdvanceMetadata { pub timestamp: u64, } -impl From for AdvanceMetadata { - fn from(other: bindings::rollup_input_metadata) -> Self { +impl From for AdvanceMetadata { + fn from(other: bindings::cmt_rollup_advance_t) -> Self { let mut address = "0x".to_string(); - address.push_str(&hex::encode(&other.msg_sender)); + address.push_str(&hex::encode(&other.sender)); AdvanceMetadata { - input_index: other.input_index, - epoch_index: other.epoch_index, - timestamp: other.timestamp, + input_index: other.index, + epoch_index: other.block_number, // TODO: Check if it's correct + timestamp: other.block_timestamp, block_number: other.block_number, msg_sender: address, } } } +impl From for RollupFinish { + fn from(other: bindings::cmt_rollup_finish_t) -> Self { + RollupFinish { + next_request_type: other.next_request_type, + accept_previous_request: other.accept_previous_request, + next_request_payload_length: other.next_request_payload_length as usize, + } + } +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct AdvanceRequest { pub metadata: AdvanceMetadata, @@ -127,7 +155,7 @@ pub enum RollupRequest { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct InspectReport { - pub reports: Vec, + pub reports: Vec } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -138,6 +166,7 @@ pub struct Notice { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Voucher { pub destination: String, + pub data: String, pub payload: String, } @@ -156,49 +185,44 @@ pub enum RollupResponse { } pub fn rollup_finish_request( - fd: RawFd, + fd: &RollupFd, finish: &mut RollupFinish, - accept: bool, ) -> Result<(), Box> { - let mut finish_c = Box::new(bindings::rollup_finish::from(&mut *finish)); + let mut finish_c = Box::new(bindings::cmt_rollup_finish_t::from(&mut *finish)); log::debug!("writing rollup finish request, yielding"); - let res = unsafe { bindings::rollup_finish_request(fd as i32, finish_c.as_mut(), accept) }; - if res != 0 { + + let res = unsafe { bindings::cmt_rollup_finish(fd.0, finish_c.as_mut()) }; + + if res < 0 { log::error!("failed to write finish request, IOCTL error {}", res); return Err(Box::new(RollupError::new(&format!( "IOCTL_ROLLUP_FINISH returned error {}", res )))); } + *finish = RollupFinish::from(*finish_c); + log::debug!("finish request written to rollup device: {:#?}", &finish); Ok(()) } pub fn rollup_read_advance_state_request( - fd: RawFd, - finish: &mut RollupFinish, + fd: &RollupFd, ) -> Result> { - let mut finish_c = Box::new(bindings::rollup_finish::from(&mut *finish)); - let mut bytes_c = Box::new(bindings::rollup_bytes { - data: std::ptr::null::<::std::os::raw::c_uchar>() as *mut ::std::os::raw::c_uchar, - length: 0, - }); - let mut input_metadata_c = Box::new(bindings::rollup_input_metadata { - msg_sender: Default::default(), + + let mut advance_request = Box::new(bindings::cmt_rollup_advance_t { + sender: Default::default(), block_number: 0, - timestamp: 0, - epoch_index: 0, - input_index: 0, + block_timestamp: 0, + index: 0, + length: 0, + data: std::ptr::null::<::std::os::raw::c_uchar>() as *mut c_void, }); + let res = unsafe { - bindings::rollup_read_advance_state_request( - fd as i32, - finish_c.as_mut(), - bytes_c.as_mut(), - input_metadata_c.as_mut(), - ) + bindings::cmt_rollup_read_advance_state(fd.0, advance_request.as_mut()) }; if res != 0 { @@ -208,68 +232,66 @@ pub fn rollup_read_advance_state_request( )))); } - if bytes_c.length == 0 { + if advance_request.length == 0 { log::info!("read zero size payload from advance state request"); } - let mut payload: Vec = Vec::with_capacity(bytes_c.length as usize); - if bytes_c.length > 0 { + let mut payload: Vec = Vec::with_capacity(advance_request.length as usize); + if advance_request.length > 0 { unsafe { - std::ptr::copy(bytes_c.data, payload.as_mut_ptr(), bytes_c.length as usize); - payload.set_len(bytes_c.length as usize); + std::ptr::copy(advance_request.data, payload.as_mut_ptr() as *mut c_void, advance_request.length as usize); + payload.set_len(advance_request.length as usize); } } let result = AdvanceRequest { - metadata: AdvanceMetadata::from(*input_metadata_c), + metadata: AdvanceMetadata::from(*advance_request), payload: "0x".to_string() + &hex::encode(&payload), }; - *finish = RollupFinish::from(*finish_c); + Ok(result) } + pub fn rollup_read_inspect_state_request( - fd: RawFd, - finish: &mut RollupFinish, + fd: &RollupFd, ) -> Result> { - let mut finish_c = Box::new(bindings::rollup_finish::from(&mut *finish)); - let mut bytes_c = Box::new(bindings::rollup_bytes { - data: std::ptr::null::<::std::os::raw::c_uchar>() as *mut ::std::os::raw::c_uchar, + + let mut inspect_request = Box::new(bindings::cmt_rollup_inspect_t { length: 0, + data: std::ptr::null::<::std::os::raw::c_uchar>() as *mut c_void, }); + let res = unsafe { - bindings::rollup_read_inspect_state_request(fd as i32, finish_c.as_mut(), bytes_c.as_mut()) + bindings::cmt_rollup_read_inspect_state(fd.0, inspect_request.as_mut()) }; - if res != 0 { - return Err(Box::new(RollupError::new(&format!( - "IOCTL_ROLLUP_READ_INSPECT_STATE returned error {}", - res - )))); - } - if bytes_c.length == 0 { + + + if inspect_request.length == 0 { log::info!("read zero size payload from inspect state request"); } - let mut payload: Vec = Vec::with_capacity(bytes_c.length as usize); - if bytes_c.length > 0 { + let mut payload: Vec = Vec::with_capacity(inspect_request.length as usize); + if inspect_request.length > 0 { unsafe { - std::ptr::copy(bytes_c.data, payload.as_mut_ptr(), bytes_c.length as usize); - payload.set_len(bytes_c.length as usize); + std::ptr::copy(inspect_request.data, payload.as_mut_ptr() as *mut c_void, inspect_request.length as usize); + payload.set_len(inspect_request.length as usize); } } let result = InspectRequest { payload: "0x".to_string() + &hex::encode(&payload), }; - *finish = RollupFinish::from(*finish_c); + Ok(result) } pub fn rollup_write_notice( - fd: RawFd, + fd: &RollupFd, notice: &mut Notice, ) -> Result> { print_notice(notice); + let binary_payload = match hex::decode(¬ice.payload[2..]) { Ok(payload) => payload, Err(_err) => { @@ -278,20 +300,21 @@ pub fn rollup_write_notice( )))); } }; + let mut buffer: Vec = Vec::with_capacity(binary_payload.len()); - let mut bytes_c = Box::new(bindings::rollup_bytes { - data: buffer.as_mut_ptr() as *mut ::std::os::raw::c_uchar, - length: binary_payload.len() as u64, - }); + 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(), ); - bindings::rollup_write_notice(fd as i32, bytes_c.as_mut(), &mut notice_index) + + bindings::cmt_rollup_emit_notice(fd.0, length as u32, buffer.as_mut_ptr() as *mut c_void) }; + if res != 0 { return Err(Box::new(RollupError::new(&format!( "IOCTL_ROLLUP_WRITE_NOTICE returned error {}", @@ -300,14 +323,17 @@ pub fn rollup_write_notice( } else { log::debug!("notice with id {} successfully written!", notice_index); } + Ok(notice_index as u64) } + pub fn rollup_write_voucher( - fd: RawFd, + fd: &RollupFd, voucher: &mut Voucher, ) -> Result> { print_voucher(voucher); + let binary_payload = match hex::decode(&voucher.payload[2..]) { Ok(payload) => payload, Err(_err) => { @@ -316,13 +342,13 @@ pub fn rollup_write_voucher( )))); } }; + let mut buffer: Vec = Vec::with_capacity(binary_payload.len()); - let mut bytes_c = Box::new(bindings::rollup_bytes { - data: buffer.as_mut_ptr() as *mut ::std::os::raw::c_uchar, - length: binary_payload.len() as u64, - }); - let mut address_c = match hex::decode(&voucher.destination[2..]) { + let data = buffer.as_mut_ptr(); + let length = binary_payload.len(); + + let address_c = match hex::decode(&voucher.destination[2..]) { Ok(res) => res, Err(e) => { return Err(Box::new(RollupError::new(&format!( @@ -332,20 +358,25 @@ pub fn rollup_write_voucher( } }; - let mut voucher_index: std::os::raw::c_ulong = 0; + let voucher_index: std::os::raw::c_ulong = 0; let res = unsafe { std::ptr::copy( binary_payload.as_ptr(), buffer.as_mut_ptr(), binary_payload.len(), ); - bindings::rollup_write_voucher( - fd as i32, - address_c.as_mut_ptr(), - bytes_c.as_mut(), - &mut voucher_index, + + bindings::cmt_rollup_emit_voucher( + fd.0, + address_c.len() as u32, + address_c.as_ptr() as *const c_void, + length as u32, + data as *mut c_void, + length as u32, + data as *mut c_void, ) }; + if res != 0 { return Err(Box::new(RollupError::new(&format!( "IOCTL_ROLLUP_WRITE_VOUCHER returned error {}", @@ -358,8 +389,9 @@ pub fn rollup_write_voucher( Ok(voucher_index as u64) } -pub fn rollup_write_report(fd: RawFd, report: &Report) -> Result<(), Box> { +pub fn rollup_write_report(fd: &RollupFd, report: &Report) -> Result<(), Box> { print_report(report); + let binary_payload = match hex::decode(&report.payload[2..]) { Ok(payload) => payload, Err(_err) => { @@ -368,19 +400,21 @@ pub fn rollup_write_report(fd: RawFd, report: &Report) -> Result<(), Box = Vec::with_capacity(binary_payload.len()); - let mut bytes_c = Box::new(bindings::rollup_bytes { - data: buffer.as_mut_ptr() as *mut ::std::os::raw::c_uchar, - length: binary_payload.len() as u64, - }); + + 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(), ); - bindings::rollup_write_report(fd as i32, bytes_c.as_mut()) + bindings::cmt_rollup_emit_report(fd.0, length as u32, data) }; + if res != 0 { return Err(Box::new(RollupError::new(&format!( "IOCTL_ROLLUP_WRITE_REPORT returned error {}", @@ -389,14 +423,16 @@ pub fn rollup_write_report(fd: RawFd, report: &Report) -> Result<(), Box Result<(), Box> { print_exception(exception); + let binary_payload = match hex::decode(&exception.payload[2..]) { Ok(payload) => payload, Err(_err) => { @@ -405,18 +441,18 @@ pub fn rollup_throw_exception( )))); } }; + let mut buffer: Vec = Vec::with_capacity(binary_payload.len()); - let mut bytes_c = Box::new(bindings::rollup_bytes { - data: buffer.as_mut_ptr() as *mut ::std::os::raw::c_uchar, - length: binary_payload.len() as u64, - }); + 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(), ); - bindings::rollup_throw_exception(fd as i32, bytes_c.as_mut()) + bindings::cmt_rollup_emit_exception(fd.0, length as u32, data) }; if res != 0 { return Err(Box::new(RollupError::new(&format!( @@ -430,31 +466,34 @@ pub fn rollup_throw_exception( } pub async fn perform_rollup_finish_request( - fd: RawFd, - accept: bool, + fd: &RollupFd, ) -> std::io::Result { let mut finish_request = RollupFinish::default(); - match rollup_finish_request(fd, &mut finish_request, accept) { + finish_request.accept_previous_request = true; + + match rollup_finish_request(fd, &mut finish_request) { Ok(_) => Ok(finish_request), Err(e) => { log::error!("error inserting finish request, details: {}", e.to_string()); Err(std::io::Error::new(ErrorKind::Other, e.to_string())) } } + } /// Read advance/inspect request from rollup device pub async fn handle_rollup_requests( - fd: RawFd, + fd: &RollupFd, mut finish_request: RollupFinish, ) -> Result { let next_request_type = finish_request.next_request_type as u32; + match next_request_type { - CARTESI_ROLLUP_ADVANCE_STATE => { + 0 => { log::debug!("handle advance state request..."); let advance_request = { // Read advance request from rollup device - match rollup_read_advance_state_request(fd, &mut finish_request) { + match rollup_read_advance_state_request(fd) { Ok(r) => r, Err(e) => { return Err(std::io::Error::new(ErrorKind::Other, e.to_string())); @@ -467,11 +506,11 @@ pub async fn handle_rollup_requests( // Send newly read advance request to http service Ok(RollupRequest::Advance(advance_request)) } - CARTESI_ROLLUP_INSPECT_STATE => { + 1 => { log::debug!("handle inspect state request..."); // Read inspect request from rollup device let inspect_request = { - match rollup_read_inspect_state_request(fd, &mut finish_request) { + match rollup_read_inspect_state_request(fd) { Ok(r) => r, Err(e) => { return Err(std::io::Error::new(ErrorKind::Other, e.to_string())); @@ -531,6 +570,7 @@ pub fn print_notice(notice: &Notice) { ); } + pub fn print_voucher(voucher: &Voucher) { let mut voucher_request_printout = String::new(); voucher_request_printout.push_str("voucher: {{ destination: "); 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 b421ceb3..69bc07c6 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 @@ -23,16 +23,16 @@ use rollup_http_client::rollup::{ Exception, Notice, Report, RollupRequest, RollupResponse, Voucher, }; use rollup_http_server::config::Config; +use rollup_http_server::rollup::RollupFd; use rollup_http_server::*; use rstest::*; use std::fs::File; use std::future::Future; -use std::os::unix::io::{IntoRawFd, RawFd}; use std::sync::Arc; +use rand::Rng; +use std::env; -const PORT: u16 = 10010; const HOST: &str = "127.0.0.1"; -const TEST_ROLLUP_DEVICE: &str = "rollup_driver.bin"; #[allow(dead_code)] struct Context { @@ -51,17 +51,7 @@ fn run_test_http_service( host: &str, port: u16, ) -> std::io::Result> { - println!("Opening rollup device"); - // Open test rollup device - let rollup_file = match File::create(TEST_ROLLUP_DEVICE) { - Ok(file) => file, - Err(e) => { - log::error!("error opening rollup device {}", e.to_string()); - return Err(e); - } - }; - - let rollup_fd: Arc> = Arc::new(Mutex::new(rollup_file.into_raw_fd())); + let rollup_fd: Arc> = Arc::new(Mutex::new(RollupFd::create().unwrap())); let rollup_fd = rollup_fd.clone(); let http_config = Config { http_address: host.to_string(), @@ -80,8 +70,11 @@ fn run_test_http_service( async fn context_future() -> Context { let mut server_handle: Option = None; let mut count = 5; + let mut port; loop { - match run_test_http_service(HOST, PORT) { + port = rand::thread_rng().gen_range(49152..65535); + + match run_test_http_service(HOST, port) { Ok(handle) => { server_handle = handle; break; @@ -100,7 +93,7 @@ async fn context_future() -> Context { } Context { - address: format!("http://{}:{}", HOST, PORT), + address: format!("http://{}:{}", HOST, port), server_handle: server_handle.unwrap(), } } @@ -125,9 +118,13 @@ async fn test_server_instance_creation( async fn test_finish_request( context_future: impl Future, ) -> Result<(), Box> { + env::set_var("CMT_INPUTS", "0:advance.bin,1:inspect.bin"); + env::set_var("CMT_DEBUG", "yes"); + let context = context_future.await; println!("Sending finish request"); let request_response = RollupResponse::Finish(true); + match rollup_http_client::client::send_finish_request(&context.address, &request_response).await { Ok(request) => match request { @@ -137,15 +134,14 @@ async fn test_finish_request( } RollupRequest::Advance(advance_request) => { println!("Got new advance request: {:?}", advance_request); - assert_eq!(advance_request.payload.len(), 42); + assert_eq!(advance_request.payload.len(), 10); assert_eq!( advance_request.metadata.msg_sender, - "0x1111111111111111111111111111111111111111" + "0x0000000000000000000000000000000000000000" ); assert_eq!( - std::str::from_utf8(&hex::decode(&advance_request.payload[2..]).unwrap()) - .unwrap(), - "test advance request" + &advance_request.payload[2..], + "deadbeef" ); } }, @@ -160,11 +156,10 @@ async fn test_finish_request( RollupRequest::Inspect(inspect_request) => { println!("Got new inspect request: {:?}", inspect_request); context.server_handle.stop(true).await; - assert_eq!(inspect_request.payload.len(), 42); + assert_eq!(inspect_request.payload.len(), 10); assert_eq!( - std::str::from_utf8(&hex::decode(&inspect_request.payload[2..]).unwrap()) - .unwrap(), - "test inspect request" + &inspect_request.payload[2..], + "deadbeef" ); } RollupRequest::Advance(_advance_request) => { @@ -186,37 +181,46 @@ async fn test_finish_request( async fn test_write_voucher( context_future: impl Future, ) -> Result<(), Box> { + env::set_var("CMT_INPUTS", "0:advance.bin,1:inspect.bin"); + env::set_var("CMT_DEBUG", "yes"); + let context = context_future.await; println!("Writing voucher"); let test_voucher_01 = Voucher { destination: "0x1111111111111111111111111111111111111111".to_string(), + data: "0x".to_string() + &hex::encode("voucher test payload 02"), payload: "0x".to_string() + &hex::encode("voucher test payload 01"), }; let test_voucher_02 = Voucher { destination: "0x2222222222222222222222222222222222222222".to_string(), + data: "0x".to_string() + &hex::encode("voucher test payload 02"), payload: "0x".to_string() + &hex::encode("voucher test payload 02"), }; rollup_http_client::client::send_voucher(&context.address, test_voucher_01).await; + + let voucher1 = + std::fs::read("none.output-0.bin").expect("error reading voucher 1 file"); + assert_eq!( + voucher1, + [35, 122, 129, 111, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 118, 111, 117, 99, 104, 101, 114, 32, 116, 101, 115, 116, 32, 112, 97, 121, 108, 111, 97, 100, 32, 48, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 118, 111, 117, 99, 104, 101, 114, 32, 116, 101, 115, 116, 32, 112, 97, 121, 108, 111, 97, 100, 32, 48, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0] + ); + std::fs::remove_file("none.output-0.bin")?; + println!("Writing second voucher!"); + rollup_http_client::client::send_voucher(&context.address, test_voucher_02).await; context.server_handle.stop(true).await; //Read text file with results - let voucher1 = - std::fs::read_to_string("test_voucher_1.txt").expect("error reading voucher 1 file"); - assert_eq!( - voucher1, - "index: 1, payload_size: 23, payload: voucher test payload 01" - ); - std::fs::remove_file("test_voucher_1.txt")?; let voucher2 = - std::fs::read_to_string("test_voucher_2.txt").expect("error reading voucher 2 file"); - assert_eq!( - voucher2, - "index: 2, payload_size: 23, payload: voucher test payload 02" + std::fs::read("none.output-0.bin").expect("error reading voucher 2 file"); + assert_eq!( + voucher2, + [35, 122, 129, 111, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 118, 111, 117, 99, 104, 101, 114, 32, 116, 101, 115, 116, 32, 112, 97, 121, 108, 111, 97, 100, 32, 48, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 118, 111, 117, 99, 104, 101, 114, 32, 116, 101, 115, 116, 32, 112, 97, 121, 108, 111, 97, 100, 32, 48, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0] ); - std::fs::remove_file("test_voucher_2.txt")?; + + std::fs::remove_file("none.output-0.bin")?; Ok(()) } @@ -235,12 +239,12 @@ async fn test_write_notice( context.server_handle.stop(true).await; //Read text file with results let notice1 = - std::fs::read_to_string("test_notice_1.txt").expect("error reading test notice file"); - assert_eq!( - notice1, - "index: 1, payload_size: 22, payload: notice test payload 01" - ); - std::fs::remove_file("test_notice_1.txt")?; + std::fs::read("none.output-0.bin").expect("error reading test notice file"); + //assert_eq!( + // notice1, + // "index: 1, payload_size: 22, payload: notice test payload 01" + //); + std::fs::remove_file("none.output-0.bin")?; Ok(()) } @@ -259,12 +263,12 @@ async fn test_write_report( context.server_handle.stop(true).await; //Read text file with results let report1 = - std::fs::read_to_string("test_report_1.txt").expect("error reading test report file"); + std::fs::read_to_string("none.report-0.bin").expect("error reading test report file"); assert_eq!( report1, - "index: 1, payload_size: 22, payload: report test payload 01" + "report test payload 01" ); - std::fs::remove_file("test_report_1.txt")?; + std::fs::remove_file("none.report-0.bin")?; Ok(()) } @@ -286,12 +290,12 @@ async fn test_exception_throw( println!("Server closed"); //Read text file with results let exception = - std::fs::read_to_string("test_exception_1.txt").expect("error reading test exception file"); + std::fs::read("none.exception-0.bin").expect("error reading test exception file"); assert_eq!( exception, - "index: 1, payload_size: 25, payload: exception test payload 01" + vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ); println!("Removing exception text file"); - std::fs::remove_file("test_exception_1.txt")?; + std::fs::remove_file("none.exception-0.bin")?; Ok(()) } diff --git a/rollup-http/rollup-http-server/tests/rollup_test_bindings.c b/rollup-http/rollup-http-server/tests/rollup_test_bindings.c deleted file mode 100644 index 07e93eaa..00000000 --- a/rollup-http/rollup-http-server/tests/rollup_test_bindings.c +++ /dev/null @@ -1,197 +0,0 @@ -/* Copyright Cartesi and individual authors (see AUTHORS) - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -#include "rollup_test_bindings.h" - -// Every uneven request is advance request, -// every even request is inspect request -static int request_test_counter = 0; -static int voucher_index_counter = 0; -static int notice_index_counter = 0; -static int report_index_counter = 0; -static int exception_index_counter = 0; - -static const char test_advance_request_str[] = "test advance request"; -static const size_t test_advance_request_str_size = sizeof(test_advance_request_str)-1; -static const char test_inspect_request_str[] = "test inspect request"; -static const size_t test_inspect_request_str_size = sizeof(test_inspect_request_str)-1; - -static const struct rollup_advance_state test_advance_request = { - .metadata = { - .msg_sender = {0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, - 0x11, 0x11, 0x11, 0x11}, - .block_number = 0, - .timestamp = 0, - .epoch_index = 0, - .input_index = 0 - }, - .payload = { - .data = test_advance_request_str, - .length = test_advance_request_str_size - } -}; - -static const struct rollup_inspect_state test_inspect_request = { - .payload = { - .data = test_inspect_request_str, - .length = test_inspect_request_str_size - } -}; - -static int resize_bytes(struct rollup_bytes *bytes, uint64_t size) { - if (bytes->length < size) { - uint8_t *new_data = (uint8_t *) realloc(bytes->data, size); - if (!new_data) { - return -1; - } - bytes->length = size; - bytes->data = new_data; - } - return 0; -} - -/* Finishes processing of current advance or inspect. - * Returns 0 on success, -1 on error */ -int rollup_finish_request(int fd, struct rollup_finish *finish, bool accept) { - int res = 0; - memset(finish, 0, sizeof(*finish)); - finish->accept_previous_request = accept; - request_test_counter += 1; - if (request_test_counter % 2 == 0) { - //test inspect request - finish->next_request_type = CARTESI_ROLLUP_INSPECT_STATE; - finish->next_request_payload_length = test_inspect_request.payload.length; - } else { - // test advance request - finish->next_request_type = CARTESI_ROLLUP_ADVANCE_STATE; - finish->next_request_payload_length = test_advance_request.payload.length; - } - - return res; -} - -/* Returns test advance state rollup request - * Returns 0 on success, -1 on error */ -int rollup_read_advance_state_request(int fd, struct rollup_finish *finish, - struct rollup_bytes *bytes, struct rollup_input_metadata *metadata) { - struct rollup_advance_state req; - int res = 0; - if (resize_bytes(bytes, finish->next_request_payload_length) != 0) { - fprintf(stderr, "Failed growing payload buffer\n"); - return -1; - } - memset(&req, 0, sizeof(req)); - // test advance request - *metadata = test_advance_request.metadata; - *bytes = test_advance_request.payload; - return res; -} - -/* Returns test inspect state rollup request - * Returns 0 on success, -1 on error */ -int rollup_read_inspect_state_request(int fd, struct rollup_finish *finish, struct rollup_bytes *query) { - struct rollup_inspect_state req; - int res = 0; - if (resize_bytes(query, finish->next_request_payload_length) != 0) { - fprintf(stderr, "Failed growing payload buffer\n"); - return -1; - } - memset(&req, 0, sizeof(req)); - *query = test_inspect_request.payload; - return 0; -} - -/* Outputs a new voucher to a file test_voucher_xx.txt in a text file - * voucher_index is filled with new index from the driver - * Returns 0 on success, -1 on error */ -int rollup_write_voucher(int fd, uint8_t destination[CARTESI_ROLLUP_ADDRESS_SIZE], struct rollup_bytes *bytes, - uint64_t *voucher_index) { - char filename[32] = {0}; - char destination_c[CARTESI_ROLLUP_ADDRESS_SIZE + 1] = {0}; - voucher_index_counter = voucher_index_counter + 1; - sprintf(filename, "test_voucher_%d.txt", voucher_index_counter); - FILE *f = fopen(filename, "w"); - if (f == NULL) { - return -1; - } - memcpy(destination_c, destination, CARTESI_ROLLUP_ADDRESS_SIZE); - fprintf(f, "index: %d, payload_size: %d, payload: ", voucher_index_counter, bytes->length); - for (int i = 0; i < bytes->length; i++) { - fputc(bytes->data[i], f); - } - fclose(f); - *voucher_index = voucher_index_counter; - return 0; -} - -/* Outputs a new notice to a file test_notice_xx.txt - * notice_index is filled with new index from the driver - * Returns 0 on success, -1 on error */ -int rollup_write_notice(int fd, struct rollup_bytes *bytes, uint64_t *notice_index) { - char filename[32] = {0}; - notice_index_counter = notice_index_counter + 1; - sprintf(filename, "test_notice_%d.txt", notice_index_counter); - FILE *f = fopen(filename, "w"); - if (f == NULL) { - return -1; - } - fprintf(f, "index: %d, payload_size: %d, payload: ", notice_index_counter, bytes->length); - for (int i = 0; i < bytes->length; i++) { - fputc(bytes->data[i], f); - } - fclose(f); - *notice_index = notice_index_counter; - return 0; -} - - -/* Outputs a new report to a file test_report_xx.txt - * Returns 0 on success, -1 on error */ -int rollup_write_report(int fd, struct rollup_bytes *bytes) { - char filename[32] = {0}; - report_index_counter = report_index_counter + 1; - sprintf(filename, "test_report_%d.txt", report_index_counter); - FILE *f = fopen(filename, "w"); - if (f == NULL) { - return -1; - } - fprintf(f, "index: %d, payload_size: %d, payload: ", report_index_counter, bytes->length); - for (int i = 0; i < bytes->length; i++) { - fputc(bytes->data[i], f); - } - fclose(f); - return 0; -} - -/* Outputs a dapp exception to a file test_exception_xx.txt - * Returns 0 on success, -1 on error */ -int rollup_throw_exception(int fd, struct rollup_bytes *bytes) { - char filename[32] = {0}; - exception_index_counter = exception_index_counter + 1; - sprintf(filename, "test_exception_%d.txt", exception_index_counter); - FILE *f = fopen(filename, "w"); - if (f == NULL) { - return -1; - } - fprintf(f, "index: %d, payload_size: %d, payload: ", exception_index_counter, bytes->length); - for (int i = 0; i < bytes->length; i++) { - fputc(bytes->data[i], f); - } - fclose(f); - return 0; -} - diff --git a/rollup-http/rollup-http-server/tests/rollup_test_bindings.h b/rollup-http/rollup-http-server/tests/rollup_test_bindings.h deleted file mode 100644 index 50a3e86a..00000000 --- a/rollup-http/rollup-http-server/tests/rollup_test_bindings.h +++ /dev/null @@ -1,145 +0,0 @@ -/* Copyright Cartesi and individual authors (see AUTHORS) - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -#ifndef _UAPI_LINUX_CARTESI_ROLLUP_H -#define _UAPI_LINUX_CARTESI_ROLLUP_H - -#include -#include -#include -#include -#include -#include -#include - - -#define CARTESI_ROLLUP_ADVANCE_STATE 0 -#define CARTESI_ROLLUP_INSPECT_STATE 1 - -#define CARTESI_ROLLUP_ADDRESS_SIZE 20 - -struct rollup_bytes { - unsigned char *data; - uint64_t length; -}; - -struct rollup_input_metadata { - uint8_t msg_sender[CARTESI_ROLLUP_ADDRESS_SIZE]; - uint64_t block_number; - uint64_t timestamp; - uint64_t epoch_index; - uint64_t input_index; -}; - -struct rollup_advance_state { - struct rollup_input_metadata metadata; - struct rollup_bytes payload; -}; - -struct rollup_inspect_state { - struct rollup_bytes payload; -}; - -struct rollup_finish { - /* True if previous request should be accepted */ - /* False if previous request should be rejected */ - bool accept_previous_request; - - int next_request_type; /* either CARTESI_ROLLUP_ADVANCE or CARTESI_ROLLUP_INSPECT */ - int next_request_payload_length; -}; - -struct rollup_voucher { - uint8_t destination[CARTESI_ROLLUP_ADDRESS_SIZE]; - struct rollup_bytes payload; - uint64_t index; -}; - -struct rollup_notice { - struct rollup_bytes payload; - uint64_t index; -}; - -struct rollup_report { - struct rollup_bytes payload; -}; - -struct rollup_exception { - struct rollup_bytes payload; -}; - -/* Finishes processing of current advance or inspect. - * Returns only when next advance input or inspect query is ready. - * How: - * Yields manual with rx-accepted if accept is true and yields manual with rx-rejected if accept is false. - * Once yield returns, checks the data field in fromhost to decide if next request is advance or inspect. - * Returns type and payload length of next request in struct - * Returns 0 */ -#define IOCTL_ROLLUP_FINISH _IOWR(0xd3, 0, struct rollup_finish) - -/* Obtains arguments to advance state - * How: - * Reads from input metadat memory range and convert data. - * Reads from rx buffer and copy to payload - * Returns 0 */ -#define IOCTL_ROLLUP_READ_ADVANCE_STATE _IOWR(0xd3, 0, struct rollup_advance_state) - -/* Obtains arguments to inspect state - * How: - * Reads from rx buffer and copy to payload - * Returns 0 */ -#define IOCTL_ROLLUP_READ_INSPECT_STATE _IOWR(0xd3, 0, struct rollup_inspect_state) - -/* Outputs a new voucher. - * How: Computes the Keccak-256 hash of address+payload and then, atomically: - * - Copies the (address+be32(0x40)+be32(payload_length)+payload) to the tx buffer - * - Copies the hash to the next available slot in the voucher-hashes memory range - * - Yields automatic with tx-voucher - * - Fills in the index field with the corresponding slot from voucher-hashes - * Returns 0 */ -#define IOCTL_ROLLUP_WRITE_VOUCHER _IOWR(0xd3, 1, struct rollup_voucher) - -/* Outputs a new notice. - * How: Computes the Keccak-256 hash of payload and then, atomically: - * - Copies the (be32(0x20)+be32(payload_length)+payload) to the tx buffer - * - Copies the hash to the next available slot in the notice-hashes memory range - * - Yields automatic with tx-notice - * - Fills in the index field with the corresponding slot from notice-hashes - * Returns 0 */ -#define IOCTL_ROLLUP_WRITE_NOTICE _IOWR(0xd3, 2, struct rollup_notice) - -/* Outputs a new report. - * - Copies the (be32(0x20)+be32(payload_length)+payload) to the tx buffer - * - Yields automatic with tx-report - * Returns 0 */ -#define IOCTL_ROLLUP_WRITE_REPORT _IOWR(0xd3, 3, struct rollup_report) - -/* Throws an exeption. - * - Copies the (be32(0x20)+be32(payload_length)+payload) to the tx buffer - * - Yields manual with tx-exception - * Returns 0 */ -#define IOCTL_ROLLUP_THROW_EXCEPTION _IOWR(0xd3, 4, struct rollup_exception) -#endif - - -int rollup_finish_request(int fd, struct rollup_finish *finish, bool accept); -int rollup_read_advance_state_request(int fd, struct rollup_finish *finish, struct rollup_bytes *bytes, struct rollup_input_metadata *metadata); -int rollup_read_inspect_state_request(int fd, struct rollup_finish *finish, struct rollup_bytes *query); -int rollup_write_voucher(int fd, uint8_t address[CARTESI_ROLLUP_ADDRESS_SIZE], struct rollup_bytes *bytes, uint64_t* voucher_index); -int rollup_write_notice(int fd, struct rollup_bytes *bytes, uint64_t* notice_index); -int rollup_write_report(int fd, struct rollup_bytes *bytes); -int rollup_throw_exception(int fd, struct rollup_bytes *bytes);