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: add conditions api #5

Merged
merged 20 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
images:
bash ./scripts/build_local_images.sh

module:
bash ./scripts/build_local_modules.sh

docker: images module

start: docker
cargo run --bin commit-boost -- init --config config.example.toml
cargo run --bin commit-boost -- start --docker "./cb.docker-compose.yml" --env "./.cb.env"

stop:
cargo run --bin commit-boost -- stop
8 changes: 6 additions & 2 deletions bin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ pub mod prelude {
pub use cb_common::{
commit,
commit::request::SignRequest,
config::{load_builder_module_config, load_commit_module_config, StartCommitModuleConfig},
pbs::{BuilderEvent, BuilderEventClient, OnBuilderApiEvent},
config::{
load_builder_module_config, load_commit_module_config, load_preconf_module_config,
StartCommitModuleConfig, StartPreconfModuleConfig,
},
pbs::{BuilderEvent, BuilderEventClient, OnBuilderApiEvent, RelayEntry},
types::Chain,
utils::{initialize_tracing_log, utcnow_ms, utcnow_ns, utcnow_sec, utcnow_us},
};
pub use cb_metrics::provider::MetricsProvider;
Expand Down
23 changes: 8 additions & 15 deletions config.example.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
chain = "Holesky"
chain = "Custom"

[pbs]
port = 18550
Expand All @@ -12,30 +12,23 @@ min_bid_eth = 0.0
late_in_slot_time_ms = 2000

[[relays]]
id = "example-relay"
url = "http://0xa1cec75a3f0661e99299274182938151e8433c61a19222347ea1313d839229cb4ce4e3e5aa2bdeb71c8fcf1b084963c2@abc.xyz"
headers = { X-MyCustomHeader = "MyCustomValue" }
id = "preconf-relay"
url = "http://0xa9e9cff900de07e295a044789fd4bdb6785eb0651ad282f9e76d12afd87e75180bdd64caf2e315b815d7322bd31ab48a@host.docker.internal:4040"
enable_timing_games = false
target_first_request_ms = 200
frequency_get_header_ms = 300

[signer]
[signer.loader]
key_path = "./keys.example.json"
# keys_path = ""
# secrets_path = ""

[metrics]
prometheus_config = "./docker/prometheus.yml"
use_grafana = true

[[modules]]
id = "DA_COMMIT"
type = "commit"
docker_image = "test_da_commit"
sleep_secs = 5

[[modules]]
id = "BUILDER_LOG"
type = "events"
docker_image = "test_builder_log"
id = "PRECONF"
type = "preconf"
docker_image = "test_preconf"
beacon_nodes = ["http://host.docker.internal:33001"]
chain_id = 3151908
55 changes: 50 additions & 5 deletions crates/cli/src/docker_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::{path::Path, vec};
use cb_common::{
config::{
CommitBoostConfig, ModuleKind, BUILDER_SERVER_ENV, CB_CONFIG_ENV, CB_CONFIG_NAME, JWTS_ENV,
METRICS_SERVER_ENV, MODULE_ID_ENV, MODULE_JWT_ENV, SIGNER_DIR_KEYS, SIGNER_DIR_KEYS_ENV,
SIGNER_DIR_SECRETS, SIGNER_DIR_SECRETS_ENV, SIGNER_KEYS, SIGNER_KEYS_ENV,
SIGNER_SERVER_ENV,
METRICS_SERVER_ENV, MODULE_ID_ENV, MODULE_JWT_ENV, PRECONF_SERVER_ENV, SIGNER_DIR_KEYS,
SIGNER_DIR_KEYS_ENV, SIGNER_DIR_SECRETS, SIGNER_DIR_SECRETS_ENV, SIGNER_KEYS,
SIGNER_KEYS_ENV, SIGNER_SERVER_ENV,
},
loader::SignerLoader,
utils::random_jwt,
Expand All @@ -18,7 +18,7 @@ use eyre::Result;
use indexmap::IndexMap;
use serde::Serialize;

pub(super) const CB_CONFIG_FILE: &str = "cb-config.toml";
pub(super) const CB_CONFIG_FILE: &str = "config.example.toml";
pub(super) const CB_COMPOSE_FILE: &str = "cb.docker-compose.yml";
pub(super) const CB_ENV_FILE: &str = ".cb.env";
pub(super) const CB_TARGETS_FILE: &str = "targets.json"; // needs to match prometheus.yml
Expand Down Expand Up @@ -57,6 +57,8 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()>
let builder_events_port = 30000;
let mut builder_events_modules = Vec::new();

let preconf_server_port = 40000;

// setup pbs service
targets.push(PrometheusTargetConfig {
targets: vec![format!("cb_pbs:{metrics_port}")],
Expand Down Expand Up @@ -137,6 +139,42 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()>
..Service::default()
}
}
ModuleKind::Preconf => {
needs_signer_module = true;

let jwt = random_jwt();
let jwt_name = format!("CB_JWT_{}", module.id.to_uppercase());

// module ids are assumed unique, so envs dont override each other
let module_envs = IndexMap::from([
get_env_val(MODULE_ID_ENV, &module.id),
get_env_same(CB_CONFIG_ENV),
get_env_interp(MODULE_JWT_ENV, &jwt_name),
get_env_val(METRICS_SERVER_ENV, &metrics_port.to_string()),
get_env_val(SIGNER_SERVER_ENV, &signer_server),
get_env_val(PRECONF_SERVER_ENV, &preconf_server_port.to_string()),
]);

envs.insert(jwt_name.clone(), jwt.clone());
jwts.insert(module.id.clone(), jwt);

Service {
container_name: Some(module_cid.clone()),
image: Some(module.docker_image),
ports: Ports::Short(vec![format!(
"{}:{}",
preconf_server_port, preconf_server_port
)]),
networks: Networks::Simple(vec![
METRICS_NETWORK.to_owned(),
SIGNER_NETWORK.to_owned(),
]),
volumes: vec![config_volume.clone()],
environment: Environment::KvPair(module_envs),
depends_on: DependsOnOptions::Simple(vec!["cb_signer".to_owned()]),
..Service::default()
}
}
};

services.insert(module_cid, Some(module_service));
Expand Down Expand Up @@ -288,7 +326,14 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()>
networks: Networks::Simple(vec![METRICS_NETWORK.to_owned()]),
depends_on: DependsOnOptions::Simple(vec!["cb_prometheus".to_owned()]),
environment: Environment::List(vec!["GF_SECURITY_ADMIN_PASSWORD=admin".to_owned()]),
volumes: vec![Volumes::Simple("./grafana/dashboards:/etc/grafana/provisioning/dashboards".to_owned()), Volumes::Simple("./grafana/datasources:/etc/grafana/provisioning/datasources".to_owned())],
volumes: vec![
Volumes::Simple(
"./grafana/dashboards:/etc/grafana/provisioning/dashboards".to_owned(),
),
Volumes::Simple(
"./grafana/datasources:/etc/grafana/provisioning/datasources".to_owned(),
),
],
// TODO: re-enable logging here once we move away from docker logs
logging: Some(LoggingParameters { driver: Some("none".to_owned()), options: None }),
..Service::default()
Expand Down
3 changes: 2 additions & 1 deletion crates/common/src/config/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ pub const MODULE_JWT_ENV: &str = "CB_SIGNER_JWT";
pub const METRICS_SERVER_ENV: &str = "METRICS_SERVER";
pub const SIGNER_SERVER_ENV: &str = "SIGNER_SERVER";
pub const BUILDER_SERVER_ENV: &str = "BUILDER_SERVER";
pub const PRECONF_SERVER_ENV: &str = "PRECONF_SERVER";

pub const CB_CONFIG_ENV: &str = "CB_CONFIG";
pub const CB_CONFIG_NAME: &str = "/cb-config.toml";
pub const CB_CONFIG_NAME: &str = "/config.example.toml";

pub const SIGNER_KEYS_ENV: &str = "CB_SIGNER_FILE";
pub const SIGNER_KEYS: &str = "/keys.json";
Expand Down
83 changes: 81 additions & 2 deletions crates/common/src/config/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@ use toml::Table;
use crate::{
commit::client::SignerClient,
config::{
constants::{CB_CONFIG_ENV, MODULE_ID_ENV, MODULE_JWT_ENV, SIGNER_SERVER_ENV},
constants::{
CB_CONFIG_ENV, MODULE_ID_ENV, MODULE_JWT_ENV, PRECONF_SERVER_ENV, SIGNER_SERVER_ENV,
},
load_env_var,
utils::load_file_from_env,
BUILDER_SERVER_ENV,
CommitBoostConfig, BUILDER_SERVER_ENV,
},
pbs::RelayEntry,
types::Chain,
};

#[derive(Debug, Deserialize, Serialize)]
pub enum ModuleKind {
#[serde(alias = "commit")]
Commit,
#[serde(alias = "preconf")]
Preconf,
#[serde(alias = "events")]
Events,
}
Expand Down Expand Up @@ -174,3 +179,77 @@ pub fn load_builder_module_config<T: DeserializeOwned>() -> eyre::Result<StartBu
extra: module_config.extra,
})
}

