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

refactor(katana): move messaging into its own crate #3013

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
42 changes: 33 additions & 9 deletions Cargo.lock

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

10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ members = [
"crates/katana/executor",
"crates/katana/feeder-gateway",
"crates/katana/grpc",
"crates/katana/messaging",
"crates/katana/node",
"crates/katana/node-bindings",
"crates/katana/pool",
Expand Down Expand Up @@ -98,12 +99,13 @@ katana-core = { path = "crates/katana/core", default-features = false }
katana-db = { path = "crates/katana/storage/db" }
katana-executor = { path = "crates/katana/executor" }
katana-feeder-gateway = { path = "crates/katana/feeder-gateway" }
katana-messaging = { path = "crates/katana/messaging" }
katana-node = { path = "crates/katana/node", default-features = false }
katana-node-bindings = { path = "crates/katana/node-bindings" }
katana-pipeline = { path = "crates/katana/sync/pipeline" }
katana-pool = { path = "crates/katana/pool" }
katana-primitives = { path = "crates/katana/primitives" }
katana-provider = { path = "crates/katana/storage/provider" }
katana-provider = { path = "crates/katana/storage/provider", default-features = false }
katana-rpc = { path = "crates/katana/rpc/rpc" }
katana-rpc-api = { path = "crates/katana/rpc/rpc-api", default-features = false }
katana-rpc-types = { path = "crates/katana/rpc/rpc-types" }
Expand All @@ -117,13 +119,13 @@ katana-trie = { path = "crates/katana/trie" }
# torii
torii-cli = { path = "crates/torii/cli" }
torii-client = { path = "crates/torii/client" }
torii-indexer = { path = "crates/torii/indexer" }
torii-sqlite = { path = "crates/torii/sqlite" }
torii-graphql = { path = "crates/torii/graphql" }
torii-grpc = { path = "crates/torii/grpc" }
torii-indexer = { path = "crates/torii/indexer" }
torii-relay = { path = "crates/torii/libp2p" }
torii-server = { path = "crates/torii/server" }
torii-runner = { path = "crates/torii/runner" }
torii-server = { path = "crates/torii/server" }
torii-sqlite = { path = "crates/torii/sqlite" }
torii-typed-data = { path = "crates/torii/typed-data" }

# sozo
Expand Down
45 changes: 39 additions & 6 deletions bin/katana/src/cli/init/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use anyhow::{anyhow, Result};
use cainome::cairo_serde;
use dojo_utils::{TransactionWaiter, TransactionWaitingError};
use katana_primitives::block::BlockHash;
use katana_primitives::block::{BlockHash, BlockNumber};
use katana_primitives::class::{
CompiledClassHash, ComputeClassHashError, ContractClass, ContractClassCompilationError,
ContractClassFromStrError,
Expand Down Expand Up @@ -71,12 +71,20 @@
const ATLANTIC_FACT_REGISTRY_SEPOLIA: Felt =
felt!("0x4ce7851f00b6c3289674841fd7a1b96b6fd41ed1edc248faccd672c26371b8c");

#[derive(Debug)]
pub struct DeploymentOutcome {
/// The address of the deployed settlement contract.
pub contract_address: ContractAddress,
/// The block number at which the contract was deployed.
pub block_number: BlockNumber,
}

/// Deploys the settlement contract in the settlement layer and initializes it with the right
/// necessary states.
pub async fn deploy_settlement_contract(
mut account: InitializerAccount,
chain_id: Felt,
) -> Result<ContractAddress, ContractInitError> {
) -> Result<DeploymentOutcome, ContractInitError> {

Check warning on line 87 in bin/katana/src/cli/init/deployment.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/deployment.rs#L87

Added line #L87 was not covered by tests
// This is important! Otherwise all the estimate fees after a transaction will be executed
// against invalid state.
account.set_block_id(BlockId::Tag(BlockTag::Pending));
Expand Down Expand Up @@ -163,7 +171,25 @@
})
.map_err(ContractInitError::DeploymentError)?;

TransactionWaiter::new(res.transaction_hash, account.provider()).await?;
// there's a chance that when we query the block number, that there would be a mismatch
// between the block info returned by the receipt. so we query both at the same time
// to minimize the chance of a mismatch.
let (deployment_receipt_res, block_number_res) = tokio::join!(
TransactionWaiter::new(res.transaction_hash, account.provider()),
account.provider().block_number()
);

Check warning on line 180 in bin/katana/src/cli/init/deployment.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/deployment.rs#L177-L180

Added lines #L177 - L180 were not covered by tests

let deployment_receipt = deployment_receipt_res?;
let block_number = block_number_res?;

Check warning on line 183 in bin/katana/src/cli/init/deployment.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/deployment.rs#L182-L183

Added lines #L182 - L183 were not covered by tests

// If there's no block number in the receipt, that means it's still in the pending block.
let deployment_block = if let Some(block) = deployment_receipt.block.block_number() {
block

Check warning on line 187 in bin/katana/src/cli/init/deployment.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/deployment.rs#L186-L187

Added lines #L186 - L187 were not covered by tests
} else {
// we assume the block_number is the block number of the previous block (latest) so we
// add 1 to the block_number as the number of the pending block
block_number + 1

Check warning on line 191 in bin/katana/src/cli/init/deployment.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/deployment.rs#L191

Added line #L191 was not covered by tests
};

// -----------------------------------------------------------------------
// CONTRACT INITIALIZATIONS
Expand Down Expand Up @@ -228,14 +254,21 @@

check_program_info(chain_id, deployed_appchain_contract, account.provider()).await?;

Ok(deployed_appchain_contract.into())
Ok(DeploymentOutcome {
block_number: deployment_block,
contract_address: deployed_appchain_contract.into(),
})

Check warning on line 260 in bin/katana/src/cli/init/deployment.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/deployment.rs#L257-L260

Added lines #L257 - L260 were not covered by tests
}
.await;

