Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inconsistencies in the http server api #60

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,14 @@ jobs:
run: |
cd rollup-http/rollup-http-server
MOCK_BUILD=true cargo test -- --show-output --test-threads=1

- name: Test rollup-http-server api schema
run: |
sudo apt-get update
sudo apt-get install -y wait-for-it python3 python3-pip
pip3 install schemathesis
cd rollup-http/rollup-http-server
wget https://raw.githubusercontent.com/cartesi/openapi-interfaces/main/rollup.yaml
MOCK_BUILD=true cargo run -- "echo 1" &
wait-for-it localhost:5004 --timeout=30
st run rollup.yaml --checks all --validate-schema true --hypothesis-phases explicit --base-url http://localhost:5004
7 changes: 5 additions & 2 deletions rollup-http/rollup-http-client/src/rollup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ use std::fmt;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AdvanceMetadata {
pub chain_id: u64,
pub app_contract: String,
diegonehab marked this conversation as resolved.
Show resolved Hide resolved
pub msg_sender: String,
pub input_index: u64,
pub block_number: u64,
pub block_timestamp: u64,
pub prev_randao: String,
pub input_index: u64,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
Expand Down Expand Up @@ -67,7 +70,7 @@ pub struct IndexResponse {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GIORequest {
pub domain: u16,
pub payload: String,
pub id: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
34 changes: 24 additions & 10 deletions rollup-http/rollup-http-server/src/http_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ async fn voucher(mut voucher: Json<Voucher>, data: Data<Mutex<Context>>) -> Http
voucher.destination,
voucher.destination.len()
);
return HttpResponse::BadRequest().body("Address not valid");
return HttpResponse::BadRequest()
.append_header((hyper::header::CONTENT_TYPE, "text/plain"))
.body("address not valid");
}
let context = data.lock().await;
// Write voucher to linux rollup device
Expand All @@ -106,6 +108,7 @@ async fn voucher(mut voucher: Json<Voucher>, data: Data<Mutex<Context>>) -> Http
e.to_string()
);
HttpResponse::BadRequest()
.append_header((hyper::header::CONTENT_TYPE, "text/plain"))
diegonehab marked this conversation as resolved.
Show resolved Hide resolved
.body(format!("unable to insert voucher, error details: '{}'", e))
}
};
Expand All @@ -127,7 +130,8 @@ async fn notice(mut notice: Json<Notice>, data: Data<Mutex<Context>>) -> HttpRes
Err(e) => {
log::error!("unable to insert notice, error details: '{}'", e);
HttpResponse::BadRequest()
.body(format!("Unable to insert notice, error details: '{}'", e))
.append_header((hyper::header::CONTENT_TYPE, "text/plain"))
.body(format!("unable to insert notice, error details: '{}'", e))
}
};
}
Expand All @@ -146,6 +150,7 @@ async fn report(report: Json<Report>, data: Data<Mutex<Context>>) -> HttpRespons
Err(e) => {
log::error!("unable to insert report, error details: '{}'", e);
HttpResponse::BadRequest()
.append_header((hyper::header::CONTENT_TYPE, "text/plain"))
.body(format!("unable to insert notice, error details: '{}'", e))
}
};
Expand All @@ -163,10 +168,12 @@ async fn gio(request: Json<GIORequest>, data: Data<Mutex<Context>>) -> 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()
.append_header((hyper::header::CONTENT_TYPE, "text/plain"))
.body(format!(
"unable to process gio request, error details: '{}'",
e
))
}
};
}
Expand All @@ -188,6 +195,7 @@ async fn exception(exception: Json<Exception>, data: Data<Mutex<Context>>) -> Ht
Err(e) => {
log::error!("unable to throw exception, error details: '{}'", e);
HttpResponse::BadRequest()
.append_header((hyper::header::CONTENT_TYPE, "text/plain"))
.body(format!("unable to throw exception, error details: '{}'", e))
}
};
Expand All @@ -203,7 +211,9 @@ async fn finish(finish: Json<FinishRequest>, data: Data<Mutex<Context>>) -> Http
"accept" => true,
"reject" => false,
_ => {
return HttpResponse::BadRequest().body("status must be 'accept' or 'reject'");
return HttpResponse::BadRequest()
.append_header((hyper::header::CONTENT_TYPE, "text/plain"))
.body("status must be 'accept' or 'reject'");
}
};
log::debug!(
Expand All @@ -218,7 +228,7 @@ async fn finish(finish: Json<FinishRequest>, data: Data<Mutex<Context>>) -> Http
Ok(finish_request) => {
// Received new request, process it
log::info!(
"Received new request of type {}",
"received new request of type {}",
match finish_request.next_request_type {
0 => "ADVANCE",
1 => "INSPECT",
Expand All @@ -233,7 +243,9 @@ async fn finish(finish: Json<FinishRequest>, data: Data<Mutex<Context>>) -> Http
e.to_string()
);
log::error!("{}", &error_message);
return HttpResponse::BadRequest().body(error_message);
return HttpResponse::BadRequest()
.append_header((hyper::header::CONTENT_TYPE, "text/plain"))
.body(error_message);
}
}
}
Expand All @@ -243,7 +255,9 @@ async fn finish(finish: Json<FinishRequest>, data: Data<Mutex<Context>>) -> Http
e.to_string()
);
log::error!("{}", &error_message);
return HttpResponse::BadRequest().body(error_message);
return HttpResponse::BadRequest()
.append_header((hyper::header::CONTENT_TYPE, "text/plain"))
.body(error_message);
}
};

