Skip to content

Commit

Permalink
chore: inefficient pubkey selection
Browse files Browse the repository at this point in the history
  • Loading branch information
merklefruit committed Oct 14, 2024
1 parent bd08ed5 commit 0940c46
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
4 changes: 2 additions & 2 deletions bolt-sidecar/src/client/constraints_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ impl ConstraintsClient {
}

/// Finds all delegations for the given public key.
pub fn find_delegatees(&self, pubkey: &BlsPublicKey) -> Vec<&BlsPublicKey> {
pub fn find_delegatees(&self, pubkey: &BlsPublicKey) -> Vec<BlsPublicKey> {
self.delegations
.iter()
.filter(|d| d.message.delegatee_pubkey == *pubkey)
.map(|d| &d.message.delegatee_pubkey)
.map(|d| d.message.delegatee_pubkey.clone())
.collect::<Vec<_>>()
}

Expand Down
41 changes: 29 additions & 12 deletions bolt-sidecar/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use alloy::{rpc::types::beacon::events::HeadEvent, signers::local::PrivateKeySig
use beacon_api_client::mainnet::Client as BeaconClient;
use ethereum_consensus::{
clock::{self, SlotStream, SystemTimeProvider},
crypto::bls::PublicKey as BlsPublicKey,
phase0::mainnet::SLOTS_PER_EPOCH,
};
use futures::StreamExt;
Expand Down Expand Up @@ -280,18 +281,8 @@ impl<C: StateFetcher, ECDSA: SignerECDSA> SidecarDriver<C, ECDSA> {
// Rationale:
// - If there are no delegatee keys, try to use the validator key directly if available.
// - If there are delegatee keys, try to use the first one that is available in the list.
let pubkey = if delegatees.is_empty() {
if available_pubkeys.contains(&validator_pubkey) {
validator_pubkey.clone()
} else {
error!(%target_slot, %validator_pubkey, "No authorized private key available to sign constraints");
let _ = response.send(Err(CommitmentError::Internal));
return;
}
} else if let Some(delegatee) = available_pubkeys.iter().find(|k| delegatees.contains(k)) {
delegatee.clone()
} else {
error!(%target_slot, "No delegatee key found, unable to sign constraints");
let Some(pubkey) = pick_public_key(validator_pubkey, available_pubkeys, delegatees) else {
error!(%target_slot, "No available public key to sign constraints with");
let _ = response.send(Err(CommitmentError::Internal));
return;
};
Expand Down Expand Up @@ -399,3 +390,29 @@ impl<C: StateFetcher, ECDSA: SignerECDSA> SidecarDriver<C, ECDSA> {
}
}
}

/// Pick a pubkey to sign constraints with.
///
/// Rationale:
/// - If there are no delegatee keys, try to use the validator key directly if available.
/// - If there are delegatee keys, try to use the first one that is available in the list.
fn pick_public_key(
validator: BlsPublicKey,
available: Vec<BlsPublicKey>,
delegatees: Vec<BlsPublicKey>,
) -> Option<BlsPublicKey> {
if delegatees.is_empty() {
if available.contains(&validator) {
return Some(validator);
} else {
return None;
}
} else {
for delegatee in delegatees {
if available.contains(&delegatee) {
return Some(delegatee);
}
}
}
None
}

0 comments on commit 0940c46

Please sign in to comment.