Skip to content

Commit 6077cfc

Browse files
author
Gilad Chase
committed
l1: convert events_from_other_contracts test into integration test
It requires anvil, so should not be run as a unit test
1 parent 6426548 commit 6077cfc

File tree

3 files changed

+83
-65
lines changed

3 files changed

+83
-65
lines changed

crates/papyrus_base_layer/src/anvil_base_layer.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use alloy::rpc::types::TransactionReceipt;
77
use async_trait::async_trait;
88
use colored::*;
99
use starknet_api::block::BlockHashAndNumber;
10+
use starknet_api::hash::StarkHash;
1011
use starknet_api::transaction::L1HandlerTransaction;
1112
use url::Url;
1213

@@ -33,6 +34,8 @@ pub struct AnvilBaseLayer {
3334
}
3435

3536
impl AnvilBaseLayer {
37+
pub const DEFAULT_ANVIL_L1_ACCOUNT_ADDRESS: StarkHash =
38+
StarkHash::from_hex_unchecked("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
3639
const DEFAULT_ANVIL_PORT: u16 = 8545;
3740
const DEFAULT_ANVIL_L1_DEPLOYED_ADDRESS: &str = "0x5fbdb2315678afecb367f032d93f642f64180aa3";
3841

crates/papyrus_base_layer/src/base_layer_test.rs

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,16 @@ use alloy::primitives::B256;
33
use alloy::providers::mock::Asserter;
44
use alloy::providers::{Provider, ProviderBuilder};
55
use alloy::rpc::types::{Block, BlockTransactions, Header as AlloyRpcHeader};
6-
use assert_matches::assert_matches;
76
use pretty_assertions::assert_eq;
87
use starknet_api::block::{BlockHash, BlockHashAndNumber, BlockNumber};
9-
use starknet_api::core::EntryPointSelector;
10-
use starknet_api::transaction::L1HandlerTransaction;
11-
use starknet_api::{calldata, contract_address, felt};
8+
use starknet_api::felt;
129

13-
use crate::anvil_base_layer::{send_message_to_l2, AnvilBaseLayer};
14-
use crate::constants::{EventIdentifier, LOG_MESSAGE_TO_L2_EVENT_IDENTIFIER};
1510
use crate::ethereum_base_layer_contract::{
1611
EthereumBaseLayerConfig,
1712
EthereumBaseLayerContract,
1813
Starknet,
1914
};
20-
use crate::test_utils::DEFAULT_ANVIL_L1_ACCOUNT_ADDRESS;
21-
use crate::{BaseLayerContract, L1Event};
15+
use crate::BaseLayerContract;
2216

2317
// TODO(Gilad): Use everywhere instead of relying on the confusing `#[ignore]` api to mark slow
2418
// tests.
@@ -112,60 +106,3 @@ async fn get_gas_price_and_timestamps() {
112106
let expected_original_blob_calc = 19;
113107
assert_eq!(header.blob_fee, expected_original_blob_calc);
114108
}
115-
116-
// Ensure that the base layer instance filters out events from other deployments of the core
117-
// contract.
118-
#[tokio::test]
119-
async fn events_from_other_contract() {
120-
if !in_ci() {
121-
return;
122-
}
123-
const EVENT_IDENTIFIERS: &[EventIdentifier] = &[LOG_MESSAGE_TO_L2_EVENT_IDENTIFIER];
124-
125-
let anvil_base_layer = AnvilBaseLayer::new().await;
126-
// Anvil base layer already auto-deployed a starknet contract.
127-
let this_contract = &anvil_base_layer.ethereum_base_layer.contract;
128-
129-
// Setup.
130-
131-
// Deploy another instance of the contract to the same anvil instance.
132-
let other_contract = Starknet::deploy(this_contract.provider().clone()).await.unwrap();
133-
assert_ne!(
134-
this_contract.address(),
135-
other_contract.address(),
136-
"The two contracts should be different."
137-
);
138-
139-
let this_l1_handler = L1HandlerTransaction {
140-
contract_address: contract_address!("0x12"),
141-
entry_point_selector: EntryPointSelector(felt!("0x34")),
142-
calldata: calldata!(DEFAULT_ANVIL_L1_ACCOUNT_ADDRESS, felt!("0x1"), felt!("0x2")),
143-
..Default::default()
144-
};
145-
let this_receipt = send_message_to_l2(this_contract, &this_l1_handler.clone()).await;
146-
assert!(this_receipt.status());
147-
let this_block_number = this_receipt.block_number.unwrap();
148-
149-
let other_l1_handler = L1HandlerTransaction {
150-
contract_address: contract_address!("0x56"),
151-
entry_point_selector: EntryPointSelector(felt!("0x78")),
152-
calldata: calldata!(DEFAULT_ANVIL_L1_ACCOUNT_ADDRESS, felt!("0x1"), felt!("0x2")),
153-
..Default::default()
154-
};
155-
let other_receipt = send_message_to_l2(&other_contract, &other_l1_handler.clone()).await;
156-
assert!(other_receipt.status());
157-
let other_block_number = other_receipt.block_number.unwrap();
158-
159-
let min_block_number = this_block_number.min(other_block_number).saturating_sub(1);
160-
let max_block_number = this_block_number.max(other_block_number).saturating_add(1);
161-
162-
// Test the events.
163-
let mut events = anvil_base_layer
164-
.ethereum_base_layer
165-
.events(min_block_number..=max_block_number, EVENT_IDENTIFIERS)
166-
.await
167-
.unwrap();
168-
169-
assert_eq!(events.len(), 1, "Expected only events from this contract.");
170-
assert_matches!(events.remove(0), L1Event::LogMessageToL2 { tx, .. } if tx == this_l1_handler);
171-
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use assert_matches::assert_matches;
2+
use papyrus_base_layer::anvil_base_layer::{send_message_to_l2, AnvilBaseLayer};
3+
use papyrus_base_layer::constants::{EventIdentifier, LOG_MESSAGE_TO_L2_EVENT_IDENTIFIER};
4+
use papyrus_base_layer::ethereum_base_layer_contract::Starknet;
5+
use papyrus_base_layer::{BaseLayerContract, L1Event};
6+
use pretty_assertions::assert_eq;
7+
use starknet_api::core::EntryPointSelector;
8+
use starknet_api::transaction::L1HandlerTransaction;
9+
use starknet_api::{calldata, contract_address, felt};
10+
11+
pub fn in_ci() -> bool {
12+
std::env::var("CI").is_ok()
13+
}
14+
15+
// Ensure that the base layer instance filters out events from other deployments of the core
16+
// contract.
17+
#[tokio::test]
18+
async fn events_from_other_contract() {
19+
if !in_ci() {
20+
return;
21+
}
22+
const EVENT_IDENTIFIERS: &[EventIdentifier] = &[LOG_MESSAGE_TO_L2_EVENT_IDENTIFIER];
23+
24+
let anvil_base_layer = AnvilBaseLayer::new().await;
25+
// Anvil base layer already auto-deployed a starknet contract.
26+
let this_contract = &anvil_base_layer.ethereum_base_layer.contract;
27+
28+
// Setup.
29+
30+
// Deploy another instance of the contract to the same anvil instance.
31+
let other_contract = Starknet::deploy(this_contract.provider().clone()).await.unwrap();
32+
assert_ne!(
33+
this_contract.address(),
34+
other_contract.address(),
35+
"The two contracts should be different."
36+
);
37+
38+
let this_l1_handler = L1HandlerTransaction {
39+
contract_address: contract_address!("0x12"),
40+
entry_point_selector: EntryPointSelector(felt!("0x34")),
41+
calldata: calldata!(
42+
AnvilBaseLayer::DEFAULT_ANVIL_L1_ACCOUNT_ADDRESS,
43+
felt!("0x1"),
44+
felt!("0x2")
45+
),
46+
..Default::default()
47+
};
48+
let this_receipt = send_message_to_l2(this_contract, &this_l1_handler.clone()).await;
49+
assert!(this_receipt.status());
50+
let this_block_number = this_receipt.block_number.unwrap();
51+
52+
let other_l1_handler = L1HandlerTransaction {
53+
contract_address: contract_address!("0x56"),
54+
entry_point_selector: EntryPointSelector(felt!("0x78")),
55+
calldata: calldata!(
56+
AnvilBaseLayer::DEFAULT_ANVIL_L1_ACCOUNT_ADDRESS,
57+
felt!("0x1"),
58+
felt!("0x2")
59+
),
60+
..Default::default()
61+
};
62+
let other_receipt = send_message_to_l2(&other_contract, &other_l1_handler.clone()).await;
63+
assert!(other_receipt.status());
64+
let other_block_number = other_receipt.block_number.unwrap();
65+
66+
let min_block_number = this_block_number.min(other_block_number).saturating_sub(1);
67+
let max_block_number = this_block_number.max(other_block_number).saturating_add(1);
68+
69+
// Test the events.
70+
let mut events = anvil_base_layer
71+
.ethereum_base_layer
72+
.events(min_block_number..=max_block_number, EVENT_IDENTIFIERS)
73+
.await
74+
.unwrap();
75+
76+
assert_eq!(events.len(), 1, "Expected only events from this contract.");
77+
assert_matches!(events.remove(0), L1Event::LogMessageToL2 { tx, .. } if tx == this_l1_handler);
78+
}

0 commit comments

Comments
 (0)