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
Next Next commit
feat: declare v0 working
mohiiit committed Oct 3, 2024
commit 789f457c15aa30afd2388c703f2c3b768df424c4
19 changes: 18 additions & 1 deletion crates/client/mempool/src/lib.rs
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ use mp_block::BlockId;
use mp_block::BlockTag;
use mp_block::MadaraPendingBlockInfo;
use mp_class::ConvertedClass;
use mp_transactions::broadcasted_to_blockifier;
use mp_transactions::{broadcasted_to_blockifier, BroadcastedDeclareTransactionV0, broadcasted_to_blockifier_v0};
use mp_transactions::BroadcastedToBlockifierError;
use starknet_api::core::{ContractAddress, Nonce};
use starknet_api::transaction::TransactionHash;
@@ -63,6 +63,7 @@ impl Error {
#[cfg_attr(test, mockall::automock)]
pub trait MempoolProvider: Send + Sync {
fn accept_invoke_tx(&self, tx: BroadcastedInvokeTransaction) -> Result<InvokeTransactionResult, Error>;
fn accept_declare_v0_tx(&self, tx: BroadcastedDeclareTransactionV0) -> Result<DeclareTransactionResult, Error>;
Copy link
Member

Choose a reason for hiding this comment

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

might be worth to put a comment here to say why we need declare v0 txs in the first place

fn accept_declare_tx(&self, tx: BroadcastedDeclareTransaction) -> Result<DeclareTransactionResult, Error>;
fn accept_deploy_account_tx(
&self,
@@ -181,6 +182,22 @@ impl MempoolProvider for Mempool {
Ok(res)
}

fn accept_declare_v0_tx(&self, tx: BroadcastedDeclareTransactionV0) -> Result<DeclareTransactionResult, Error> {
log::info!("Checkpoint 3: accept_declare_v0_tx: {:?}", tx);
let (tx, classes) = broadcasted_to_blockifier_v0(
tx,
self.chain_id(),
self.backend.chain_config().latest_protocol_version,
)?;

let res = DeclareTransactionResult {
transaction_hash: transaction_hash(&tx),
class_hash: declare_class_hash(&tx).expect("Created transaction should be declare"),
};
self.accept_tx(tx, classes)?;
Ok(res)
}

fn accept_declare_tx(&self, tx: BroadcastedDeclareTransaction) -> Result<DeclareTransactionResult, Error> {
let (tx, classes) = broadcasted_to_blockifier(
BroadcastedTransaction::Declare(tx),
8 changes: 7 additions & 1 deletion crates/client/rpc/src/providers/forward_to_provider.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ use starknet_core::types::{
DeclareTransactionResult, DeployAccountTransactionResult, InvokeTransactionResult,
};
use starknet_providers::{Provider, ProviderError};

use mp_transactions::BroadcastedDeclareTransactionV0;
use crate::{bail_internal_server_error, errors::StarknetRpcApiError};

use super::AddTransactionProvider;
@@ -21,6 +21,12 @@ impl<P: Provider + Send + Sync> ForwardToProvider<P> {

#[async_trait]
impl<P: Provider + Send + Sync> AddTransactionProvider for ForwardToProvider<P> {

async fn add_declare_v0_transaction(&self, declare_v0_transaction: BroadcastedDeclareTransactionV0) -> RpcResult<DeclareTransactionResult> {
// panic here, because we can't really forward it to the real FGW, or shall we enable it so that another madara full node is able to use it?
// maybe a flag for this? as discussed
unimplemented!()
Copy link
Member

Choose a reason for hiding this comment

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

Err(StarknetRpcApiError::UnimplementedMethod.into())

we really dont want to panic, it'll close down the entire node

}
async fn add_declare_transaction(
&self,
declare_transaction: BroadcastedDeclareTransaction,
6 changes: 6 additions & 0 deletions crates/client/rpc/src/providers/mempool.rs
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ use starknet_core::types::{
DeclareTransactionResult, DeployAccountTransactionResult, InvokeTransactionResult,
};
use std::sync::Arc;
use mp_transactions::BroadcastedDeclareTransactionV0;

/// This [`AddTransactionProvider`] adds the received transactions to a mempool.
pub struct MempoolAddTxProvider {
@@ -43,6 +44,11 @@ impl From<mc_mempool::Error> for StarknetRpcApiError {

#[async_trait]
impl AddTransactionProvider for MempoolAddTxProvider {

async fn add_declare_v0_transaction(&self, declare_v0_transaction: BroadcastedDeclareTransactionV0) -> RpcResult<DeclareTransactionResult> {
log::info!("Checkpoint 2: add_declare_v0_transaction: {:?}", declare_v0_transaction);
Ok(self.mempool.accept_declare_v0_tx(declare_v0_transaction).map_err(StarknetRpcApiError::from)?)
}
async fn add_declare_transaction(
&self,
declare_transaction: BroadcastedDeclareTransaction,
3 changes: 3 additions & 0 deletions crates/client/rpc/src/providers/mod.rs
Original file line number Diff line number Diff line change
@@ -9,9 +9,12 @@ use starknet_core::types::{
BroadcastedDeclareTransaction, BroadcastedDeployAccountTransaction, BroadcastedInvokeTransaction,
DeclareTransactionResult, DeployAccountTransactionResult, InvokeTransactionResult,
};
use mp_transactions::BroadcastedDeclareTransactionV0;

#[async_trait]
pub trait AddTransactionProvider: Send + Sync {

async fn add_declare_v0_transaction(&self, declare_v0_transaction: BroadcastedDeclareTransactionV0) -> RpcResult<DeclareTransactionResult>;
async fn add_declare_transaction(
&self,
declare_transaction: BroadcastedDeclareTransaction,
6 changes: 5 additions & 1 deletion crates/client/rpc/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ use mp_state_update::{
ContractStorageDiffItem, DeclaredClassItem, DeployedContractItem, NonceUpdate, ReplacedClassItem, StateDiff,
StorageEntry,
};
use mp_transactions::{InvokeTransaction, InvokeTransactionV0, Transaction};
use mp_transactions::{BroadcastedDeclareTransactionV0, InvokeTransaction, InvokeTransactionV0, Transaction};
use rstest::fixture;
use starknet_core::types::{
BroadcastedDeclareTransaction, BroadcastedDeployAccountTransaction, BroadcastedInvokeTransaction,
@@ -29,6 +29,10 @@ pub struct TestTransactionProvider;
#[cfg(test)]
#[async_trait]
impl AddTransactionProvider for TestTransactionProvider {

async fn add_declare_v0_transaction(&self, declare_v0_transaction: BroadcastedDeclareTransactionV0) -> RpcResult<DeclareTransactionResult> {
unimplemented!()
}
async fn add_declare_transaction(
&self,
_declare_transaction: BroadcastedDeclareTransaction,
13 changes: 10 additions & 3 deletions crates/client/rpc/src/versions/v0_7_1/api.rs
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ use starknet_core::types::{
use starknet_types_core::felt::Felt;

use m_proc_macros::versioned_starknet_rpc;

use mp_transactions::BroadcastedDeclareTransactionV0;
// Starknet RPC API trait and types
//
// Starkware maintains [a description of the Starknet API](https://github.com/starkware-libs/starknet-specs/blob/master/api/starknet_api_openrpc.json)
@@ -28,19 +28,26 @@ pub trait StarknetWriteRpcApi {
invoke_transaction: BroadcastedInvokeTransaction,
) -> RpcResult<InvokeTransactionResult>;

/// Submit a new class declaration transaction
/// Submit a new deploy account transaction
#[method(name = "addDeployAccountTransaction")]
async fn add_deploy_account_transaction(
&self,
deploy_account_transaction: BroadcastedDeployAccountTransaction,
) -> RpcResult<DeployAccountTransactionResult>;

/// Submit a new deploy account transaction
/// Submit a new class declaration transaction
#[method(name = "addDeclareTransaction")]
async fn add_declare_transaction(
&self,
declare_transaction: BroadcastedDeclareTransaction,
) -> RpcResult<DeclareTransactionResult>;

/// Submit a new class v0 declaration transaction
#[method(name = "addDeclareV0Transaction")]
async fn add_declare_v0_transaction(
&self,
declare_transaction_v0: BroadcastedDeclareTransactionV0,
) -> RpcResult<DeclareTransactionResult>;
}

#[versioned_starknet_rpc("V0_7_1")]
19 changes: 18 additions & 1 deletion crates/client/rpc/src/versions/v0_7_1/methods/write/mod.rs
Original file line number Diff line number Diff line change
@@ -3,11 +3,28 @@ use starknet_core::types::{
BroadcastedDeclareTransaction, BroadcastedDeployAccountTransaction, BroadcastedInvokeTransaction,
DeclareTransactionResult, DeployAccountTransactionResult, InvokeTransactionResult,
};

use mp_transactions::BroadcastedDeclareTransactionV0;
use crate::{versions::v0_7_1::StarknetWriteRpcApiV0_7_1Server, Starknet};

#[async_trait]
impl StarknetWriteRpcApiV0_7_1Server for Starknet {
/// Submit a new declare transaction to be added to the chain
///
/// # Arguments
///
/// * `declare_v0_transaction` - the declare v0 transaction to be added to the chain
///
/// # Returns
///
/// * `declare_transaction_result` - the result of the declare transaction
async fn add_declare_v0_transaction(
Copy link
Member

Choose a reason for hiding this comment

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

this should be in a separate node operator rpc endpoint, we really dont want to expose this to the outside world

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this has been resolved

&self,
declare_transaction: BroadcastedDeclareTransactionV0,
) -> RpcResult<DeclareTransactionResult> {
log::info!("add_declare_v0_transaction: {:?}", declare_transaction);
Ok(self.add_transaction_provider.add_declare_v0_transaction(declare_transaction).await?)
}

/// Submit a new declare transaction to be added to the chain
///
/// # Arguments
9 changes: 9 additions & 0 deletions crates/primitives/class/src/lib.rs
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ use std::{collections::HashMap, sync::Arc};

use starknet_types_core::felt::Felt;


pub mod class_hash;
pub mod class_update;
pub mod compile;
@@ -178,6 +179,7 @@ pub struct LegacyContractEntryPoint {
}

#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
pub enum LegacyContractAbiEntry {
Function(LegacyFunctionAbiEntry),
Event(LegacyEventAbiEntry),
@@ -190,6 +192,7 @@ pub struct LegacyFunctionAbiEntry {
pub name: String,
pub inputs: Vec<LegacyTypedParameter>,
pub outputs: Vec<LegacyTypedParameter>,
#[serde(rename = "stateMutability")]
pub state_mutability: Option<FunctionStateMutability>,
}

@@ -224,23 +227,29 @@ pub struct LegacyTypedParameter {

#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum LegacyFunctionAbiType {
#[serde(rename = "function")]
Function,
#[serde(rename = "l1_handler")]
L1Handler,
#[serde(rename = "constructor")]
Constructor,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum LegacyEventAbiType {
#[serde(rename = "event")]
Event,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum LegacyStructAbiType {
#[serde(rename = "struct")]
Struct,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum FunctionStateMutability {
#[serde(rename = "view")]
View,
}

48 changes: 47 additions & 1 deletion crates/primitives/transactions/src/broadcasted_to_blockifier.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use crate::{into_starknet_api::TransactionApiError, Transaction, TransactionWithHash};
use crate::{BroadcastedDeclareTransactionV0, into_starknet_api::TransactionApiError, Transaction, TransactionWithHash};
use blockifier::{execution::errors::ContractClassError, transaction::errors::TransactionExecutionError};
use mp_chain_config::StarknetVersion;
use mp_class::{
@@ -30,6 +30,52 @@ pub enum BroadcastedToBlockifierError {
CompiledClassHashMismatch { expected: Felt, compilation: Felt },
}

pub fn broadcasted_to_blockifier_v0(
transaction: BroadcastedDeclareTransactionV0,
chain_id: Felt,
starknet_version: StarknetVersion,
) -> Result<
(blockifier::transaction::transaction_execution::Transaction, Option<ConvertedClass>),
BroadcastedToBlockifierError,
> {
let (class_info, class_hash, extra_class_info) = {
let compressed_legacy_class: CompressedLegacyContractClass = (*transaction.contract_class).clone().into();
let class_hash = compressed_legacy_class.compute_class_hash().unwrap();
log::debug!("Computed legacy class hash: {:?}", class_hash);
let compressed_legacy_class: CompressedLegacyContractClass = (*transaction.contract_class).clone().into();
let class_blockifier = compressed_legacy_class
.to_blockifier_class()
.map_err(BroadcastedToBlockifierError::CompilationFailed)?;
let class_info = LegacyClassInfo { contract_class: Arc::new(compressed_legacy_class) };

(
Some(blockifier::execution::contract_class::ClassInfo::new(&class_blockifier, 0, 0)?),
Some(class_hash),
Some(ConvertedClass::Legacy(LegacyConvertedClass { class_hash, info: class_info })),
) };

let is_query = transaction.is_query;
let TransactionWithHash { transaction, hash } =
TransactionWithHash::from_broadcasted_v0(transaction, chain_id, starknet_version, class_hash);
let deployed_address = match &transaction {
Transaction::DeployAccount(tx) => Some(tx.calculate_contract_address()),
_ => None,
};
let transaction: starknet_api::transaction::Transaction = transaction.try_into()?;

Ok((
blockifier::transaction::transaction_execution::Transaction::from_api(
transaction,
TransactionHash(hash),
class_info,
None,
deployed_address.map(|address| address.try_into().unwrap()),
Copy link
Member

Choose a reason for hiding this comment

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

uses expect to explain that this should not panic

Copy link
Contributor Author

Choose a reason for hiding this comment

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

actually we don't need it for declare v0 but it was missing in other case so added there

is_query,
)?,
extra_class_info,
))
}

pub fn broadcasted_to_blockifier(
transaction: starknet_core::types::BroadcastedTransaction,
chain_id: Felt,
33 changes: 28 additions & 5 deletions crates/primitives/transactions/src/from_broadcasted_transaction.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use mp_chain_config::StarknetVersion;
use starknet_types_core::felt::Felt;

use crate::{
DeclareTransaction, DeclareTransactionV1, DeclareTransactionV2, DeclareTransactionV3, DeployAccountTransaction,
DeployAccountTransactionV1, DeployAccountTransactionV3, InvokeTransaction, InvokeTransactionV1,
InvokeTransactionV3, Transaction, TransactionWithHash,
};
use crate::{BroadcastedDeclareTransactionV0, DeclareTransaction, DeclareTransactionV0, DeclareTransactionV1, DeclareTransactionV2, DeclareTransactionV3, DeployAccountTransaction, DeployAccountTransactionV1, DeployAccountTransactionV3, InvokeTransaction, InvokeTransactionV1, InvokeTransactionV3, Transaction, TransactionWithHash};

// class_hash is required for DeclareTransaction
impl TransactionWithHash {
@@ -26,6 +22,18 @@ impl TransactionWithHash {
let hash = transaction.compute_hash(chain_id, starknet_version, is_query);
Self { hash, transaction }
}

pub fn from_broadcasted_v0(
tx: BroadcastedDeclareTransactionV0,
chain_id: Felt,
starknet_version: StarknetVersion,
class_hash: Option<Felt>,
) -> Self {
let is_query = tx.is_query;
let transaction: Transaction = Transaction::Declare(DeclareTransaction::from_broadcasted_v0(tx, class_hash.unwrap()));
let hash = transaction.compute_hash(chain_id, starknet_version, is_query);
Self { hash, transaction }
}
}

impl From<starknet_core::types::BroadcastedInvokeTransaction> for InvokeTransaction {
@@ -80,6 +88,21 @@ impl DeclareTransaction {
}
}
}

fn from_broadcasted_v0(tx: BroadcastedDeclareTransactionV0, class_hash: Felt) -> Self {
DeclareTransaction::V0(DeclareTransactionV0::from_broadcasted(tx, class_hash))
}
}

impl DeclareTransactionV0 {
fn from_broadcasted(tx: BroadcastedDeclareTransactionV0, class_hash: Felt) -> Self {
Self {
sender_address: tx.sender_address,
max_fee: tx.max_fee,
signature: tx.signature,
class_hash,
}
}
}

impl DeclareTransactionV1 {
18 changes: 17 additions & 1 deletion crates/primitives/transactions/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::sync::Arc;
use mp_convert::ToFelt;
use starknet_api::transaction::TransactionVersion;
use starknet_types_core::{felt::Felt, hash::StarkHash};
@@ -12,7 +13,8 @@ mod to_starknet_core;

pub mod compute_hash;
pub mod utils;
pub use broadcasted_to_blockifier::{broadcasted_to_blockifier, BroadcastedToBlockifierError};
pub use broadcasted_to_blockifier::{broadcasted_to_blockifier, BroadcastedToBlockifierError, broadcasted_to_blockifier_v0};
use mp_class::CompressedLegacyContractClass;

const SIMULATE_TX_VERSION_OFFSET: Felt = Felt::from_hex_unchecked("0x100000000000000000000000000000000");

@@ -38,6 +40,20 @@ impl TransactionWithHash {
}
}

#[derive(Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
Copy link
Member

Choose a reason for hiding this comment

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

maybe de/serialization tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

pub struct BroadcastedDeclareTransactionV0 {
/// The address of the account contract sending the declaration transaction
pub sender_address: Felt,
/// The maximal fee that can be charged for including the transaction
pub max_fee: Felt,
/// Signature
pub signature: Vec<Felt>,
/// The class to be declared
pub contract_class: Arc<CompressedLegacyContractClass>,
/// If set to `true`, uses a query-only transaction version that's invalid for execution
pub is_query: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Transaction {
Invoke(InvokeTransaction),
2 changes: 2 additions & 0 deletions crates/tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -235,6 +235,8 @@ async fn madara_can_sync_a_few_blocks() {
node.wait_for_ready().await;
node.wait_for_sync_to(19).await;

println!("we have block till: {:?}", node.json_rpc().block_hash_and_number().await.unwrap());

assert_eq!(
node.json_rpc().block_hash_and_number().await.unwrap(),
BlockHashAndNumber {