Skip to content

Commit

Permalink
surface errors in RPC methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jowparks committed Oct 18, 2024
1 parent f3ce30c commit f465725
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 103 deletions.
66 changes: 39 additions & 27 deletions crates/oreo_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,45 @@ pub enum OreoError {

impl IntoResponse for OreoError {
fn into_response(self) -> Response {
let (status_code, err_msg) = match self {
OreoError::DBError => (StatusCode::from_u16(600).unwrap(), self.to_string()),
OreoError::Duplicate(_) => (StatusCode::from_u16(601).unwrap(), self.to_string()),
OreoError::NoImported(_) => (StatusCode::from_u16(602).unwrap(), self.to_string()),
OreoError::Scanning(_) => (StatusCode::from_u16(603).unwrap(), self.to_string()),
OreoError::Syncing => (StatusCode::from_u16(604).unwrap(), self.to_string()),
OreoError::InternalRpcError(_) => (StatusCode::from_u16(605).unwrap(), self.to_string()),
OreoError::GenerateSpendProofFailed(_) => {
(StatusCode::from_u16(606).unwrap(), self.to_string())
}
OreoError::GenerateOutputProofFailed(_) => {
(StatusCode::from_u16(607).unwrap(), self.to_string())
}
OreoError::GenerateMintAssetProofFailed(_) => {
(StatusCode::from_u16(608).unwrap(), self.to_string())
}
OreoError::BalanceNotEnough => (StatusCode::from_u16(609).unwrap(), self.to_string()),
OreoError::BadMintRequest => (StatusCode::from_u16(610).unwrap(), self.to_string()),
OreoError::TransactionNotFound => {
(StatusCode::from_u16(611).unwrap(), self.to_string())
}
OreoError::SeralizeError(_) => (StatusCode::from_u16(612).unwrap(), self.to_string()),
OreoError::ParseError(_) => (StatusCode::from_u16(613).unwrap(), self.to_string()),
OreoError::DServerError(_) => (StatusCode::from_u16(614).unwrap(), self.to_string()),
OreoError::AccountStatusError(_) => (StatusCode::from_u16(615).unwrap(), self.to_string()),
OreoError::Unauthorized => (StatusCode::UNAUTHORIZED, self.to_string()),
};
let (status_code, err_msg) = get_status_code(self);
Json(json!({"code": status_code.as_u16(), "error": err_msg})).into_response()
}
}

impl From<OreoError> for Response {
fn from(err: OreoError) -> Response {
let (status_code, err_msg) = get_status_code(err);
Json(json!({"code": status_code.as_u16(), "error": err_msg})).into_response()
}
}

pub fn get_status_code(err: OreoError) -> (StatusCode, String) {
let (status_code, err_msg) = match err {
OreoError::DBError => (StatusCode::from_u16(600).unwrap(), err.to_string()),
OreoError::Duplicate(_) => (StatusCode::from_u16(601).unwrap(), err.to_string()),
OreoError::NoImported(_) => (StatusCode::from_u16(602).unwrap(), err.to_string()),
OreoError::Scanning(_) => (StatusCode::from_u16(603).unwrap(), err.to_string()),
OreoError::Syncing => (StatusCode::from_u16(604).unwrap(), err.to_string()),
OreoError::InternalRpcError(_) => (StatusCode::from_u16(605).unwrap(), err.to_string()),
OreoError::GenerateSpendProofFailed(_) => {
(StatusCode::from_u16(606).unwrap(), err.to_string())
}
OreoError::GenerateOutputProofFailed(_) => {
(StatusCode::from_u16(607).unwrap(), err.to_string())
}
OreoError::GenerateMintAssetProofFailed(_) => {
(StatusCode::from_u16(608).unwrap(), err.to_string())
}
OreoError::BalanceNotEnough => (StatusCode::from_u16(609).unwrap(), err.to_string()),
OreoError::BadMintRequest => (StatusCode::from_u16(610).unwrap(), err.to_string()),
OreoError::TransactionNotFound => {
(StatusCode::from_u16(611).unwrap(), err.to_string())
}
OreoError::SeralizeError(_) => (StatusCode::from_u16(612).unwrap(), err.to_string()),
OreoError::ParseError(_) => (StatusCode::from_u16(613).unwrap(), err.to_string()),
OreoError::DServerError(_) => (StatusCode::from_u16(614).unwrap(), err.to_string()),
OreoError::AccountStatusError(_) => (StatusCode::from_u16(615).unwrap(), err.to_string()),
OreoError::Unauthorized => (StatusCode::from_u16(401).unwrap(), err.to_string()),
};
(status_code, err_msg)
}
158 changes: 82 additions & 76 deletions crates/server/src/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::{str::FromStr, sync::Arc};

use axum::{
extract::{self, State},
response::IntoResponse,
Json,
extract::{self, State}, response::IntoResponse, Json
};
use constants::{ACCOUNT_VERSION, MAINNET_GENESIS_SEQUENCE};
use db_handler::DBHandler;
Expand Down Expand Up @@ -60,7 +58,7 @@ pub async fn import_account_handler<T: DBHandler>(
.index
.parse::<u64>()
.unwrap();
shared
let result = shared
.rpc_handler
.get_account_status(RpcGetAccountStatusRequest {
account: account_name.clone(),
Expand All @@ -71,15 +69,15 @@ pub async fn import_account_handler<T: DBHandler>(
sequence: MAINNET_GENESIS_SEQUENCE as u64,
});
if latest_height - head.sequence > 1000 {
let _ = shared.rpc_handler.set_scanning(RpcSetScanningRequest {
shared.rpc_handler.set_scanning(RpcSetScanningRequest {
account: account_name.clone(),
enabled: false,
});
let _ = shared.rpc_handler.reset_account(RpcResetAccountRequest {
})?;
shared.rpc_handler.reset_account(RpcResetAccountRequest {
account: account_name.clone(),
reset_scanning_enabled: Some(false),
reset_created_at: Some(false),
});
})?;
let scan_request = ScanRequest {
address: public_address.clone(),
in_vk: incoming_view_key.clone(),
Expand All @@ -90,19 +88,24 @@ pub async fn import_account_handler<T: DBHandler>(
let signature = sign(&default_secp(), &msg[..], &shared.secp.sk)
.unwrap()
.to_string();
let _ = shared.scan_handler.submit_scan_request(DecryptionMessage {
shared.scan_handler.submit_scan_request(DecryptionMessage {
message: scan_request,
signature,
});
})?;
}
Ok::<RpcImportAccountResponse, OreoError>(RpcImportAccountResponse {
name: account_name.clone(),
})
});
match result {
Ok(response) =>
RpcResponse {
status: 200,
data: RpcImportAccountResponse {
name: account_name.clone(),
},
}
})
.into_response()
data: response,
}.into_response(),

Err(e) => e.into_response(),
}
}
Err(e) => e.into_response(),
}
Expand Down Expand Up @@ -174,68 +177,78 @@ pub async fn rescan_account_handler<T: DBHandler>(
State(shared): State<Arc<SharedState<T>>>,
extract::Json(account): extract::Json<RpcGetAccountStatusRequest>,
) -> impl IntoResponse {
let db_account = shared.db_handler.get_account(account.account.clone()).await;
if let Err(e) = db_account {
return e.into_response();
match rescan_account(shared, account).await {
Ok(response) =>
RpcResponse {
status: 200,
data: response,
}.into_response(),
Err(err) => err.into_response(),
}
let account = db_account.unwrap();
let _ = shared.rpc_handler.set_scanning(RpcSetScanningRequest {
}


async fn rescan_account<T: DBHandler>(
shared: Arc<SharedState<T>>,
account: RpcGetAccountStatusRequest,
) -> Result<RescanAccountResponse, OreoError> {
let account = shared.db_handler.get_account(account.account.clone()).await?;
shared.rpc_handler.set_scanning(RpcSetScanningRequest {
account: account.name.clone(),
enabled: false,
});
let _ = shared.rpc_handler.reset_account(RpcResetAccountRequest {
})?;
shared.rpc_handler.reset_account(RpcResetAccountRequest {
account: account.name.clone(),
reset_scanning_enabled: Some(false),
reset_created_at: Some(false),
});
let update_scan_status = shared
})?;
shared
.db_handler
.update_scan_status(account.address.clone(), true)
.await;
if let Err(err) = update_scan_status {
error!("Failed to update scan status: {:?}", err);
return err.into_response();
}
if let Ok(status) = shared
.await?;
let status = shared
.rpc_handler
.get_account_status(RpcGetAccountStatusRequest {
account: account.name.clone(),
})
{
let head = status.data.account.head.unwrap_or(BlockInfo {
hash: shared.genesis_hash.clone(),
sequence: MAINNET_GENESIS_SEQUENCE as u64,
});
let scan_request = ScanRequest {
address: account.address.clone(),
in_vk: account.in_vk.clone(),
out_vk: account.out_vk.clone(),
head: Some(head),
};
let msg = bincode::serialize(&scan_request).unwrap();
let signature = sign(&default_secp(), &msg[..], &shared.secp.sk)
.unwrap()
.to_string();
let scan_request = shared.scan_handler.submit_scan_request(DecryptionMessage {
message: scan_request,
signature,
});
if let Err(err) = scan_request {
error!("Failed to submit scan request: {:?}", err);
return err.into_response();
}
}
RpcResponse {
status: 200,
data: RescanAccountResponse { success: true },
}
.into_response()
})?;
let head = status.data.account.head.unwrap_or(BlockInfo {
hash: shared.genesis_hash.clone(),
sequence: MAINNET_GENESIS_SEQUENCE as u64,
});
let scan_request = ScanRequest {
address: account.address.clone(),
in_vk: account.in_vk.clone(),
out_vk: account.out_vk.clone(),
head: Some(head),
};
let msg = bincode::serialize(&scan_request).unwrap();
let signature = sign(&default_secp(), &msg[..], &shared.secp.sk)
.unwrap()
.to_string();
shared.scan_handler.submit_scan_request(DecryptionMessage {
message: scan_request,
signature,
})?;
Ok(RescanAccountResponse { success: true })
}

