Skip to content

Commit

Permalink
refactor(sidecar): error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
thedevbirb committed Oct 11, 2024
1 parent 1de7d09 commit b60efa9
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 25 deletions.
15 changes: 10 additions & 5 deletions bolt-sidecar/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
CommitmentRequest, ConstraintsMessage, FetchPayloadRequest, LocalPayloadFetcher,
SignedConstraints, TransactionExt,
},
signer::{keystore::KeystoreSigner, local::LocalSigner},
signer::{keystore::KeystoreSigner, local::LocalSigner, SignerError},
start_builder_proxy_server,
state::{fetcher::StateFetcher, ConsensusState, ExecutionState, HeadTracker, StateClient},
telemetry::ApiMetrics,
Expand Down Expand Up @@ -266,12 +266,17 @@ impl<C: StateFetcher, ECDSA: SignerECDSA> SidecarDriver<C, ECDSA> {
let digest = message.digest();

let signature = match self.constraint_signer {
SignerBLS::Local(ref signer) => signer.sign_commit_boost_root(digest),
SignerBLS::CommitBoost(ref signer) => signer.sign_commit_boost_root(digest).await,
SignerBLS::Keystore(ref signer) => {
signer.sign_commit_boost_root(digest, cl_public_key_to_arr(pubkey.clone()))
SignerBLS::Local(ref signer) => {
signer.sign_commit_boost_root(digest).map_err(SignerError::LocalSigner)
}
SignerBLS::CommitBoost(ref signer) => {
signer.sign_commit_boost_root(digest).await.map_err(SignerError::CommitBoost)
}
SignerBLS::Keystore(ref signer) => signer
.sign_commit_boost_root(digest, cl_public_key_to_arr(pubkey.clone()))
.map_err(SignerError::Keystore),
};

let signed_constraints = match signature {
Ok(signature) => SignedConstraints { message, signature },
Err(e) => {
Expand Down
16 changes: 10 additions & 6 deletions bolt-sidecar/src/signer/commit_boost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ use std::{str::FromStr, sync::Arc};

use alloy::{rpc::types::beacon::BlsSignature, signers::Signature};
use cb_common::{
commit::{client::SignerClient, request::SignConsensusRequest},
commit::{client::SignerClient, error::SignerClientError, request::SignConsensusRequest},
signer::EcdsaPublicKey,
};
use commit_boost::prelude::SignProxyRequest;
use ethereum_consensus::crypto::bls::PublicKey as BlsPublicKey;
use eyre::ErrReport;
use parking_lot::RwLock;
use thiserror::Error;
use tracing::{debug, error, info};
Expand All @@ -31,14 +30,19 @@ pub enum CommitBoostError {
#[error("failed to sign constraint: {0}")]
NoSignature(String),
#[error("failed to create signer client: {0}")]
SignerClientError(#[from] ErrReport),
SignerClientError(#[from] SignerClientError),
#[error("error in commit boost signer: {0}")]
Other(#[from] eyre::Report),
}

type Result<T> = std::result::Result<T, CommitBoostError>;

#[allow(unused)]
impl CommitBoostSigner {
/// Create a new [CommitBoostSigner] instance
pub async fn new(signer_server_address: String, jwt: &str) -> Result<Self, CommitBoostError> {
let signer_client = SignerClient::new(signer_server_address, jwt)?;
pub async fn new(signer_server_address: String, jwt: &str) -> Result<Self> {
let signer_client =
SignerClient::new(signer_server_address, jwt).map_err(CommitBoostError::Other)?;

let client = Self {
signer_client,
Expand Down Expand Up @@ -117,7 +121,7 @@ impl CommitBoostSigner {
}

/// Sign an object root with the Commit Boost domain.
pub async fn sign_commit_boost_root(&self, data: [u8; 32]) -> eyre::Result<BlsSignature> {
pub async fn sign_commit_boost_root(&self, data: [u8; 32]) -> Result<BlsSignature> {
// convert the pubkey from ethereum_consensus to commit-boost format
let pubkey = cb_common::signer::BlsPublicKey::from(
alloy::rpc::types::beacon::BlsPublicKey::from_slice(self.pubkey().as_ref()),
Expand Down
4 changes: 2 additions & 2 deletions bolt-sidecar/src/signer/keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ fn keystore_paths(keys_path: Option<&str>) -> Result<Vec<PathBuf>> {

let mut keystores_paths = vec![];
// Iter over the `keys` directory
for entry in fs::read_dir(keys_path)? {
let path = entry?.path();
for entry in fs::read_dir(keys_path).map_err(KeystoreError::ReadFromDirectory)? {
let path = entry.map_err(KeystoreError::ReadFromDirectory)?.path();
if path.is_dir() {
for entry in fs::read_dir(path)? {
let path = entry?.path();
Expand Down
28 changes: 17 additions & 11 deletions bolt-sidecar/src/signer/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ pub use blst::min_pk::SecretKey;
/// The BLS Domain Separator used in Ethereum 2.0.
pub const BLS_DST_PREFIX: &[u8] = b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_";

#[derive(Debug, thiserror::Error)]
pub enum LocalSignerError {
#[error("Failed to compute signing root: {0}")]
SigningRootComputation(#[from] ethereum_consensus::error::Error),
#[error("Invalid signature: {0}")]
InvalidSignature(String),
}

type Result<T> = std::result::Result<T, LocalSignerError>;

/// A BLS signer that can sign any type that implements the [`SignableBLS`] trait.
#[derive(Clone)]
pub struct LocalSigner {
Expand Down Expand Up @@ -38,17 +48,17 @@ impl LocalSigner {
}

/// Sign an SSZ object root with the Application Builder domain.
pub fn sign_application_builder_root(&self, root: [u8; 32]) -> eyre::Result<BLSSig> {
pub fn sign_application_builder_root(&self, root: [u8; 32]) -> Result<BLSSig> {
self.sign_root(root, self.chain.application_builder_domain())
}

/// Sign an SSZ object root with the Commit Boost domain.
pub fn sign_commit_boost_root(&self, root: [u8; 32]) -> eyre::Result<BLSSig> {
pub fn sign_commit_boost_root(&self, root: [u8; 32]) -> Result<BLSSig> {
self.sign_root(root, self.chain.commit_boost_domain())
}

/// Sign an SSZ object root with the given domain.
pub fn sign_root(&self, root: [u8; 32], domain: [u8; 32]) -> eyre::Result<BLSSig> {
pub fn sign_root(&self, root: [u8; 32], domain: [u8; 32]) -> Result<BLSSig> {
let signing_root = compute_signing_root(&root, domain)?;
let sig = self.key.sign(signing_root.as_slice(), BLS_DST_PREFIX, &[]);
Ok(BLSSig::from_slice(&sig.to_bytes()))
Expand All @@ -59,16 +69,12 @@ impl LocalSigner {
&self,
root: [u8; 32],
signature: &Signature,
) -> eyre::Result<()> {
) -> Result<()> {
self.verify_root(root, signature, self.chain.application_builder_domain())
}

/// Verify the signature with the public key of the signer using the Commit Boost domain.
pub fn verify_commit_boost_root(
&self,
root: [u8; 32],
signature: &Signature,
) -> eyre::Result<()> {
pub fn verify_commit_boost_root(&self, root: [u8; 32], signature: &Signature) -> Result<()> {
self.verify_root(root, signature, self.chain.commit_boost_domain())
}

Expand All @@ -78,15 +84,15 @@ impl LocalSigner {
root: [u8; 32],
signature: &Signature,
domain: [u8; 32],
) -> eyre::Result<()> {
) -> Result<()> {
let signing_root = compute_signing_root(&root, domain)?;
let pk = blst::min_pk::PublicKey::from_bytes(self.pubkey().as_ref()).unwrap();

let res = signature.verify(true, signing_root.as_ref(), BLS_DST_PREFIX, &[], &pk, true);
if res == BLST_ERROR::BLST_SUCCESS {
Ok(())
} else {
eyre::bail!(format!("Invalid signature: {:?}", res))
Err(LocalSignerError::InvalidSignature(format!("{res:?}")))
}
}
}
Expand Down
13 changes: 12 additions & 1 deletion bolt-sidecar/src/signer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use keystore::KeystoreSigner;
pub mod local;
use local::LocalSigner;

#[derive(Debug, Clone)]
/// Signer for BLS signatures.
#[derive(Debug, Clone)]
pub enum SignerBLS {
/// Local signer with a BLS secret key.
Local(LocalSigner),
Expand All @@ -17,3 +17,14 @@ pub enum SignerBLS {
/// Signer consisting of multiple keypairs loaded from ERC-2335 keystores files.
Keystore(KeystoreSigner),
}

/// Error in the signer.
#[derive(Debug, thiserror::Error)]
pub enum SignerError {
#[error("local signer error: {0}")]
LocalSigner(#[from] local::LocalSignerError),
#[error("commit boost signer error: {0}")]
CommitBoost(commit_boost::CommitBoostError),
#[error("keystore signer error: {0}")]
Keystore(keystore::KeystoreError),
}

0 comments on commit b60efa9

Please sign in to comment.