Expand Down
16 changes: 8 additions & 8 deletions rollup-http/rollup-http-server/src/rollup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl cmt_abi_u256_t {
cmt_abi_encode_uint_nn(
binary.len(),
binary.as_mut_ptr() as *const u8,
value.data.as_mut_ptr()
value.data.as_mut_ptr(),
diegonehab marked this conversation as resolved.
Show resolved Hide resolved
)
};
if rc != 0 {
Expand Down Expand Up @@ -152,7 +152,7 @@ impl cmt_abi_address_t {
pub struct GIORequest {
#[validate(range(min = 0x10))] // avoid overlapping with our HTIF_YIELD_MANUAL_REASON_*
diegonehab marked this conversation as resolved.
Show resolved Hide resolved
pub domain: u16,
pub payload: String,
pub id: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -185,12 +185,12 @@ impl From<cmt_rollup_advance_t> for AdvanceMetadata {
prev_randao.push_str(&hex::encode(&other.prev_randao.data));
AdvanceMetadata {
chain_id: other.chain_id,
app_contract: app_contract,
msg_sender: msg_sender,
app_contract,
msg_sender,
block_timestamp: other.block_timestamp,
block_number: other.block_number,
input_index: other.index,
prev_randao: prev_randao,
prev_randao,
}
}
}
Expand Down Expand Up @@ -354,7 +354,8 @@ pub fn rollup_read_inspect_state_request(
payload: cmt_abi_bytes_t {
length: 0,
data: std::ptr::null::<::std::os::raw::c_uchar>() as *mut c_void,
}});
},
});

let res = unsafe { cmt_rollup_read_inspect_state(fd.0, inspect_request.as_mut()) };

Expand Down Expand Up @@ -502,8 +503,7 @@ pub fn gio_request(
fd: &RollupFd,
gio: &GIORequest,
) -> Result<GIOResponse, Box<dyn std::error::Error>> {
println!("going to do gio_request");
let binary_payload = match hex::decode(&gio.payload[2..]) {
let binary_payload = match hex::decode(&gio.id[2..]) {
Ok(payload) => payload,
Err(_err) => {
return Err(Box::new(RollupError::new(&format!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ async fn test_gio_request(
println!("Sending gio request");
let test_gio_request = GIORequest {
domain: 0x100,
payload: "0x".to_string() + &hex::encode("gio test payload 01"),
id: "0x".to_string() + &hex::encode("gio test payload 01"),
};
rollup_http_client::client::send_gio_request(&context.address, test_gio_request.clone()).await;
context.server_handle.stop(true).await;
Expand Down
Loading