Skip to content

Commit

Permalink
Merge branch 'main' into dup-field
Browse files Browse the repository at this point in the history
  • Loading branch information
jsvisa authored Oct 19, 2024
2 parents 50d43ab + d910f4e commit 5e7df1d
Show file tree
Hide file tree
Showing 12 changed files with 393 additions and 312 deletions.
4 changes: 1 addition & 3 deletions crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,7 @@ mod serde_from {
//!
//! We serialize via [`TaggedTxEnvelope`] and deserialize via
//! [`MaybeTaggedTxEnvelope`].
use crate::{Signed, TxEip1559, TxEip2930, TxEip4844Variant, TxEip7702, TxLegacy};

use super::TxEnvelope;
use crate::{Signed, TxEip1559, TxEip2930, TxEip4844Variant, TxEip7702, TxEnvelope, TxLegacy};

#[derive(Debug, serde::Deserialize)]
#[serde(untagged)]
Expand Down
84 changes: 83 additions & 1 deletion crates/consensus/src/transaction/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ use crate::{
/// 4. EIP4844 [`TxEip4844Variant`]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type"))]
#[cfg_attr(
feature = "serde",
serde(
from = "serde_from::MaybeTaggedTypedTransaction",
into = "serde_from::TaggedTypedTransaction"
)
)]
#[doc(alias = "TypedTx", alias = "TxTyped", alias = "TransactionTyped")]
pub enum TypedTransaction {
/// Legacy transaction
Expand Down Expand Up @@ -300,3 +306,79 @@ impl<T: From<TxEnvelope>> From<TxEnvelope> for alloy_serde::WithOtherFields<T> {
Self::new(value.into())
}
}

