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(katana): initialize slot paymaster account #3014

Merged
merged 4 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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: 0 additions & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions bin/katana/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ clap_complete.workspace = true
comfy-table = "7.1.1"
dojo-utils.workspace = true
inquire = "0.7.5"
lazy_static.workspace = true
piltover = { git = "https://github.com/keep-starknet-strange/piltover.git", rev = "fb9d988" }
rand.workspace = true
shellexpand = "3.1.0"
Expand All @@ -41,7 +40,8 @@ rstest.workspace = true
starknet.workspace = true

[features]
default = [ "jemalloc", "katana-cli/slot" ]
default = [ "init-slot", "jemalloc", "katana-cli/slot" ]

init-custom-settlement-chain = [ ]
init-slot = [ ]
jemalloc = [ ]
38 changes: 26 additions & 12 deletions bin/katana/src/cli/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use katana_primitives::genesis::constant::DEFAULT_PREFUNDED_ACCOUNT_BALANCE;
use katana_primitives::genesis::Genesis;
use katana_primitives::{ContractAddress, Felt, U256};
use lazy_static::lazy_static;
use prompt::CARTRIDGE_SN_SEPOLIA_PROVIDER;
use starknet::accounts::{ExecutionEncoding, SingleOwnerAccount};
use starknet::core::utils::{cairo_short_string_to_felt, parse_cairo_short_string};
Expand All @@ -24,6 +23,8 @@

mod deployment;
mod prompt;
#[cfg(feature = "init-slot")]
mod slot;

#[derive(Debug, Args)]
pub struct InitArgs {
Expand Down Expand Up @@ -54,13 +55,17 @@
/// Specify the path of the directory where the configuration files will be stored at.
#[arg(long)]
output_path: Option<PathBuf>,

#[cfg(feature = "init-slot")]
#[command(flatten)]
slot: slot::SlotArgs,
}

impl InitArgs {
// TODO:
// - deploy bridge contract
pub(crate) async fn execute(self) -> anyhow::Result<()> {
let output = if let Some(output) = self.process_args().await {
let output = if let Some(output) = self.configure_from_args().await {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L68 was not covered by tests
output?
} else {
prompt::prompt().await?
Expand All @@ -75,7 +80,12 @@
};

let id = ChainId::parse(&output.id)?;
let genesis = GENESIS.clone();

#[cfg_attr(not(feature = "init-slot"), allow(unused_mut))]
let mut genesis = generate_genesis();
#[cfg(feature = "init-slot")]
slot::add_paymasters_to_genesis(&mut genesis, &output.slot_paymasters.unwrap_or_default());

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

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/mod.rs#L85-L88

Added lines #L85 - L88 were not covered by tests
// At the moment, the fee token is limited to a predefined token.
let fee_contract = FeeContract::default();
let chain_spec = rollup::ChainSpec { id, genesis, settlement, fee_contract };
Expand All @@ -92,7 +102,7 @@
Ok(())
}

async fn process_args(&self) -> Option<anyhow::Result<Outcome>> {
async fn configure_from_args(&self) -> Option<anyhow::Result<Outcome>> {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L105 was not covered by tests
// Here we just check that if `id` is present, then all the other required* arguments must
// be present as well. This is guaranteed by `clap`.
if let Some(id) = self.id.clone() {
Expand Down Expand Up @@ -144,6 +154,8 @@
rpc_url: settlement_url,
account: settlement_account_address,
settlement_id: parse_cairo_short_string(&l1_chain_id).unwrap(),
#[cfg(feature = "init-slot")]
slot_paymasters: self.slot.paymaster_accounts.clone(),

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

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/mod.rs#L157-L158

Added lines #L157 - L158 were not covered by tests
}))
} else {
None
Expand All @@ -167,16 +179,18 @@
pub rpc_url: Url,

pub deployment_outcome: DeploymentOutcome,

#[cfg(feature = "init-slot")]
pub slot_paymasters: Option<Vec<slot::PaymasterAccountArgs>>,
}

lazy_static! {
static ref GENESIS: Genesis = {
// master account
let accounts = DevAllocationsGenerator::new(1).with_balance(U256::from(DEFAULT_PREFUNDED_ACCOUNT_BALANCE)).generate();
let mut genesis = Genesis::default();
genesis.extend_allocations(accounts.into_iter().map(|(k, v)| (k, v.into())));
genesis
};
fn generate_genesis() -> Genesis {
let accounts = DevAllocationsGenerator::new(1)
.with_balance(U256::from(DEFAULT_PREFUNDED_ACCOUNT_BALANCE))
.generate();
let mut genesis = Genesis::default();
genesis.extend_allocations(accounts.into_iter().map(|(k, v)| (k, v.into())));
genesis

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

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/mod.rs#L187-L193

Added lines #L187 - L193 were not covered by tests
}

#[derive(Debug, thiserror::Error)]
Expand Down
15 changes: 14 additions & 1 deletion bin/katana/src/cli/init/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,24 @@
DeploymentOutcome { contract_address: address, block_number }
};

// Prompt for slot paymaster accounts
let mut slot_paymasters = Vec::new();

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L129 was not covered by tests

while Confirm::new("Add slot paymaster account?").with_default(true).prompt()? {
let public_key = CustomType::<Felt>::new("Paymaster public key")
.with_formatter(&|input: Felt| format!("{input:#x}"))
.prompt()?;

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

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/prompt.rs#L131-L134

Added lines #L131 - L134 were not covered by tests

slot_paymasters.push(super::slot::PaymasterAccountArgs { public_key });

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L136 was not covered by tests
}

Ok(Outcome {
id: chain_id,
deployment_outcome,
account: account_address,
rpc_url: settlement_url,
account: account_address,

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L143 was not covered by tests
settlement_id: parse_cairo_short_string(&l1_chain_id)?,
#[cfg(feature = "init-slot")]
slot_paymasters: Some(slot_paymasters),

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L146 was not covered by tests
})
}
92 changes: 92 additions & 0 deletions bin/katana/src/cli/init/slot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use std::str::FromStr;

