Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle rpc requests unwrap crash in stratumserver.rs #2446

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 31 additions & 28 deletions servers/src/mining/stratumserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,22 @@ impl StratumServer {
};

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 @@ -353,33 +359,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