Skip to content

Commit

Permalink
fix(katana): include msg sender in the tx calldata (#2550)
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy authored Oct 16, 2024
1 parent fbef8a8 commit 70ecca7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
7 changes: 6 additions & 1 deletion crates/katana/rpc/rpc-types/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
36 changes: 34 additions & 2 deletions crates/katana/rpc/rpc/tests/messaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 70ecca7

Please sign in to comment.