use anyhow::Result;
use clap::Args;
use katana_primitives::genesis::allocation::{
GenesisAccount, GenesisAccountAlloc, GenesisAllocation,
};
use katana_primitives::genesis::constant::{
DEFAULT_ACCOUNT_CLASS_HASH, DEFAULT_PREFUNDED_ACCOUNT_BALANCE,
};
use katana_primitives::genesis::Genesis;
use katana_primitives::{ContractAddress, Felt, U256};

#[derive(Debug, Args)]
#[command(next_help_heading = "Slot options")]
pub struct SlotArgs {
#[arg(long)]
pub slot: bool,

Check warning on line 18 in bin/katana/src/cli/init/slot.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/slot.rs#L18

Added line #L18 was not covered by tests

#[arg(requires_all = ["id", "slot"])]
#[arg(long = "slot.paymasters", value_delimiter = ',')]
pub paymaster_accounts: Option<Vec<PaymasterAccountArgs>>,

Check warning on line 22 in bin/katana/src/cli/init/slot.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/slot.rs#L22

Added line #L22 was not covered by tests
Comment on lines +20 to +22
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Ohayo sensei! The argument constraints need adjustment.

The requires_all = ["id", "slot"] constraint contradicts the PR objectives. According to the example usage katana init --slot --slot.paymasters 0x1,0x2,0x3, only the --slot flag is needed, not an id argument.

-    #[arg(requires_all = ["id", "slot"])]
+    #[arg(requires = "slot")]
     #[arg(long = "slot.paymasters", value_delimiter = ',')]
     pub paymaster_accounts: Option<Vec<PaymasterAccountArgs>>,
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#[arg(requires_all = ["id", "slot"])]
#[arg(long = "slot.paymasters", value_delimiter = ',')]
pub paymaster_accounts: Option<Vec<PaymasterAccountArgs>>,
#[arg(requires = "slot")]
#[arg(long = "slot.paymasters", value_delimiter = ',')]
pub paymaster_accounts: Option<Vec<PaymasterAccountArgs>>,

}

#[derive(Debug, Clone)]
pub struct PaymasterAccountArgs {
/// The public key of the paymaster account.
pub public_key: Felt,
}

impl FromStr for PaymasterAccountArgs {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self> {
Ok(PaymasterAccountArgs { public_key: Felt::from_str(s)? })
}

Check warning on line 36 in bin/katana/src/cli/init/slot.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/slot.rs#L34-L36

Added lines #L34 - L36 were not covered by tests
}

pub fn add_paymasters_to_genesis(
genesis: &mut Genesis,
slot_paymasters: &[PaymasterAccountArgs],
) -> Vec<ContractAddress> {
let mut accounts = Vec::with_capacity(slot_paymasters.len());

for paymaster in slot_paymasters {
let public_key = paymaster.public_key;
let class_hash = DEFAULT_ACCOUNT_CLASS_HASH;
let balance = U256::from(DEFAULT_PREFUNDED_ACCOUNT_BALANCE);

let (addr, account) = GenesisAccount::new_with_balance(public_key, class_hash, balance);
let account = GenesisAllocation::Account(GenesisAccountAlloc::Account(account));
accounts.push((addr, account));
}

let addresses: Vec<ContractAddress> = accounts.iter().map(|(addr, ..)| *addr).collect();
genesis.extend_allocations(accounts);

addresses
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_add_paymasters_to_genesis() {
let mut genesis = Genesis::default();
let mut paymasters = Vec::new();

for i in 0..3 {
paymasters.push(PaymasterAccountArgs { public_key: Felt::from(i) });
}

let expected_addresses = add_paymasters_to_genesis(&mut genesis, &paymasters);

for (i, addr) in expected_addresses.iter().enumerate() {
let account = genesis.allocations.get(addr).expect("account missing");
match account {
GenesisAllocation::Account(GenesisAccountAlloc::Account(account)) => {
assert_eq!(account.public_key, Felt::from(i));
assert_eq!(account.class_hash, DEFAULT_ACCOUNT_CLASS_HASH);
assert_eq!(
account.balance,
Some(U256::from(DEFAULT_PREFUNDED_ACCOUNT_BALANCE))
);
}

_ => panic!("Expected GenesisAccountAlloc::Account"),

Check warning on line 88 in bin/katana/src/cli/init/slot.rs

View check run for this annotation

Codecov / codecov/patch

bin/katana/src/cli/init/slot.rs#L88

Added line #L88 was not covered by tests
}
}
}
}
Loading