Skip to content

Commit

Permalink
clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-maron committed Feb 14, 2024
1 parent a8588fd commit aed7e76
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
11 changes: 7 additions & 4 deletions broker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ impl<BrokerScheme: SignatureScheme, UserScheme: SignatureScheme> Broker<BrokerSc
/// - The heartbeat (Discovery) task
/// - The user connection handler
/// - The broker connection handler
/// - If time went backwards :(
pub async fn start(self) -> Result<()> {
// Spawn the heartbeat task, which we use to register with `Discovery` every so often.
// We also use it to check for new brokers who may have joined.
Expand Down Expand Up @@ -248,10 +249,12 @@ impl<BrokerScheme: SignatureScheme, UserScheme: SignatureScheme> Broker<BrokerSc
if let Some(metrics_bind_address) = self.metrics_bind_address {
// Set that we are running for timekeeping purposes
RUNNING_SINCE.set(
SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("time went backwards")
.as_secs() as i64,
bail!(
SystemTime::now().duration_since(UNIX_EPOCH),
Time,
"time went backwards"
)
.as_secs() as i64,
);

// Spawn the serving task
Expand Down
37 changes: 15 additions & 22 deletions proto/src/crypto/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ pub trait SignatureScheme: Send + Sync + Clone + 'static {
type PublicKey: Serializable + Eq + Send + Sync;

/// Sign a message using a private key
///
///
/// # Errors
/// If signing fails
fn sign(private_key: &Self::PrivateKey, message: &[u8]) -> Result<Vec<u8>>;

/// Verify a message with the public key, the message itself, and the signature.
///
///
/// # Returns
/// - false if verification failed
/// - true if verification succeeded
Expand All @@ -34,13 +34,13 @@ pub trait SignatureScheme: Send + Sync + Clone + 'static {
/// Allows for us to be generic over a serializable [signature | public key].
pub trait Serializable: Sized {
/// Serialize `Self` to a `Vec<u8>`.
///
///
/// # Errors
/// - If serialization fails
fn serialize(&self) -> Result<Vec<u8>>;

/// Deserialize `Self` from a `Vec<u8>`.
///
///
/// # Errors
/// - If deserialization fails
fn deserialize(serialized: &[u8]) -> Result<Self>;
Expand All @@ -56,11 +56,10 @@ pub struct KeyPair<Scheme: SignatureScheme> {
pub private_key: Scheme::PrivateKey,
}


/// An example implementation of `Serializable` for Jellyfish's `bls_over_bn254`.
impl Serializable for jf_primitives::signatures::bls_over_bn254::VerKey {
/// Serialize `Self` using `ark-serialize` (uncompressed)
///
///
/// # Errors
/// - If serialization fails
fn serialize(&self) -> Result<Vec<u8>> {
Expand All @@ -70,7 +69,7 @@ impl Serializable for jf_primitives::signatures::bls_over_bn254::VerKey {
}

/// Deserialize `Self` using `ark-serialize` (uncompressed)
///
///
/// # Errors
/// - If deserialization fails
fn deserialize(serialized: &[u8]) -> Result<Self> {
Expand All @@ -86,14 +85,14 @@ impl SignatureScheme for BLS {

/// Sign using the private key and the message. We have to serialize the signature
/// so we can return it as a `Vec<u8>`.
///
///
/// # Errors
/// - If serialization fails
/// - If signing fails
fn sign(private_key: &Self::PrivateKey, message: &[u8]) -> Result<Vec<u8>> {
// Sign the message
let serialized_signature =
<BLS as JfSignatureScheme>::sign(&(), private_key, message, &mut DeterministicRng(0))?;
<Self as JfSignatureScheme>::sign(&(), private_key, message, &mut DeterministicRng(0))?;

// Serialize the signature
let mut buf = vec![];
Expand All @@ -105,25 +104,19 @@ impl SignatureScheme for BLS {

/// Verify the signature using the public key and the message. We have to deserialize the signature
/// so we can return it as a `Vec<u8>`.
///
///
/// # Errors
/// - If signature deserialization fails
/// - If signing fails
fn verify(public_key: &Self::PublicKey, message: &[u8], signature: &[u8]) -> bool {
// Deserialize the signature
let signature =
match <BLS as JfSignatureScheme>::Signature::deserialize_uncompressed(signature) {
Ok(signature) => signature,
Err(_) => {
return false;
}
};
let Ok(signature) =
<Self as JfSignatureScheme>::Signature::deserialize_uncompressed(signature)
else {
return false;
};

// Verify the signature
match <BLS as JfSignatureScheme>::verify(&(), public_key, message, &signature) {
Ok(_) => true,
Err(_) => false,
}
<Self as JfSignatureScheme>::verify(&(), public_key, message, &signature).is_ok()
}
}

2 changes: 2 additions & 0 deletions proto/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub enum Error {
Parse(String),
/// A file-related (either read or write) error. An example is a failed read of a certificate file.
File(String),
/// A time-related error. An example is if time went backwards.
Time(String),
/// An error that is used to specify that a required task has exited.
Exited(String),
}
Expand Down
9 changes: 8 additions & 1 deletion proto/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use std::net::SocketAddr;

use tracing::error;
use warp::Filter;

/// Start the metrics server that should run forever on a particular port
Expand All @@ -12,7 +13,13 @@ pub async fn serve_metrics(bind_address: SocketAddr) {
let encoder = prometheus::TextEncoder::new();
let metric_families = prometheus::gather();

encoder.encode_to_string(&metric_families).unwrap()
match encoder.encode_to_string(&metric_families) {
Ok(metrics) => metrics,
Err(err) => {
error!("failed to encode metrics: {err}");
"failed to encode metrics".to_string()
}
}
});

// Serve the route on the specified port
Expand Down

0 comments on commit aed7e76

Please sign in to comment.