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

feat(op): define OpTxType #12443

Merged
merged 7 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions crates/optimism/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ workspace = true

[dependencies]
reth-primitives.workspace = true
reth-primitives-traits.workspace = true
alloy-primitives.workspace = true
alloy-consensus.workspace = true
op-alloy-consensus.workspace = true
alloy-eips.workspace = true
alloy-rlp.workspace = true
derive_more.workspace = true
bytes.workspace = true
1 change: 1 addition & 0 deletions crates/optimism/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

pub mod bedrock;
pub mod op_tx_type;
197 changes: 197 additions & 0 deletions crates/optimism/primitives/src/op_tx_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
//! newtype pattern on op_alloy_consensus::OpTxType.
//! OpTxType that implements reth_primitives_traits::TxType.
//! This type is required because a Compact impl is needed on the deposit tx type.

use alloy_eips::eip2718::Eip2718Error;
use alloy_primitives::{U64, U8};
use alloy_rlp::{Decodable, Encodable};
use bytes::BufMut;
use core::fmt::Debug;
use derive_more::Display;
use op_alloy_consensus::OpTxType as AlloyOpTxType;
use reth_primitives_traits::TxType;
use std::convert::TryFrom;

/// Wrapper type for `AlloyOpTxType` to implement `TxType` trait.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Display, Ord, Hash)]
pub struct OpTxType(AlloyOpTxType);

impl From<AlloyOpTxType> for OpTxType {
fn from(tx_type: AlloyOpTxType) -> Self {
OpTxType(tx_type)
}
}

impl From<OpTxType> for u8 {
fn from(tx_type: OpTxType) -> Self {
tx_type.0 as u8
}
}

impl From<OpTxType> for U8 {
fn from(tx_type: OpTxType) -> Self {
U8::from(u8::from(tx_type))
}
}

impl TryFrom<u8> for OpTxType {
type Error = Eip2718Error;

fn try_from(value: u8) -> Result<Self, Self::Error> {
AlloyOpTxType::try_from(value)
.map(OpTxType)
.map_err(|_| Eip2718Error::UnexpectedType(value))
}
}
ozgunozerk marked this conversation as resolved.
Show resolved Hide resolved

impl Default for OpTxType {
fn default() -> Self {
// TODO: default to `Deposit` for optimism ?
ozgunozerk marked this conversation as resolved.
Show resolved Hide resolved
OpTxType(AlloyOpTxType::Deposit)
}
}
ozgunozerk marked this conversation as resolved.
Show resolved Hide resolved

impl PartialEq<u8> for OpTxType {
fn eq(&self, other: &u8) -> bool {
let self_as_u8: u8 = (*self).into();
&self_as_u8 == other
}
}

impl TryFrom<u64> for OpTxType {
type Error = Eip2718Error;

fn try_from(value: u64) -> Result<Self, Self::Error> {
if value > u8::MAX as u64 {
return Err(Eip2718Error::UnexpectedType(0));
emhane marked this conversation as resolved.
Show resolved Hide resolved
}
Self::try_from(value as u8)
}
}

impl TryFrom<U64> for OpTxType {
type Error = Eip2718Error;

fn try_from(value: U64) -> Result<Self, Self::Error> {
let u64_value: u64 = value.try_into().map_err(|_| Eip2718Error::UnexpectedType(0))?;
Self::try_from(u64_value)
}
}
ozgunozerk marked this conversation as resolved.
Show resolved Hide resolved

impl Encodable for OpTxType {
fn length(&self) -> usize {
let value: u8 = (*self).into();
value.length()
}

fn encode(&self, out: &mut dyn BufMut) {
let value: u8 = (*self).into();
value.encode(out);
}
}

impl Decodable for OpTxType {
fn decode(buf: &mut &[u8]) -> Result<Self, alloy_rlp::Error> {
let value = u8::decode(buf)?;
OpTxType::try_from(value).map_err(|_| alloy_rlp::Error::Custom("Invalid transaction type"))
}
}
ozgunozerk marked this conversation as resolved.
Show resolved Hide resolved

impl TxType for OpTxType {}
ozgunozerk marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(test)]
mod tests {
use super::*;
use alloy_eips::eip2718::Eip2718Error;
use alloy_primitives::{U64, U8};
use alloy_rlp::{Decodable, Encodable};
use bytes::BytesMut;
use op_alloy_consensus::OpTxType as AlloyOpTxType;

#[test]
fn test_from_alloy_op_tx_type() {
let alloy_tx = AlloyOpTxType::Deposit;
let op_tx: OpTxType = OpTxType::from(alloy_tx);
assert_eq!(op_tx, OpTxType(AlloyOpTxType::Deposit));
}

#[test]
fn test_from_op_tx_type_to_u8() {
let op_tx = OpTxType(AlloyOpTxType::Deposit);
let tx_type_u8: u8 = op_tx.into();
assert_eq!(tx_type_u8, AlloyOpTxType::Deposit as u8);
}

#[test]
fn test_from_op_tx_type_to_u8_u8() {
let op_tx = OpTxType(AlloyOpTxType::Deposit);
let tx_type_u8: U8 = op_tx.into();
assert_eq!(tx_type_u8, U8::from(AlloyOpTxType::Deposit as u8));
}

#[test]
fn test_try_from_u8() {
let op_tx = OpTxType::try_from(AlloyOpTxType::Deposit as u8).unwrap();
assert_eq!(op_tx, OpTxType(AlloyOpTxType::Deposit));
}

#[test]
fn test_try_from_invalid_u8() {
let invalid_value: u8 = 255;
let result = OpTxType::try_from(invalid_value);
assert!(matches!(result, Err(Eip2718Error::UnexpectedType(v)) if v == invalid_value));
}

#[test]
fn test_try_from_u64() {
let op_tx = OpTxType::try_from(AlloyOpTxType::Deposit as u64).unwrap();
assert_eq!(op_tx, OpTxType(AlloyOpTxType::Deposit));
}

#[test]
fn test_try_from_u64_out_of_range() {
let result = OpTxType::try_from(u64::MAX);
assert!(matches!(result, Err(Eip2718Error::UnexpectedType(0))));
}

#[test]
fn test_try_from_u64_within_range() {
let valid_value: U64 = U64::from(AlloyOpTxType::Deposit as u64);
let op_tx = OpTxType::try_from(valid_value).unwrap();
assert_eq!(op_tx, OpTxType(AlloyOpTxType::Deposit));
}

#[test]
fn test_default() {
let default_tx = OpTxType::default();
assert_eq!(default_tx, OpTxType(AlloyOpTxType::Deposit));
}

#[test]
fn test_partial_eq_u8() {
let op_tx = OpTxType(AlloyOpTxType::Deposit);
assert_eq!(op_tx, AlloyOpTxType::Deposit as u8);
}

#[test]
fn test_encodable() {
let op_tx = OpTxType(AlloyOpTxType::Deposit);
let mut buf = BytesMut::new();
op_tx.encode(&mut buf);
assert_eq!(buf, BytesMut::from(&[AlloyOpTxType::Deposit as u8][..]));
}

#[test]
fn test_decodable() {
let mut buf: &[u8] = &[AlloyOpTxType::Deposit as u8];
let decoded_tx = OpTxType::decode(&mut buf).unwrap();
assert_eq!(decoded_tx, OpTxType(AlloyOpTxType::Deposit));
}

#[test]
fn test_decodable_invalid() {
let mut buf: &[u8] = &[255];
let result = OpTxType::decode(&mut buf);
assert!(result.is_err());
}
}
Loading