-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.rs
More file actions
280 lines (254 loc) · 10.3 KB
/
Copy pathconfig.rs
File metadata and controls
280 lines (254 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use crate::{
quincey::Quincey,
tasks::{
block::cfg::SignetCfgEnv,
cache::{BundlePoller, CacheSystem, CacheTask, TxPoller},
env::{EnvTask, SimEnv},
},
};
use alloy::{
network::{Ethereum, EthereumWallet},
primitives::Address,
providers::{
Identity, ProviderBuilder, RootProvider,
fillers::{
BlobGasFiller, ChainIdFiller, FillProvider, GasFiller, JoinFill, NonceFiller,
SimpleNonceManager, WalletFiller,
},
},
};
use eyre::Result;
use init4_bin_base::{
perms::{Authenticator, OAuthConfig, SharedToken},
utils::{
calc::SlotCalculator,
from_env::FromEnv,
signer::{LocalOrAws, SignerError},
},
};
use signet_zenith::Zenith;
use std::borrow::Cow;
use tokio::sync::watch;
/// Type alias for the provider used to simulate against rollup state.
pub type RuProvider = RootProvider<Ethereum>;
/// A [`Zenith`] contract instance using [`Provider`] as the provider.
pub type ZenithInstance<P = HostProvider> = Zenith::ZenithInstance<P, alloy::network::Ethereum>;
/// Type alias for the provider used to build and submit blocks to the host.
pub type HostProvider = FillProvider<
JoinFill<
JoinFill<
JoinFill<
JoinFill<JoinFill<Identity, BlobGasFiller>, GasFiller>,
NonceFiller<SimpleNonceManager>,
>,
ChainIdFiller,
>,
WalletFiller<EthereumWallet>,
>,
RootProvider,
>;
/// Configuration for a builder running a specific rollup on a specific host
/// chain.
#[derive(Debug, Clone, FromEnv)]
pub struct BuilderConfig {
/// The chain ID of the host chain
#[from_env(var = "HOST_CHAIN_ID", desc = "The chain ID of the host chain")]
pub host_chain_id: u64,
/// The chain ID of the rollup chain
#[from_env(var = "RU_CHAIN_ID", desc = "The chain ID of the rollup chain")]
pub ru_chain_id: u64,
/// URL for Host RPC node.
#[from_env(var = "HOST_RPC_URL", desc = "URL for Host RPC node", infallible)]
pub host_rpc_url: Cow<'static, str>,
/// URL for the Rollup RPC node.
#[from_env(var = "ROLLUP_RPC_URL", desc = "URL for Rollup RPC node", infallible)]
pub ru_rpc_url: Cow<'static, str>,
/// Additional RPC URLs to which to broadcast transactions.
/// NOTE: should not include the host_rpc_url value
#[from_env(
var = "TX_BROADCAST_URLS",
desc = "Additional RPC URLs to which the builder broadcasts transactions",
infallible,
optional
)]
pub tx_broadcast_urls: Vec<Cow<'static, str>>,
/// address of the Zenith contract on Host.
#[from_env(var = "ZENITH_ADDRESS", desc = "address of the Zenith contract on Host")]
pub zenith_address: Address,
/// address of the Builder Helper contract on Host.
#[from_env(
var = "BUILDER_HELPER_ADDRESS",
desc = "address of the Builder Helper contract on Host"
)]
pub builder_helper_address: Address,
/// URL for remote Quincey Sequencer server to sign blocks.
/// Disregarded if a sequencer_signer is configured.
#[from_env(
var = "QUINCEY_URL",
desc = "URL for remote Quincey Sequencer server to sign blocks",
infallible
)]
pub quincey_url: Cow<'static, str>,
/// Port for the Builder server.
#[from_env(var = "BUILDER_PORT", desc = "Port for the Builder server")]
pub builder_port: u16,
/// Key to access Sequencer Wallet - AWS Key ID _OR_ local private key.
/// Set IFF using local Sequencer signing instead of remote Quincey signing.
#[from_env(
var = "SEQUENCER_KEY",
desc = "Key to access Sequencer Wallet - AWS Key ID _OR_ local private key, set IFF using local Sequencer signing instead of remote Quincey signing",
infallible,
optional
)]
pub sequencer_key: Option<String>,
/// Key to access Builder transaction submission wallet - AWS Key ID _OR_ local private key.
#[from_env(
var = "BUILDER_KEY",
desc = "Key to access Builder transaction submission wallet - AWS Key ID _OR_ local private key",
infallible
)]
pub builder_key: String,
/// Buffer in seconds in which the `submitBlock` transaction must confirm on the Host chain.
#[from_env(
var = "BLOCK_CONFIRMATION_BUFFER",
desc = "Buffer in seconds in which the `submitBlock` transaction must confirm on the Host chain"
)]
pub block_confirmation_buffer: u64,
/// Address on Rollup to which Builder will receive user transaction fees.
#[from_env(
var = "BUILDER_REWARDS_ADDRESS",
desc = "Address on Rollup to which Builder will receive user transaction fees"
)]
pub builder_rewards_address: Address,
/// Gas limit for RU block.
/// NOTE: a "smart" builder would determine this programmatically by simulating the block.
#[from_env(var = "ROLLUP_BLOCK_GAS_LIMIT", desc = "Gas limit for RU block")]
pub rollup_block_gas_limit: u64,
/// URL of the tx pool to poll for incoming transactions.
#[from_env(
var = "TX_POOL_URL",
desc = "URL of the tx pool to poll for incoming transactions",
infallible
)]
pub tx_pool_url: Cow<'static, str>,
/// Duration in seconds transactions can live in the tx-pool cache.
#[from_env(
var = "TX_POOL_CACHE_DURATION",
desc = "Duration in seconds transactions can live in the tx-pool cache"
)]
pub tx_pool_cache_duration: u64,
/// Oauth2 configuration for the builder to connect to init4 services.
pub oauth: OAuthConfig,
/// The max number of simultaneous block simulations to run.
#[from_env(
var = "CONCURRENCY_LIMIT",
desc = "The max number of simultaneous block simulations to run"
)]
pub concurrency_limit: usize,
/// The slot calculator for the builder.
pub slot_calculator: SlotCalculator,
}
impl BuilderConfig {
/// Connect to the Builder signer.
pub async fn connect_builder_signer(&self) -> Result<LocalOrAws, SignerError> {
LocalOrAws::load(&self.builder_key, Some(self.host_chain_id)).await
}
/// Connect to the Sequencer signer.
pub async fn connect_sequencer_signer(&self) -> eyre::Result<Option<LocalOrAws>> {
if let Some(sequencer_key) = &self.sequencer_key {
LocalOrAws::load(sequencer_key, Some(self.host_chain_id))
.await
.map_err(Into::into)
.map(Some)
} else {
Ok(None)
}
}
/// Connect to the Rollup rpc provider.
pub fn connect_ru_provider(&self) -> RootProvider<Ethereum> {
static ONCE: std::sync::OnceLock<RootProvider<Ethereum>> = std::sync::OnceLock::new();
ONCE.get_or_init(|| {
let url = url::Url::parse(&self.ru_rpc_url).expect("failed to parse URL");
RootProvider::new_http(url)
})
.clone()
}
/// Connect to the Host rpc provider.
pub async fn connect_host_provider(&self) -> eyre::Result<HostProvider> {
let builder_signer = self.connect_builder_signer().await?;
ProviderBuilder::new_with_network()
.disable_recommended_fillers()
.filler(BlobGasFiller)
.with_gas_estimation()
.with_nonce_management(SimpleNonceManager::default())
.fetch_chain_id()
.wallet(EthereumWallet::from(builder_signer))
.connect(&self.host_rpc_url)
.await
.map_err(Into::into)
}
/// Connect additional broadcast providers.
pub fn connect_additional_broadcast(&self) -> Vec<RootProvider> {
self.tx_broadcast_urls
.iter()
.map(|url_str| {
let url = url::Url::parse(url_str).expect("failed to parse URL");
RootProvider::new_http(url)
})
.collect::<Vec<_>>()
}
/// Connect to the Zenith instance, using the specified provider.
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.oauth);
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))
}
/// Create an [`EnvTask`] using this config.
pub async fn env_task(&self) -> EnvTask {
let ru_provider = self.connect_ru_provider();
let host_provider =
self.connect_host_provider().await.expect("failed to configure host provider");
EnvTask::new(self.clone(), ru_provider, host_provider)
}
/// Spawn a new [`CacheSystem`] using this config. This contains the
/// joinhandles for [`TxPoller`] and [`BundlePoller`] and [`CacheTask`], as
/// well as the [`SimCache`] and the block env watcher.
///
/// [`SimCache`]: signet_sim::SimCache
pub fn spawn_cache_system(&self, block_env: watch::Receiver<Option<SimEnv>>) -> CacheSystem {
// Tx Poller pulls transactions from the cache
let tx_poller = TxPoller::new(self);
let (tx_receiver, tx_poller) = tx_poller.spawn();
// Bundle Poller pulls bundles from the cache
let bundle_poller = BundlePoller::new(self, self.oauth_token());
let (bundle_receiver, bundle_poller) = bundle_poller.spawn();
// Set up the cache task
let cache_task = CacheTask::new(block_env.clone(), bundle_receiver, tx_receiver);
let (sim_cache, cache_task) = cache_task.spawn();
CacheSystem { cache_task, tx_poller, bundle_poller, sim_cache }
}
/// Create a [`SignetCfgEnv`] using this config.
pub const fn cfg_env(&self) -> SignetCfgEnv {
SignetCfgEnv { chain_id: self.ru_chain_id }
}
}