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:
Simon B 2019-01-26 22:21:11 +01:00 committed by hashmap
parent c8fd0575ed
commit 92cbfa20a6

View file

@ -305,16 +305,22 @@ impl StratumServer {
the_message.clear(); the_message.clear();
let mut stratum_stats = stratum_stats.write(); let mut stratum_stats = stratum_stats.write();
let worker_stats_id = stratum_stats let worker_stats_id = match stratum_stats
.worker_stats .worker_stats
.iter() .iter()
.position(|r| r.id == workers_l[num].id) .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(); stratum_stats.worker_stats[worker_stats_id].last_seen = SystemTime::now();
// Call the handler function for requested method // Call the handler function for requested method
let response = match request.method.as_str() { let response = match request.method.as_str() {
"login" => { "login" => {
if self.current_block_versions.is_empty() {
continue;
}
stratum_stats.worker_stats[worker_stats_id].initial_block_height = stratum_stats.worker_stats[worker_stats_id].initial_block_height =
self.current_block_versions.last().unwrap().header.height; self.current_block_versions.last().unwrap().header.height;
self.handle_login(request.params, &mut workers_l[num]) 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 // Package the reply as RpcResponse json
let rpc_response: String; let resp = match response {
match response { Err(response) => RpcResponse {
Err(response) => { id: id,
let resp = RpcResponse { jsonrpc: String::from("2.0"),
id: request.id, method: request.method,
jsonrpc: String::from("2.0"), result: None,
method: request.method, error: Some(response),
result: None, },
error: Some(response), Ok(response) => RpcResponse {
}; id: id,
rpc_response = serde_json::to_string(&resp).unwrap(); jsonrpc: String::from("2.0"),
} method: request.method,
Ok(response) => { result: Some(response),
let resp = RpcResponse { error: None,
id: request.id, },
jsonrpc: String::from("2.0"), };
method: request.method, if let Ok(rpc_response) = serde_json::to_string(&resp) {
result: Some(response), // Send the reply
error: None, workers_l[num].write_message(rpc_response);
}; } else {
rpc_response = serde_json::to_string(&resp).unwrap(); warn!("handle_rpc_requests: failed responding to {:?}", request.id);
} };
}
// Send the reply
workers_l[num].write_message(rpc_response);
} }
None => {} // No message for us from this worker None => {} // No message for us from this worker
} }