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-runner): allow configuring chain id #2554

Merged
merged 4 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 21 additions & 2 deletions crates/katana/runner/macro/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
pub db_dir: Option<syn::Expr>,
pub block_time: Option<syn::Expr>,
pub log_path: Option<syn::Expr>,
pub chain_id: Option<syn::Expr>,
}

impl Configuration {
Expand All @@ -32,6 +33,7 @@
validation: None,
block_time: None,
crate_name: None,
chain_id: None,
}
}

Expand Down Expand Up @@ -105,6 +107,19 @@
self.accounts = Some(accounts);
Ok(())
}

fn set_chain_id(
&mut self,
chain_id: syn::Expr,
span: proc_macro2::Span,
) -> Result<(), syn::Error> {
if self.chain_id.is_some() {
return Err(syn::Error::new(span, "`chain_id` set multiple times."));

Check warning on line 117 in crates/katana/runner/macro/src/config.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/runner/macro/src/config.rs#L117

Added line #L117 was not covered by tests
}

self.chain_id = Some(chain_id);
Ok(())
}
}

enum RunnerArg {
Expand All @@ -113,6 +128,7 @@
Validation,
Accounts,
DbDir,
ChainId,
}

impl std::str::FromStr for RunnerArg {
Expand All @@ -125,9 +141,10 @@
"validation" => Ok(RunnerArg::Validation),
"accounts" => Ok(RunnerArg::Accounts),
"db_dir" => Ok(RunnerArg::DbDir),
"chain_id" => Ok(RunnerArg::ChainId),
_ => Err(format!(
"Unknown attribute {s} is specified; expected one of: `fee`, `validation`, \
`accounts`, `db_dir`, `block_time`",
`accounts`, `db_dir`, `block_time`, `chain_id`",

Check warning on line 147 in crates/katana/runner/macro/src/config.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/runner/macro/src/config.rs#L147

Added line #L147 was not covered by tests
)),
}
}
Expand Down Expand Up @@ -172,7 +189,9 @@
RunnerArg::DbDir => {
config.set_db_dir(expr.clone(), Spanned::span(&namevalue))?
}

RunnerArg::ChainId => {
config.set_chain_id(expr.clone(), Spanned::span(&namevalue))?
}
RunnerArg::Fee => config.set_fee(expr.clone(), Spanned::span(&namevalue))?,
}
}
Expand Down
6 changes: 5 additions & 1 deletion crates/katana/runner/macro/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@
}

if let Some(value) = config.log_path {
cfg = quote_spanned! (last_stmt_start_span=> #cfg, log_path: Some(#value), );
cfg = quote_spanned! (last_stmt_start_span=> #cfg log_path: Some(#value), );

Check warning on line 77 in crates/katana/runner/macro/src/entry.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/runner/macro/src/entry.rs#L77

Added line #L77 was not covered by tests
}

if let Some(value) = config.chain_id {
cfg = quote_spanned! (last_stmt_start_span=> #cfg chain_id: Some(#value), );
}

if config.dev {
Expand Down
7 changes: 7 additions & 0 deletions crates/katana/runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ pub struct KatanaRunnerConfig {
pub db_dir: Option<PathBuf>,
/// Whether to run the katana runner with the `dev` rpc endpoints.
pub dev: bool,
/// The chain id to use.
pub chain_id: Option<Felt>,
}

impl Default for KatanaRunnerConfig {
Expand All @@ -82,6 +84,7 @@ impl Default for KatanaRunnerConfig {
messaging: None,
db_dir: None,
dev: false,
chain_id: None,
}
}
}
Expand Down Expand Up @@ -123,6 +126,10 @@ impl KatanaRunner {
.dev(config.dev)
.fee(!config.disable_fee);

if let Some(id) = config.chain_id {
builder = builder.chain_id(id);
}

if let Some(block_time_ms) = config.block_time {
builder = builder.block_time(block_time_ms);
}
Expand Down
12 changes: 11 additions & 1 deletion crates/katana/runner/tests/runner.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
use katana_runner::RunnerCtx;
use starknet::macros::short_string;
use starknet::providers::Provider;

#[katana_runner::test(fee = false, accounts = 7)]
fn simple(runner: &RunnerCtx) {
assert_eq!(runner.accounts().len(), 7);
}

#[tokio::test(flavor = "multi_thread")]
#[katana_runner::test(chain_id = short_string!("SN_SEPOLIA"))]
async fn custom_chain_id(runner: &RunnerCtx) {
let provider = runner.provider();
let id = provider.chain_id().await.unwrap();
assert_eq!(id, short_string!("SN_SEPOLIA"));
}

#[katana_runner::test]
fn with_return(_: &RunnerCtx) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
Expand All @@ -15,6 +24,7 @@ fn with_return(_: &RunnerCtx) -> Result<(), Box<dyn std::error::Error>> {
#[katana_runner::test]
async fn with_async(ctx: &RunnerCtx) -> Result<(), Box<dyn std::error::Error>> {
let provider = ctx.provider();
let _ = provider.chain_id().await?;
let id = provider.chain_id().await?;
assert_eq!(id, short_string!("KATANA"));
Ok(())
}
Loading