Skip to content

Commit

Permalink
L2 -> L1 messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
fabrobles92 committed May 5, 2024
1 parent 6a11b1d commit 1a5f775
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/katana/rpc/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ tokio.workspace = true
tower = { version = "0.4.13", features = [ "full" ] }
tower-http = { version = "0.4.1", features = [ "full" ] }
tracing.workspace = true
cainome.workspace = true

[dev-dependencies]
assert_matches = "1.5.0"
Expand Down
40 changes: 39 additions & 1 deletion crates/katana/rpc/rpc/tests/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;

use cainome::cairo_serde::EthAddress;
use cainome::rs::abigen;

use dojo_test_utils::sequencer::{get_default_test_starknet_config, TestSequencer};
use dojo_world::utils::TransactionWaiter;
use katana_core::sequencer::SequencerConfig;
Expand Down Expand Up @@ -200,6 +203,8 @@ sol!(
"tests/test_data/solidity/Contract1Compiled.json"
);

abigen!(CairoMessagingContract, "/Users/fabrobles/Fab/dojo_fork/crates/katana/rpc/rpc/tests/test_data/cairo_l1_msg_contract.json");

#[tokio::test(flavor = "multi_thread")]
async fn test_messaging_l1_l2() {
// Prepare Anvil + Messaging Contracts
Expand Down Expand Up @@ -296,6 +301,7 @@ async fn test_messaging_l1_l2() {
felt!("0x033d18fcfd3ae75ae4e8a275ce649220ed718b68dc53425b388fedcdbeab5097")
);

// Messaging between L1 -> L2
let builder = contract_c1
.sendMessage(
U256::from_str("0x033d18fcfd3ae75ae4e8a275ce649220ed718b68dc53425b388fedcdbeab5097")
Expand All @@ -307,7 +313,6 @@ async fn test_messaging_l1_l2() {
.gas(12000000)
.value(Uint::from(1));

// Messaging between L1 -> L2
let receipt = builder
.send()
.await
Expand Down Expand Up @@ -343,4 +348,37 @@ async fn test_messaging_l1_l2() {
panic!("Error, No L1handler transaction")
}
}

// Messaging between L2 -> L1
let cairo_messaging_contract = CairoMessagingContract::new(contract_address, &starknet_account);
let tx = cairo_messaging_contract
.send_message_value(
&EthAddress::from(
FieldElement::from_str(&contract_c1.address().to_string().as_str()).unwrap(),
),
&FieldElement::from(2u8),
)
.send()
.await
.expect("Call to send_message_value failed");

TransactionWaiter::new(tx.transaction_hash, starknet_account.provider())
.with_tx_status(TransactionFinalityStatus::AcceptedOnL2)
.await
.expect("Invalid tx receipt");

let builder = contract_c1
.consumeMessage(
U256::from_str(contract_address.to_string().as_str()).unwrap(),
vec![U256::from(2)],
)
.value(Uint::from(1))
.gas(12000000)
.nonce(4);

//Wait for the message to reach L1
tokio::time::sleep(Duration::from_millis(8000)).await;

let tx_receipt = builder.send().await.unwrap().get_receipt().await.unwrap();
assert!(tx_receipt.status());
}

0 comments on commit 1a5f775

Please sign in to comment.