Skip to content

Commit

Permalink
refactor(katana): display genesis state properly on startup (#1492)
Browse files Browse the repository at this point in the history
* refactor(katana): display genesis state properly

* fix
  • Loading branch information
kariy authored Jan 30, 2024
1 parent 10031f0 commit ce58ff2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 72 deletions.
142 changes: 75 additions & 67 deletions bin/katana/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use std::io;
use std::net::SocketAddr;
use std::sync::Arc;

use clap::{CommandFactory, Parser};
use clap_complete::{generate, Shell};
use console::Style;
use katana_core::sequencer::KatanaSequencer;
use katana_primitives::contract::ContractAddress;
use katana_primitives::contract::{ClassHash, ContractAddress};
use katana_primitives::genesis::allocation::GenesisAccountAlloc;
use katana_primitives::genesis::constant::{
DEFAULT_FEE_TOKEN_ADDRESS, DEFAULT_LEGACY_ERC20_CONTRACT_CLASS_HASH,
DEFAULT_LEGACY_UDC_CLASS_HASH, DEFAULT_UDC_ADDRESS,
};
use katana_primitives::genesis::Genesis;
use katana_rpc::{spawn, NodeHandle};
use metrics::prometheus_exporter;
use tokio::signal::ctrl_c;
Expand All @@ -24,10 +22,10 @@ use args::KatanaArgs;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = KatanaArgs::parse();
config.init_logging()?;
let args = KatanaArgs::parse();
args.init_logging()?;

if let Some(command) = config.command {
if let Some(command) = args.command {
match command {
Completions { shell } => {
print_completion(shell);
Expand All @@ -36,40 +34,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
}

let server_config = config.server_config();
let sequencer_config = config.sequencer_config();
let starknet_config = config.starknet_config();
let server_config = args.server_config();
let sequencer_config = args.sequencer_config();
let starknet_config = args.starknet_config();

let sequencer = Arc::new(KatanaSequencer::new(sequencer_config, starknet_config).await?);
let NodeHandle { addr, handle, .. } = spawn(Arc::clone(&sequencer), server_config).await?;

if !config.silent {
let mut accounts = sequencer.backend().config.genesis.accounts().peekable();
let account_class_hash = accounts.peek().unwrap().1.class_hash();

if config.json_log {
info!(
"{}",
serde_json::json!({
"accounts": accounts.map(|a| serde_json::json!(a)).collect::<Vec<_>>(),
"seed": format!("{}", config.starknet.seed),
"address": format!("{addr}"),
})
)
} else {
print_intro(
accounts,
config.starknet.seed.clone(),
format!(
"🚀 JSON-RPC server started: {}",
Style::new().red().apply_to(format!("http://{addr}"))
),
format!("{:#064x}", account_class_hash),
);
}
if !args.silent {
let genesis = &sequencer.backend().config.genesis;
print_intro(&args, genesis, addr);
}

if let Some(listen_addr) = config.metrics {
if let Some(listen_addr) = args.metrics {
let prometheus_handle = prometheus_exporter::install_recorder("katana")?;

info!(target: "katana::cli", addr = %listen_addr, "Starting metrics endpoint");
Expand All @@ -94,16 +71,25 @@ fn print_completion(shell: Shell) {
generate(shell, &mut command, name, &mut io::stdout());
}

fn print_intro<'a>(
accounts: impl Iterator<Item = (&'a ContractAddress, &'a GenesisAccountAlloc)>,
seed: String,
address: String,
account_class_hash: String,
) {
println!(
"{}",
Style::new().red().apply_to(
r"
fn print_intro(args: &KatanaArgs, genesis: &Genesis, address: SocketAddr) {
let mut accounts = genesis.accounts().peekable();
let account_class_hash = accounts.peek().map(|e| e.1.class_hash());
let seed = &args.starknet.seed;

if args.json_log {
info!(
"{}",
serde_json::json!({
"accounts": accounts.map(|a| serde_json::json!(a)).collect::<Vec<_>>(),
"seed": format!("{}", seed),
"address": format!("{address}"),
})
)
} else {
println!(
"{}",
Style::new().red().apply_to(
r"
██╗ ██╗ █████╗ ████████╗ █████╗ ███╗ ██╗ █████╗
Expand All @@ -113,38 +99,59 @@ fn print_intro<'a>(
██║ ██╗██║ ██║ ██║ ██║ ██║██║ ╚████║██║ ██║
╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═══╝╚═╝ ╚═╝
"
)
);
)
);

print_genesis_contracts(genesis, account_class_hash);
print_genesis_accounts(accounts);

println!(
r"
ACCOUNTS SEED
=============
{seed}
"
);

let addr = format!(
"🚀 JSON-RPC server started: {}",
Style::new().red().apply_to(format!("http://{address}"))
);

println!("\n{addr}\n\n",);
}
}

fn print_genesis_contracts(genesis: &Genesis, account_class_hash: Option<ClassHash>) {
println!(
r"
PREDEPLOYED CONTRACTS
==================
| Contract | Fee Token
| Address | {DEFAULT_FEE_TOKEN_ADDRESS}
| Class Hash | {DEFAULT_LEGACY_ERC20_CONTRACT_CLASS_HASH:#064x}
| Address | {}
| Class Hash | {:#064x}",
genesis.fee_token.address, genesis.fee_token.class_hash,
);

if let Some(ref udc) = genesis.universal_deployer {
println!(
r"
| Contract | Universal Deployer
| Address | {DEFAULT_UDC_ADDRESS}
| Class Hash | {DEFAULT_LEGACY_UDC_CLASS_HASH:#064x}
| Address | {}
| Class Hash | {:#064x}",
udc.address, udc.class_hash
)
}

if let Some(hash) = account_class_hash {
println!(
r"
| Contract | Account Contract
| Class Hash | {account_class_hash}
"
);

print_genesis_accounts(accounts);

println!(
r"
ACCOUNTS SEED
=============
{seed}
"
);

println!("\n{address}\n\n");
| Class Hash | {hash:#064x}"
)
}
}

fn print_genesis_accounts<'a, Accounts>(accounts: Accounts)
Expand All @@ -153,6 +160,7 @@ where
{
println!(
r"
PREFUNDED ACCOUNTS
=================="
);
Expand Down
3 changes: 1 addition & 2 deletions crates/katana/core/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use katana_primitives::block::{
};
use katana_primitives::chain::ChainId;
use katana_primitives::env::{BlockEnv, CfgEnv, FeeTokenAddressses};
use katana_primitives::genesis::constant::DEFAULT_FEE_TOKEN_ADDRESS;
use katana_primitives::receipt::Receipt;
use katana_primitives::state::StateUpdatesWithDeclaredClasses;
use katana_primitives::transaction::TxWithHash;
Expand Down Expand Up @@ -202,7 +201,7 @@ impl Backend {
validate_max_n_steps: self.config.env.validate_max_steps,
max_recursion_depth: MAX_RECURSION_DEPTH,
fee_token_addresses: FeeTokenAddressses {
eth: DEFAULT_FEE_TOKEN_ADDRESS,
eth: self.config.genesis.fee_token.address,
strk: Default::default(),
},
}
Expand Down
8 changes: 6 additions & 2 deletions crates/katana/primitives/src/genesis/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,9 @@ mod tests {

assert_eq!(
genesis.universal_deployer.clone().unwrap().address,
Some(ContractAddress::from(felt!("0x77")))
Some(ContractAddress::from(felt!(
"0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf"
)))
);
assert_eq!(genesis.universal_deployer.unwrap().class, None);

Expand Down Expand Up @@ -636,7 +638,9 @@ mod tests {
gas_prices: GasPrices { eth: 1111, strk: 2222 },
universal_deployer: Some(UniversalDeployerConfig {
class_hash: DEFAULT_LEGACY_UDC_CLASS_HASH,
address: ContractAddress::from(felt!("0x77")),
address: ContractAddress::from(felt!(
"0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf"
)),
}),
};

Expand Down
2 changes: 1 addition & 1 deletion crates/katana/primitives/src/genesis/test-genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"class": "0x8"
},
"universalDeployer": {
"address": "0x77"
"address": "0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf"
},
"accounts": {
"0x66efb28ac62686966ae85095ff3a772e014e7fbf56d4c5f6fac5606d4dde23a": {
Expand Down

0 comments on commit ce58ff2

Please sign in to comment.