Skip to content
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

refactor: move all alloy-compat code to a standalone module #8192

Merged
merged 4 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 0 additions & 45 deletions crates/primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,51 +147,6 @@ impl Deref for Block {
}
}

#[cfg(feature = "alloy-compat")]
impl TryFrom<alloy_rpc_types::Block> for Block {
type Error = alloy_rpc_types::ConversionError;

fn try_from(block: alloy_rpc_types::Block) -> Result<Self, Self::Error> {
use alloy_rpc_types::ConversionError;

let body = {
let transactions: Result<Vec<TransactionSigned>, ConversionError> = match block
.transactions
{
alloy_rpc_types::BlockTransactions::Full(transactions) => transactions
.into_iter()
.map(|tx| {
let signature = tx.signature.ok_or(ConversionError::MissingSignature)?;
Ok(TransactionSigned::from_transaction_and_signature(
tx.try_into()?,
crate::Signature {
r: signature.r,
s: signature.s,
odd_y_parity: signature
.y_parity
.unwrap_or(alloy_rpc_types::Parity(false))
.0,
},
))
})
.collect(),
alloy_rpc_types::BlockTransactions::Hashes(_) |
alloy_rpc_types::BlockTransactions::Uncle => {
return Err(ConversionError::MissingFullTransactions)
}
};
transactions?
};

Ok(Self {
header: block.header.try_into()?,
body,
ommers: Default::default(),
withdrawals: block.withdrawals.map(Into::into),
})
}
}

/// Sealed block with senders recovered from transactions.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct BlockWithSenders {
Expand Down
93 changes: 93 additions & 0 deletions crates/primitives/src/compat/alloy_compat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#[cfg(feature = "alloy-compat")]
use crate::{Block, Header, TransactionSigned};
#[cfg(feature = "alloy-compat")]
impl TryFrom<alloy_rpc_types::Block> for Block {
type Error = alloy_rpc_types::ConversionError;

fn try_from(block: alloy_rpc_types::Block) -> Result<Self, Self::Error> {
use alloy_rpc_types::ConversionError;

let body = {
let transactions: Result<Vec<TransactionSigned>, ConversionError> = match block
.transactions
{
alloy_rpc_types::BlockTransactions::Full(transactions) => transactions
.into_iter()
.map(|tx| {
let signature = tx.signature.ok_or(ConversionError::MissingSignature)?;
Ok(TransactionSigned::from_transaction_and_signature(
tx.try_into()?,
crate::Signature {
r: signature.r,
s: signature.s,
odd_y_parity: signature
.y_parity
.unwrap_or(alloy_rpc_types::Parity(false))
.0,
},
))
})
.collect(),
alloy_rpc_types::BlockTransactions::Hashes(_) |
alloy_rpc_types::BlockTransactions::Uncle => {
return Err(ConversionError::MissingFullTransactions)
}
};
transactions?
};

Ok(Self {
header: block.header.try_into()?,
body,
ommers: Default::default(),
withdrawals: block.withdrawals.map(Into::into),
})
}
}

#[cfg(feature = "alloy-compat")]
impl TryFrom<alloy_rpc_types::Header> for Header {
type Error = alloy_rpc_types::ConversionError;

fn try_from(header: alloy_rpc_types::Header) -> Result<Self, Self::Error> {
use alloy_rpc_types::ConversionError;

Ok(Self {
base_fee_per_gas: header
.base_fee_per_gas
.map(|base_fee_per_gas| {
base_fee_per_gas.try_into().map_err(ConversionError::BaseFeePerGasConversion)
})
.transpose()?,
beneficiary: header.miner,
blob_gas_used: header
.blob_gas_used
.map(|blob_gas_used| {
blob_gas_used.try_into().map_err(ConversionError::BlobGasUsedConversion)
})
.transpose()?,
difficulty: header.difficulty,
excess_blob_gas: header
.excess_blob_gas
.map(|excess_blob_gas| {
excess_blob_gas.try_into().map_err(ConversionError::ExcessBlobGasConversion)
})
.transpose()?,
extra_data: header.extra_data,
gas_limit: header.gas_limit.try_into().map_err(ConversionError::GasLimitConversion)?,
gas_used: header.gas_used.try_into().map_err(ConversionError::GasUsedConversion)?,
logs_bloom: header.logs_bloom,
mix_hash: header.mix_hash.unwrap_or_default(),
nonce: u64::from_be_bytes(header.nonce.unwrap_or_default().0),
number: header.number.ok_or(ConversionError::MissingBlockNumber)?,
ommers_hash: header.uncles_hash,
parent_beacon_block_root: header.parent_beacon_block_root,
parent_hash: header.parent_hash,
receipts_root: header.receipts_root,
state_root: header.state_root,
timestamp: header.timestamp,
transactions_root: header.transactions_root,
withdrawals_root: header.withdrawals_root,
})
}
}
47 changes: 0 additions & 47 deletions crates/primitives/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,53 +485,6 @@ impl Decodable for Header {
}
}

#[cfg(feature = "alloy-compat")]
impl TryFrom<alloy_rpc_types::Header> for Header {
type Error = alloy_rpc_types::ConversionError;

fn try_from(header: alloy_rpc_types::Header) -> Result<Self, Self::Error> {
use alloy_rpc_types::ConversionError;

Ok(Self {
base_fee_per_gas: header
.base_fee_per_gas
.map(|base_fee_per_gas| {
base_fee_per_gas.try_into().map_err(ConversionError::BaseFeePerGasConversion)
})
.transpose()?,
beneficiary: header.miner,
blob_gas_used: header
.blob_gas_used
.map(|blob_gas_used| {
blob_gas_used.try_into().map_err(ConversionError::BlobGasUsedConversion)
})
.transpose()?,
difficulty: header.difficulty,
excess_blob_gas: header
.excess_blob_gas
.map(|excess_blob_gas| {
excess_blob_gas.try_into().map_err(ConversionError::ExcessBlobGasConversion)
})
.transpose()?,
extra_data: header.extra_data,
gas_limit: header.gas_limit.try_into().map_err(ConversionError::GasLimitConversion)?,
gas_used: header.gas_used.try_into().map_err(ConversionError::GasUsedConversion)?,
logs_bloom: header.logs_bloom,
mix_hash: header.mix_hash.unwrap_or_default(),
nonce: u64::from_be_bytes(header.nonce.unwrap_or_default().0),
number: header.number.ok_or(ConversionError::MissingBlockNumber)?,
ommers_hash: header.uncles_hash,
parent_beacon_block_root: header.parent_beacon_block_root,
parent_hash: header.parent_hash,
receipts_root: header.receipts_root,
state_root: header.state_root,
timestamp: header.timestamp,
transactions_root: header.transactions_root,
withdrawals_root: header.withdrawals_root,
})
}
}

/// Errors that can occur during header sanity checks.
#[derive(thiserror::Error, Debug, PartialEq, Eq, Clone)]
pub enum HeaderValidationError {
Expand Down
1 change: 0 additions & 1 deletion crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ mod storage;
pub mod transaction;
pub mod trie;
mod withdrawal;

pub use account::{Account, Bytecode};
#[cfg(any(test, feature = "arbitrary"))]
pub use block::{generate_valid_header, valid_header_strategy};
Expand Down