Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for declare v0, l1 handler and l1->l2 messages #321

Merged
merged 35 commits into from
Oct 21, 2024
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
789f457
feat: declare v0 working
Oct 3, 2024
d448b65
debug: debuging declare v0
Oct 4, 2024
b842ae6
main merged
Oct 4, 2024
3da2f1a
adding comments for debugging
Oct 7, 2024
2938bef
debugging with bootstraper
Oct 7, 2024
b8c554c
making l1 handler execute in the mempool
Oct 8, 2024
62e174f
feat: refactored Mempool to accept L1Handler txn
Oct 9, 2024
4c16c8e
fixing test for l1 message and mempool inner
Oct 10, 2024
7f31dde
merged with main
Oct 10, 2024
50dcc7a
formatting and linting
Oct 10, 2024
f9650f5
formatting and changelog
Oct 10, 2024
334c258
comments removed
Oct 10, 2024
7c98ebd
main merged
Oct 10, 2024
bc05e4f
Merge branch 'main' into feat/declare_v0
Trantorian1 Oct 11, 2024
7c63396
validation added for txns
Oct 11, 2024
1485eca
feat: main merged
Oct 15, 2024
c857b79
resolving issues
Oct 15, 2024
8e510c9
refactor: declare v0 refactored with different rpc namespace
Oct 16, 2024
0ca66af
main merged
Oct 16, 2024
501eaf8
merged with upstream
Oct 16, 2024
21d12f5
merged with main
Oct 16, 2024
2ad4d19
msgL2 hash added
Oct 16, 2024
fc16ff3
formatting and linting
Oct 16, 2024
2209885
tests added for proc macros, error added for empty namespace, removed…
Oct 18, 2024
ed5b6c0
fixing comments
Oct 18, 2024
eaf5411
main merged
Oct 18, 2024
f21baad
fix: build fix
Oct 18, 2024
77038a2
feat: added error when namespace is not present or method is not pres…
Oct 18, 2024
7669670
feat: de/serialize tests added for BroadcastedDeclareTransactionV0, c…
Oct 18, 2024
51054f8
fix: state diff special address should have current_block - 10 block …
Oct 19, 2024
b7a2a3e
feat: updating filter of l1 message to check between last and finaliz…
Oct 19, 2024
8ed287c
linting and formatting
Oct 19, 2024
638c645
refactor: reverting the event filter in l1 messaging
Oct 19, 2024
859e50c
Merge branch 'main' into feat/declare_v0
Mohiiit Oct 21, 2024
2fa3b46
extra clones removed
Oct 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
formatting and linting
mohiiit committed Oct 10, 2024
commit 50dcc7aa52bb7f6489ca6482370c18cf974f85ba
2 changes: 1 addition & 1 deletion crates/client/db/src/class_db.rs
Original file line number Diff line number Diff line change
@@ -121,7 +121,7 @@ impl MadaraBackend {
) -> Result<(), MadaraStorageError> {
log::debug!("store classes has been called");
log::debug!("converted classes length: {:?}", converted_classes.len());

let mut writeopts = WriteOptions::new();
writeopts.disable_wal(true);

53 changes: 32 additions & 21 deletions crates/client/eth/src/l1_messaging.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
use alloy::eips::BlockNumberOrTag;
use anyhow::Context;
use futures::StreamExt;
use std::sync::Arc;
use std::thread::sleep;
use std::time::Duration;
use crate::client::StarknetCoreContract::LogMessageToL2;
use crate::client::{EthereumClient, StarknetCoreContract};
use crate::utils::u256_to_felt;
use alloy::primitives::{keccak256, FixedBytes, U256, b256, address};
use alloy::eips::BlockNumberOrTag;
use alloy::primitives::{address, b256, keccak256, FixedBytes, U256};
use alloy::providers::Provider;
use alloy::rpc::types::Filter;
use alloy::sol_types::SolValue;
use anyhow::Context;
use blockifier::transaction::transaction_execution::Transaction as BlockifierTransation;
use blockifier::transaction::transactions::L1HandlerTransaction as BlockifierL1HandlerTransaction;
use futures::StreamExt;
use log::Level::Debug;
use mc_db::{l1_db::LastSyncedEventBlock, MadaraBackend};
use mc_mempool::Mempool;
use mp_utils::channel_wait_or_graceful_shutdown;
use starknet_api::core::{ChainId, ContractAddress, EntryPointSelector, Nonce};
use starknet_api::transaction::{
Calldata, Fee, L1HandlerTransaction, Transaction, TransactionHash, TransactionVersion,
};
use blockifier::transaction::transaction_execution::Transaction as BlockifierTransation;
use starknet_api::transaction_hash::get_transaction_hash;
use starknet_types_core::felt::Felt;
use std::sync::Arc;
use std::thread::sleep;
use std::time::Duration;
use url::ParseError::SetHostOnCannotBeABaseUrl;
use mc_mempool::Mempool;
use alloy::providers::Provider;

impl EthereumClient {
/// Get cancellation status of an L1 to L2 message
@@ -46,7 +46,12 @@ impl EthereumClient {
}
}

pub async fn sync(backend: &MadaraBackend, client: &EthereumClient, chain_id: &ChainId, mempool: Arc<Mempool>) -> anyhow::Result<()> {
pub async fn sync(
backend: &MadaraBackend,
client: &EthereumClient,
chain_id: &ChainId,
mempool: Arc<Mempool>,
) -> anyhow::Result<()> {
tracing::info!("⟠ Starting L1 Messages Syncing...");

let last_synced_event_block = match backend.messaging_last_synced_l1_block_with_event() {
@@ -59,14 +64,19 @@ pub async fn sync(backend: &MadaraBackend, client: &EthereumClient, chain_id: &C
return Err(e.into());
}
};
log::debug!("we are inside sync and we will be calling the event filter now and last synced event block is: {:?}", last_synced_event_block);
log::debug!(
"we are inside sync and we will be calling the event filter now and last synced event block is: {:?}",
last_synced_event_block
);
let event_filter = client.l1_core_contract.event_filter::<StarknetCoreContract::LogMessageToL2>();
log::debug!("event filter here is: {:?}", event_filter);

let address = client.l1_core_contract.address().clone();
let transfer_event_signature =
b256!("db80dd488acf86d17c747445b0eabb5d57c541d3bd7b6b87af987858e5066b2b");
let filter = Filter::new().event_signature(transfer_event_signature).from_block(BlockNumberOrTag::Number(last_synced_event_block.block_number)).address(address);
let transfer_event_signature = b256!("db80dd488acf86d17c747445b0eabb5d57c541d3bd7b6b87af987858e5066b2b");
let filter = Filter::new()
.event_signature(transfer_event_signature)
.from_block(BlockNumberOrTag::Number(last_synced_event_block.block_number))
.address(address);
// You could also use the event name instead of the event signature like so:
// .event("Transfer(address,address,uint256)")

@@ -116,7 +126,9 @@ pub async fn sync(backend: &MadaraBackend, client: &EthereumClient, chain_id: &C
continue;
}

match process_l1_message(backend, &event, &meta.block_number, &meta.log_index, chain_id, mempool.clone()).await {
match process_l1_message(backend, &event, &meta.block_number, &meta.log_index, chain_id, mempool.clone())
.await
{
Ok(Some(tx_hash)) => {
tracing::info!(
"⟠ L1 Message from block: {:?}, transaction_hash: {:?}, log_index: {:?} submitted, \
@@ -151,7 +163,7 @@ async fn process_l1_message(
l1_block_number: &Option<u64>,
event_index: &Option<u64>,
chain_id: &ChainId,
mempool: Arc<Mempool>
mempool: Arc<Mempool>,
) -> anyhow::Result<Option<TransactionHash>> {
let transaction = parse_handle_l1_message_transaction(event)?;
let tx_nonce = transaction.nonce;
@@ -236,6 +248,7 @@ mod l1_messaging_tests {
use std::{sync::Arc, time::Duration};

use super::Felt;
use crate::l1_messaging::sync;
use crate::{
client::{
EthereumClient, L1BlockMetrics,
@@ -253,15 +266,14 @@ mod l1_messaging_tests {
transports::http::{Client, Http},
};
use mc_db::DatabaseService;
use mc_mempool::{GasPriceProvider, L1DataProvider, Mempool};
use mc_metrics::{MetricsRegistry, MetricsService};
use mp_chain_config::ChainConfig;
use rstest::*;
use starknet_api::core::Nonce;
use tempfile::TempDir;
use tracing_test::traced_test;
use url::Url;
use mc_mempool::{GasPriceProvider, L1DataProvider, Mempool};
use crate::l1_messaging::sync;

use self::DummyContract::DummyContractInstance;

@@ -272,7 +284,7 @@ mod l1_messaging_tests {
db_service: Arc<DatabaseService>,
dummy_contract: DummyContractInstance<Http<Client>, RootProvider<Http<Client>>>,
eth_client: EthereumClient,
mempool: Arc<Mempool>
mempool: Arc<Mempool>,
}

// LogMessageToL2 from https://etherscan.io/tx/0x21980d6674d33e50deee43c6c30ef3b439bd148249b4539ce37b7856ac46b843
@@ -373,7 +385,6 @@ mod l1_messaging_tests {
.expect("Failed to create database service"),
);


let l1_gas_setter = GasPriceProvider::new();
let l1_data_provider: Arc<dyn L1DataProvider> = Arc::new(l1_gas_setter.clone());

2 changes: 1 addition & 1 deletion crates/client/eth/src/state_update.rs
Original file line number Diff line number Diff line change
@@ -6,12 +6,12 @@ use crate::{
use anyhow::Context;
use futures::StreamExt;
use mc_db::MadaraBackend;
use mp_convert::ToFelt;
use mp_transactions::MAIN_CHAIN_ID;
use mp_utils::channel_wait_or_graceful_shutdown;
use serde::Deserialize;
use starknet_api::core::ChainId;
use starknet_types_core::felt::Felt;
use mp_convert::ToFelt;

#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct L1StateUpdate {
8 changes: 4 additions & 4 deletions crates/client/eth/src/sync.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::sync::Arc;
use crate::client::EthereumClient;
use crate::l1_gas_price::gas_price_worker;
use crate::l1_messaging::sync;
use crate::state_update::state_update_worker;
use mc_mempool::{GasPriceProvider, Mempool};
use starknet_api::core::ChainId;
use starknet_types_core::felt::Felt;
use std::sync::Arc;
use std::time::Duration;
use starknet_api::core::ChainId;
use crate::l1_messaging::sync;

use mc_db::MadaraBackend;

@@ -17,7 +17,7 @@ pub async fn l1_sync_worker(
l1_gas_provider: GasPriceProvider,
gas_price_sync_disabled: bool,
gas_price_poll_ms: Duration,
mempool: Arc<Mempool>
mempool: Arc<Mempool>,
) -> anyhow::Result<()> {
tokio::try_join!(
state_update_worker(backend, eth_client, chain_id.clone()),
19 changes: 7 additions & 12 deletions crates/client/mempool/src/block_production.rs
Original file line number Diff line number Diff line change
@@ -242,12 +242,8 @@ impl<Mempool: MempoolProvider> BlockProductionTask<Mempool> {
if to_take > 0 {
self.mempool.take_txs_chunk(/* extend */ &mut txs_to_process, batch_size);

txs_to_process_blockifier.extend(
txs_to_process
.iter()
.skip(cur_len)
.map(|tx| clone_transaction(&tx.tx)),
);
txs_to_process_blockifier
.extend(txs_to_process.iter().skip(cur_len).map(|tx| clone_transaction(&tx.tx)));
}

if txs_to_process.is_empty() {
@@ -257,8 +253,7 @@ impl<Mempool: MempoolProvider> BlockProductionTask<Mempool> {

stats.n_batches += 1;

txs_to_process_blockifier
.extend(txs_to_process.iter().map(|tx| clone_transaction(&tx.tx)));
txs_to_process_blockifier.extend(txs_to_process.iter().map(|tx| clone_transaction(&tx.tx)));

// Execute the transactions.
let all_results = self.executor.execute_txs(&txs_to_process_blockifier);
@@ -284,10 +279,10 @@ impl<Mempool: MempoolProvider> BlockProductionTask<Mempool> {
self.declared_classes.push(class);
}

self.block.inner.receipts.push(from_blockifier_execution_info(
&execution_info,
&clone_transaction(&mempool_tx.tx),
));
self.block
.inner
.receipts
.push(from_blockifier_execution_info(&execution_info, &clone_transaction(&mempool_tx.tx)));
let converted_tx = TransactionWithHash::from(clone_transaction(&mempool_tx.tx)); // TODO: too many tx clones!
self.block.info.tx_hashes.push(converted_tx.hash);
self.block.inner.transactions.push(converted_tx.transaction);
52 changes: 28 additions & 24 deletions crates/client/mempool/src/inner.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a

#![proptest_config(ProptestConfig::with_cases(5))] // comment this when developing, this is mostly for faster ci & whole workspace `cargo test`

at the bottom of the file that you should try commenting to see if the proptest passes locally
(don't commit it though)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine for now, but please add a comment saying that the handling of nonces on l1 handler txs is wrong

Also, when I get to fixing it, what tests should I run to see that I did not break l1 messages in the process?

Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@

use crate::{clone_transaction, contract_addr, nonce, tx_hash};
use blockifier::transaction::account_transaction::AccountTransaction;
use blockifier::transaction::transaction_execution::Transaction;
use mp_class::ConvertedClass;
use starknet_api::{
core::{ContractAddress, Nonce},
@@ -19,7 +20,6 @@ use std::{
iter,
time::SystemTime,
};
use blockifier::transaction::transaction_execution::Transaction;

pub type ArrivedAtTimestamp = SystemTime;

@@ -232,14 +232,17 @@ impl MempoolInner {

log::debug!("Checkpoint: inside the insert tx");


let contract_addr = mempool_tx.contract_address();
log::debug!("Checkpoint: contract address here is: {:?}", contract_addr);

let arrived_at = mempool_tx.arrived_at;

let deployed_contract_address =
if let Transaction::AccountTransaction(AccountTransaction::DeployAccount(tx)) = &mempool_tx.tx { Some(tx.contract_address) } else { None };
if let Transaction::AccountTransaction(AccountTransaction::DeployAccount(tx)) = &mempool_tx.tx {
Some(tx.contract_address)
} else {
None
};

if let Some(contract_address) = &deployed_contract_address {
if !self.deployed_contracts.insert(*contract_address) && !force {
@@ -353,18 +356,18 @@ mod tests {
test_utils::{contracts::FeatureContract, CairoVersion},
transaction::transactions::{DeclareTransaction, InvokeTransaction, L1HandlerTransaction},
};
use mc_exec::execution::TxInfo;
use proptest::prelude::*;
use proptest_derive::Arbitrary;
use starknet_api::{
data_availability::DataAvailabilityMode,
transaction::{DeclareTransactionV3, InvokeTransactionV3},
};
use starknet_types_core::felt::Felt;
use mc_exec::execution::TxInfo;

use super::*;
use std::fmt;
use starknet_api::transaction::Fee;
use std::fmt;
// use blockifier::transaction::transaction_execution::Transaction::L1HandlerTransaction;
// use starknet_api::transaction::L1HandlerTransaction;

@@ -466,26 +469,27 @@ mod tests {
)
.unwrap(),
)),
TxTy::InvokeFunction => Transaction::AccountTransaction(AccountTransaction::Invoke(InvokeTransaction::new(
starknet_api::transaction::InvokeTransaction::V3(InvokeTransactionV3 {
resource_bounds: Default::default(),
tip: Default::default(),
signature: Default::default(),
nonce,
sender_address: contract_addr,
calldata: Default::default(),
nonce_data_availability_mode: DataAvailabilityMode::L1,
fee_data_availability_mode: DataAvailabilityMode::L1,
paymaster_data: Default::default(),
account_deployment_data: Default::default(),
}),
tx_hash,
))),
TxTy::InvokeFunction => {
Transaction::AccountTransaction(AccountTransaction::Invoke(InvokeTransaction::new(
starknet_api::transaction::InvokeTransaction::V3(InvokeTransactionV3 {
resource_bounds: Default::default(),
tip: Default::default(),
signature: Default::default(),
nonce,
sender_address: contract_addr,
calldata: Default::default(),
nonce_data_availability_mode: DataAvailabilityMode::L1,
fee_data_availability_mode: DataAvailabilityMode::L1,
paymaster_data: Default::default(),
account_deployment_data: Default::default(),
}),
tx_hash,
)))
}
TxTy::L1Handler => Transaction::L1HandlerTransaction(L1HandlerTransaction::create_for_testing(
Fee(0), ContractAddress::from(1u16)
)

),
Fee(0),
ContractAddress::from(1u16),
)),
};

Insert(MempoolTransaction { tx, arrived_at, converted_class: None }, force)
Loading