Skip to content

Commit ac0491c

Browse files
authored
chore: use bin base (#55)
* chore: use bin base * chore: clean up deps * fix: clippy * lint: clippy
1 parent ca67465 commit ac0491c

File tree

13 files changed

+104
-29
lines changed

13 files changed

+104
-29
lines changed

Cargo.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ name = "transaction-submitter"
2222
path = "bin/submit_transaction.rs"
2323

2424
[dependencies]
25+
init4-bin-base = "0.1.0"
26+
2527
zenith-types = "0.13"
2628

2729
alloy = { version = "0.7.3", features = ["full", "json-rpc", "signer-aws", "rpc-types-mev", "rlp"] }
28-
alloy-rlp = { version = "0.3.4" }
2930

3031
aws-config = "1.1.7"
3132
aws-sdk-kms = "1.15.0"
@@ -41,13 +42,9 @@ axum = "0.7.5"
4142
eyre = "0.6.12"
4243
openssl = { version = "0.10", features = ["vendored"] }
4344
reqwest = { version = "0.11.24", features = ["blocking", "json"] }
44-
ruint = "1.12.1"
4545
serde_json = "1.0"
4646
thiserror = "1.0.68"
4747
tokio = { version = "1.36.0", features = ["full", "macros", "rt-multi-thread"] }
48-
tracing-subscriber = "0.3.18"
4948

5049
async-trait = "0.1.80"
5150
oauth2 = "4.4.2"
52-
metrics = "0.24.1"
53-
metrics-exporter-prometheus = "0.16.0"

bin/builder.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,20 @@ use builder::tasks::block::BlockBuilder;
66
use builder::tasks::metrics::MetricsTask;
77
use builder::tasks::oauth::Authenticator;
88
use builder::tasks::submit::SubmitTask;
9-
use metrics_exporter_prometheus::PrometheusBuilder;
109

1110
use tokio::select;
1211

1312
#[tokio::main]
1413
async fn main() -> eyre::Result<()> {
15-
tracing_subscriber::fmt::try_init().unwrap();
14+
let _guard = init4_bin_base::init4();
15+
1616
let span = tracing::info_span!("zenith-builder");
1717

1818
let config = BuilderConfig::load_from_env()?.clone();
1919
let host_provider = config.connect_host_provider().await?;
2020
let ru_provider = config.connect_ru_provider().await?;
2121
let authenticator = Authenticator::new(&config);
2222

23-
PrometheusBuilder::new().install().expect("failed to install prometheus exporter");
24-
2523
tracing::debug!(rpc_url = config.host_rpc_url.as_ref(), "instantiated provider");
2624

2725
let sequencer_signer = config.connect_sequencer_signer().await?;

bin/submit_transaction.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@ use alloy::{
77
};
88
use aws_config::BehaviorVersion;
99
use builder::config::{load_address, load_string, load_u64, load_url, Provider};
10-
use metrics::counter;
11-
use metrics::histogram;
12-
use metrics_exporter_prometheus::PrometheusBuilder;
10+
use init4_bin_base::{
11+
deps::metrics::{counter, histogram},
12+
init4,
13+
};
1314
use std::time::{Duration, Instant};
1415
use tokio::time::timeout;
1516

1617
#[tokio::main]
1718
async fn main() {
18-
tracing_subscriber::fmt::try_init().unwrap();
19-
20-
tracing::trace!("installing metrics collector");
21-
PrometheusBuilder::new().install().expect("failed to install prometheus exporter");
19+
init4();
2220

2321
tracing::trace!("connecting to provider");
2422
let (provider, recipient_address, sleep_time) = connect_from_config().await;

src/config.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ pub struct BuilderConfig {
9595
pub oauth_token_refresh_interval: u64,
9696
}
9797

98+
/// Error loading the configuration.
9899
#[derive(Debug, thiserror::Error)]
99100
pub enum ConfigError {
100101
/// Error loading from environment variable
@@ -148,7 +149,11 @@ pub type WalletlessProvider = FillProvider<
148149
BoxTransport,
149150
Ethereum,
150151
>;
151-
pub type ZenithInstance = Zenith::ZenithInstance<BoxTransport, Provider, alloy::network::Ethereum>;
152+
153+
/// A Zenith contract instance, using some provider `P` (defaults to
154+
/// [`Provider`]).
155+
pub type ZenithInstance<P = Provider> =
156+
Zenith::ZenithInstance<BoxTransport, P, alloy::network::Ethereum>;
152157

153158
impl BuilderConfig {
154159
/// Load the builder configuration from environment variables.
@@ -187,10 +192,12 @@ impl BuilderConfig {
187192
})
188193
}
189194

195+
/// Connect to the Builder signer.
190196
pub async fn connect_builder_signer(&self) -> Result<LocalOrAws, ConfigError> {
191197
LocalOrAws::load(&self.builder_key, Some(self.host_chain_id)).await.map_err(Into::into)
192198
}
193199

200+
/// Connect to the Sequencer signer.
194201
pub async fn connect_sequencer_signer(&self) -> Result<Option<LocalOrAws>, ConfigError> {
195202
match &self.sequencer_key {
196203
Some(sequencer_key) => LocalOrAws::load(sequencer_key, Some(self.host_chain_id))
@@ -221,6 +228,7 @@ impl BuilderConfig {
221228
.map_err(Into::into)
222229
}
223230

231+
/// Connect additional broadcast providers.
224232
pub async fn connect_additional_broadcast(
225233
&self,
226234
) -> Result<Vec<RootProvider<BoxTransport>>, ConfigError> {
@@ -233,33 +241,41 @@ impl BuilderConfig {
233241
Ok(providers)
234242
}
235243

236-
pub fn connect_zenith(&self, provider: Provider) -> ZenithInstance {
244+
/// Connect to the Zenith instance, using the specified provider.
245+
pub const fn connect_zenith(&self, provider: Provider) -> ZenithInstance {
237246
Zenith::new(self.zenith_address, provider)
238247
}
239248
}
240249

250+
/// Load a string from an environment variable.
241251
pub fn load_string(key: &str) -> Result<String, ConfigError> {
242252
env::var(key).map_err(|_| ConfigError::missing(key))
243253
}
244254

255+
/// Load a string from an environment variable, returning None if the variable
256+
/// is not set.
245257
fn load_string_option(key: &str) -> Option<String> {
246258
load_string(key).ok()
247259
}
248260

261+
/// Load a boolean from an environment variable.
249262
pub fn load_u64(key: &str) -> Result<u64, ConfigError> {
250263
let val = load_string(key)?;
251264
val.parse::<u64>().map_err(Into::into)
252265
}
253266

267+
/// Load a u16 from an environment variable.
254268
fn load_u16(key: &str) -> Result<u16, ConfigError> {
255269
let val = load_string(key)?;
256270
val.parse::<u16>().map_err(Into::into)
257271
}
258272

273+
/// Load a URL from an environment variable.
259274
pub fn load_url(key: &str) -> Result<Cow<'static, str>, ConfigError> {
260275
load_string(key).map(Into::into)
261276
}
262277

278+
/// Load an address from an environment variable.
263279
pub fn load_address(key: &str) -> Result<Address, ConfigError> {
264280
let address = load_string(key)?;
265281
Address::from_str(&address).map_err(Into::into)

src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
1+
//! Builder binary components.
2+
3+
#![warn(
4+
missing_copy_implementations,
5+
missing_debug_implementations,
6+
missing_docs,
7+
unreachable_pub,
8+
clippy::missing_const_for_fn,
9+
rustdoc::all
10+
)]
11+
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
12+
#![deny(unused_must_use, rust_2018_idioms)]
13+
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
14+
15+
/// Configuration for the Builder binary.
116
pub mod config;
17+
18+
/// Implements the `/healthcheck` endpoint.
219
pub mod service;
20+
21+
/// Builder transaction signer.
322
pub mod signer;
23+
24+
/// Actor-based tasks used to construct a builder.
425
pub mod tasks;
26+
27+
/// Utilities.
528
pub mod utils;
29+
30+
use openssl as _;

src/signer.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ use aws_config::BehaviorVersion;
88
/// Abstraction over local signer or
99
#[derive(Debug, Clone)]
1010
pub enum LocalOrAws {
11+
/// Local signer
1112
Local(PrivateKeySigner),
13+
/// AWS signer
1214
Aws(AwsSigner),
1315
}
1416

17+
/// Error during signing
1518
#[derive(Debug, thiserror::Error)]
1619
pub enum SignerError {
1720
/// Error during [`AwsSigner`] instantiation

src/tasks/block.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub struct InProgressBlock {
2828

2929
impl InProgressBlock {
3030
/// Create a new `InProgressBlock`
31-
pub fn new() -> Self {
31+
pub const fn new() -> Self {
3232
Self { transactions: Vec::new(), raw_encoding: OnceLock::new(), hash: OnceLock::new() }
3333
}
3434

@@ -116,16 +116,22 @@ impl InProgressBlock {
116116
}
117117
}
118118

119-
/// BlockBuilder is a task that periodically builds a block then sends it for signing and submission.
119+
/// BlockBuilder is a task that periodically builds a block then sends it for
120+
/// signing and submission.
121+
#[derive(Debug)]
120122
pub struct BlockBuilder {
123+
/// Configuration.
121124
pub config: BuilderConfig,
125+
/// A provider that cannot sign transactions.
122126
pub ru_provider: WalletlessProvider,
127+
/// A poller for fetching transactions.
123128
pub tx_poller: TxPoller,
129+
/// A poller for fetching bundles.
124130
pub bundle_poller: BundlePoller,
125131
}
126132

127133
impl BlockBuilder {
128-
// create a new block builder with the given config.
134+
/// Create a new block builder with the given config.
129135
pub fn new(
130136
config: &BuilderConfig,
131137
authenticator: Authenticator,
@@ -139,7 +145,8 @@ impl BlockBuilder {
139145
}
140146
}
141147

142-
/// Fetches transactions from the cache and ingests them into the in progress block
148+
/// Fetches transactions from the cache and ingests them into the in
149+
/// progress block
143150
async fn get_transactions(&mut self, in_progress: &mut InProgressBlock) {
144151
trace!("query transactions from cache");
145152
let txns = self.tx_poller.check_tx_cache().await;

src/tasks/bundler.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,31 @@ use std::collections::HashMap;
1010
use std::time::{Duration, Instant};
1111
use zenith_types::ZenithEthBundle;
1212

13+
/// A bundle response from the tx-pool endpoint, containing a UUID and a
14+
/// [`ZenithEthBundle`].
1315
#[derive(Debug, Clone, Serialize, Deserialize)]
1416
pub struct Bundle {
17+
/// The bundle id (a UUID)
1518
pub id: String,
19+
/// The bundle itself
1620
pub bundle: ZenithEthBundle,
1721
}
1822

1923
/// Response from the tx-pool containing a list of bundles.
2024
#[derive(Debug, Clone, Serialize, Deserialize)]
2125
pub struct TxPoolBundleResponse {
26+
/// the list of bundles
2227
pub bundles: Vec<Bundle>,
2328
}
2429

2530
/// The BundlePoller polls the tx-pool for bundles and manages the seen bundles.
31+
#[derive(Debug)]
2632
pub struct BundlePoller {
33+
/// Configuration
2734
pub config: BuilderConfig,
35+
/// [`Authenticator`] for fetching OAuth tokens
2836
pub authenticator: Authenticator,
37+
/// Already seen bundle UUIDs
2938
pub seen_uuids: HashMap<String, Instant>,
3039
}
3140

src/tasks/metrics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::config::Provider;
22
use alloy::{primitives::TxHash, providers::Provider as _};
3-
use metrics::{counter, histogram};
3+
use init4_bin_base::deps::metrics::{counter, histogram};
44
use std::time::Instant;
55
use tokio::{sync::mpsc, task::JoinHandle};
66
use tracing::{debug, error};

src/tasks/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1+
/// Block creation task
12
pub mod block;
3+
4+
/// Bundle poller task
25
pub mod bundler;
6+
7+
/// Tx submission metric task
38
pub mod metrics;
9+
10+
/// OAuth token refresh task
411
pub mod oauth;
12+
13+
/// Tx submission task
514
pub mod submit;
15+
16+
/// Tx polling task
617
pub mod tx_poller;

src/tasks/oauth.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Token = StandardTokenResponse<EmptyExtraTokenFields, BasicTokenType>;
1818
/// Readers are guaranteed to not read stale tokens as the [RwLock] guarantees that write tasks (refreshing the token) will claim priority over read access.
1919
#[derive(Debug, Clone)]
2020
pub struct Authenticator {
21+
/// Configuration
2122
pub config: BuilderConfig,
2223
inner: Arc<RwLock<AuthenticatorInner>>,
2324
}
@@ -26,6 +27,7 @@ pub struct Authenticator {
2627
/// Contains the token that is being used for authentication.
2728
#[derive(Debug)]
2829
pub struct AuthenticatorInner {
30+
/// The token
2931
pub token: Option<Token>,
3032
}
3133

@@ -36,7 +38,8 @@ impl Default for AuthenticatorInner {
3638
}
3739

3840
impl AuthenticatorInner {
39-
pub fn new() -> Self {
41+
/// Creates a new AuthenticatorInner with no token set.
42+
pub const fn new() -> Self {
4043
Self { token: None }
4144
}
4245
}
@@ -140,7 +143,7 @@ mod tests {
140143
}
141144

142145
#[allow(dead_code)]
143-
pub fn setup_test_config() -> Result<BuilderConfig> {
146+
fn setup_test_config() -> Result<BuilderConfig> {
144147
let config = BuilderConfig {
145148
host_chain_id: 17000,
146149
ru_chain_id: 17001,

src/tasks/submit.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use alloy::{
1616
transports::TransportError,
1717
};
1818
use eyre::{bail, eyre};
19-
use metrics::{counter, histogram};
19+
use init4_bin_base::deps::metrics::{counter, histogram};
2020
use oauth2::TokenResponse;
2121
use std::time::Instant;
2222
use tokio::{sync::mpsc, task::JoinHandle};
@@ -43,13 +43,19 @@ macro_rules! spawn_provider_send {
4343

4444
use super::oauth::Authenticator;
4545

46+
/// Control flow for transaction submission.
47+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
4648
pub enum ControlFlow {
49+
/// Retry
4750
Retry,
51+
/// Skip
4852
Skip,
53+
/// Succesfully submitted
4954
Done,
5055
}
5156

5257
/// Submits sidecars in ethereum txns to mainnet ethereum
58+
#[derive(Debug, Clone)]
5359
pub struct SubmitTask {
5460
/// Ethereum Provider
5561
pub host_provider: Provider,
@@ -63,7 +69,7 @@ pub struct SubmitTask {
6369
pub config: crate::config::BuilderConfig,
6470
/// Authenticator
6571
pub authenticator: Authenticator,
66-
// Channel over which to send pending transactions
72+
/// Channel over which to send pending transactions
6773
pub outbound_tx_channel: mpsc::UnboundedSender<TxHash>,
6874
}
6975

0 commit comments

Comments
 (0)