Skip to content

Commit

Permalink
updates tx-poller to latest tx cache API (#47)
Browse files Browse the repository at this point in the history
This PR updates the tx-poller in the Zenith builder to be compatible with the latest tx cache changes.
  • Loading branch information
dylanlott authored Oct 21, 2024
1 parent 097c34f commit c88f23b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
Cargo.lock
.vscode/launch.json
14 changes: 6 additions & 8 deletions crates/builder/src/tasks/tx_poller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ pub use crate::config::BuilderConfig;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TxPoolResponse {
#[serde(rename = "key")]
key: TxHash,
#[serde(rename = "value")]
value: TxEnvelope,
transactions: Vec<TxEnvelope>,
}

/// Implements a poller for the block builder to pull transactions from the transaction pool.
Expand All @@ -42,12 +39,13 @@ impl TxPoller {
/// unique transactions that haven't been seen before are sent into the builder pipeline.
pub async fn check_tx_pool(&mut self) -> Result<Vec<TxEnvelope>, Error> {
let mut unique: Vec<TxEnvelope> = Vec::new();
let url: Url = Url::parse(&self.config.tx_pool_url)?.join("get")?;

let url: Url = Url::parse(&self.config.tx_pool_url)?.join("transactions")?;
let result = self.client.get(url).send().await?;
let parsed: Vec<TxPoolResponse> = from_slice(&result.bytes().await?)?;
let response: TxPoolResponse = from_slice(result.text().await?.as_bytes())?;

parsed.iter().for_each(|entry| {
self.check_cache(entry.value.clone(), &mut unique);
response.transactions.iter().for_each(|entry| {
self.check_cache(entry.clone(), &mut unique);
});

Ok(unique)
Expand Down
72 changes: 38 additions & 34 deletions crates/builder/tests/tx_poller_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,49 @@ mod tests {
use alloy::consensus::{SignableTransaction, TxEip1559, TxEnvelope};
use alloy::signers::{local::PrivateKeySigner, SignerSync};
use alloy_primitives::{bytes, Address, TxKind, U256};

use builder::config::BuilderConfig;
use builder::tasks::{block::BlockBuilder, tx_poller};
use reqwest::Url;
use eyre::Result;

#[ignore = "integration test"]
#[tokio::test]
async fn test_tx_roundtrip() {
// create a new test environment
let (_, config) = setup_test_builder().await;
async fn test_tx_roundtrip() -> Result<()> {
// Create a new test environment
let (_, config) = setup_test_builder().await?;

// Post a transaction to the cache
post_tx(&config).await?;

// Create a new poller
let mut poller = tx_poller::TxPoller::new(&config);

// post a tx to the cache
post_tx(config.clone()).await;
// Fetch transactions the pool
let transactions = poller.check_tx_pool().await?;

// assert that we parsed at least one transaction
let got = tx_poller::TxPoller::new(&config).check_tx_pool().await.unwrap();
assert!(!got.is_empty());
// Ensure at least one transaction exists
assert!(!transactions.is_empty());

Ok(())
}

async fn post_tx(config: BuilderConfig) {
// create a new test environment
async fn post_tx(config: &BuilderConfig) -> Result<()> {
let client = reqwest::Client::new();

// create a new signed test transaction
let wallet = PrivateKeySigner::random();
let tx_envelope = new_test_tx(&wallet);
let tx_envelope = new_test_tx(&wallet)?;

let url = format!("{}/transactions", config.tx_pool_url);
let response = client.post(&url).json(&tx_envelope).send().await?;

let url: Url = Url::parse(&config.tx_pool_url).unwrap().join("add").unwrap();
if !response.status().is_success() {
let error_text = response.text().await?;
eyre::bail!("Failed to post transaction: {}", error_text);
}

// send that transaction to ensure there is at least one tx in pool to parse
let _ = client.post(url).json(&tx_envelope).send().await.unwrap();
Ok(())
}

// returns a new signed test transaction with blank values
fn new_test_tx(wallet: &PrivateKeySigner) -> TxEnvelope {
// Returns a new signed test transaction with default values
fn new_test_tx(wallet: &PrivateKeySigner) -> Result<TxEnvelope> {
let tx = TxEip1559 {
chain_id: 17001,
nonce: 1,
Expand All @@ -50,38 +58,34 @@ mod tests {
input: bytes!(""),
..Default::default()
};
let tx_hash = wallet.sign_hash_sync(&tx.signature_hash()).unwrap();
TxEnvelope::Eip1559(tx.into_signed(tx_hash))
let signature = wallet.sign_hash_sync(&tx.signature_hash())?;
Ok(TxEnvelope::Eip1559(tx.into_signed(signature)))
}

// sets up a block builder with test values
async fn setup_test_builder() -> (BlockBuilder, BuilderConfig) {
// Sets up a block builder with test values
async fn setup_test_builder() -> Result<(BlockBuilder, BuilderConfig)> {
let config = BuilderConfig {
host_chain_id: 17000,
ru_chain_id: 17001,
host_rpc_url: "http://rpc.api.signet.sh".into(),
zenith_address: Address::from_str("0x0000000000000000000000000000000000000000")
.unwrap(),
host_rpc_url: "http://rpc.holesky.signet.sh".into(),
zenith_address: Address::default(),
quincey_url: "http://localhost:8080".into(),
builder_port: 8080,
sequencer_key: None,
builder_key: "0000000000000000000000000000000000000000000000000000000000000000".into(),
incoming_transactions_buffer: 1,
block_confirmation_buffer: 1,
builder_rewards_address: Address::from_str(
"0x0000000000000000000000000000000000000000",
)
.unwrap(),
builder_rewards_address: Address::default(),
rollup_block_gas_limit: 100_000,
tx_pool_url: "http://localhost:9000".into(),
tx_pool_url: "http://localhost:9000/".into(),
tx_pool_cache_duration: 5,
tx_pool_poll_interval: 5,
oauth_client_id: "some_client_id".into(),
oauth_client_secret: "some_client_secret".into(),
oauth_authenticate_url: "http://localhost:8080".into(),
oauth_token_url: "http://localhost:8080".into(),
oauth_audience: "some_audience".into(),
oauth_audience: "https://transactions.holesky.signet.sh".into(),
};
(BlockBuilder::new(&config), config)
Ok((BlockBuilder::new(&config), config))
}
}

0 comments on commit c88f23b

Please sign in to comment.