Skip to content

Commit

Permalink
Handle rpc requests unwrap crash in stratumserver.rs (mimblewimble#2446)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sesam authored and hashmap committed Jan 26, 2019
1 parent c8fd057 commit 92cbfa2
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions servers/src/mining/stratumserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 92cbfa2

Please sign in to comment.