/// Runtime config to start a module
#[derive(Debug, Clone)]
pub struct StartPreconfModuleConfig<T = ()> {
pub id: String,
pub chain: Chain,
pub signer_client: SignerClient,
pub server_port: u16,
pub relays: Vec<RelayEntry>,
pub extra: T,
}

pub fn load_preconf_module_config<T: DeserializeOwned>() -> Result<StartPreconfModuleConfig<T>> {
let module_id = load_env_var(MODULE_ID_ENV)?;
let module_jwt = load_env_var(MODULE_JWT_ENV)?;
let signer_server_address = load_env_var(SIGNER_SERVER_ENV)?;
let preconf_requests_port: u16 = load_env_var(PRECONF_SERVER_ENV)?.parse()?;

#[derive(Debug, Deserialize)]
struct ThisModuleConfig<U> {
#[serde(flatten)]
static_config: StaticModuleConfig,
#[serde(flatten)]
extra: U,
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum ThisModule<U> {
Target(ThisModuleConfig<U>),
#[allow(dead_code)]
Other(Table),
}

#[derive(Deserialize, Debug)]
struct StubConfig<U> {
chain: Chain,
modules: Vec<ThisModule<U>>,
}

// load module config including the extra data (if any)
let config: StubConfig<T> = load_file_from_env(CB_CONFIG_ENV)?;

// find all matching modules config
let matches: Vec<ThisModuleConfig<T>> = config
.modules
.into_iter()
.filter_map(|m| match m {
ThisModule::Target(config) => Some(config),
_ => None,
})
.collect();

eyre::ensure!(!matches.is_empty(), "Failed to find matching config type");

let module_config = matches
.into_iter()
.find(|m| m.static_config.id == module_id)
.wrap_err(format!("failed to find module for {module_id}"))?;

let signer_client = SignerClient::new(signer_server_address, &module_jwt)?;

let pbs_config = CommitBoostConfig::from_env_path()?;
let relays = pbs_config.relays.into_iter().map(|r| r.entry).collect::<Vec<RelayEntry>>();

Ok(StartPreconfModuleConfig {
id: module_config.static_config.id,
chain: config.chain,
signer_client,
server_port: preconf_requests_port,
extra: module_config.extra,
relays,
})
}
8 changes: 8 additions & 0 deletions crates/common/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,11 @@ pub const HELDER_BUILDER_DOMAIN: [u8; 32] = [
15, 75, 232, 122, 96, 208, 102, 76, 201, 209, 255,
];
pub const HELDER_GENESIS_TIME_SECONDS: u64 = 1718967660;

// CUSTOM (LimeChain)
pub const CUSTOM_FORK_VERSION: [u8; 4] = [80, 0, 0, 56];

pub const CUSTOM_BUILDER_DOMAIN: [u8; 32] = [0, 0, 0, 1, 11, 65, 190, 76, 219, 52, 209, 131, 221,
220, 165, 57, 131, 55, 98, 109, 205, 207, 175, 23, 32, 193, 32, 45, 59, 149, 248, 78
];
pub const CUSTOM_GENESIS_TIME_SECONDS: u64 = 1724410824;
5 changes: 5 additions & 0 deletions crates/common/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::constants::{
HOLESKY_BUILDER_DOMAIN, HOLESKY_FORK_VERSION, HOLESKY_GENESIS_TIME_SECONDS,
MAINNET_BUILDER_DOMAIN, MAINNET_FORK_VERSION, MAINNET_GENESIS_TIME_SECONDS,
RHEA_BUILDER_DOMAIN, RHEA_FORK_VERSION, RHEA_GENESIS_TIME_SECONDS,
CUSTOM_BUILDER_DOMAIN, CUSTOM_FORK_VERSION, CUSTOM_GENESIS_TIME_SECONDS,
};

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
Expand All @@ -13,6 +14,7 @@ pub enum Chain {
Holesky,
Rhea,
Helder,
Custom,
}

impl Chain {
Expand All @@ -22,6 +24,7 @@ impl Chain {
Chain::Holesky => HOLESKY_BUILDER_DOMAIN,
Chain::Rhea => RHEA_BUILDER_DOMAIN,
Chain::Helder => HELDER_BUILDER_DOMAIN,
Chain::Custom => CUSTOM_BUILDER_DOMAIN,
}
}

Expand All @@ -31,6 +34,7 @@ impl Chain {
Chain::Holesky => HOLESKY_FORK_VERSION,
Chain::Rhea => RHEA_FORK_VERSION,
Chain::Helder => HELDER_FORK_VERSION,
Chain::Custom => CUSTOM_FORK_VERSION,
}
}

