|
| 1 | +use crate::{ |
| 2 | + get_timestamp_f64, |
| 3 | + types::{PublisherType, ScrapedRelayBlockBid}, |
| 4 | +}; |
| 5 | +use alloy_primitives::{Address, BlockHash, U256}; |
| 6 | +use alloy_rpc_types_beacon::BlsPublicKey; |
| 7 | +use eyre::eyre; |
| 8 | +use ssz::Decode; |
| 9 | +use ssz_derive::Decode; |
| 10 | +use tokio_tungstenite::tungstenite::Message; |
| 11 | +use tracing::debug; |
| 12 | + |
| 13 | +/// Common to Ultrasound and Titan. |
| 14 | +#[derive(Debug, Decode)] |
| 15 | +struct TopBidUpdate { |
| 16 | + /// Millisecond timestamp at which this became the top bid |
| 17 | + pub timestamp: u64, |
| 18 | + pub slot: u64, |
| 19 | + pub block_number: u64, |
| 20 | + pub block_hash: BlockHash, |
| 21 | + pub parent_hash: BlockHash, |
| 22 | + pub builder_pubkey: BlsPublicKey, |
| 23 | + pub fee_recipient: Address, |
| 24 | + pub value: U256, |
| 25 | +} |
| 26 | + |
| 27 | +fn block_bid_from_update( |
| 28 | + update: TopBidUpdate, |
| 29 | + relay_name: &str, |
| 30 | + publisher_name: &str, |
| 31 | + publisher_type: PublisherType, |
| 32 | +) -> ScrapedRelayBlockBid { |
| 33 | + ScrapedRelayBlockBid { |
| 34 | + publisher_name: publisher_name.to_owned(), |
| 35 | + publisher_type: publisher_type.to_owned(), |
| 36 | + builder_pubkey: Some(update.builder_pubkey), |
| 37 | + relay_name: relay_name.to_owned(), |
| 38 | + parent_hash: update.parent_hash, |
| 39 | + block_hash: update.block_hash, |
| 40 | + seen_time: get_timestamp_f64(), |
| 41 | + relay_time: Some(update.timestamp as f64 / 1000.), |
| 42 | + value: update.value, |
| 43 | + slot_number: update.slot, |
| 44 | + gas_used: None, |
| 45 | + fee_recipient: Some(update.fee_recipient), |
| 46 | + proposer_fee_recipient: None, |
| 47 | + optimistic_submission: None, |
| 48 | + block_number: update.block_number, |
| 49 | + extra_data: None, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +pub fn parse_message( |
| 54 | + message: Message, |
| 55 | + relay_name: &str, |
| 56 | + publisher_name: &str, |
| 57 | + publisher_type: PublisherType, |
| 58 | +) -> eyre::Result<Option<ScrapedRelayBlockBid>> { |
| 59 | + match message { |
| 60 | + Message::Binary(data) => { |
| 61 | + let update = |
| 62 | + TopBidUpdate::from_ssz_bytes(&data).map_err(|_| eyre!("unable to deserialize"))?; |
| 63 | + debug!("Got message: {:?}", update); |
| 64 | + let bid = block_bid_from_update(update, relay_name, publisher_name, publisher_type); |
| 65 | + Ok(Some(bid)) |
| 66 | + } |
| 67 | + _ => { |
| 68 | + eyre::bail!("Unhandled {publisher_type:?} WS message: {message:?}"); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments