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

Add /status endpoint to signer #4280

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions libsigner/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ pub enum SignerEvent {
SignerMessages(Vec<SignerMessage>),
/// A new block proposal validation response from the node
BlockValidationResponse(BlockValidateResponse),
/// Status endpoint request
StatusCheck,
}

/// Trait to implement a stop-signaler for the event receiver thread.
Expand Down Expand Up @@ -381,6 +383,14 @@ impl EventReceiver for SignerEventReceiver {
return Err(EventError::Terminated);
}
let request = http_server.recv()?;

if request.url() == "/status" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear, this is a POST not a GET, right? These are events that are POSTed by the node to the signer's event observer client.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this code it would respond to both GET and POST, though I have no problem keeping it to just GET. This is for the signer's event receiver, but the goal is to have a status endpoint for monitoring, which is why GET made sense.

This does "pollute" the event receiver, but the alternative is to have a separate endpoint/port to listen on for monitoring, if we want to have some kind of automated status check.

request
.respond(HttpResponse::from_string("OK"))
.expect("response failed");
return Ok(SignerEvent::StatusCheck);
}

if request.method() != &HttpMethod::Post {
return Err(EventError::MalformedRequest(format!(
"Unrecognized method '{}'",
Expand Down
20 changes: 19 additions & 1 deletion libsigner/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

mod http;

use std::io::Write;
use std::io::{Read, Write};
use std::net::{SocketAddr, TcpStream, ToSocketAddrs};
use std::sync::mpsc::{channel, Receiver, Sender};
use std::time::Duration;
Expand Down Expand Up @@ -138,6 +138,24 @@ fn test_simple_signer() {

num_sent += 1;
}
// Test the /status endpoint
{
let mut sock = match TcpStream::connect(endpoint) {
Ok(sock) => sock,
Err(..) => {
sleep_ms(100);
return;
}
};
let req = "GET /status HTTP/1.0\r\nConnection: close\r\n\r\n";
sock.write_all(req.as_bytes()).unwrap();
let mut buf = [0; 128];
sock.read(&mut buf).unwrap();
let res_str = std::str::from_utf8(&buf).unwrap();
let expected_status_res = "HTTP/1.0 200 OK\r\n";
assert_eq!(expected_status_res, &res_str[..expected_status_res.len()]);
sock.flush().unwrap();
}
});

let running_signer = signer.spawn(endpoint).unwrap();
Expand Down
3 changes: 3 additions & 0 deletions stacks-signer/src/runloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,9 @@ impl<C: Coordinator> SignerRunLoop<Vec<OperationResult>, RunLoopCommand> for Run
debug!("Received block proposals from the miners...");
self.handle_proposed_blocks(blocks);
}
Some(SignerEvent::StatusCheck) => {
debug!("Received a status check event.")
}
None => {
// No event. Do nothing.
debug!("No event received")
Expand Down