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

chore: simplify optimism tx compat #8925

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 21 additions & 25 deletions crates/primitives/src/alloy_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,30 +182,27 @@ impl TryFrom<alloy_rpc_types::Transaction> for Transaction {
}))
}
#[cfg(feature = "optimism")]
Some(TxType::Deposit) => Ok(Self::Deposit(crate::transaction::TxDeposit {
source_hash: tx
Some(TxType::Deposit) => {
let fields = tx
.other
.get_deserialized::<String>("sourceHash")
.ok_or_else(|| ConversionError::Custom("MissingSourceHash".to_string()))?
.map_err(|_| ConversionError::Custom("MissingSourceHash".to_string()))?
.parse()
.map_err(|_| ConversionError::Custom("InvalidSourceHash".to_string()))?,
from: tx.from,
to: TxKind::from(tx.to),
mint: Option::transpose(
tx.other.get_deserialized::<alloy_primitives::U128>("mint"),
)
.map_err(|_| ConversionError::Custom("MissingMintValue".to_string()))?
.map(|num| num.to::<u128>())
.filter(|num| *num > 0),
value: tx.value,
gas_limit: tx
.gas
.try_into()
.map_err(|_| ConversionError::Eip2718Error(RlpError::Overflow.into()))?,
is_system_transaction: tx.from == crate::constants::OP_SYSTEM_TX_FROM_ADDR,
input: tx.input,
})),
.deserialize_into::<alloy_rpc_types::optimism::OptimismTransactionFields>()
.map_err(|e| ConversionError::Custom(e.to_string()))?;
Ok(Self::Deposit(crate::transaction::TxDeposit {
source_hash: fields
.source_hash
.ok_or_else(|| ConversionError::Custom("MissingSourceHash".to_string()))?,
from: tx.from,
to: TxKind::from(tx.to),
mint: fields.mint.map(|n| n.to::<u128>()).filter(|n| *n != 0),
value: tx.value,
gas_limit: tx
.gas
.try_into()
.map_err(|_| ConversionError::Eip2718Error(RlpError::Overflow.into()))?,
is_system_transaction: fields.is_system_tx.unwrap_or(false),
input: tx.input,
}))
}
}
}
}
Expand Down Expand Up @@ -272,14 +269,14 @@ impl TryFrom<alloy_rpc_types::Signature> for Signature {
}

#[cfg(test)]
#[cfg(feature = "optimism")]
mod tests {
use super::*;
use alloy_primitives::{B256, U256};
use alloy_rpc_types::Transaction as AlloyTransaction;
use revm_primitives::{address, Address};

#[test]
#[cfg(feature = "optimism")]
fn optimism_deposit_tx_conversion_no_mint() {
let input = r#"{
"blockHash": "0xef664d656f841b5ad6a2b527b963f1eb48b97d7889d742f6cbff6950388e24cd",
Expand Down Expand Up @@ -330,7 +327,6 @@ mod tests {
}

#[test]
#[cfg(feature = "optimism")]
fn optimism_deposit_tx_conversion_mint() {
let input = r#"{
"blockHash": "0x7194f63b105e93fb1a27c50d23d62e422d4185a68536c55c96284911415699b2",
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ impl BlockWithSenders {
/// Sealed Ethereum full block.
///
/// Withdrawals can be optionally included at the end of the RLP encoded message.
#[derive_arbitrary(rlp)]
#[derive_arbitrary(rlp 32)]
#[derive(
Debug,
Clone,
Expand Down
5 changes: 2 additions & 3 deletions crates/storage/codecs/derive/src/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ pub fn maybe_generate_tests(args: TokenStream, ast: &DeriveInput) -> TokenStream

#[test]
fn malformed_rlp_header_check() {
use rand::RngCore;
use rand::RngCore;

// get random instance of type
let mut raw = [0u8;1024];
let mut raw = [0u8; 1024];
rand::thread_rng().fill_bytes(&mut raw);
let mut unstructured = arbitrary::Unstructured::new(&raw[..]);
let val = <super::#type_ident as arbitrary::Arbitrary>::arbitrary(&mut unstructured);
Expand All @@ -72,7 +72,6 @@ pub fn maybe_generate_tests(args: TokenStream, ast: &DeriveInput) -> TokenStream
let res = super::#type_ident::decode(&mut b.as_ref());
assert!(res.is_err(), "malformed header was decoded");
}

});
} else if let Ok(num) = arg.to_string().parse() {
default_cases = num;
Expand Down
Loading