-
Notifications
You must be signed in to change notification settings - Fork 0
updates the tx-poller to stream transactions #60
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
Closed
dylanlott
wants to merge
3
commits into
main
from
03-07-updates_the_tx-poller_to_stream_transactions
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
@@ -1,39 +1,94 @@ | ||
//! Transaction service responsible for fetching and sending trasnsactions to the simulator. | ||
use crate::config::BuilderConfig; | ||
use alloy::consensus::TxEnvelope; | ||
use eyre::Error; | ||
use reqwest::{Client, Url}; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::from_slice; | ||
use tokio::{sync::mpsc, task::JoinHandle, time}; | ||
use tracing::{Instrument, debug, trace}; | ||
|
||
pub use crate::config::BuilderConfig; | ||
|
||
/// Response from the tx-pool endpoint. | ||
/// Models a response from the transaction pool. | ||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct TxPoolResponse { | ||
/// Holds the transactions property as a list on the response. | ||
transactions: Vec<TxEnvelope>, | ||
} | ||
|
||
/// Implements a poller for the block builder to pull transactions from the transaction pool. | ||
#[derive(Debug)] | ||
#[derive(Debug, Clone)] | ||
pub struct TxPoller { | ||
/// config for the builder | ||
/// Config values from the Builder. | ||
pub config: BuilderConfig, | ||
/// Reqwest client for fetching transactions from the tx-pool | ||
/// Reqwest Client for fetching transactions from the cache. | ||
pub client: Client, | ||
/// Defines the interval at which the service should poll the cache. | ||
pub poll_interval_ms: u64, | ||
} | ||
|
||
/// TxPoller implements a poller that fetches unique transactions from the transaction pool. | ||
/// [`TxPoller`] implements a poller task that fetches unique transactions from the transaction pool. | ||
impl TxPoller { | ||
/// returns a new TxPoller with the given config. | ||
/// Returns a new [`TxPoller`] with the given config. | ||
/// * Defaults to 1000ms poll interval (1s). | ||
pub fn new(config: &BuilderConfig) -> Self { | ||
Self { config: config.clone(), client: Client::new() } | ||
Self { config: config.clone(), client: Client::new(), poll_interval_ms: 1000 } | ||
} | ||
|
||
/// Returns a new [`TxPoller`] with the given config and cache polling interval in milliseconds. | ||
pub fn new_with_poll_interval_ms(config: &BuilderConfig, poll_interval_ms: u64) -> Self { | ||
Self { config: config.clone(), client: Client::new(), poll_interval_ms } | ||
} | ||
|
||
/// polls the tx-pool for unique transactions and evicts expired transactions. | ||
/// unique transactions that haven't been seen before are sent into the builder pipeline. | ||
/// Polls the transaction cache for transactions. | ||
pub async fn check_tx_cache(&mut self) -> Result<Vec<TxEnvelope>, Error> { | ||
let url: Url = Url::parse(&self.config.tx_pool_url)?.join("transactions")?; | ||
let result = self.client.get(url).send().await?; | ||
let response: TxPoolResponse = from_slice(result.text().await?.as_bytes())?; | ||
Ok(response.transactions) | ||
} | ||
|
||
async fn task_future(mut self, outbound: mpsc::UnboundedSender<TxEnvelope>) { | ||
loop { | ||
let span = tracing::debug_span!("TxPoller::loop", url = %self.config.tx_pool_url); | ||
|
||
// Enter the span for the next check. | ||
let _guard = span.enter(); | ||
|
||
// Check this here to avoid making the web request if we know | ||
// we don't need the results. | ||
if outbound.is_closed() { | ||
trace!("No receivers left, shutting down"); | ||
break; | ||
} | ||
// exit the span after the check. | ||
drop(_guard); | ||
|
||
match self.check_tx_cache().instrument(span.clone()).await { | ||
Ok(transactions) => { | ||
let _guard = span.entered(); | ||
debug!(count = ?transactions.len(), "found transactions"); | ||
for tx in transactions.into_iter() { | ||
if outbound.send(tx).is_err() { | ||
// If there are no receivers, we can shut down | ||
trace!("No receivers left, shutting down"); | ||
break; | ||
} | ||
} | ||
} | ||
// If fetching was an error, we log and continue. We expect | ||
// these to be transient network issues. | ||
Err(e) => { | ||
debug!(error = %e, "Error fetching transactions"); | ||
} | ||
} | ||
time::sleep(time::Duration::from_millis(self.poll_interval_ms)).await; | ||
} | ||
} | ||
|
||
/// Spawns a task that continuously polls the cache for transactions and sends any it finds to its sender. | ||
pub fn spawn(self) -> (mpsc::UnboundedReceiver<TxEnvelope>, JoinHandle<()>) { | ||
let (outbound, inbound) = mpsc::unbounded_channel(); | ||
let jh = tokio::spawn(self.task_future(outbound)); | ||
(inbound, jh) | ||
} | ||
} |
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.
nit: typo:
transactions