From 193569f33bd7408cf1e961caad8880e807d0143e Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Mon, 16 Dec 2024 16:49:09 +0200 Subject: [PATCH] chore(starknet_api): create a new method for l1 handler using version 0 --- crates/native_blockifier/src/py_l1_handler.rs | 15 +++++++-------- .../src/converters/transaction.rs | 5 +---- crates/papyrus_rpc/src/v0_8/transaction.rs | 9 +-------- .../papyrus_rpc/src/v0_8/transaction_test.rs | 14 ++++++-------- .../starknet_api/src/test_utils/l1_handler.rs | 15 +++++++-------- crates/starknet_api/src/transaction.rs | 19 +++++++++++++++++++ .../src/reader/objects/transaction.rs | 11 +++++++++++ 7 files changed, 52 insertions(+), 36 deletions(-) diff --git a/crates/native_blockifier/src/py_l1_handler.rs b/crates/native_blockifier/src/py_l1_handler.rs index 9af53c0189..ede488bc01 100644 --- a/crates/native_blockifier/src/py_l1_handler.rs +++ b/crates/native_blockifier/src/py_l1_handler.rs @@ -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}; @@ -20,13 +20,12 @@ struct PyL1HandlerTransaction { impl TryFrom for starknet_api::transaction::L1HandlerTransaction { type Error = NativeBlockifierInputError; fn try_from(tx: PyL1HandlerTransaction) -> Result { - 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))), + )) } } diff --git a/crates/papyrus_protobuf/src/converters/transaction.rs b/crates/papyrus_protobuf/src/converters/transaction.rs index 5232afcfd1..a376bfc349 100644 --- a/crates/papyrus_protobuf/src/converters/transaction.rs +++ b/crates/papyrus_protobuf/src/converters/transaction.rs @@ -7,7 +7,6 @@ use prost::Message; use starknet_api::block::GasPrice; use starknet_api::core::{ClassHash, CompiledClassHash, EntryPointSelector, Nonce}; use starknet_api::execution_resources::GasAmount; -use starknet_api::transaction::constants::L1_HANDLER_VERSION; use starknet_api::transaction::fields::{ AccountDeploymentData, AllResourceBounds, @@ -1254,8 +1253,6 @@ impl From for protobuf::transaction::Deploy { impl TryFrom for L1HandlerTransaction { type Error = ProtobufConversionError; fn try_from(value: protobuf::transaction::L1HandlerV0) -> Result { - let version = L1_HANDLER_VERSION; - let nonce = Nonce( value .nonce @@ -1284,7 +1281,7 @@ impl TryFrom 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)) } } diff --git a/crates/papyrus_rpc/src/v0_8/transaction.rs b/crates/papyrus_rpc/src/v0_8/transaction.rs index 6ea8d75b8b..33bfaf6a0c 100644 --- a/crates/papyrus_rpc/src/v0_8/transaction.rs +++ b/crates/papyrus_rpc/src/v0_8/transaction.rs @@ -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, @@ -1281,13 +1280,7 @@ impl From 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) } } diff --git a/crates/papyrus_rpc/src/v0_8/transaction_test.rs b/crates/papyrus_rpc/src/v0_8/transaction_test.rs index 645776169f..4b61923f14 100644 --- a/crates/papyrus_rpc/src/v0_8/transaction_test.rs +++ b/crates/papyrus_rpc/src/v0_8/transaction_test.rs @@ -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, @@ -38,16 +37,15 @@ 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"), @@ -55,7 +53,7 @@ lazy_static::lazy_static! { felt!("0x9184e72a000"), felt!("0x0") ], - }; + ); } // The msg hash of the L1Handler transaction. diff --git a/crates/starknet_api/src/test_utils/l1_handler.rs b/crates/starknet_api/src/test_utils/l1_handler.rs index bc848a6b01..beb669e979 100644 --- a/crates/starknet_api/src/test_utils/l1_handler.rs +++ b/crates/starknet_api/src/test_utils/l1_handler.rs @@ -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 { @@ -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, } diff --git a/crates/starknet_api/src/transaction.rs b/crates/starknet_api/src/transaction.rs index b2bf7e3808..9f650eb7c3 100644 --- a/crates/starknet_api/src/transaction.rs +++ b/crates/starknet_api/src/transaction.rs @@ -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, diff --git a/crates/starknet_client/src/reader/objects/transaction.rs b/crates/starknet_client/src/reader/objects/transaction.rs index e341c2ea20..95feee046e 100644 --- a/crates/starknet_client/src/reader/objects/transaction.rs +++ b/crates/starknet_client/src/reader/objects/transaction.rs @@ -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, @@ -151,6 +152,16 @@ pub struct L1HandlerTransaction { impl From 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,