Skip to content

Commit

Permalink
refactor!: rollup-http-server based on libcmt
Browse files Browse the repository at this point in the history
  • Loading branch information
algebraic-dev authored and alexmikhalevich committed Mar 20, 2024
1 parent b052ae2 commit 47e9d8b
Show file tree
Hide file tree
Showing 13 changed files with 1,363 additions and 932 deletions.
1 change: 1 addition & 0 deletions rollup-http/rollup-http-client/src/rollup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
2 changes: 1 addition & 1 deletion rollup-http/rollup-http-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ tonic-build = "0.5"

[dev-dependencies]
rollup-http-client = {path = "../rollup-http-client"}

rand = "0.8.5"


[profile.release]
Expand Down
14 changes: 1 addition & 13 deletions rollup-http/rollup-http-server/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
6 changes: 3 additions & 3 deletions rollup-http/rollup-http-server/src/dapp_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>, rollup_fd: Arc<Mutex<RawFd>>) {
pub async fn run(args: Vec<String>, rollup_fd: Arc<Mutex<RollupFd>>) {
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 {
Expand All @@ -40,7 +40,7 @@ pub async fn run(args: Vec<String>, rollup_fd: Arc<Mutex<RawFd>>) {
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);
}
Expand Down
21 changes: 10 additions & 11 deletions rollup-http/rollup-http-server/src/http_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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,
};
Expand All @@ -40,7 +39,7 @@ enum RollupHttpRequest {
/// Create new instance of http server
pub fn create_server(
config: &Config,
rollup_fd: Arc<Mutex<RawFd>>,
rollup_fd: Arc<Mutex<RollupFd>>,
) -> std::io::Result<actix_server::Server> {
let server = HttpServer::new(move || {
let data = Data::new(Mutex::new(Context {
Expand All @@ -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<Mutex<RawFd>>,
rollup_fd: Arc<Mutex<RollupFd>>,
server_ready: Arc<Notify>,
) -> std::io::Result<()> {
log::info!("starting http dispatcher http service!");
Expand All @@ -90,7 +89,7 @@ async fn voucher(mut voucher: Json<Voucher>, data: Data<Mutex<Context>>) -> 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 {
Expand All @@ -114,7 +113,7 @@ async fn notice(mut notice: Json<Notice>, data: Data<Mutex<Context>>) -> 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 {
Expand All @@ -135,7 +134,7 @@ async fn report(report: Json<Report>, data: Data<Mutex<Context>>) -> 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("")
Expand All @@ -157,7 +156,7 @@ async fn exception(exception: Json<Exception>, data: Data<Mutex<Context>>) -> 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("")
Expand Down Expand Up @@ -190,7 +189,7 @@ async fn finish(finish: Json<FinishRequest>, data: Data<Mutex<Context>>) -> 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!(
Expand All @@ -201,7 +200,7 @@ async fn finish(finish: Json<FinishRequest>, data: Data<Mutex<Context>>) -> 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!(
Expand Down Expand Up @@ -260,5 +259,5 @@ struct Error {
}

struct Context {
pub rollup_fd: Arc<Mutex<RawFd>>,
pub rollup_fd: Arc<Mutex<RollupFd>>,
}
16 changes: 2 additions & 14 deletions rollup-http/rollup-http-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<Mutex<RawFd>> = Arc::new(Mutex::new(rollup_file.into_raw_fd()));
let rollup_fd: Arc<Mutex<RollupFd>> = 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
Expand Down
143 changes: 0 additions & 143 deletions rollup-http/rollup-http-server/src/rollup/bindings.c

This file was deleted.

46 changes: 0 additions & 46 deletions rollup-http/rollup-http-server/src/rollup/bindings.h

This file was deleted.

Loading

0 comments on commit 47e9d8b

Please sign in to comment.