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 16, 2024
1 parent df835d3 commit 3be46ce
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 40 deletions.
6 changes: 0 additions & 6 deletions crates/blockifier/src/abi/constants.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
use starknet_api::transaction::TransactionVersion;
use starknet_types_core::felt::Felt;

// The version is considered 0 for L1-Handler transaction hash calculation purposes.
pub const L1_HANDLER_VERSION: TransactionVersion = TransactionVersion(Felt::ZERO);

// OS-related constants.
pub const L1_TO_L2_MSG_HEADER_SIZE: usize = 5;
pub const L2_TO_L1_MSG_HEADER_SIZE: usize = 3;
Expand Down
14 changes: 6 additions & 8 deletions crates/native_blockifier/src/py_l1_handler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::sync::Arc;

use blockifier::abi::constants;
use pyo3::prelude::*;
use starknet_api::core::{ContractAddress, EntryPointSelector, Nonce};
use starknet_api::executable_transaction::L1HandlerTransaction;
Expand All @@ -21,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 @@ -1253,8 +1253,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 = TransactionVersion(Felt::ZERO);

let nonce = Nonce(
value
.nonce
Expand Down Expand Up @@ -1283,7 +1281,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 @@ -1280,14 +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 {
// TODO: This should be of transaction version 0.
version: TransactionVersion::ONE,
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
13 changes: 6 additions & 7 deletions crates/papyrus_rpc/src/v0_8/transaction_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,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: TransactionVersion::ZERO,
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
22 changes: 22 additions & 0 deletions crates/starknet_api/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub static QUERY_VERSION_BASE: LazyLock<Felt> = LazyLock::new(|| {
Felt::TWO.pow(QUERY_VERSION_BASE_BIT)
});

// The version is considered 0 for L1-Handler transaction hash calculation purposes.
pub const L1_HANDLER_VERSION: TransactionVersion = TransactionVersion(Felt::ZERO);

pub trait TransactionHasher {
fn calculate_transaction_hash(
&self,
Expand Down Expand Up @@ -682,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: 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::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 3be46ce

Please sign in to comment.