mirror of
https://github.com/mimblewimble/grin.git
synced 2025-02-01 17:01:09 +03:00
Handle rpc requests unwrap crash in stratumserver.rs (#2446)
* protect rpc_response from None.unwrap * protect login handler from crashing before current_block_versions has had time to get populated * protect worker_stats_id finding from None
This commit is contained in:
parent
c8fd0575ed
commit
92cbfa20a6
1 changed files with 31 additions and 28 deletions
|
@ -305,16 +305,22 @@ impl StratumServer {
|
|||
the_message.clear();
|
||||
|
||||
let mut stratum_stats = stratum_stats.write();
|
||||
let worker_stats_id = stratum_stats
|
||||
let worker_stats_id = match stratum_stats
|
||||
.worker_stats
|
||||
.iter()
|
||||
.position(|r| r.id == workers_l[num].id)
|
||||
.unwrap();
|
||||
{
|
||||
Some(id) => id,
|
||||
None => continue,
|
||||
};
|
||||
stratum_stats.worker_stats[worker_stats_id].last_seen = SystemTime::now();
|
||||
|
||||
// Call the handler function for requested method
|
||||
let response = match request.method.as_str() {
|
||||
"login" => {
|
||||
if self.current_block_versions.is_empty() {
|
||||
continue;
|
||||
}
|
||||
stratum_stats.worker_stats[worker_stats_id].initial_block_height =
|
||||
self.current_block_versions.last().unwrap().header.height;
|
||||
self.handle_login(request.params, &mut workers_l[num])
|
||||
|
@ -356,33 +362,30 @@ impl StratumServer {
|
|||
}
|
||||
};
|
||||
|
||||
let id = request.id.clone();
|
||||
// Package the reply as RpcResponse json
|
||||
let rpc_response: String;
|
||||
match response {
|
||||
Err(response) => {
|
||||
let resp = RpcResponse {
|
||||
id: request.id,
|
||||
jsonrpc: String::from("2.0"),
|
||||
method: request.method,
|
||||
result: None,
|
||||
error: Some(response),
|
||||
};
|
||||
rpc_response = serde_json::to_string(&resp).unwrap();
|
||||
}
|
||||
Ok(response) => {
|
||||
let resp = RpcResponse {
|
||||
id: request.id,
|
||||
jsonrpc: String::from("2.0"),
|
||||
method: request.method,
|
||||
result: Some(response),
|
||||
error: None,
|
||||
};
|
||||
rpc_response = serde_json::to_string(&resp).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
// Send the reply
|
||||
workers_l[num].write_message(rpc_response);
|
||||
let resp = match response {
|
||||
Err(response) => RpcResponse {
|
||||
id: id,
|
||||
jsonrpc: String::from("2.0"),
|
||||
method: request.method,
|
||||
result: None,
|
||||
error: Some(response),
|
||||
},
|
||||
Ok(response) => RpcResponse {
|
||||
id: id,
|
||||
jsonrpc: String::from("2.0"),
|
||||
method: request.method,
|
||||
result: Some(response),
|
||||
error: None,
|
||||
},
|
||||
};
|
||||
if let Ok(rpc_response) = serde_json::to_string(&resp) {
|
||||
// Send the reply
|
||||
workers_l[num].write_message(rpc_response);
|
||||
} else {
|
||||
warn!("handle_rpc_requests: failed responding to {:?}", request.id);
|
||||
};
|
||||
}
|
||||
None => {} // No message for us from this worker
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue