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
  • Loading branch information
ooochoche authored and enitrat committed Oct 4, 2024
1 parent e1ccb18 commit b15af60
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 42 deletions.
55 changes: 55 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::eth_transaction::common::TxKind;
use crate::traits::SpanDefault;
use crate::errors::{RLPError, EthTransactionError, RLPErrorTrait};
use crate::rlp::{RLPItem, RLPHelpersTrait};
use crate::traits::{DefaultSignature};


#[derive(Copy, Drop, Debug, Default, PartialEq, Serde)]
Expand Down Expand Up @@ -37,3 +40,55 @@ 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),
})
}
}
47 changes: 5 additions & 42 deletions crates/utils/src/eth_transaction/transaction.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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::traits::bytes::U8SpanExTrait;
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 b15af60

Please sign in to comment.