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

replace anvil OP types with op-alloy #8595

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
10 changes: 6 additions & 4 deletions crates/anvil/core/src/eth/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use alloy_consensus::{
};
use alloy_eips::eip2718::{Decodable2718, Eip2718Error, Encodable2718};
use alloy_primitives::{
Address, Bloom, Bytes, Log, Parity, Signature, TxHash, TxKind, B256, U256, U64,
Address, Bloom, Bytes, Log, Parity, Signature, TxHash, TxKind, B256, U128, U256, U64,
};
use alloy_rlp::{length_of_length, Decodable, Encodable, Header};
use alloy_rpc_types::{
Expand Down Expand Up @@ -57,13 +57,15 @@ pub fn transaction_request_to_typed(
other,
} = tx;

// Special case: OP-stack deposit tx
if transaction_type == Some(0x7E) || has_optimism_fields(&other) {
return Some(TypedTransactionRequest::Deposit(DepositTransactionRequest {
from: from.unwrap_or_default(),
source_hash: other.get_deserialized::<B256>("sourceHash")?.ok()?,
kind: to.unwrap_or_default(),
mint: other.get_deserialized::<U256>("mint")?.ok()?,
mint: other
.get_deserialized::<Option<U128>>("mint")?
.ok()?
.map_or(Some(0), |mint| mint.try_into().ok()),
programskillforverification marked this conversation as resolved.
Show resolved Hide resolved
value: value.unwrap_or_default(),
gas_limit: gas.unwrap_or_default(),
is_system_tx: other.get_deserialized::<bool>("isSystemTx")?.ok()?,
Expand Down Expand Up @@ -646,7 +648,7 @@ impl PendingTransaction {
access_list: vec![],
optimism: OptimismFields {
source_hash: Some(*source_hash),
mint: Some(mint.to::<u128>()),
mint: *mint,
is_system_transaction: Some(*is_system_tx),
enveloped_tx: None,
},
Expand Down
51 changes: 37 additions & 14 deletions crates/anvil/core/src/eth/transaction/optimism.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
use alloy_consensus::{SignableTransaction, Signed, Transaction};
use alloy_primitives::{keccak256, Address, Bytes, ChainId, Signature, TxKind, B256, U256};
use alloy_rlp::{
length_of_length, Decodable, Encodable, Error as DecodeError, Header as RlpHeader,
length_of_length, Buf, BufMut, Decodable, Encodable, Error as DecodeError, Header as RlpHeader,
EMPTY_STRING_CODE,
};
use bytes::BufMut;
use serde::{Deserialize, Serialize};
use std::mem;

pub const DEPOSIT_TX_TYPE_ID: u8 = 0x7E;

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct DepositTransactionRequest {
programskillforverification marked this conversation as resolved.
Show resolved Hide resolved
pub source_hash: B256,
pub from: Address,
#[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "TxKind::is_create"))]
pub kind: TxKind,
pub mint: U256,
#[cfg_attr(feature = "serde", serde(default, with = "alloy_serde::quantity::opt"))]
pub mint: Option<u128>,
pub value: U256,
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
pub gas_limit: u128,
pub is_system_tx: bool,
pub input: Bytes,
Expand All @@ -35,7 +40,11 @@ impl DepositTransactionRequest {
self.source_hash.encode(out);
self.from.encode(out);
self.kind.encode(out);
self.mint.encode(out);
if let Some(mint) = self.mint {
mint.encode(out);
} else {
out.put_u8(EMPTY_STRING_CODE);
};
self.value.encode(out);
self.gas_limit.encode(out);
self.is_system_tx.encode(out);
Expand All @@ -48,7 +57,7 @@ impl DepositTransactionRequest {
len += self.source_hash.length();
len += self.from.length();
len += self.kind.length();
len += self.mint.length();
len += self.mint.map_or(1, |mint| mint.length());
len += self.value.length();
len += self.gas_limit.length();
len += self.is_system_tx.length();
Expand All @@ -74,7 +83,12 @@ impl DepositTransactionRequest {
source_hash: Decodable::decode(buf)?,
from: Decodable::decode(buf)?,
kind: Decodable::decode(buf)?,
mint: Decodable::decode(buf)?,
mint: if *buf.first().ok_or(DecodeError::InputTooShort)? == EMPTY_STRING_CODE {
buf.advance(1);
None
} else {
Some(Decodable::decode(buf)?)
},
value: Decodable::decode(buf)?,
gas_limit: Decodable::decode(buf)?,
is_system_tx: Decodable::decode(buf)?,
Expand Down Expand Up @@ -234,14 +248,14 @@ impl Encodable for DepositTransactionRequest {
}

/// An op-stack deposit transaction.
/// See <https://github.com/ethereum-optimism/optimism/blob/develop/specs/deposits.md#the-deposited-transaction-type>
/// See <https://github.com/ethereum-optimism/specs/blob/main/specs/protocol/deposits.md>
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct DepositTransaction {
pub nonce: u64,
pub source_hash: B256,
pub from: Address,
pub kind: TxKind,
pub mint: U256,
pub mint: Option<u128>,
pub value: U256,
pub gas_limit: u128,
pub is_system_tx: bool,
Expand Down Expand Up @@ -278,7 +292,11 @@ impl DepositTransaction {
self.source_hash.encode(out);
self.from.encode(out);
self.kind.encode(out);
self.mint.encode(out);
if let Some(mint) = self.mint {
mint.encode(out);
} else {
out.put_u8(EMPTY_STRING_CODE);
};
self.value.encode(out);
self.gas_limit.encode(out);
self.is_system_tx.encode(out);
Expand All @@ -291,7 +309,7 @@ impl DepositTransaction {
len += self.source_hash.length();
len += self.from.length();
len += self.kind.length();
len += self.mint.length();
len += self.mint.map_or(1, |mint| mint.length());
len += self.value.length();
len += self.gas_limit.length();
len += self.is_system_tx.length();
Expand Down Expand Up @@ -332,7 +350,12 @@ impl DepositTransaction {
source_hash: Decodable::decode(buf)?,
from: Decodable::decode(buf)?,
kind: Decodable::decode(buf)?,
mint: Decodable::decode(buf)?,
mint: if *buf.first().ok_or(DecodeError::InputTooShort)? == EMPTY_STRING_CODE {
buf.advance(1);
None
} else {
Some(Decodable::decode(buf)?)
},
value: Decodable::decode(buf)?,
gas_limit: Decodable::decode(buf)?,
is_system_tx: Decodable::decode(buf)?,
Expand Down Expand Up @@ -371,7 +394,7 @@ mod tests {
source_hash: B256::default(),
from: Address::default(),
kind: TxKind::Call(Address::default()),
mint: U256::from(100),
mint: Some(100_u128),
value: U256::from(100),
gas_limit: 50000,
is_system_tx: false,
Expand All @@ -391,7 +414,7 @@ mod tests {
source_hash: B256::default(),
from: Address::default(),
kind: TxKind::Call(Address::default()),
mint: U256::from(100),
mint: Some(100_u128),
value: U256::from(100),
gas_limit: 50000,
is_system_tx: false,
Expand All @@ -413,7 +436,7 @@ mod tests {
source_hash: B256::default(),
from: Address::default(),
kind: TxKind::Call(Address::default()),
mint: U256::from(100),
mint: Some(100_u128),
value: U256::from(100),
gas_limit: 50000,
is_system_tx: false,
Expand Down