diff --git a/rollup-http/rollup-http-client/src/client.rs b/rollup-http/rollup-http-client/src/client.rs index 72eb0b0c..3fb25202 100644 --- a/rollup-http/rollup-http-client/src/client.rs +++ b/rollup-http/rollup-http-client/src/client.rs @@ -14,7 +14,10 @@ // limitations under the License. // -use crate::rollup::{AdvanceRequest, Exception, IndexResponse, InspectRequest, Notice, Report, RollupRequest, RollupResponse, Voucher, GIORequest}; +use crate::rollup::{ + AdvanceRequest, Exception, GIORequest, IndexResponse, InspectRequest, Notice, Report, + RollupRequest, RollupResponse, Voucher, +}; use hyper::Response; use serde::{Deserialize, Serialize}; use std::io::ErrorKind; @@ -23,13 +26,9 @@ use std::io::ErrorKind; #[serde(tag = "request_type")] enum RollupHttpRequest { #[serde(rename = "advance_state")] - Advance { - data: AdvanceRequest, - }, + Advance { data: AdvanceRequest }, #[serde(rename = "inspect_state")] - Inspect { - data: InspectRequest, - }, + Inspect { data: InspectRequest }, } pub async fn send_voucher(rollup_http_server_addr: &str, voucher: Voucher) { @@ -99,14 +98,19 @@ pub async fn send_report(rollup_http_server_addr: &str, report: Report) { } } -pub async fn send_gio_request(rollup_http_server_addr: &str, gio_request: GIORequest) -> Response { +pub async fn send_gio_request( + rollup_http_server_addr: &str, + gio_request: GIORequest, +) -> Response { log::debug!("sending gio request to {}", rollup_http_server_addr); let client = hyper::Client::new(); let req = hyper::Request::builder() .method(hyper::Method::POST) .header(hyper::header::CONTENT_TYPE, "application/json") .uri(rollup_http_server_addr.to_string() + "/gio") - .body(hyper::Body::from(serde_json::to_string(&gio_request).unwrap())) + .body(hyper::Body::from( + serde_json::to_string(&gio_request).unwrap(), + )) .expect("gio request"); match client.request(req).await { Ok(res) => { @@ -115,16 +119,16 @@ pub async fn send_gio_request(rollup_http_server_addr: &str, gio_request: GIOReq } Err(e) => { log::error!("failed to send gio request to rollup http server: {}", e); - Response::builder().status(500).body(hyper::Body::empty()).unwrap() + Response::builder() + .status(500) + .body(hyper::Body::empty()) + .unwrap() } } } pub async fn throw_exception(rollup_http_server_addr: &str, exception: Exception) { - log::debug!( - "throwing exception request to {}", - rollup_http_server_addr - ); + log::debug!("throwing exception request to {}", rollup_http_server_addr); let client = hyper::Client::new(); let req = hyper::Request::builder() .method(hyper::Method::POST) diff --git a/rollup-http/rollup-http-client/src/lib.rs b/rollup-http/rollup-http-client/src/lib.rs index 0bb4a0e9..2e40f68f 100644 --- a/rollup-http/rollup-http-client/src/lib.rs +++ b/rollup-http/rollup-http-client/src/lib.rs @@ -1,2 +1,2 @@ pub mod client; -pub mod rollup; \ No newline at end of file +pub mod rollup; diff --git a/rollup-http/rollup-http-server/src/http_service.rs b/rollup-http/rollup-http-server/src/http_service.rs index 812d2028..8afc64c0 100644 --- a/rollup-http/rollup-http-server/src/http_service.rs +++ b/rollup-http/rollup-http-server/src/http_service.rs @@ -20,13 +20,14 @@ use actix_web::{middleware::Logger, web::Data, App, HttpResponse, HttpServer}; use actix_web_validator::Json; use async_mutex::Mutex; use serde::{Deserialize, Serialize}; -use tokio::sync::Notify; use serde_json::json; +use tokio::sync::Notify; use crate::config::Config; -use crate::rollup::{self, RollupFd, GIORequest}; +use crate::rollup::{self, GIORequest, RollupFd}; use crate::rollup::{ - AdvanceRequest, Exception, InspectRequest, Notice, Report, RollupRequest, FinishRequest, Voucher, + AdvanceRequest, Exception, FinishRequest, InspectRequest, Notice, Report, RollupRequest, + Voucher, }; #[derive(Debug, Serialize, Deserialize)] @@ -155,7 +156,6 @@ async fn report(report: Json, data: Data>) -> HttpRespons async fn gio(request: Json, data: Data>) -> HttpResponse { log::debug!("received gio request {:#?}", request); let context = data.lock().await; - // Write report to linux rollup device return match rollup::gio_request(&*context.rollup_fd.lock().await, &request.0) { Ok(result) => { log::debug!("gio successfully processed, response: {:#?}", result); @@ -163,8 +163,10 @@ async fn gio(request: Json, data: Data>) -> HttpRespo } Err(e) => { log::error!("unable to process gio request, error details: '{}'", e); - HttpResponse::BadRequest() - .body(format!("unable to process gio request, error details: '{}'", e)) + HttpResponse::BadRequest().body(format!( + "unable to process gio request, error details: '{}'", + e + )) } }; } diff --git a/rollup-http/rollup-http-server/src/rollup/mod.rs b/rollup-http/rollup-http-server/src/rollup/mod.rs index 6c03ecfc..646be3e3 100644 --- a/rollup-http/rollup-http-server/src/rollup/mod.rs +++ b/rollup-http/rollup-http-server/src/rollup/mod.rs @@ -20,11 +20,11 @@ use std::io::ErrorKind; +use lazy_static::lazy_static; use libc::c_void; +use regex::Regex; use serde::{Deserialize, Serialize}; use validator::Validate; -use regex::Regex; -use lazy_static::lazy_static; include!(concat!(env!("OUT_DIR"), "/bindings.rs")); @@ -116,7 +116,7 @@ impl From<&mut RollupFinish> for cmt_rollup_finish_t { #[derive(Debug, Clone, Serialize, Deserialize, Validate)] pub struct GIORequest { - #[validate(range(min = 0x10))] // avoid overlapping with our HTIF_YIELD_MANUAL_REASON_* + #[validate(range(min = 0x10))] // avoid overlapping with our HTIF_YIELD_MANUAL_REASON_* pub domain: u16, pub payload: String, } @@ -181,7 +181,7 @@ pub struct FinishRequest { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct InspectReport { - pub reports: Vec + pub reports: Vec, } #[derive(Debug, Clone, Serialize, Deserialize, Validate)] @@ -239,7 +239,6 @@ pub fn rollup_finish_request( pub fn rollup_read_advance_state_request( fd: &RollupFd, ) -> Result> { - let mut advance_request = Box::new(cmt_rollup_advance_t { chain_id: 0, msg_sender: Default::default(), @@ -251,9 +250,7 @@ pub fn rollup_read_advance_state_request( payload: std::ptr::null::<::std::os::raw::c_uchar>() as *mut c_void, }); - let res = unsafe { - cmt_rollup_read_advance_state(fd.0, advance_request.as_mut()) - }; + let res = unsafe { cmt_rollup_read_advance_state(fd.0, advance_request.as_mut()) }; if res != 0 { return Err(Box::new(RollupError::new(&format!( @@ -269,7 +266,11 @@ pub fn rollup_read_advance_state_request( let mut payload: Vec = Vec::with_capacity(advance_request.payload_length as usize); if advance_request.payload_length > 0 { unsafe { - std::ptr::copy(advance_request.payload, payload.as_mut_ptr() as *mut c_void, advance_request.payload_length as usize); + std::ptr::copy( + advance_request.payload, + payload.as_mut_ptr() as *mut c_void, + advance_request.payload_length as usize, + ); payload.set_len(advance_request.payload_length as usize); } } @@ -282,19 +283,15 @@ pub fn rollup_read_advance_state_request( Ok(result) } - pub fn rollup_read_inspect_state_request( fd: &RollupFd, ) -> Result> { - let mut inspect_request = Box::new(cmt_rollup_inspect_t { payload_length: 0, payload: std::ptr::null::<::std::os::raw::c_uchar>() as *mut c_void, }); - let res = unsafe { - cmt_rollup_read_inspect_state(fd.0, inspect_request.as_mut()) - }; + let res = unsafe { cmt_rollup_read_inspect_state(fd.0, inspect_request.as_mut()) }; if res != 0 { return Err(Box::new(RollupError::new(&format!( @@ -307,11 +304,18 @@ pub fn rollup_read_inspect_state_request( log::info!("read zero size payload from inspect state request"); } - println!("inspect_request.payload_length: {}", inspect_request.payload_length); + println!( + "inspect_request.payload_length: {}", + inspect_request.payload_length + ); let mut payload: Vec = Vec::with_capacity(inspect_request.payload_length as usize); if inspect_request.payload_length > 0 { unsafe { - std::ptr::copy(inspect_request.payload, payload.as_mut_ptr() as *mut c_void, inspect_request.payload_length as usize); + std::ptr::copy( + inspect_request.payload, + payload.as_mut_ptr() as *mut c_void, + inspect_request.payload_length as usize, + ); payload.set_len(inspect_request.payload_length as usize); } } @@ -349,7 +353,12 @@ pub fn rollup_write_notice( binary_payload.len(), ); - cmt_rollup_emit_notice(fd.0, length as u32, buffer.as_mut_ptr() as *mut c_void, &mut notice_index) + cmt_rollup_emit_notice( + fd.0, + length as u32, + buffer.as_mut_ptr() as *mut c_void, + &mut notice_index, + ) }; if res != 0 { @@ -364,7 +373,6 @@ pub fn rollup_write_notice( Ok(notice_index as u64) } - pub fn rollup_write_voucher( fd: &RollupFd, voucher: &mut Voucher, @@ -442,7 +450,10 @@ pub fn rollup_write_voucher( Ok(voucher_index as u64) } -pub fn rollup_write_report(fd: &RollupFd, 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..]) { @@ -480,7 +491,10 @@ pub fn rollup_write_report(fd: &RollupFd, report: &Report) -> Result<(), Box Result> { +pub fn gio_request( + fd: &RollupFd, + gio: &GIORequest, +) -> Result> { println!("going to do gio_request"); let binary_payload = match hex::decode(&gio.payload[2..]) { Ok(payload) => payload, @@ -510,9 +524,7 @@ pub fn gio_request(fd: &RollupFd, gio: &GIORequest) -> Result() as *mut c_void, }); - let res = unsafe { - cmt_gio_request(fd.0, gio_request.as_mut()) - }; + let res = unsafe { cmt_gio_request(fd.0, gio_request.as_mut()) }; if res != 0 { return Err(Box::new(RollupError::new(&format!( @@ -689,7 +701,6 @@ 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 4ba3ce05..95797b86 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 @@ -19,19 +19,19 @@ extern crate rollup_http_server; use actix_server::ServerHandle; use async_mutex::Mutex; +use rand::Rng; use rollup_http_client::rollup::{ - Exception, Notice, Report, RollupRequest, RollupResponse, Voucher, GIORequest, + Exception, GIORequest, 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::future::Future; -use std::sync::Arc; -use rand::Rng; use std::env; use std::fs::File; +use std::future::Future; use std::io::Write; +use std::sync::Arc; const HOST: &str = "127.0.0.1"; @@ -156,7 +156,10 @@ async fn test_finish_request( let mut inspect_file = File::create(inspect_path)?; inspect_file.write_all(&inspect_binary_data)?; - env::set_var("CMT_INPUTS", format!("0:{0},1:{1}", advance_path, inspect_path)); + env::set_var( + "CMT_INPUTS", + format!("0:{0},1:{1}", advance_path, inspect_path), + ); env::set_var("CMT_DEBUG", "yes"); let context = context_future.await; @@ -216,18 +219,20 @@ async fn test_finish_request( Ok(()) } -fn check_voucher_or_fail( - original_voucher: Voucher, - output_filename: &str, -) { +fn check_voucher_or_fail(original_voucher: Voucher, output_filename: &str) { // we try to decode the produced voucher with a third-party lib to see if it matches // the expected values - let data = - std::fs::read(output_filename).expect("error reading voucher file"); + let data = std::fs::read(output_filename).expect("error reading voucher file"); let decoded_voucher = ethabi::decode( - &[ethabi::ParamType::Address, ethabi::ParamType::Uint(256), ethabi::ParamType::Bytes], + &[ + ethabi::ParamType::Address, + ethabi::ParamType::Uint(256), + ethabi::ParamType::Bytes, + ], &data[4..], // skip the first 4 bytes that are the function signature - ).ok().unwrap(); + ) + .ok() + .unwrap(); assert_eq!( "0x".to_string() + &decoded_voucher[0].to_string(), @@ -292,12 +297,13 @@ async fn test_write_notice( // we try to decode the produced voucher with a third-party lib to see if it matches // the expected values - let data = - std::fs::read("none.output-0.bin").expect("error reading test notice file"); + let data = std::fs::read("none.output-0.bin").expect("error reading test notice file"); let decoded_notice = ethabi::decode( &[ethabi::ParamType::Bytes], &data[4..], // skip the first 4 bytes that are the function signature - ).ok().unwrap(); + ) + .ok() + .unwrap(); assert_eq!( "0x".to_string() + &decoded_notice[0].to_string(), @@ -324,10 +330,7 @@ async fn test_write_report( //Read text file with results let report1 = std::fs::read_to_string("none.report-0.bin").expect("error reading test report file"); - assert_eq!( - report1, - "report test payload 01" - ); + assert_eq!(report1, "report test payload 01"); std::fs::remove_file("none.report-0.bin")?; Ok(()) @@ -349,12 +352,8 @@ async fn test_gio_request( context.server_handle.stop(true).await; //Read text file with results - let gio = - std::fs::read_to_string("none.gio-0.bin").expect("error reading test gio file"); - assert_eq!( - gio, - "gio test payload 01" - ); + let gio = std::fs::read_to_string("none.gio-0.bin").expect("error reading test gio file"); + assert_eq!(gio, "gio test payload 01"); std::fs::remove_file("none.gio-0.bin")?; Ok(()) diff --git a/sys-utils/libcmt/.gitignore b/sys-utils/libcmt/.gitignore index 8975f803..944c0294 100644 --- a/sys-utils/libcmt/.gitignore +++ b/sys-utils/libcmt/.gitignore @@ -1,4 +1,3 @@ build compile_flags.txt */*.clang-tidy* -compile_flags.txt diff --git a/sys-utils/libcmt/src/io-mock.c b/sys-utils/libcmt/src/io-mock.c index e954b766..6f4be31c 100644 --- a/sys-utils/libcmt/src/io-mock.c +++ b/sys-utils/libcmt/src/io-mock.c @@ -57,6 +57,7 @@ int cmt_io_init(cmt_io_driver_t *_me) { me->output_seq = 0; me->report_seq = 0; me->exception_seq = 0; + me->gio_seq = 0; return 0; } @@ -117,6 +118,8 @@ static int load_next_input(cmt_io_driver_mock_t *me, struct cmt_io_yield *rr) { me->output_seq = 0; me->report_seq = 0; me->exception_seq = 0; + me->gio_seq = 0; + me->rx->end = me->rx->begin + file_length; if (cmt_util_debug_enabled()) { (void) fprintf(stderr, "processing filename: \"%s\" (%lu), type: %d\n", filepath, file_length, me->input_type); @@ -218,7 +221,7 @@ static int mock_tx_gio(cmt_io_driver_mock_t *me, struct cmt_io_yield *rr) { (void) fprintf(stderr, "Expected cmd to be MANUAL\n"); return -EINVAL; } - return store_next_output(me, "gio-", &me->exception_seq, rr); + return store_next_output(me, "gio-", &me->gio_seq, rr); } /* These behaviours are defined by the cartesi-machine emulator */ diff --git a/sys-utils/libcmt/src/io.h b/sys-utils/libcmt/src/io.h index 41dd06f6..87723352 100644 --- a/sys-utils/libcmt/src/io.h +++ b/sys-utils/libcmt/src/io.h @@ -106,6 +106,7 @@ typedef struct { int output_seq; int report_seq; int exception_seq; + int gio_seq; } cmt_io_driver_mock_t; /** Implementation specific cmio state. */ diff --git a/sys-utils/libcmt/src/keccak.c b/sys-utils/libcmt/src/keccak.c index 38a9a61c..a282ba92 100644 --- a/sys-utils/libcmt/src/keccak.c +++ b/sys-utils/libcmt/src/keccak.c @@ -13,8 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "abi.h" #include "keccak.h" +#include "abi.h" #include diff --git a/sys-utils/libcmt/tests/io_echo.c b/sys-utils/libcmt/tests/io_echo.c deleted file mode 100644 index 449b239f..00000000 --- a/sys-utils/libcmt/tests/io_echo.c +++ /dev/null @@ -1,92 +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 "io.h" - -#include -#include -#include -#include - -int next(union cmt_io_driver *io, uint32_t *n) { - struct cmt_io_yield req[1] = {{ - .dev = HTIF_DEVICE_YIELD, - .cmd = HTIF_YIELD_CMD_MANUAL, - .reason = HTIF_YIELD_MANUAL_REASON_RX_ACCEPTED, - .data = *n, - }}; - if (cmt_io_yield(io, req)) { - fprintf(stderr, "%s:%d failed to yield\n", __FILE__, __LINE__); - return -1; - } - *n = req->data; - return req->reason; -} - -int voucher(union cmt_io_driver *io, uint32_t n) { - struct cmt_io_yield req[1] = {{ - .dev = HTIF_DEVICE_YIELD, - .cmd = HTIF_YIELD_CMD_AUTOMATIC, - .reason = HTIF_YIELD_AUTOMATIC_REASON_TX_OUTPUT, - .data = n, - }}; - return cmt_io_yield(io, req); -} - -int report(union cmt_io_driver *io, uint32_t n) { - struct cmt_io_yield req[1] = {{ - .dev = HTIF_DEVICE_YIELD, - .cmd = HTIF_YIELD_CMD_AUTOMATIC, - .reason = HTIF_YIELD_AUTOMATIC_REASON_TX_REPORT, - .data = n, - }}; - return cmt_io_yield(io, req); -} - -int exception(union cmt_io_driver *io, uint32_t n) { - struct cmt_io_yield req[1] = {{ - .dev = HTIF_DEVICE_YIELD, - .cmd = HTIF_YIELD_CMD_MANUAL, - .reason = HTIF_YIELD_MANUAL_REASON_TX_EXCEPTION, - .data = n, - }}; - return cmt_io_yield(io, req); -} - -int main(void) { - /* init ------------------------------------------------------------- */ - union cmt_io_driver io[1]; - int rc = cmt_io_init(io); - if (rc) { - fprintf(stderr, "%s:%d failed to init with: %s\n", __FILE__, __LINE__, strerror(-rc)); - return EXIT_FAILURE; - } - - cmt_buf_t tx = cmt_io_get_tx(io); - cmt_buf_t rx = cmt_io_get_rx(io); - - int type; - uint32_t n = 0; - while ((type = next(io, &n)) >= 0) { - memcpy(tx.begin, rx.begin, n); - voucher(io, n); - report(io, n); - exception(io, n); - } - - /* fini ------------------------------------------------------------- */ - cmt_io_fini(io); - return 0; -} diff --git a/sys-utils/libcmt/tests/rollup_echo.c b/sys-utils/libcmt/tests/rollup_echo.c deleted file mode 100644 index e9651b2a..00000000 --- a/sys-utils/libcmt/tests/rollup_echo.c +++ /dev/null @@ -1,92 +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.h" - -#include -#include -#include -#include - -/* Data encoding: - * - cast calldata "EvmAdvance(address,uint256,uint256,uint256,bytes)" \ - * 0x0000000000000000000000000000000000000000 \ - * 0x0000000000000000000000000000000000000001 \ - * 0x0000000000000000000000000000000000000002 \ - * 0x0000000000000000000000000000000000000003 \ - * 0x`echo "hello world" | xxd -r -p -c0` > "" - * - * Data decoding: - * - cast calldata-decode "Voucher(address,uint256,bytes)" 0x`xxd -p -c0 ""` - * - */ -int main(void) { - cmt_rollup_t rollup; - - if (cmt_rollup_init(&rollup)) - return EXIT_FAILURE; - // cmt_rollup_load_merkle(rollup, "/tmp/merkle.dat"); - - uint8_t small[] = { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, - }; - for (;;) { - int rc; - cmt_rollup_finish_t finish = {.accept_previous_request = true}; - cmt_rollup_advance_t advance; - // cmt_rollup_inspect_t inspect; - - if (cmt_rollup_finish(&rollup, &finish) < 0) - goto teardown; - - switch (finish.next_request_type) { - case HTIF_YIELD_REASON_ADVANCE: - rc = cmt_rollup_read_advance_state(&rollup, &advance); - if (rc < 0) { - fprintf(stderr, "%s:%d Error on advance %s (%d)\n", __FILE__, __LINE__, strerror(-rc), (-rc)); - break; - } - - rc = cmt_rollup_emit_voucher(&rollup, sizeof advance.msg_sender, advance.msg_sender, sizeof small, small, advance.payload_length, advance.payload, NULL); - if (rc < 0) { - fprintf(stderr, "%s:%d Error on voucher %s (%d)\n", __FILE__, __LINE__, strerror(-rc), (-rc)); - break; - } - - rc = cmt_rollup_emit_notice(&rollup, advance.payload_length, advance.payload, NULL); - if (rc < 0) { - fprintf(stderr, "%s:%d Error on notice %s (%d)\n", __FILE__, __LINE__, strerror(-rc), (-rc)); - break; - } - - rc = cmt_rollup_emit_report(&rollup, advance.payload_length, advance.payload); - if (rc < 0) { - fprintf(stderr, "%s:%d Error on notice %s (%d)\n", __FILE__, __LINE__, strerror(-rc), (-rc)); - break; - } - break; - case HTIF_YIELD_REASON_INSPECT: - break; - } - } - -teardown: - // cmt_rollup_save_merkle(&rollup, "/tmp/merkle.dat"); - cmt_rollup_fini(&rollup); - return 0; -}