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

feat: Pragma ExEx #6

Merged
merged 20 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Next release

- feat: `exex_pragma_dispatch` implementation
- feat: Madara ExExs proof of concept
- feat(cli): launcher script and release workflows
- fix: cleaned cli settings for sequencer, devnet and full
Expand Down
24 changes: 24 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ members = [
"crates/primitives/chain_config",
"crates/primitives/utils",
"crates/primitives/exex",
"crates/primitives/rpc_provider",
"crates/proc-macros",
"crates/tests",
]
Expand All @@ -45,6 +46,7 @@ default-members = [
"crates/primitives/chain_config",
"crates/primitives/utils",
"crates/primitives/exex",
"crates/primitives/rpc_provider",
"crates/proc-macros",
"crates/tests",
]
Expand Down Expand Up @@ -95,6 +97,7 @@ mp-state-update = { path = "crates/primitives/state_update", default-features =
mp-utils = { path = "crates/primitives/utils", default-features = false }
mp-chain-config = { path = "crates/primitives/chain_config", default-features = false }
mp-exex = { path = "crates/primitives/exex", default-features = false }
mp-rpc-provider = { path = "crates/primitives/rpc_provider", default-features = false }

# Madara client
mc-telemetry = { path = "crates/client/telemetry" }
Expand Down
4 changes: 2 additions & 2 deletions configs/presets/devnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ versioned_constants:
"0.13.2": "crates/primitives/chain_config/resources/versioned_constants_13_2.json"
eth_core_contract_address: "0xE2Bb56ee936fd6433DC0F6e7e3b8365C906AA057"
latest_protocol_version: "0.13.2"
block_time: "1s"
pending_block_update_time: "500ms"
block_time: "5s"
pending_block_update_time: "1s"
execution_batch_size: 16
bouncer_config:
block_max_capacity:
Expand Down
12 changes: 7 additions & 5 deletions crates/client/mempool/src/block_production.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ impl<Mempool: MempoolProvider> BlockProductionTask<Mempool> {
// This is compute heavy as it does the commitments and trie computations.
let import_result = close_block(
&self.importer,
block_to_close,
block_to_close.clone(),
&new_state_diff,
self.backend.chain_config().chain_id.clone(),
block_n,
Expand All @@ -432,7 +432,7 @@ impl<Mempool: MempoolProvider> BlockProductionTask<Mempool> {
self.current_pending_tick = 0;

log::info!("⛏️ Closed block #{} with {} transactions - {:?}", block_n, n_txs, start_time.elapsed());
let _ = self.notify_exexs(block_n).context("Sending notification to ExExs");
let _ = self.notify_exexs(block_to_close, block_n).context("Sending notification to ExExs");

Ok(())
}
Expand Down Expand Up @@ -487,12 +487,14 @@ impl<Mempool: MempoolProvider> BlockProductionTask<Mempool> {
}

/// Sends a notification to the ExExs that a block has been closed.
fn notify_exexs(&self, block_n: u64) -> anyhow::Result<()> {
fn notify_exexs(&self, block_produced: MadaraPendingBlock, block_number: u64) -> anyhow::Result<()> {
let Some(manager) = self.exex_manager.as_ref() else {
return Ok(());
};

let notification = ExExNotification::BlockClosed { new: BlockNumber(block_n) };
let notification = ExExNotification::BlockProduced {
block: Box::new(block_produced),
block_number: BlockNumber(block_number),
};
manager.send(notification).map_err(|e| anyhow::anyhow!("Could not send ExEx notification: {}", e))
}
}
1 change: 1 addition & 0 deletions crates/client/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mp-chain-config = { workspace = true }
mp-class = { workspace = true }
mp-convert = { workspace = true, default-features = true }
mp-receipt = { workspace = true }
mp-rpc-provider = { workspace = true }
mp-state-update = { workspace = true }
mp-transactions = { workspace = true }
mp-utils = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/client/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod utils;
pub mod versions;

use jsonrpsee::RpcModule;
use mp_rpc_provider::AddTransactionProvider;
use starknet_types_core::felt::Felt;
use std::sync::Arc;

Expand All @@ -23,7 +24,6 @@ use mp_chain_config::{ChainConfig, RpcVersion};
use mp_convert::ToFelt;

use errors::{StarknetRpcApiError, StarknetRpcResult};
use providers::AddTransactionProvider;
use utils::ResultExt;

/// A Starknet RPC server for Madara
Expand Down
3 changes: 1 addition & 2 deletions crates/client/rpc/src/providers/forward_to_provider.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use jsonrpsee::core::{async_trait, RpcResult};
use mp_rpc_provider::AddTransactionProvider;
use starknet_core::types::{
BroadcastedDeclareTransaction, BroadcastedDeployAccountTransaction, BroadcastedInvokeTransaction,
DeclareTransactionResult, DeployAccountTransactionResult, InvokeTransactionResult,
Expand All @@ -7,8 +8,6 @@ use starknet_providers::{Provider, ProviderError};

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

use super::AddTransactionProvider;

pub struct ForwardToProvider<P: Provider + Send + Sync> {
provider: P,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/client/rpc/src/providers/mempool.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::AddTransactionProvider;
use crate::{errors::StarknetRpcApiError, utils::display_internal_server_error};
use jsonrpsee::core::{async_trait, RpcResult};
use mc_mempool::Mempool;
use mc_mempool::MempoolProvider;
use mp_rpc_provider::AddTransactionProvider;
use starknet_core::types::{
BroadcastedDeclareTransaction, BroadcastedDeployAccountTransaction, BroadcastedInvokeTransaction,
DeclareTransactionResult, DeployAccountTransactionResult, InvokeTransactionResult,
Expand Down
24 changes: 0 additions & 24 deletions crates/client/rpc/src/providers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,3 @@ pub mod mempool;

pub use forward_to_provider::*;
pub use mempool::*;

use jsonrpsee::core::{async_trait, RpcResult};
use starknet_core::types::{
BroadcastedDeclareTransaction, BroadcastedDeployAccountTransaction, BroadcastedInvokeTransaction,
DeclareTransactionResult, DeployAccountTransactionResult, InvokeTransactionResult,
};

#[async_trait]
pub trait AddTransactionProvider: Send + Sync {
async fn add_declare_transaction(
&self,
declare_transaction: BroadcastedDeclareTransaction,
) -> RpcResult<DeclareTransactionResult>;

async fn add_deploy_account_transaction(
&self,
deploy_account_transaction: BroadcastedDeployAccountTransaction,
) -> RpcResult<DeployAccountTransactionResult>;

async fn add_invoke_transaction(
&self,
invoke_transaction: BroadcastedInvokeTransaction,
) -> RpcResult<InvokeTransactionResult>;
}
3 changes: 2 additions & 1 deletion crates/client/rpc/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use mp_chain_config::{ChainConfig, StarknetVersion};
use mp_receipt::{
ExecutionResources, ExecutionResult, FeePayment, InvokeTransactionReceipt, PriceUnit, TransactionReceipt,
};
use mp_rpc_provider::AddTransactionProvider;
use mp_state_update::{
ContractStorageDiffItem, DeclaredClassItem, DeployedContractItem, NonceUpdate, ReplacedClassItem, StateDiff,
StorageEntry,
Expand All @@ -21,7 +22,7 @@ use starknet_core::types::{
};
use std::sync::Arc;

use crate::{providers::AddTransactionProvider, Starknet};
use crate::Starknet;

#[cfg(test)]
pub struct TestTransactionProvider;
Expand Down
1 change: 0 additions & 1 deletion crates/client/rpc/src/utils/block.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/client/rpc/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub(crate) mod block;
pub(crate) mod transaction;

use std::fmt;
Expand Down
2 changes: 1 addition & 1 deletion crates/client/sync/src/l2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn notify_exexs(exex_manager: &Option<ExExManagerHandle>, block_n: u64) -> anyho
return Ok(());
};

let notification = ExExNotification::BlockClosed { new: BlockNumber(block_n) };
let notification = ExExNotification::BlockSynced { new: BlockNumber(block_n) };
manager.send(notification).map_err(|e| anyhow::anyhow!("Could not send ExEx notification: {}", e))
}

Expand Down
4 changes: 4 additions & 0 deletions crates/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ mp-block = { workspace = true }
mp-chain-config = { workspace = true }
mp-convert = { workspace = true }
mp-exex = { workspace = true }
mp-rpc-provider = { workspace = true }
mp-transactions = { workspace = true }
mp-utils = { workspace = true }

# Starknet
Expand All @@ -53,6 +55,7 @@ governor.workspace = true
hyper.workspace = true
ip_network.workspace = true
jsonrpsee.workspace = true
lazy_static = { workspace = true }
log = { workspace = true }
primitive-types.workspace = true
rand = { workspace = true }
Expand All @@ -61,6 +64,7 @@ reqwest = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
serde_yaml.workspace = true
starknet-signers = { workspace = true }
thiserror.workspace = true
tokio = { workspace = true }
tower-http.workspace = true
Expand Down
83 changes: 80 additions & 3 deletions crates/node/src/extensions/pragma_dispatch.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,92 @@
//! ExEx of Pragma Dispatcher
//! Adds a new TX at the end of each block, dispatching a message through
//! Hyperlane.
use std::sync::Arc;

use futures::StreamExt;
use mp_exex::{ExExContext, ExExEvent};
use starknet_api::felt;
use starknet_core::types::{
BroadcastedInvokeTransaction, BroadcastedInvokeTransactionV1, BroadcastedTransaction, Felt,
};
use starknet_signers::SigningKey;

use mc_devnet::{Call, Multicall, Selector};
use mc_mempool::transaction_hash;
use mp_chain_config::ChainConfig;
use mp_convert::ToFelt;
use mp_exex::{ExExContext, ExExEvent, ExExNotification};
use mp_transactions::broadcasted_to_blockifier;

lazy_static::lazy_static! {
pub static ref ACCOUNT_ADDRESS: Felt = felt!("0x4a2b383d808b7285cc98b2309f974f5111633c84fd82c9375c118485d2d57ba");
pub static ref PRIVATE_KEY: SigningKey = SigningKey::from_secret_scalar(felt!("0x7a9779748888c95d96bbbce041b5109c6ffc0c4f30561c0170384a5922d9e91"));

pub static ref PRAGMA_DISPATCHER_ADDRESS: Felt = felt!("0x2a85bd616f912537c50a49a4076db02c00b29b2cdc8a197ce92ed1837fa875b");
pub static ref PRAGMA_FEED_IDS: Vec<Felt> = vec![
felt!("18669995996566340"), // BTC/USD: Spot Median
felt!("19514442401534788"), // ETH/USD: Spot Median
];
}

/// 🧩 Pragma main ExEx.
/// At the end of each produced block by the node, adds a new dispatch transaction
/// using the Pragma Dispatcher contract.
pub async fn exex_pragma_dispatch(mut ctx: ExExContext) -> anyhow::Result<()> {
let mut nonce = Felt::ZERO;
while let Some(notification) = ctx.notifications.next().await {
let block_number = notification.closed_block();
log::info!("👋 Hello from the ExEx (triggered at block #{})", block_number);
let block_number = match notification {
ExExNotification::BlockProduced { block: _, block_number } => block_number,
EvolveArt marked this conversation as resolved.
Show resolved Hide resolved
ExExNotification::BlockSynced { new } => {
// This ExEx doesn't do anything for Synced blocks from the Full node
ctx.events.send(ExExEvent::FinishedHeight(new))?;
continue;
}
};

// Create the new Dispatch TX.
let dispatch_tx = create_dispatch_tx(ctx.chain_config.clone(), &nonce)?;
nonce += Felt::ONE;

log::info!("🧩 [#{}] Pragma's ExEx: Adding dispatch transaction...", block_number);
ctx.rpc_add_txs_method_provider.add_invoke_transaction(dispatch_tx).await?;

ctx.events.send(ExExEvent::FinishedHeight(block_number))?;
}
Ok(())
}

/// Creates a new Dispatch transaction.
/// The transaction will be signed using the `ACCOUNT_ADDRESS` and `PRIVATE_KEY` constants.
fn create_dispatch_tx(chain_config: Arc<ChainConfig>, nonce: &Felt) -> anyhow::Result<BroadcastedInvokeTransaction> {
let mut tx = BroadcastedInvokeTransaction::V1(BroadcastedInvokeTransactionV1 {
sender_address: *ACCOUNT_ADDRESS,
calldata: Multicall::default()
.with(Call {
to: *PRAGMA_DISPATCHER_ADDRESS,
selector: Selector::from("dispatch"),
calldata: PRAGMA_FEED_IDS.clone(),
})
.flatten()
.collect(),
max_fee: Felt::ZERO, // TODO: ?
signature: vec![],
nonce: *nonce,
is_query: false,
});

let (blockifier_tx, _) = broadcasted_to_blockifier(
BroadcastedTransaction::Invoke(tx.clone()),
chain_config.chain_id.to_felt(),
chain_config.latest_protocol_version,
)?;

let signature = PRIVATE_KEY.sign(&transaction_hash(&blockifier_tx))?;

let tx_signature = match &mut tx {
BroadcastedInvokeTransaction::V1(tx) => &mut tx.signature,
BroadcastedInvokeTransaction::V3(tx) => &mut tx.signature,
};
*tx_signature = vec![signature.r, signature.s];

Ok(tx)
}
Loading
Loading