From e8ec0a7d8ec866ea10e30dafe2dacf90d1561226 Mon Sep 17 00:00:00 2001 From: Ammar Arif Date: Wed, 16 Oct 2024 16:38:21 -0400 Subject: [PATCH] fix: include message from address in the tx calldata --- crates/katana/rpc/rpc-types/src/message.rs | 7 ++++- crates/katana/rpc/rpc/tests/messaging.rs | 36 ++++++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/crates/katana/rpc/rpc-types/src/message.rs b/crates/katana/rpc/rpc-types/src/message.rs index 5bb95ffa83..77f49e0551 100644 --- a/crates/katana/rpc/rpc-types/src/message.rs +++ b/crates/katana/rpc/rpc-types/src/message.rs @@ -27,13 +27,18 @@ impl MsgFromL1 { &self.0.payload, ); + // In an l1_handler transaction, the first element of the calldata is always the Ethereum + // address of the sender (msg.sender). https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/messaging-mechanism/#l1-l2-messages + let mut calldata = vec![Felt::from(self.0.from_address)]; + calldata.extend(self.0.payload); + L1HandlerTx { nonce, chain_id, + calldata, message_hash, paid_fee_on_l1, version: Felt::ZERO, - calldata: self.0.payload, contract_address: self.0.to_address.into(), entry_point_selector: self.0.entry_point_selector, } diff --git a/crates/katana/rpc/rpc/tests/messaging.rs b/crates/katana/rpc/rpc/tests/messaging.rs index fecc5155c6..ac11f7e8a3 100644 --- a/crates/katana/rpc/rpc/tests/messaging.rs +++ b/crates/katana/rpc/rpc/tests/messaging.rs @@ -330,10 +330,42 @@ async fn estimate_message_fee() -> Result<()> { // Compute the contract address of the l1 handler contract let l1handler_address = get_contract_address(Felt::ZERO, class_hash, &[], Felt::ZERO); - // Attempt to estimate the cost of calling a #[l1handler] function + // This is the function signature of the #[l1handler] function we''re gonna call. Though the + // function accepts two arguments, we're only gonna pass one argument, as the `from_address` + // of the `MsgFromL1` will be automatically injected as part of the function calldata. + // + // See https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/messaging-mechanism/#l1-l2-messages. + // + // #[l1_handler] + // fn msg_handler_value(ref self: ContractState, from_address: felt252, value: felt252) let entry_point_selector = selector!("msg_handler_value"); - let payload = vec![felt!("0x123"), felt!("123")]; // function arguments + let payload = vec![felt!("123")]; + let from_address = felt!("0x1337"); + let to_address = l1handler_address; + + let msg = MsgFromL1 { + payload, + to_address, + entry_point_selector, + from_address: from_address.try_into()?, + }; + + let result = provider.estimate_message_fee(msg, BlockId::Tag(BlockTag::Pending)).await; + assert!(result.is_ok()); + + // #[derive(Drop, Serde)] + // struct MyData { + // a: felt252, + // b: felt252, + // } + // + // #[l1_handler] + // fn msg_handler_struct(ref self: ContractState, from_address: felt252, data: MyData) + + let entry_point_selector = selector!("msg_handler_struct"); + // [ MyData.a , MyData.b ] + let payload = vec![felt!("1"), felt!("2")]; let from_address = felt!("0x1337"); let to_address = l1handler_address;