|
| 1 | +use crate::block_data::BlockExtractor; |
| 2 | +use init4_bin_base::utils::calc::SlotCalculator; |
| 3 | +use reth::transaction_pool::TransactionPool; |
| 4 | +use url::Url; |
| 5 | + |
| 6 | +/// Errors that can occur while building the [`BlockExtractor`] with a |
| 7 | +/// [`BlockExtractorBuilder`]. |
| 8 | +#[derive(Debug, thiserror::Error)] |
| 9 | +pub enum BuilderError { |
| 10 | + /// The transaction pool was not provided. |
| 11 | + #[error("transaction pool is required")] |
| 12 | + MissingPool, |
| 13 | + /// The explorer URL was not provided or could not be parsed. |
| 14 | + #[error("explorer URL is required and must be valid")] |
| 15 | + MissingExplorerUrl, |
| 16 | + /// The URL provided was invalid. |
| 17 | + #[error("invalid URL provided")] |
| 18 | + Url(#[from] url::ParseError), |
| 19 | + /// The client was not provided. |
| 20 | + #[error("client is required")] |
| 21 | + MissingClient, |
| 22 | + /// The client failed to build. |
| 23 | + #[error("failed to build client: {0}")] |
| 24 | + Client(#[from] reqwest::Error), |
| 25 | + /// The slot calculator was not provided. |
| 26 | + #[error("slot calculator is required")] |
| 27 | + MissingSlotCalculator, |
| 28 | +} |
| 29 | + |
| 30 | +/// Builder for the [`BlockExtractor`]. |
| 31 | +#[derive(Debug, Default, Clone)] |
| 32 | +pub struct BlockExtractorBuilder<Pool> { |
| 33 | + pool: Option<Pool>, |
| 34 | + explorer_url: Option<String>, |
| 35 | + client: Option<reqwest::Client>, |
| 36 | + cl_url: Option<String>, |
| 37 | + pylon_url: Option<String>, |
| 38 | + slot_calculator: Option<SlotCalculator>, |
| 39 | +} |
| 40 | + |
| 41 | +impl<Pool> BlockExtractorBuilder<Pool> { |
| 42 | + /// Set the transaction pool to use for the extractor. |
| 43 | + pub fn with_pool<P2>(self, pool: P2) -> BlockExtractorBuilder<P2> { |
| 44 | + BlockExtractorBuilder { |
| 45 | + pool: Some(pool), |
| 46 | + explorer_url: self.explorer_url, |
| 47 | + client: self.client, |
| 48 | + cl_url: self.cl_url, |
| 49 | + pylon_url: self.pylon_url, |
| 50 | + slot_calculator: self.slot_calculator, |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// Set the transaction pool to use a mock test pool. |
| 55 | + #[cfg(feature = "test-utils")] |
| 56 | + pub fn with_test_pool( |
| 57 | + self, |
| 58 | + ) -> BlockExtractorBuilder<reth_transaction_pool::test_utils::TestPool> { |
| 59 | + self.with_pool(reth_transaction_pool::test_utils::testing_pool()) |
| 60 | + } |
| 61 | + |
| 62 | + /// Set the blob explorer URL to use for the extractor. This will be used |
| 63 | + /// to construct a [`foundry_blob_explorers::Client`]. |
| 64 | + pub fn with_explorer_url(mut self, explorer_url: &str) -> Self { |
| 65 | + self.explorer_url = Some(explorer_url.to_string()); |
| 66 | + self |
| 67 | + } |
| 68 | + |
| 69 | + /// Set the [`reqwest::Client`] to use for the extractor. This client will |
| 70 | + /// be used to make requests to the blob explorer, and the CL and Pylon URLs |
| 71 | + /// if provided. |
| 72 | + pub fn with_client(mut self, client: reqwest::Client) -> Self { |
| 73 | + self.client = Some(client); |
| 74 | + self |
| 75 | + } |
| 76 | + |
| 77 | + /// Set the [`reqwest::Client`] via a [reqwest::ClientBuilder]. This |
| 78 | + /// function will immediately build the client and return an error if it |
| 79 | + /// fails. |
| 80 | + /// |
| 81 | + /// This client will be used to make requests to the blob explorer, and the |
| 82 | + /// CL and Pylon URLs if provided. |
| 83 | + pub fn with_client_builder(self, client: reqwest::ClientBuilder) -> Result<Self, BuilderError> { |
| 84 | + client.build().map(|client| self.with_client(client)).map_err(Into::into) |
| 85 | + } |
| 86 | + |
| 87 | + /// Set the CL URL to use for the extractor. |
| 88 | + pub fn with_cl_url(mut self, cl_url: &str) -> Result<Self, BuilderError> { |
| 89 | + self.cl_url = Some(cl_url.to_string()); |
| 90 | + Ok(self) |
| 91 | + } |
| 92 | + |
| 93 | + /// Set the Pylon URL to use for the extractor. |
| 94 | + pub fn with_pylon_url(mut self, pylon_url: &str) -> Result<Self, BuilderError> { |
| 95 | + self.pylon_url = Some(pylon_url.to_string()); |
| 96 | + Ok(self) |
| 97 | + } |
| 98 | + |
| 99 | + /// Set the slot calculator to use for the extractor. |
| 100 | + pub const fn with_slot_calculator( |
| 101 | + mut self, |
| 102 | + slot_calculator: SlotCalculator, |
| 103 | + ) -> BlockExtractorBuilder<Pool> { |
| 104 | + self.slot_calculator = Some(slot_calculator); |
| 105 | + self |
| 106 | + } |
| 107 | + |
| 108 | + /// Set the slot calculator to use for the extractor, using the Pecornino |
| 109 | + /// host configuration. |
| 110 | + pub const fn with_pecornino_slots(mut self) -> BlockExtractorBuilder<Pool> { |
| 111 | + self.slot_calculator = Some(SlotCalculator::pecorino_host()); |
| 112 | + self |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +impl<Pool: TransactionPool> BlockExtractorBuilder<Pool> { |
| 117 | + /// Build the [`BlockExtractor`] with the provided parameters. |
| 118 | + pub fn build(self) -> Result<BlockExtractor<Pool>, BuilderError> { |
| 119 | + let pool = self.pool.ok_or(BuilderError::MissingPool)?; |
| 120 | + |
| 121 | + let explorer_url = self.explorer_url.ok_or(BuilderError::MissingExplorerUrl)?; |
| 122 | + |
| 123 | + let cl_url = self.cl_url.map(parse_url).transpose()?; |
| 124 | + |
| 125 | + let pylon_url = self.pylon_url.map(parse_url).transpose()?; |
| 126 | + |
| 127 | + let client = self.client.ok_or(BuilderError::MissingClient)?; |
| 128 | + |
| 129 | + let explorer = |
| 130 | + foundry_blob_explorers::Client::new_with_client(explorer_url, client.clone()); |
| 131 | + |
| 132 | + let slot_calculator = self.slot_calculator.ok_or(BuilderError::MissingSlotCalculator)?; |
| 133 | + |
| 134 | + Ok(BlockExtractor::new(pool, explorer, client, cl_url, pylon_url, slot_calculator)) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +fn parse_url(url: String) -> Result<Url, BuilderError> { |
| 139 | + Url::parse(url.as_ref()).map_err(BuilderError::Url) |
| 140 | +} |
0 commit comments