|
| 1 | +use eth2::SensitiveUrl; |
| 2 | +use reqwest::{Client, Response, Url}; |
| 3 | +use serde::{Deserialize, Serialize}; |
| 4 | +use std::collections::HashMap; |
| 5 | +use std::time::Duration; |
| 6 | +use types::Slot; |
| 7 | + |
| 8 | +pub use config::Config; |
| 9 | + |
| 10 | +mod config; |
| 11 | +mod error; |
| 12 | + |
| 13 | +const TIMEOUT: Duration = Duration::from_secs(50); |
| 14 | + |
| 15 | +#[derive(Debug)] |
| 16 | +pub enum Error { |
| 17 | + Reqwest(reqwest::Error), |
| 18 | + Url(url::ParseError), |
| 19 | + BlockprintNotSynced, |
| 20 | + Other(String), |
| 21 | +} |
| 22 | + |
| 23 | +impl From<reqwest::Error> for Error { |
| 24 | + fn from(e: reqwest::Error) -> Self { |
| 25 | + Error::Reqwest(e) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +impl From<url::ParseError> for Error { |
| 30 | + fn from(e: url::ParseError) -> Self { |
| 31 | + Error::Url(e) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +pub struct WatchBlockprintClient { |
| 36 | + pub client: Client, |
| 37 | + pub server: SensitiveUrl, |
| 38 | + pub username: Option<String>, |
| 39 | + pub password: Option<String>, |
| 40 | +} |
| 41 | + |
| 42 | +#[derive(Debug, Deserialize, Serialize)] |
| 43 | +pub struct BlockprintSyncingResponse { |
| 44 | + pub greatest_block_slot: Slot, |
| 45 | + pub synced: bool, |
| 46 | +} |
| 47 | + |
| 48 | +#[derive(Debug, Deserialize, Serialize)] |
| 49 | +pub struct BlockprintResponse { |
| 50 | + pub proposer_index: i32, |
| 51 | + pub slot: Slot, |
| 52 | + pub best_guess_single: String, |
| 53 | +} |
| 54 | + |
| 55 | +impl WatchBlockprintClient { |
| 56 | + async fn get(&self, url: Url) -> Result<Response, Error> { |
| 57 | + let mut builder = self.client.get(url).timeout(TIMEOUT); |
| 58 | + if let Some(username) = &self.username { |
| 59 | + builder = builder.basic_auth(username, self.password.as_ref()); |
| 60 | + } |
| 61 | + let response = builder.send().await.map_err(Error::Reqwest)?; |
| 62 | + |
| 63 | + if !response.status().is_success() { |
| 64 | + return Err(Error::Other(response.text().await?)); |
| 65 | + } |
| 66 | + |
| 67 | + Ok(response) |
| 68 | + } |
| 69 | + |
| 70 | + // Returns the `greatest_block_slot` as reported by the Blockprint server. |
| 71 | + // Will error if the Blockprint server is not synced. |
| 72 | + pub async fn ensure_synced(&self) -> Result<Slot, Error> { |
| 73 | + let url = self.server.full.join("sync/")?.join("status")?; |
| 74 | + |
| 75 | + let response = self.get(url).await?; |
| 76 | + |
| 77 | + let result = response.json::<BlockprintSyncingResponse>().await?; |
| 78 | + if !result.synced { |
| 79 | + return Err(Error::BlockprintNotSynced); |
| 80 | + } |
| 81 | + |
| 82 | + Ok(result.greatest_block_slot) |
| 83 | + } |
| 84 | + |
| 85 | + pub async fn blockprint_all_validators( |
| 86 | + &self, |
| 87 | + highest_validator: i32, |
| 88 | + ) -> Result<HashMap<i32, String>, Error> { |
| 89 | + let url = self |
| 90 | + .server |
| 91 | + .full |
| 92 | + .join("validator/")? |
| 93 | + .join("blocks/")? |
| 94 | + .join("latest")?; |
| 95 | + |
| 96 | + let response = self.get(url).await?; |
| 97 | + |
| 98 | + let mut result = response.json::<Vec<BlockprintResponse>>().await?; |
| 99 | + result.retain(|print| print.proposer_index <= highest_validator); |
| 100 | + |
| 101 | + let mut map: HashMap<i32, String> = HashMap::with_capacity(result.len()); |
| 102 | + for print in result { |
| 103 | + map.insert(print.proposer_index, print.best_guess_single); |
| 104 | + } |
| 105 | + |
| 106 | + Ok(map) |
| 107 | + } |
| 108 | + |
| 109 | + pub async fn blockprint_proposers_between( |
| 110 | + &self, |
| 111 | + start_slot: Slot, |
| 112 | + end_slot: Slot, |
| 113 | + ) -> Result<HashMap<i32, String>, Error> { |
| 114 | + let url = self |
| 115 | + .server |
| 116 | + .full |
| 117 | + .join("blocks/")? |
| 118 | + .join(&format!("{start_slot}/{end_slot}"))?; |
| 119 | + |
| 120 | + let response = self.get(url).await?; |
| 121 | + |
| 122 | + let result = response.json::<Vec<BlockprintResponse>>().await?; |
| 123 | + |
| 124 | + let mut map: HashMap<i32, String> = HashMap::with_capacity(result.len()); |
| 125 | + for print in result { |
| 126 | + map.insert(print.proposer_index, print.best_guess_single); |
| 127 | + } |
| 128 | + |
| 129 | + Ok(map) |
| 130 | + } |
| 131 | +} |
0 commit comments