#[cfg(feature = "serde")]
mod serde_from {
//! NB: Why do we need this?
//!
//! Because the tag may be missing, we need an abstraction over tagged (with
//! type) and untagged (always legacy). This is
//! [`MaybeTaggedTypedTransaction`].
//!
//! The tagged variant is [`TaggedTypedTransaction`], which always has a
//! type tag.
//!
//! We serialize via [`TaggedTypedTransaction`] and deserialize via
//! [`MaybeTaggedTypedTransaction`].
use crate::{TxEip1559, TxEip2930, TxEip4844Variant, TxEip7702, TxLegacy, TypedTransaction};

#[derive(Debug, serde::Deserialize)]
#[serde(untagged)]
pub(crate) enum MaybeTaggedTypedTransaction {
Tagged(TaggedTypedTransaction),
Untagged(TxLegacy),
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(tag = "type")]
pub(crate) enum TaggedTypedTransaction {
/// Legacy transaction
#[serde(rename = "0x00", alias = "0x0")]
Legacy(TxLegacy),
/// EIP-2930 transaction
#[serde(rename = "0x01", alias = "0x1")]
Eip2930(TxEip2930),
/// EIP-1559 transaction
#[serde(rename = "0x02", alias = "0x2")]
Eip1559(TxEip1559),
/// EIP-4844 transaction
#[serde(rename = "0x03", alias = "0x3")]
Eip4844(TxEip4844Variant),
/// EIP-7702 transaction
#[serde(rename = "0x04", alias = "0x4")]
Eip7702(TxEip7702),
}

impl From<MaybeTaggedTypedTransaction> for TypedTransaction {
fn from(value: MaybeTaggedTypedTransaction) -> Self {
match value {
MaybeTaggedTypedTransaction::Tagged(tagged) => tagged.into(),
MaybeTaggedTypedTransaction::Untagged(tx) => Self::Legacy(tx),
}
}
}

impl From<TaggedTypedTransaction> for TypedTransaction {
fn from(value: TaggedTypedTransaction) -> Self {
match value {
TaggedTypedTransaction::Legacy(signed) => Self::Legacy(signed),
TaggedTypedTransaction::Eip2930(signed) => Self::Eip2930(signed),
TaggedTypedTransaction::Eip1559(signed) => Self::Eip1559(signed),
TaggedTypedTransaction::Eip4844(signed) => Self::Eip4844(signed),
TaggedTypedTransaction::Eip7702(signed) => Self::Eip7702(signed),
}
}
}

impl From<TypedTransaction> for TaggedTypedTransaction {
fn from(value: TypedTransaction) -> Self {
match value {
TypedTransaction::Legacy(signed) => Self::Legacy(signed),
TypedTransaction::Eip2930(signed) => Self::Eip2930(signed),
TypedTransaction::Eip1559(signed) => Self::Eip1559(signed),
TypedTransaction::Eip4844(signed) => Self::Eip4844(signed),
TypedTransaction::Eip7702(signed) => Self::Eip7702(signed),
}
}
}
}
1 change: 1 addition & 0 deletions crates/provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ tower-http = { workspace = true, features = [
http-body-util.workspace = true
http.workspace = true
alloy-rpc-types-engine = { workspace = true, features = ["jwt"] }
ci_info.workspace = true

[features]
default = ["reqwest", "reqwest-default-tls"]
Expand Down
8 changes: 0 additions & 8 deletions crates/provider/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,6 @@ mod tests {
use alloy_primitives::U256;
use std::{future::Future, time::Duration};

fn init_tracing() {
let _ = tracing_subscriber::fmt::try_init();
}

async fn timeout<T: Future>(future: T) -> T::Output {
try_timeout(future).await.expect("Timeout")
}
Expand All @@ -207,8 +203,6 @@ mod tests {
yield_block(true).await;
}
async fn yield_block(ws: bool) {
init_tracing();

let anvil = Anvil::new().spawn();

let url = if ws { anvil.ws_endpoint() } else { anvil.endpoint() };
Expand Down Expand Up @@ -240,8 +234,6 @@ mod tests {
// Make sure that we can process more blocks than fits in the cache.
const BLOCKS_TO_MINE: usize = BLOCK_CACHE_SIZE.get() + 1;

init_tracing();

let anvil = Anvil::new().spawn();

let url = if ws { anvil.ws_endpoint() } else { anvil.endpoint() };
Expand Down
52 changes: 29 additions & 23 deletions crates/provider/src/ext/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,39 +86,45 @@ where
#[cfg(test)]
mod test {
use super::*;
use crate::ProviderBuilder;
use crate::{ext::test::async_ci_only, ProviderBuilder};
use alloy_node_bindings::{utils::run_with_tempdir, Geth};

#[tokio::test]
async fn node_info() {
run_with_tempdir("geth-test-", |temp_dir| async move {
let geth = Geth::new().disable_discovery().data_dir(temp_dir).spawn();
let provider = ProviderBuilder::new().on_http(geth.endpoint_url());
let node_info = provider.node_info().await.unwrap();
assert!(node_info.enode.starts_with("enode://"));
async_ci_only(|| async move {
run_with_tempdir("geth-test-", |temp_dir| async move {
let geth = Geth::new().disable_discovery().data_dir(temp_dir).spawn();
let provider = ProviderBuilder::new().on_http(geth.endpoint_url());
let node_info = provider.node_info().await.unwrap();
assert!(node_info.enode.starts_with("enode://"));
})
.await;
})
.await;
}

#[tokio::test]
async fn admin_peers() {
run_with_tempdir("geth-test-1", |temp_dir_1| async move {
run_with_tempdir("geth-test-2", |temp_dir_2| async move {
let geth1 = Geth::new().disable_discovery().data_dir(&temp_dir_1).spawn();
let mut geth2 =
Geth::new().disable_discovery().port(0u16).data_dir(&temp_dir_2).spawn();

let provider1 = ProviderBuilder::new().on_http(geth1.endpoint_url());
let provider2 = ProviderBuilder::new().on_http(geth2.endpoint_url());
let node1_info = provider1.node_info().await.unwrap();
let node1_id = node1_info.id;
let node1_enode = node1_info.enode;

let added = provider2.add_peer(&node1_enode).await.unwrap();
assert!(added);
geth2.wait_to_add_peer(&node1_id).unwrap();
let peers = provider2.peers().await.unwrap();
assert_eq!(peers[0].enode, node1_enode);
async_ci_only(|| async move {
run_with_tempdir("geth-test-1", |temp_dir_1| async move {
run_with_tempdir("geth-test-2", |temp_dir_2| async move {
let geth1 = Geth::new().disable_discovery().data_dir(&temp_dir_1).spawn();
let mut geth2 =
Geth::new().disable_discovery().port(0u16).data_dir(&temp_dir_2).spawn();

let provider1 = ProviderBuilder::new().on_http(geth1.endpoint_url());
let provider2 = ProviderBuilder::new().on_http(geth2.endpoint_url());
let node1_info = provider1.node_info().await.unwrap();
let node1_id = node1_info.id;
let node1_enode = node1_info.enode;

let added = provider2.add_peer(&node1_enode).await.unwrap();
assert!(added);
geth2.wait_to_add_peer(&node1_id).unwrap();
let peers = provider2.peers().await.unwrap();
assert_eq!(peers[0].enode, node1_enode);
})
.await;
})
.await;
})
Expand Down
Loading

0 comments on commit 5e7df1d

Please sign in to comment.