Skip to content

Commit

Permalink
fix: address inconsistencies in the http server api
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmikhalevich committed Jun 26, 2024
1 parent 4e27e55 commit de550dc
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 21 deletions.
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,
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"))
.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(),
)
};
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_*
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

0 comments on commit de550dc

Please sign in to comment.