match result {
Ok(addr) => sp.success(&format!("Deployment successful ({addr})")),
match &result {
Ok(outcome) => sp.success(&format!(
"Deployment successful ({}) at block #{}",
outcome.contract_address, outcome.block_number
)),

Check warning on line 268 in bin/katana/src/cli/init/deployment.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/deployment.rs#L264-L268

Added lines #L264 - L268 were not covered by tests
Err(..) => sp.fail("Deployment failed"),
}

result
}

Expand Down
25 changes: 19 additions & 6 deletions bin/katana/src/cli/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use anyhow::Context;
use clap::Args;
use deployment::DeploymentOutcome;
use katana_chain_spec::rollup::{ChainConfigDir, FeeContract};
use katana_chain_spec::{rollup, SettlementLayer};
use katana_primitives::block::BlockNumber;
use katana_primitives::chain::ChainId;
use katana_primitives::genesis::allocation::DevAllocationsGenerator;
use katana_primitives::genesis::constant::DEFAULT_PREFUNDED_ACCOUNT_BALANCE;
Expand Down Expand Up @@ -42,9 +44,13 @@
settlement_account_private_key: Option<Felt>,

#[arg(long = "settlement-contract")]
#[arg(requires_all = ["id", "settlement_chain", "settlement_account", "settlement_account_private_key"])]
#[arg(requires_all = ["id", "settlement_chain", "settlement_account", "settlement_account_private_key", "settlement_contract_deployed_block"])]
settlement_contract: Option<ContractAddress>,

#[arg(long = "settlement-contract-deployed-block")]
#[arg(requires_all = ["id", "settlement_chain", "settlement_account", "settlement_account_private_key", "settlement_contract"])]
settlement_contract_deployed_block: Option<BlockNumber>,

/// Specify the path of the directory where the configuration files will be stored at.
#[arg(long)]
output_path: Option<PathBuf>,
Expand All @@ -64,7 +70,8 @@
account: output.account,
rpc_url: output.rpc_url,
id: ChainId::parse(&output.settlement_id)?,
core_contract: output.settlement_contract,
block: output.deployment_outcome.block_number,
core_contract: output.deployment_outcome.contract_address,

Check warning on line 74 in bin/katana/src/cli/init/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/mod.rs#L73-L74

Added lines #L73 - L74 were not covered by tests
};

let id = ChainId::parse(&output.id)?;
Expand Down Expand Up @@ -105,12 +112,18 @@
Arc::new(JsonRpcClient::new(HttpTransport::new(settlement_url.clone())));
let l1_chain_id = l1_provider.chain_id().await.unwrap();

