Skip to content

chore: delte constants" #83

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ path = "bin/submit_transaction.rs"
init4-bin-base = "0.3"

signet-zenith = { git = "https://github.com/init4tech/signet-sdk", branch = "main" }
signet-constants = { git = "https://github.com/init4tech/signet-sdk", branch = "main" }
signet-types = { git = "https://github.com/init4tech/signet-sdk", branch = "main" }
signet-bundle = { git = "https://github.com/init4tech/signet-sdk", branch = "main" }
signet-sim = { git = "https://github.com/init4tech/signet-sdk", branch = "main" }
Expand Down
31 changes: 8 additions & 23 deletions bin/builder.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use builder::{
config::BuilderConfig,
service::serve_builder,
tasks::{
block::Simulator, bundler, metrics::MetricsTask, oauth::Authenticator, submit::SubmitTask,
tx_poller,
},
tasks::{block::Simulator, bundler, metrics::MetricsTask, submit::SubmitTask, tx_poller},
};
use init4_bin_base::{deps::tracing, utils::from_env::FromEnv};
use signet_sim::SimCache;
Expand All @@ -22,35 +19,26 @@ async fn main() -> eyre::Result<()> {

let config = BuilderConfig::from_env()?.clone();
let constants = SignetSystemConstants::pecorino();
let authenticator = Authenticator::new(&config)?;
let token = config.oauth_token();

let (host_provider, sequencer_signer) =
tokio::try_join!(config.connect_host_provider(), config.connect_sequencer_signer(),)?;
let (host_provider, quincey) =
tokio::try_join!(config.connect_host_provider(), config.connect_quincey())?;
let ru_provider = config.connect_ru_provider();

let zenith = config.connect_zenith(host_provider.clone());

let metrics = MetricsTask { host_provider: host_provider.clone() };
let metrics = MetricsTask { host_provider };
let (tx_channel, metrics_jh) = metrics.spawn();

let submit = SubmitTask {
token: authenticator.token(),
host_provider,
zenith,
client: reqwest::Client::new(),
sequencer_signer,
config: config.clone(),
outbound_tx_channel: tx_channel,
};
let submit =
SubmitTask { zenith, quincey, config: config.clone(), outbound_tx_channel: tx_channel };

let tx_poller = tx_poller::TxPoller::new(&config);
let (tx_receiver, tx_poller_jh) = tx_poller.spawn();

let bundle_poller = bundler::BundlePoller::new(&config, authenticator.token());
let bundle_poller = bundler::BundlePoller::new(&config, token);
let (bundle_receiver, bundle_poller_jh) = bundle_poller.spawn();

let authenticator_jh = authenticator.spawn();

let (submit_channel, submit_jh) = submit.spawn();

let sim_items = SimCache::new();
Expand Down Expand Up @@ -94,9 +82,6 @@ async fn main() -> eyre::Result<()> {
_ = server => {
tracing::info!("server finished");
}
_ = authenticator_jh => {
tracing::info!("authenticator finished");
}
}

tracing::info!("shutting down");
Expand Down
33 changes: 32 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::signer::{LocalOrAws, SignerError};
use crate::{
quincey::Quincey,
signer::{LocalOrAws, SignerError},
tasks::oauth::{Authenticator, SharedToken},
};
use alloy::{
network::{Ethereum, EthereumWallet},
primitives::Address,
Expand Down Expand Up @@ -209,4 +213,31 @@ impl BuilderConfig {
pub const fn connect_zenith(&self, provider: HostProvider) -> ZenithInstance {
Zenith::new(self.zenith_address, provider)
}

/// Get an oauth2 token for the builder, starting the authenticator if it
// is not already running.
pub fn oauth_token(&self) -> SharedToken {
static ONCE: std::sync::OnceLock<SharedToken> = std::sync::OnceLock::new();

ONCE.get_or_init(|| {
let authenticator = Authenticator::new(self).unwrap();
let token = authenticator.token();
authenticator.spawn();
token
})
.clone()
}

/// Connect to a Quincey, owned or shared.
pub async fn connect_quincey(&self) -> eyre::Result<Quincey> {
if let Some(signer) = self.connect_sequencer_signer().await? {
return Ok(Quincey::new_owned(signer));
}

let client = reqwest::Client::new();
let url = url::Url::parse(&self.quincey_url)?;
let token = self.oauth_token();

Ok(Quincey::new_remote(client, url, token))
}
}
36 changes: 0 additions & 36 deletions src/constants.rs

This file was deleted.

6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
#![deny(unused_must_use, rust_2018_idioms)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

/// Constants for the Builder.
pub mod constants;

/// Configuration for the Builder binary.
pub mod config;

/// Quincey client for signing requests.
pub mod quincey;

/// Implements the `/healthcheck` endpoint.
pub mod service;

Expand Down
80 changes: 80 additions & 0 deletions src/quincey.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use crate::{signer::LocalOrAws, tasks::oauth::SharedToken};
use alloy::signers::Signer;
use eyre::bail;
use init4_bin_base::deps::tracing::{self, debug, info, instrument, trace};
use oauth2::TokenResponse;
use reqwest::Client;
use signet_types::{SignRequest, SignResponse};

/// A quincey client for making requests to the Quincey API.
#[derive(Debug, Clone)]
pub enum Quincey {
/// A remote quincey, this is used for production environments.
/// The client will access the Quincey API over HTTP(S) via OAuth.
Remote {
/// The remote client.
client: Client,
/// The base URL for the remote API.
url: reqwest::Url,
/// OAuth shared token.
token: SharedToken,
},
/// An owned quincey, either local or AWS. This is used primarily for
/// testing and development environments. The client will simulate the
/// Quincey API using a local or AWS KMS key.
Owned(LocalOrAws),
}

impl Quincey {
/// Creates a new Quincey client from the provided URL and token.
pub const fn new_remote(client: Client, url: reqwest::Url, token: SharedToken) -> Self {
Self::Remote { client, url, token }
}

/// Creates a new Quincey client for making requests to the Quincey API.
pub const fn new_owned(client: LocalOrAws) -> Self {
Self::Owned(client)
}

async fn sup_owned(&self, sig_request: &SignRequest) -> eyre::Result<SignResponse> {
let Self::Owned(signer) = &self else { eyre::bail!("not an owned client") };

info!("signing with owned quincey");
signer
.sign_hash(&sig_request.signing_hash())
.await
.map_err(Into::into)
.map(|sig| SignResponse { sig, req: *sig_request })
}

async fn sup_remote(&self, sig_request: &SignRequest) -> eyre::Result<SignResponse> {
let Self::Remote { client, url, token } = &self else { bail!("not a remote client") };

let Some(token) = token.read() else { bail!("no token available") };

let resp: reqwest::Response = client
.post(url.clone())
.json(sig_request)
.bearer_auth(token.access_token().secret())
.send()
.await?
.error_for_status()?;

let body = resp.bytes().await?;

debug!(bytes = body.len(), "retrieved response body");
trace!(body = %String::from_utf8_lossy(&body), "response body");

serde_json::from_slice(&body).map_err(Into::into)
}

/// Get a signature for the provided request, by either using the owned
/// or remote client.
#[instrument(skip(self))]
pub async fn get_signature(&self, sig_request: &SignRequest) -> eyre::Result<SignResponse> {
match self {
Self::Owned(_) => self.sup_owned(sig_request).await,
Self::Remote { .. } => self.sup_remote(sig_request).await,
}
}
}
5 changes: 2 additions & 3 deletions src/tasks/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//! and turns them into valid Pecorino blocks for network submission.
use crate::{
config::{BuilderConfig, RuProvider},
constants::{BASEFEE_DEFAULT, PECORINO_CHAIN_ID},
tasks::bundler::Bundle,
};
use alloy::{
Expand Down Expand Up @@ -347,7 +346,7 @@ impl Simulator {
Some(basefee) => basefee,
None => {
warn!("get basefee failed - RPC error likely occurred");
BASEFEE_DEFAULT
todo!()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intended in this commit?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes and explicitly flagged in the pr description. i would rather have a crash than faulty behavior

}
};
debug!(basefee = basefee, "setting basefee");
Expand Down Expand Up @@ -433,7 +432,7 @@ impl trevm::Cfg for PecorinoCfg {
fn fill_cfg_env(&self, cfg_env: &mut CfgEnv) {
let CfgEnv { chain_id, spec, .. } = cfg_env;

*chain_id = PECORINO_CHAIN_ID;
*chain_id = signet_constants::pecorino::RU_CHAIN_ID;
*spec = SpecId::default();
}
}
Expand Down
Loading