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

feat(sidecar): configurable ValidatorIndex #101

Merged
merged 14 commits into from
Jul 3, 2024
54 changes: 52 additions & 2 deletions bolt-sidecar/bin/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use bolt_sidecar::{
BuilderProxyConfig, Config, MevBoostClient,
};

use beacon_api_client::ProposerDuty;
use tokio::sync::mpsc;
use tracing::info;

Expand Down Expand Up @@ -51,6 +52,8 @@ async fn main() -> eyre::Result<()> {
let (payload_tx, mut payload_rx) = mpsc::channel(16);
let payload_fetcher = LocalPayloadFetcher::new(payload_tx);

let validator_indexes = config.validator_indexes;

tokio::spawn(async move {
loop {
if let Err(e) =
Expand Down Expand Up @@ -89,9 +92,10 @@ async fn main() -> eyre::Result<()> {
"Validation against execution state passed"
);

let validator_index = find_validator_index_for_slot(&validator_indexes, &consensus_state.get_epoch().proposer_duties, request.slot);
merklefruit marked this conversation as resolved.
Show resolved Hide resolved

// parse the request into constraints and sign them with the sidecar signer
// TODO: get the validator index from somewhere
let message = ConstraintsMessage::build(0, request.slot, request.clone());
let message = ConstraintsMessage::build(validator_index, request.slot, request.clone());

let signature = signer.sign(&message.digest())?;
let signed_constraints: BatchedSignedConstraints =
Expand Down Expand Up @@ -138,3 +142,49 @@ async fn main() -> eyre::Result<()> {

Ok(())
}

/// Filters the proposer duties and returns the validator index for a given slot
/// if it doesn't exists then returns 0 by default.
pub fn find_validator_index_for_slot(
merklefruit marked this conversation as resolved.
Show resolved Hide resolved
validator_indexes: &[u64],
proposer_duties: &[ProposerDuty],
slot: u64,
) -> u64 {
for duty in proposer_duties {
if duty.slot == slot && validator_indexes.contains(&(duty.validator_index as u64)) {
return duty.validator_index as u64;
}
}
0
}
merklefruit marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(test)]
mod tests {
use crate::find_validator_index_for_slot;
use beacon_api_client::ProposerDuty;

#[test]
fn test_filter_index() {
let validator_indexes = vec![11, 22, 33];
let proposer_duties = vec![
ProposerDuty {
public_key: Default::default(),
slot: 1,
validator_index: 11,
},
ProposerDuty {
public_key: Default::default(),
slot: 2,
validator_index: 22,
},
ProposerDuty {
public_key: Default::default(),
slot: 3,
validator_index: 33,
},
];

let result = find_validator_index_for_slot(&validator_indexes, &proposer_duties, 2);
assert_eq!(result, 22);
}
}
25 changes: 23 additions & 2 deletions bolt-sidecar/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::crypto::bls::random_bls_secret;
use blst::min_pk::SecretKey;
use clap::{ArgGroup, Args, Parser};

use crate::crypto::bls::random_bls_secret;

merklefruit marked this conversation as resolved.
Show resolved Hide resolved
/// Command-line options for the sidecar
#[derive(Parser, Debug)]
pub struct Opts {
Expand All @@ -27,6 +26,9 @@ pub struct Opts {
/// Max commitments to accept per block
#[clap(short = 'm', long)]
pub(super) max_commitments: Option<usize>,
/// Validator indexes
#[clap(short = 'v', long)]
pub(super) validator_index: Option<String>,
thedevbirb marked this conversation as resolved.
Show resolved Hide resolved
/// Signing options
#[clap(flatten)]
pub(super) signing: SigningOpts,
Expand Down Expand Up @@ -68,6 +70,8 @@ pub struct Config {
pub mevboost_proxy_port: u16,
/// Limits for the sidecar
pub limits: Limits,
/// Validator indexes
pub validator_indexes: Vec<u64>,
}

impl Default for Config {
Expand All @@ -82,6 +86,7 @@ impl Default for Config {
private_key: Some(random_bls_secret()),
mevboost_proxy_port: 18551,
limits: Limits::default(),
validator_indexes: vec![0], // Default to 0 index
merklefruit marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down Expand Up @@ -127,10 +132,26 @@ impl TryFrom<Opts> for Config {
config.beacon_api_url = opts.beacon_api_url.trim_end_matches('/').to_string();
config.mevboost_url = opts.mevboost_url.trim_end_matches('/').to_string();

if let Some(validator_index) = opts.validator_index {
config.validator_indexes = parse_validator_indexes(&validator_index)?;
}

Ok(config)
}
}

/// Parse the validator indexes to seperate them by commas
fn parse_validator_indexes(input: &str) -> eyre::Result<Vec<u64>> {
input
.split(',')
.map(|s| {
s.trim()
.parse()
.map_err(|e| eyre::eyre!("Invalid index: {}: {:?}", s, e))
})
.collect()
}

/// Limits for the sidecar.
#[derive(Debug, Clone)]
pub struct Limits {
Expand Down
4 changes: 4 additions & 0 deletions bolt-sidecar/src/state/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ impl ConsensusState {
self.epoch.proposer_duties = duties.1;
Ok(())
}

pub fn get_epoch(&self) -> &Epoch {
&self.epoch
}
}

/// Get the current timestamp.
Expand Down
5 changes: 0 additions & 5 deletions bolt-sidecar/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ pub enum StateError {
Rpc(#[from] TransportError),
}

#[derive(Debug, Clone)]
struct ProposerDuties {
assigned_slots: Vec<u64>,
}

#[cfg(test)]
mod tests {
use alloy_consensus::constants::ETH_TO_WEI;
Expand Down