let settlement_contract = if let Some(contract) = self.settlement_contract {
let deployment_outcome = if let Some(contract) = self.settlement_contract {

Check warning on line 115 in bin/katana/src/cli/init/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/mod.rs#L115

Added line #L115 was not covered by tests
let chain_id = cairo_short_string_to_felt(&id).unwrap();
deployment::check_program_info(chain_id, contract.into(), &l1_provider)
.await
.unwrap();
contract

DeploymentOutcome {
contract_address: contract,
block_number: self
.settlement_contract_deployed_block
.expect("must exist at this point"),
}

Check warning on line 126 in bin/katana/src/cli/init/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/mod.rs#L120-L126

Added lines #L120 - L126 were not covered by tests
}
// If settlement contract is not provided, then we will deploy it.
else {
Expand All @@ -127,7 +140,7 @@

Some(Ok(Outcome {
id,
settlement_contract,
deployment_outcome,

Check warning on line 143 in bin/katana/src/cli/init/mod.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/mod.rs#L143

Added line #L143 was not covered by tests
rpc_url: settlement_url,
account: settlement_account_address,
settlement_id: parse_cairo_short_string(&l1_chain_id).unwrap(),
Expand All @@ -153,7 +166,7 @@
// the rpc url for the settlement layer.
pub rpc_url: Url,

pub settlement_contract: ContractAddress,
pub deployment_outcome: DeploymentOutcome,
}

lazy_static! {
Expand Down
13 changes: 10 additions & 3 deletions bin/katana/src/cli/init/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use anyhow::{Context, Result};
use inquire::{Confirm, CustomType, Select};
use katana_primitives::block::BlockNumber;
use katana_primitives::{ContractAddress, Felt};
use starknet::accounts::{ExecutionEncoding, SingleOwnerAccount};
use starknet::core::types::{BlockId, BlockTag};
Expand All @@ -13,6 +14,7 @@
use tokio::runtime::Handle;

use super::{deployment, Outcome};
use crate::cli::init::deployment::DeploymentOutcome;

pub const CARTRIDGE_SN_SEPOLIA_PROVIDER: &str = "https://api.cartridge.gg/x/starknet/sepolia";

Expand Down Expand Up @@ -97,7 +99,7 @@

// The core settlement contract on L1c.
// Prompt the user whether to deploy the settlement contract or not.
let settlement_contract =
let deployment_outcome =

Check warning on line 102 in bin/katana/src/cli/init/prompt.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/prompt.rs#L102

Added line #L102 was not covered by tests
if Confirm::new("Deploy settlement contract?").with_default(true).prompt()? {
let chain_id = cairo_short_string_to_felt(&chain_id)?;
deployment::deploy_settlement_contract(account, chain_id).await?
Expand All @@ -115,12 +117,17 @@
"Invalid settlement contract. The contract might have been configured incorrectly.",
)?;

address
let block_number =
CustomType::<BlockNumber>::new("Settlement contract deployment block")
.with_help_message("The block at which the settlement contract was deployed")
.prompt()?;

Check warning on line 123 in bin/katana/src/cli/init/prompt.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/prompt.rs#L120-L123

Added lines #L120 - L123 were not covered by tests

DeploymentOutcome { contract_address: address, block_number }

Check warning on line 125 in bin/katana/src/cli/init/prompt.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/prompt.rs#L125

Added line #L125 was not covered by tests
};

Ok(Outcome {
id: chain_id,
settlement_contract,
deployment_outcome,

Check warning on line 130 in bin/katana/src/cli/init/prompt.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/prompt.rs#L130

Added line #L130 was not covered by tests
account: account_address,
rpc_url: settlement_url,
settlement_id: parse_cairo_short_string(&l1_chain_id)?,
Expand Down
2 changes: 1 addition & 1 deletion crates/katana/chain-spec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ url.workspace = true

[dev-dependencies]
katana-executor.workspace = true
katana-provider = { workspace = true, features = ["test-utils"] }
katana-provider = { workspace = true, features = [ "test-utils" ] }
rstest.workspace = true
similar-asserts.workspace = true
tempfile.workspace = true
Expand Down
7 changes: 7 additions & 0 deletions crates/katana/chain-spec/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use katana_primitives::block::BlockNumber;
use katana_primitives::chain::ChainId;
use katana_primitives::genesis::Genesis;
use katana_primitives::{eth, ContractAddress};
Expand Down Expand Up @@ -79,6 +80,9 @@ pub enum SettlementLayer {

// - The core appchain contract used to settlement
core_contract: eth::Address,

// the block at which the core contract was deployed
block: alloy_primitives::BlockNumber,
},

Starknet {
Expand All @@ -93,5 +97,8 @@ pub enum SettlementLayer {

// - The core appchain contract used to settlement
core_contract: ContractAddress,

// the block at which the core contract was deployed
block: BlockNumber,
},
}
1 change: 1 addition & 0 deletions crates/katana/chain-spec/src/rollup/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ mod tests {
genesis: Genesis::default(),
fee_contract: FeeContract { strk: ContractAddress::default() },
settlement: SettlementLayer::Starknet {
block: 0,
id: ChainId::default(),
account: ContractAddress::default(),
core_contract: ContractAddress::default(),
Expand Down
1 change: 1 addition & 0 deletions crates/katana/chain-spec/src/rollup/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ mod tests {
let fee_contract = FeeContract::default();

let settlement = SettlementLayer::Starknet {
block: 0,
id: ChainId::default(),
account: Default::default(),
core_contract: Default::default(),
Expand Down
1 change: 1 addition & 0 deletions crates/katana/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ version.workspace = true
[dependencies]
katana-chain-spec.workspace = true
katana-core.workspace = true
katana-messaging.workspace = true
katana-node.workspace = true
katana-primitives.workspace = true
katana-rpc.workspace = true
Expand Down
Loading
Loading