-
Notifications
You must be signed in to change notification settings - Fork 35
Flashblocks #98
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
ferranbt
wants to merge
34
commits into
main
Choose a base branch
from
flashblocks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Flashblocks #98
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
10efd2d
Initial for fb
ferranbt e6fc052
Merge main
ferranbt e7673c1
Rebase main
ferranbt 46f5f2d
Rebase again
ferranbt bb8736e
Merge main
ferranbt 00e3622
Merge
ferranbt 99958e1
Use separated crate
ferranbt 44390ba
Some moves
ferranbt 32ab828
Add diff
ferranbt 245b75e
Add partial block building
ferranbt 0074fed
Add lib
ferranbt 6ada749
Rebased main
ferranbt 397628c
Prov update alloy deps
ferranbt 9c26323
Fix imports and make fb service public
ferranbt 0b089a3
Add log
ferranbt e1d6068
Add better print statements
ferranbt ada938c
Add outbound server
ferranbt 691a11e
add wss support (#106)
cody-wang-cb ef4a70c
Add debug statement
ferranbt 6a2a5f7
Merge develop
ferranbt e74a1fd
Bump rustTls version
ferranbt 9bfd6f3
deps: alloy 0.12.5 (#137)
efbig e287ac8
Rebase
ferranbt 67f5974
Fix lint
ferranbt b4de9bf
Rebase main
ferranbt e20dbc5
Fix lint
ferranbt dac67b0
reth 1.3.11
cody-wang-cb e224906
reth 1.3.9 and metrics
cody-wang-cb 6088534
Merge pull request #176 from cody-wang-cb/cody/reth-1.3.9
avalonche 62723d7
metrics updates
jowparks 616df18
Update metrics and error handling
jowparks 2c867b0
add code commenting, remove unneeded error metric, PR comments
jowparks 73e7f02
Merge pull request #178 from jowparks/metrics-updates
avalonche 7691533
Bump deps (#189)
SozinM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use super::primitives::FlashblocksPayloadV1; | ||
use futures::StreamExt; | ||
use tokio::sync::mpsc; | ||
use tokio_tungstenite::{connect_async, tungstenite::Message}; | ||
use tracing::{error, info}; | ||
use url::Url; | ||
|
||
pub struct FlashblocksReceiverService { | ||
url: Url, | ||
sender: mpsc::Sender<FlashblocksPayloadV1>, | ||
} | ||
|
||
impl FlashblocksReceiverService { | ||
pub fn new( | ||
url: String, | ||
sender: mpsc::Sender<FlashblocksPayloadV1>, | ||
) -> Result<Self, url::ParseError> { | ||
Ok(Self { | ||
url: Url::parse(&url)?, | ||
sender, | ||
}) | ||
} | ||
|
||
pub async fn run(self) { | ||
loop { | ||
match self.connect_and_handle().await { | ||
Ok(()) => break, | ||
Err(e) => { | ||
error!( | ||
message = "Flashblocks receiver connection error, retrying in 5 seconds", | ||
error = %e | ||
); | ||
tokio::time::sleep(std::time::Duration::from_secs(5)).await; | ||
} | ||
} | ||
} | ||
} | ||
|
||
async fn connect_and_handle(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { | ||
let (ws_stream, _) = connect_async(self.url.as_str()).await?; | ||
let (_, mut read) = ws_stream.split(); | ||
|
||
info!("Connected to Flashblocks receiver at {}", self.url); | ||
|
||
while let Some(msg) = read.next().await { | ||
let msg = msg?; | ||
match msg { | ||
Message::Text(text) => { | ||
if let Ok(flashblocks_msg) = serde_json::from_str::<FlashblocksPayloadV1>(&text) | ||
// TODO: Version this | ||
{ | ||
self.sender.send(flashblocks_msg).await?; | ||
} | ||
} | ||
_ => continue, | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
mod inbound; | ||
mod outbound; | ||
pub mod primitives; | ||
mod service; | ||
use inbound::FlashblocksReceiverService; | ||
use outbound::FlashblocksOutboundService; | ||
use std::net::SocketAddr; | ||
use tokio::sync::mpsc; | ||
|
||
pub use service::FlashblocksService; | ||
|
||
pub struct Flashblocks {} | ||
|
||
impl Flashblocks { | ||
pub fn run(builder_url: String, outbound_addr: String) -> eyre::Result<FlashblocksService> { | ||
let (tx, rx) = mpsc::channel(100); | ||
let (outbound_tx, outbound_rx) = mpsc::channel(100); | ||
|
||
let receiver = FlashblocksReceiverService::new(builder_url, tx)?; | ||
|
||
tokio::spawn(async move { | ||
let _ = receiver.run().await; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a need for error handling here? |
||
}); | ||
|
||
// Create and spawn the outbound WebSocket service | ||
let outbound_service = FlashblocksOutboundService::new(outbound_rx); | ||
let addr: SocketAddr = outbound_addr | ||
.parse() | ||
.map_err(|e| eyre::eyre!("Invalid outbound address {}: {}", outbound_addr, e))?; | ||
|
||
tokio::spawn(async move { | ||
if let Err(e) = outbound_service.run(addr).await { | ||
tracing::error!("Outbound service error: {}", e); | ||
} | ||
}); | ||
|
||
let service = FlashblocksService::new(outbound_tx); | ||
let mut service_handle = service.clone(); | ||
tokio::spawn(async move { | ||
service_handle.run(rx).await; | ||
}); | ||
|
||
Ok(service) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use super::primitives::FlashblocksPayloadV1; | ||
use futures::{SinkExt, StreamExt}; | ||
use std::{net::SocketAddr, sync::Arc}; | ||
use tokio::{ | ||
net::TcpListener, | ||
sync::{Mutex, mpsc}, | ||
}; | ||
use tokio_tungstenite::{accept_async, tungstenite::Message}; | ||
use tracing::{debug, error, info}; | ||
|
||
type WebSocketSink = | ||
futures::stream::SplitSink<tokio_tungstenite::WebSocketStream<tokio::net::TcpStream>, Message>; | ||
|
||
pub struct FlashblocksOutboundService { | ||
clients: Arc<Mutex<Vec<WebSocketSink>>>, | ||
receiver: mpsc::Receiver<FlashblocksPayloadV1>, | ||
} | ||
|
||
impl FlashblocksOutboundService { | ||
pub fn new(receiver: mpsc::Receiver<FlashblocksPayloadV1>) -> Self { | ||
Self { | ||
clients: Arc::new(Mutex::new(Vec::new())), | ||
receiver, | ||
} | ||
} | ||
|
||
pub async fn run(mut self, addr: SocketAddr) -> Result<(), Box<dyn std::error::Error>> { | ||
let listener = TcpListener::bind(&addr).await?; | ||
info!("Outbound WebSocket server listening on: {}", addr); | ||
|
||
let clients = self.clients.clone(); | ||
|
||
// Spawn a task to handle new WebSocket connections | ||
tokio::spawn(async move { | ||
while let Ok((stream, addr)) = listener.accept().await { | ||
debug!("New WebSocket connection from: {}", addr); | ||
let clients = clients.clone(); | ||
|
||
tokio::spawn(async move { | ||
match accept_async(stream).await { | ||
Ok(ws_stream) => { | ||
let (sink, _) = ws_stream.split(); | ||
clients.lock().await.push(sink); | ||
debug!("Client added: {}", addr); | ||
} | ||
Err(e) => error!("Error accepting WebSocket connection: {}", e), | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
// Handle incoming messages from the channel and broadcast them | ||
while let Some(payload) = self.receiver.recv().await { | ||
let message = match serde_json::to_string(&payload) { | ||
Ok(msg) => Message::Text(msg.into()), | ||
Err(e) => { | ||
error!("Failed to serialize payload: {}", e); | ||
continue; | ||
} | ||
}; | ||
|
||
let mut clients = self.clients.lock().await; | ||
clients.retain_mut(|sink| { | ||
let send_future = sink.send(message.clone()); | ||
match futures::executor::block_on(send_future) { | ||
Ok(_) => true, | ||
Err(e) => { | ||
error!("Failed to send message to client: {}", e); | ||
false // Remove failed client | ||
} | ||
} | ||
}); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you add docs to README and .env.example