pub async fn update_scan_status_handler<T: DBHandler>(
State(shared): State<Arc<SharedState<T>>>,
extract::Json(response): extract::Json<DecryptionMessage<ScanResponse>>,
) -> impl IntoResponse {
match update_scan_status(shared, response).await {
Ok(response) =>
RpcResponse {
status: 200,
data: response,
}.into_response(),
Err(err) => err.into_response(),
}
}
pub async fn update_scan_status<T: DBHandler>(
shared: Arc<SharedState<T>>,
response: DecryptionMessage<ScanResponse>,
) -> Result<SuccessResponse, OreoError> {
let DecryptionMessage {
mut message,
signature,
Expand All @@ -250,29 +263,22 @@ pub async fn update_scan_status_handler<T: DBHandler>(
&shared.secp.pk,
) {
if x {
let db_account = shared.db_handler.get_account(message.account.clone()).await;
if let Err(e) = db_account {
return e.into_response();
}
let account = db_account.unwrap();
let account = shared.db_handler.get_account(message.account.clone()).await?;
message.account = account.name.clone();
info!("set account message: {:?}", message.clone());
let resp = shared.rpc_handler.set_account_head(message);
if resp.is_err() {
info!("Failed to update account head: {:?}", resp.unwrap_err());
}
let _ = shared.rpc_handler.set_scanning(RpcSetScanningRequest {
shared.rpc_handler.set_account_head(message)?;
shared.rpc_handler.set_scanning(RpcSetScanningRequest {
account: account.name.clone(),
enabled: true,
});
let _ = shared
})?;
shared
.db_handler
.update_scan_status(account.address, false)
.await;
return Json(SuccessResponse { success: true }).into_response();
.await?;
return Ok(SuccessResponse { success: true });
}
}
Json(SuccessResponse { success: false }).into_response()
Ok(SuccessResponse { success: false })
}

pub async fn get_balances_handler<T: DBHandler>(
Expand Down

0 comments on commit f465725

Please sign in to comment.