Skip to content

Commit

Permalink
chore(starknet_api): create a new method for l1 handler using version 0
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Dec 17, 2024
1 parent 033da3a commit 8c8742e
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 36 deletions.
15 changes: 7 additions & 8 deletions crates/native_blockifier/src/py_l1_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use pyo3::prelude::*;
use starknet_api::core::{ContractAddress, EntryPointSelector, Nonce};
use starknet_api::executable_transaction::L1HandlerTransaction;
use starknet_api::transaction::fields::{Calldata, Fee};
use starknet_api::transaction::{constants, TransactionHash};
use starknet_api::transaction::TransactionHash;

use crate::errors::{NativeBlockifierInputError, NativeBlockifierResult};
use crate::py_utils::{from_py_felts, py_attr, PyFelt};
Expand All @@ -20,13 +20,12 @@ struct PyL1HandlerTransaction {
impl TryFrom<PyL1HandlerTransaction> for starknet_api::transaction::L1HandlerTransaction {
type Error = NativeBlockifierInputError;
fn try_from(tx: PyL1HandlerTransaction) -> Result<Self, Self::Error> {
Ok(Self {
version: constants::L1_HANDLER_VERSION,
nonce: Nonce(tx.nonce.0),
contract_address: ContractAddress::try_from(tx.contract_address.0)?,
entry_point_selector: EntryPointSelector(tx.entry_point_selector.0),
calldata: Calldata(Arc::from(from_py_felts(tx.calldata))),
})
Ok(Self::new(
Nonce(tx.nonce.0),
ContractAddress::try_from(tx.contract_address.0)?,
EntryPointSelector(tx.entry_point_selector.0),
Calldata(Arc::from(from_py_felts(tx.calldata))),
))
}
}

Expand Down
4 changes: 1 addition & 3 deletions crates/papyrus_protobuf/src/converters/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1254,8 +1254,6 @@ impl From<DeployTransaction> for protobuf::transaction::Deploy {
impl TryFrom<protobuf::transaction::L1HandlerV0> for L1HandlerTransaction {
type Error = ProtobufConversionError;
fn try_from(value: protobuf::transaction::L1HandlerV0) -> Result<Self, Self::Error> {
let version = L1_HANDLER_VERSION;

let nonce = Nonce(
value
.nonce
Expand Down Expand Up @@ -1284,7 +1282,7 @@ impl TryFrom<protobuf::transaction::L1HandlerV0> for L1HandlerTransaction {

let calldata = Calldata(calldata.into());

Ok(Self { version, nonce, contract_address, entry_point_selector, calldata })
Ok(Self::new(nonce, contract_address, entry_point_selector, calldata))
}
}

Expand Down
9 changes: 1 addition & 8 deletions crates/papyrus_rpc/src/v0_8/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use starknet_api::core::{
use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::execution_resources::GasAmount;
use starknet_api::serde_utils::bytes_from_hex_str;
use starknet_api::transaction::constants::L1_HANDLER_VERSION;
use starknet_api::transaction::fields::{
AccountDeploymentData,
AllResourceBounds,
Expand Down Expand Up @@ -1281,13 +1280,7 @@ impl From<MessageFromL1> for L1HandlerTransaction {
let mut calldata = vec![sender_as_felt];
calldata.extend_from_slice(&message.payload.0);
let calldata = Calldata(Arc::new(calldata));
Self {
version: L1_HANDLER_VERSION,
contract_address: message.to_address,
entry_point_selector: message.entry_point_selector,
calldata,
..Default::default()
}
Self::new(Nonce::default(), message.to_address, message.entry_point_selector, calldata)
}
}

Expand Down
16 changes: 7 additions & 9 deletions crates/papyrus_rpc/src/v0_8/transaction_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use papyrus_test_utils::{
use pretty_assertions::assert_eq;
use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector, Nonce};
use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::transaction::constants::L1_HANDLER_VERSION;
use starknet_api::transaction::fields::{
AccountDeploymentData,
Calldata,
Expand All @@ -17,7 +16,7 @@ use starknet_api::transaction::fields::{
Tip,
TransactionSignature,
};
use starknet_api::transaction::{L1HandlerTransaction, Transaction, TransactionVersion};
use starknet_api::transaction::{L1HandlerTransaction, Transaction};
use starknet_api::{calldata, contract_address, felt, nonce};
use starknet_client::writer::objects::transaction as client_transaction;

Expand All @@ -38,24 +37,23 @@ use super::{

lazy_static::lazy_static! {
// A transaction from MAINNET with tx hash 0x439e12f67962c353182d72b4af12c3f11eaba4b36e552aebcdcd6db66971bdb.
static ref L1_HANDLER_TX: L1HandlerTransaction = L1HandlerTransaction {
version: L1_HANDLER_VERSION,
nonce: nonce!(0x18e94d),
contract_address: contract_address!(
static ref L1_HANDLER_TX: L1HandlerTransaction = L1HandlerTransaction::new(
nonce!(0x18e94d),
contract_address!(
"0x73314940630fd6dcda0d772d4c972c4e0a9946bef9dabf4ef84eda8ef542b82"
),
entry_point_selector: EntryPointSelector(felt!(
EntryPointSelector(felt!(
"0x1b64b1b3b690b43b9b514fb81377518f4039cd3e4f4914d8a6bdf01d679fb19"
)),
calldata: calldata![
calldata![
felt!("0xae0ee0a63a2ce6baeeffe56e7714fb4efe48d419"),
felt!("0x455448"),
felt!("0xc27947400e26e534e677afc2e9b2ec1bab14fc89"),
felt!("0x4af4754baf89f1b8b449215a8ea7ce558824a33a5393eaa3829658549f2bfa2"),
felt!("0x9184e72a000"),
felt!("0x0")
],
};
);
}

// The msg hash of the L1Handler transaction.
Expand Down
15 changes: 7 additions & 8 deletions crates/starknet_api/src/test_utils/l1_handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::core::{ContractAddress, EntryPointSelector, Nonce};
use crate::executable_transaction::L1HandlerTransaction as ExecutableL1HandlerTransaction;
use crate::transaction::fields::{Calldata, Fee};
use crate::transaction::{L1HandlerTransaction, TransactionHash, TransactionVersion};
use crate::transaction::{L1HandlerTransaction, TransactionHash};

#[derive(Clone, Default)]
pub struct L1HandlerTxArgs {
Expand Down Expand Up @@ -34,13 +34,12 @@ pub fn executable_l1_handler_tx(
l1_handler_tx_args: L1HandlerTxArgs,
) -> ExecutableL1HandlerTransaction {
ExecutableL1HandlerTransaction {
tx: L1HandlerTransaction {
version: TransactionVersion::ZERO, // The transaction version of L1 handler is always 0.
nonce: l1_handler_tx_args.nonce,
contract_address: l1_handler_tx_args.contract_address,
entry_point_selector: l1_handler_tx_args.entry_point_selector,
calldata: l1_handler_tx_args.calldata,
},
tx: L1HandlerTransaction::new(
l1_handler_tx_args.nonce,
l1_handler_tx_args.contract_address,
l1_handler_tx_args.entry_point_selector,
l1_handler_tx_args.calldata,
),
tx_hash: l1_handler_tx_args.tx_hash,
paid_fee_on_l1: l1_handler_tx_args.paid_fee_on_l1,
}
Expand Down
19 changes: 19 additions & 0 deletions crates/starknet_api/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,25 @@ pub struct L1HandlerTransaction {
pub calldata: Calldata,
}

impl L1HandlerTransaction {
/// A new method is implemented for this struct even though all fields are public because for a
/// properly formatted transaction, the transaction version is always 0.
pub fn new(
nonce: Nonce,
contract_address: ContractAddress,
entry_point_selector: EntryPointSelector,
calldata: Calldata,
) -> Self {
Self {
version: constants::L1_HANDLER_VERSION,
nonce,
contract_address,
entry_point_selector,
calldata,
}
}
}

impl TransactionHasher for L1HandlerTransaction {
fn calculate_transaction_hash(
&self,
Expand Down
11 changes: 11 additions & 0 deletions crates/starknet_client/src/reader/objects/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ impl Transaction {
#[serde(deny_unknown_fields)]
pub struct L1HandlerTransaction {
pub transaction_hash: TransactionHash,
// TODO: The version field should always be set to 0.
pub version: TransactionVersion,
#[serde(default)]
pub nonce: Nonce,
Expand All @@ -151,6 +152,16 @@ pub struct L1HandlerTransaction {

impl From<L1HandlerTransaction> for starknet_api::transaction::L1HandlerTransaction {
fn from(l1_handler_tx: L1HandlerTransaction) -> Self {
if l1_handler_tx.version != starknet_api::transaction::constants::L1_HANDLER_VERSION {
error!("L1HandlerTransaction version is not supported: {:?}", l1_handler_tx.version);
}
// starknet_api::transaction::L1HandlerTransaction::new(
// l1_handler_tx.nonce,
// l1_handler_tx.contract_address,
// l1_handler_tx.entry_point_selector,
// l1_handler_tx.calldata,
// )

starknet_api::transaction::L1HandlerTransaction {
version: l1_handler_tx.version,
nonce: l1_handler_tx.nonce,
Expand Down

0 comments on commit 8c8742e

Please sign in to comment.