Skip to content

Commit

Permalink
[core] Handle RPC invoke error (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
m4sterchain authored and mssun committed Dec 18, 2019
1 parent b472d0f commit 3f95208
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
25 changes: 24 additions & 1 deletion mesatee_core/src/error.rs
Expand Up @@ -23,6 +23,8 @@ use std::fmt;
use std::{io, net};
use teaclave_utils;

use serde_derive;

pub type Result<T> = std::result::Result<T, Error>;

/// Status for Ecall
Expand All @@ -32,6 +34,7 @@ pub struct EnclaveStatus(u32);
/// Status for Ocall
pub type UntrustedStatus = EnclaveStatus;

#[derive(serde_derive::Serialize, serde_derive::Deserialize)]
pub struct Error {
repr: Repr,
}
Expand All @@ -42,8 +45,10 @@ impl fmt::Debug for Error {
}
}

#[derive(serde_derive::Serialize, serde_derive::Deserialize)]
enum Repr {
Simple(ErrorKind),
#[serde(skip)]
Custom(Box<Custom>),
}

Expand All @@ -53,7 +58,18 @@ struct Custom {
error: Box<dyn std::error::Error + Send + Sync>,
}

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[derive(
serde_derive::Serialize,
serde_derive::Deserialize,
Clone,
Copy,
Debug,
Eq,
Hash,
Ord,
PartialEq,
PartialOrd,
)]
pub enum ErrorKind {
/// The operation lacked the necessary privileges to complete.
PermissionDenied,
Expand Down Expand Up @@ -341,6 +357,13 @@ impl Error {
}
}

pub fn into_simple_error(self) -> Error {
match self.repr {
Repr::Simple(_) => self,
Repr::Custom(c) => Error::from(c.kind),
}
}

pub fn kind(&self) -> ErrorKind {
match self.repr {
Repr::Custom(ref c) => c.kind,
Expand Down
28 changes: 21 additions & 7 deletions mesatee_core/src/rpc/mod.rs
Expand Up @@ -21,7 +21,7 @@
#[cfg(feature = "mesalock_sgx")]
use std::prelude::v1::*;

use crate::Result;
use crate::{Error, ErrorKind, Result};
use serde::{de::DeserializeOwned, Serialize};
use std::io::{Read, Write};

Expand Down Expand Up @@ -65,10 +65,18 @@ where
// recv_buf should be a serialized incoming request U
// The server needs deser it into U first
let request: U = serde_json::from_slice(&recv_buf)?;
debug!("request: {:?}", request);
let result: V = x.handle_invoke(request)?;
let response: Vec<u8> = serde_json::to_vec(&result)?;
debug!("response {:?}", response);
debug!("SERVER get request: {:?}", request);
let result: Result<V> = x.handle_invoke(request).map_err(|e| e.into_simple_error());
debug!("SERVER handle_invoke result: {:?}", result);

let response = match serde_json::to_vec(&result) {
Ok(resp) => resp,
Err(_) => {
let r: Result<V> = Err(Error::from(ErrorKind::InternalRPCError));
serde_json::to_vec(&r).expect("infallable")
}
};
debug!("SERVER send response {:?}", response);

// Now the result is stored in ret and we need to sent it back.
// `ret` is cleared here. Performance is not very good.
Expand All @@ -89,10 +97,16 @@ where

fn invoke(&mut self, input: U) -> Result<V> {
let request_payload: Vec<u8> = serde_json::to_vec(&input)?;

debug!("CLIENT: sending req: {:?}", request_payload);
send_vec(self, request_payload)?;

let result_buf: Vec<u8> = receive_vec(self)?;
let response: V = serde_json::from_slice(&result_buf)?;
Ok(response)
debug!("CLIENT: receiving resp: {:?}", result_buf);

let resp: Result<V> = serde_json::from_slice(&result_buf)?;

resp
}
}

Expand Down

0 comments on commit 3f95208

Please sign in to comment.