From 397b8e22fa8999e1a415530af040d2eac9aa6bc9 Mon Sep 17 00:00:00 2001 From: mohanson Date: Mon, 15 Apr 2019 10:27:25 +0800 Subject: [PATCH] Fix an error in method `wait` --- jsonrpc/src/main.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/jsonrpc/src/main.rs b/jsonrpc/src/main.rs index 08237762..18ba5545 100644 --- a/jsonrpc/src/main.rs +++ b/jsonrpc/src/main.rs @@ -36,7 +36,7 @@ fn rpc_handler( let mut result = convention::Response::default(); result.id = reqjson.id.clone(); - match rpc_select(&app_state, reqjson.method.as_str()) { + match rpc_select(&app_state, reqjson.method.as_str(), reqjson.params) { Ok(ok) => result.result = ok, Err(e) => result.error = Some(e), } @@ -47,16 +47,31 @@ fn rpc_handler( }) } -fn rpc_select(app_state: &AppState, method: &str) -> Result { +fn rpc_select( + app_state: &AppState, + method: &str, + params: Vec, +) -> Result { match method { "ping" => { let r = app_state.network.read().unwrap().ping(); Ok(Value::from(r)) } - "wait" => match app_state.network.read().unwrap().wait(4).wait() { - Ok(ok) => Ok(Value::from(ok)), - Err(e) => Err(convention::ErrorData::new(500, &format!("{:?}", e)[..])), - }, + "wait" => { + if params.len() != 1 || !params[0].is_u64() { + return Err(convention::ErrorData::std(-32602)); + } + match app_state + .network + .read() + .unwrap() + .wait(params[0].as_u64().unwrap()) + .wait() + { + Ok(ok) => Ok(Value::from(ok)), + Err(e) => Err(convention::ErrorData::new(500, &format!("{:?}", e)[..])), + } + } "get" => { let r = app_state.network.read().unwrap().get(); Ok(Value::from(r))