Skip to content

Commit

Permalink
refactor: decode_legacy_tx for consistency with typed transactions (#…
Browse files Browse the repository at this point in the history
…1012)

* refactor: decode_legacy_tx for consistency with typed transactions

* fmt

---------

Co-authored-by: enitrat <msaug@protonmail.com>
  • Loading branch information
ooochoche and enitrat authored Oct 4, 2024
1 parent e1ccb18 commit f6c15f5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 43 deletions.
57 changes: 57 additions & 0 deletions crates/utils/src/eth_transaction/legacy.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::errors::{RLPError, EthTransactionError, RLPErrorTrait};
use crate::eth_transaction::common::TxKind;
use crate::rlp::{RLPItem, RLPHelpersTrait};
use crate::traits::SpanDefault;
use crate::traits::{DefaultSignature};


#[derive(Copy, Drop, Debug, Default, PartialEq, Serde)]
Expand Down Expand Up @@ -37,3 +40,57 @@ pub struct TxLegacy {
/// input data of the message call.
pub input: Span<u8>,
}

#[generate_trait]
pub impl _impl of TxLegacyTrait {
/// Decodes the RLP-encoded fields into a TxLegacy struct.
///
/// # Arguments
///
/// * `data` - A span of RLPItems containing the encoded transaction fields
///
/// # Returns
///
/// A Result containing either the decoded TxLegacy struct or an EthTransactionError
fn decode_fields(ref data: Span<RLPItem>) -> Result<TxLegacy, EthTransactionError> {
let boxed_fields = data
.multi_pop_front::<7>()
.ok_or(EthTransactionError::RLPError(RLPError::InputTooShort))?;
let [
nonce_encoded,
gas_price_encoded,
gas_limit_encoded,
to_encoded,
value_encoded,
input_encoded,
chain_id_encoded
] =
(*boxed_fields)
.unbox();

let nonce = nonce_encoded.parse_u64_from_string().map_err()?;
let gas_price = gas_price_encoded.parse_u128_from_string().map_err()?;
let gas_limit = gas_limit_encoded.parse_u64_from_string().map_err()?;
let to = to_encoded.try_parse_address_from_string().map_err()?;
let value = value_encoded.parse_u256_from_string().map_err()?;
let input = input_encoded.parse_bytes_from_string().map_err()?;
let chain_id = chain_id_encoded.parse_u64_from_string().map_err()?;

let transact_to = match to {
Option::Some(to) => { TxKind::Call(to) },
Option::None => { TxKind::Create }
};

Result::Ok(
TxLegacy {
nonce,
gas_price,
gas_limit,
to: transact_to,
value,
input,
chain_id: Option::Some(chain_id),
}
)
}
}
49 changes: 6 additions & 43 deletions crates/utils/src/eth_transaction/transaction.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use crate::errors::{RLPError, EthTransactionError, RLPErrorTrait};
use crate::eth_transaction::common::{TxKind, TxKindTrait};
use crate::eth_transaction::eip1559::{TxEip1559, TxEip1559Trait};
use crate::eth_transaction::eip2930::{AccessListItem, TxEip2930, TxEip2930Trait};
use crate::eth_transaction::legacy::TxLegacy;
use crate::eth_transaction::legacy::{TxLegacy, TxLegacyTrait};
use crate::eth_transaction::tx_type::{TxType};
use crate::rlp::{RLPItem, RLPTrait, RLPHelpersTrait};
use crate::rlp::{RLPItem, RLPTrait};
use crate::traits::bytes::U8SpanExTrait;
use crate::traits::{DefaultSignature};

Expand Down Expand Up @@ -237,59 +237,22 @@ pub impl _TransactionUnsigned of TransactionUnsignedTrait {
ref encoded_tx_data: Span<u8>
) -> Result<TransactionUnsigned, EthTransactionError> {
let rlp_decoded_data = RLPTrait::decode(encoded_tx_data);
let rlp_decoded_data = rlp_decoded_data.map_err()?;
let mut rlp_decoded_data = rlp_decoded_data.map_err()?;

if (rlp_decoded_data.len() != 1) {
return Result::Err(
EthTransactionError::TopLevelRlpListWrongLength(rlp_decoded_data.len())
);
}

let rlp_decoded_data = *rlp_decoded_data.at(0);
let legacy_tx: TxLegacy = match rlp_decoded_data {
let rpl_item = *rlp_decoded_data.at(0);
let legacy_tx: TxLegacy = match rpl_item {
RLPItem::String => { Result::Err(EthTransactionError::ExpectedRLPItemToBeList)? },
RLPItem::List(mut val) => {
if (val.len() != 9) {
return Result::Err(EthTransactionError::LegacyTxWrongPayloadLength(val.len()));
}

let boxed_fields = val
.multi_pop_front::<7>()
.ok_or(EthTransactionError::RLPError(RLPError::InputTooShort))?;
let [
nonce_encoded,
gas_price_encoded,
gas_limit_encoded,
to_encoded,
value_encoded,
input_encoded,
chain_id_encoded
] =
(*boxed_fields)
.unbox();

let nonce = nonce_encoded.parse_u64_from_string().map_err()?;
let gas_price = gas_price_encoded.parse_u128_from_string().map_err()?;
let gas_limit = gas_limit_encoded.parse_u64_from_string().map_err()?;
let to = to_encoded.try_parse_address_from_string().map_err()?;
let value = value_encoded.parse_u256_from_string().map_err()?;
let input = input_encoded.parse_bytes_from_string().map_err()?;
let chain_id = chain_id_encoded.parse_u64_from_string().map_err()?;

let transact_to = match to {
Option::Some(to) => { TxKind::Call(to) },
Option::None => { TxKind::Create }
};

TxLegacy {
nonce,
gas_price,
gas_limit,
to: transact_to,
value,
input,
chain_id: Option::Some(chain_id),
}
TxLegacyTrait::decode_fields(ref val)?
}
};

Expand Down

0 comments on commit f6c15f5

Please sign in to comment.