Expand All @@ -40,6 +44,7 @@ impl Chain {
Chain::Holesky => HOLESKY_GENESIS_TIME_SECONDS,
Chain::Rhea => RHEA_GENESIS_TIME_SECONDS,
Chain::Helder => HELDER_GENESIS_TIME_SECONDS,
Chain::Custom => CUSTOM_GENESIS_TIME_SECONDS,
}
}
}
22 changes: 22 additions & 0 deletions examples/preconf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,43 @@ rust-version.workspace = true
[dependencies]
commit-boost = { path = "../../bin" }

# ethereum
alloy = { workspace = true, features = ["ssz"] }
ethereum-types.workspace = true
ethereum_serde_utils.workspace = true
ethereum_ssz_derive.workspace = true
ethereum_ssz.workspace = true
ssz_rs = "0.9.0"
ssz_types.workspace = true
ethereum-consensus = { git = "https://github.com/ralexstokes/ethereum-consensus", rev = "cf3c404043230559660810bc0c9d6d5a8498d819" }

# async / threads
tokio.workspace = true
futures.workspace = true

# networking
axum.workspace = true
reqwest.workspace = true
reqwest-eventsource = "0.6.0"

# serialization
serde_json.workspace = true
serde.workspace = true
serde_with = "3.3.0"

# telemetry
tracing.workspace = true
prometheus.workspace = true

# crypto
tree_hash.workspace = true
tree_hash_derive.workspace = true

# misc
thiserror.workspace = true
eyre.workspace = true
rand.workspace = true
url.workspace = true
color-eyre.workspace = true
lazy_static.workspace = true
bincode = "1.3.3"
4 changes: 2 additions & 2 deletions examples/preconf/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ COPY . .
RUN cargo build --release --bin preconf


FROM ubuntu AS runtime
FROM ubuntu:22.04 AS runtime
WORKDIR /app

RUN apt-get update
RUN apt-get install -y openssl ca-certificates libssl3 libssl-dev
RUN apt-get install -y openssl ca-certificates libssl3 libssl-dev && rm -rf /var/lib/apt/lists/*

COPY --from=builder /app/target/release/preconf /usr/local/bin
ENTRYPOINT ["/usr/local/bin/preconf"]
Expand Down
Loading
Loading