Skip to content

Commit

Permalink
Merge pull request #387 from chainbound/lore/feat/metadata-endpoint
Browse files Browse the repository at this point in the history
feat(sidecar): metadata endpoint
  • Loading branch information
thedevbirb authored Nov 14, 2024
2 parents 90c2ee8 + 2634355 commit 6face34
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 16 deletions.
11 changes: 10 additions & 1 deletion bolt-sidecar/src/api/commitments/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use super::{
jsonrpc::{JsonPayload, JsonResponse},
server::CommitmentsApiInner,
spec::{
CommitmentError, CommitmentsApi, RejectionError, GET_VERSION_METHOD,
CommitmentError, CommitmentsApi, RejectionError, GET_METADATA_METHOD, GET_VERSION_METHOD,
REQUEST_INCLUSION_METHOD,
},
};
Expand All @@ -45,6 +45,15 @@ pub async fn rpc_entrypoint(
}))
}

GET_METADATA_METHOD => {
let response = JsonResponse {
id: payload.id,
result: serde_json::to_value(api.limits()).expect("infallible"),
..Default::default()
};
Ok(Json(response))
}

REQUEST_INCLUSION_METHOD => {
// Validate the authentication header and extract the signer and signature
let (signer, signature) = auth_from_headers(&headers).inspect_err(|e| {
Expand Down
64 changes: 52 additions & 12 deletions bolt-sidecar/src/api/commitments/server.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::{
collections::HashSet,
fmt,
future::Future,
net::{SocketAddr, ToSocketAddrs},
pin::Pin,
sync::Arc,
};

use alloy::primitives::Address;
use axum::{
middleware,
routing::{get, post},
Expand All @@ -22,6 +20,7 @@ use tracing::{error, info};

use crate::{
api::commitments::handlers,
config::limits::LimitsOpts,
primitives::{
commitment::{InclusionCommitment, SignedCommitment},
CommitmentRequest, InclusionRequest,
Expand Down Expand Up @@ -49,15 +48,19 @@ pub struct CommitmentEvent {
pub struct CommitmentsApiInner {
/// Event notification channel
events: mpsc::Sender<CommitmentEvent>,
/// Optional whitelist of ECDSA public keys
#[allow(unused)]
whitelist: Option<HashSet<Address>>,
/// The sidecar's operating limits that should be exposed in a metadata endpoint
limits: LimitsOpts,
}

impl CommitmentsApiInner {
/// Create a new API server with an optional whitelist of ECDSA public keys.
pub fn new(events: mpsc::Sender<CommitmentEvent>) -> Self {
Self { events, whitelist: None }
/// Creates a new instance of the commitments API handler.
pub fn new(events: mpsc::Sender<CommitmentEvent>, limits: LimitsOpts) -> Self {
Self { events, limits }
}

/// Returns the operating limits for the sidecar.
pub fn limits(&self) -> LimitsOpts {
self.limits
}
}

Expand Down Expand Up @@ -119,8 +122,8 @@ impl CommitmentsApiServer {
}

/// Runs the JSON-RPC server, sending events to the provided channel.
pub async fn run(&mut self, events_tx: mpsc::Sender<CommitmentEvent>) {
let api = Arc::new(CommitmentsApiInner::new(events_tx));
pub async fn run(&mut self, events_tx: mpsc::Sender<CommitmentEvent>, limits: LimitsOpts) {
let api = Arc::new(CommitmentsApiInner::new(events_tx, limits));

let router = make_router(api);

Expand Down Expand Up @@ -188,7 +191,7 @@ mod test {

let (events_tx, _) = mpsc::channel(1);

server.run(events_tx).await;
server.run(events_tx, LimitsOpts::default()).await;
let addr = server.local_addr();

let sk = SecretKey::random(&mut rand::thread_rng());
Expand Down Expand Up @@ -230,7 +233,7 @@ mod test {

let (events_tx, mut events) = mpsc::channel(1);

server.run(events_tx).await;
server.run(events_tx, LimitsOpts::default()).await;
let addr = server.local_addr();

let sk = SecretKey::random(&mut rand::thread_rng());
Expand Down Expand Up @@ -281,4 +284,41 @@ mod test {

rx.await.unwrap();
}

#[tokio::test]
async fn test_request_metadata() {
let _ = tracing_subscriber::fmt::try_init();

let mut server = CommitmentsApiServer::new("0.0.0.0:0");

let (events_tx, _) = mpsc::channel(1);

server.run(events_tx, LimitsOpts::default()).await;
let addr = server.local_addr();

let payload = json!({
"jsonrpc": "2.0",
"id": 1,
"method": "bolt_metadata",
"params": []
});

let url = format!("http://{addr}");

let client = reqwest::Client::new();

let response = client
.post(url)
.json(&payload)
.send()
.await
.unwrap()
.json::<JsonResponse>()
.await
.unwrap();

let limits: LimitsOpts = serde_json::from_value(response.result).unwrap();

assert_eq!(limits, LimitsOpts::default());
}
}
2 changes: 2 additions & 0 deletions bolt-sidecar/src/api/commitments/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub(super) const GET_VERSION_METHOD: &str = "bolt_getVersion";

pub(super) const REQUEST_INCLUSION_METHOD: &str = "bolt_requestInclusion";

pub(super) const GET_METADATA_METHOD: &str = "bolt_metadata";

pub(super) const MAX_REQUEST_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(6);

/// Error type for the commitments API.
Expand Down
4 changes: 2 additions & 2 deletions bolt-sidecar/src/config/limits.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::num::NonZero;

use clap::Parser;
use serde::Deserialize;

/// Default max commitments to accept per block.
pub const DEFAULT_MAX_COMMITMENTS: usize = 128;
Expand All @@ -13,7 +12,8 @@ pub const DEFAULT_MAX_COMMITTED_GAS: u64 = 10_000_000;
pub const DEFAULT_MIN_PRIORITY_FEE: u128 = 1_000_000_000; // 1 Gwei

/// Limits for the sidecar.
#[derive(Debug, Parser, Clone, Copy, Deserialize)]
#[cfg_attr(test, derive(PartialEq))]
#[derive(Debug, Parser, Clone, Copy, serde::Serialize, serde::Deserialize)]
pub struct LimitsOpts {
/// Max number of commitments to accept per block
#[clap(
Expand Down
2 changes: 1 addition & 1 deletion bolt-sidecar/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl<C: StateFetcher, ECDSA: SignerECDSA> SidecarDriver<C, ECDSA> {
// start the commitments api server
let api_addr = format!("0.0.0.0:{}", opts.port);
let (api_events_tx, api_events_rx) = mpsc::channel(1024);
CommitmentsApiServer::new(api_addr).run(api_events_tx).await;
CommitmentsApiServer::new(api_addr).run(api_events_tx, opts.limits).await;

let unsafe_skip_consensus_checks = opts.unsafe_disable_consensus_checks;

Expand Down

0 comments on commit 6face34

Please sign in to comment.