Skip to content

Commit

Permalink
Add default gas limit value to cairo-run
Browse files Browse the repository at this point in the history
  • Loading branch information
maciektr committed Dec 18, 2023
1 parent 15b46d8 commit eecdba1
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 26 deletions.
50 changes: 43 additions & 7 deletions extensions/scarb-cairo-run/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct Args {

fn main() -> Result<()> {
let args: Args = Args::parse();
let available_gas = GasLimit::parse(args.available_gas);

let ui = Ui::new(Verbosity::default(), OutputFormat::Text);

Expand Down Expand Up @@ -76,16 +77,16 @@ fn main() -> Result<()> {
.into_v1()
.with_context(|| format!("failed to load Sierra program: {path}"))?;

if args.available_gas.is_none() && sierra_program.program.requires_gas_counter() {
if available_gas.is_disabled() && sierra_program.program.requires_gas_counter() {
bail!("program requires gas counter, please provide `--available-gas` argument");
}

let runner = SierraCasmRunner::new(
sierra_program.program,
if args.available_gas.is_some() {
Some(Default::default())
} else {
if available_gas.is_disabled() {
None
} else {
Some(Default::default())
},
Default::default(),
)?;
Expand All @@ -94,14 +95,15 @@ fn main() -> Result<()> {
.run_function_with_starknet_context(
runner.find_function("::main")?,
&[],
args.available_gas,
available_gas.value(),
StarknetState::default(),
)
.context("failed to run the function")?;

ui.print(Summary {
result,
print_full_memory: args.print_full_memory,
gas_defined: available_gas.is_defined(),
});

Ok(())
Expand All @@ -110,6 +112,7 @@ fn main() -> Result<()> {
struct Summary {
result: RunResultStarknet,
print_full_memory: bool,
gas_defined: bool,
}

impl Message for Summary {
Expand All @@ -133,8 +136,10 @@ impl Message for Summary {
}
}

if let Some(gas) = self.result.gas_counter {
println!("Remaining gas: {gas}");
if self.gas_defined {
if let Some(gas) = self.result.gas_counter {
println!("Remaining gas: {gas}");
}
}

if self.print_full_memory {
Expand All @@ -156,3 +161,34 @@ impl Message for Summary {
todo!("JSON output is not implemented yet for this command")
}
}

enum GasLimit {
Disabled,
Unlimited,
Limited(usize),
}
impl GasLimit {
pub fn parse(value: Option<usize>) -> Self {
match value {
Some(0) => GasLimit::Disabled,
Some(value) => GasLimit::Limited(value),
None => GasLimit::Unlimited,
}
}

pub fn is_disabled(&self) -> bool {
matches!(self, GasLimit::Disabled)
}

pub fn is_defined(&self) -> bool {
!matches!(self, GasLimit::Unlimited)
}

pub fn value(&self) -> Option<usize> {
match self {
GasLimit::Disabled => None,
GasLimit::Limited(value) => Some(*value),
GasLimit::Unlimited => Some(usize::MAX),
}
}
}
52 changes: 33 additions & 19 deletions extensions/scarb-cairo-run/tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use snapbox::cmd::{cargo_bin, Command};
use scarb_test_support::cargo::manifest_dir;

#[test]
fn hello_world() {
fn scarb_build_is_called() {
let example = manifest_dir()
.parent()
.unwrap()
Expand All @@ -16,17 +16,9 @@ fn hello_world() {

let t = TempDir::new().unwrap();

Command::new(cargo_bin("scarb"))
.env("SCARB_TARGET_DIR", t.path())
.arg("build")
.current_dir(example.clone())
.assert()
.success();
Command::new(cargo_bin("scarb"))
.env("SCARB_TARGET_DIR", t.path())
.arg("cairo-run")
.arg("--available-gas")
.arg("2000000")
.current_dir(example)
.assert()
.success()
Expand All @@ -35,12 +27,37 @@ fn hello_world() {
Finished release target(s) in [..]
Running hello_world
Run completed successfully, returning [987]
Remaining gas: 1953640
"#});
}

#[test]
fn scarb_build_is_called() {
fn build_can_be_skipped() {
let example = manifest_dir()
.parent()
.unwrap()
.parent()
.unwrap()
.join("examples")
.join("hello_world");

let t = TempDir::new().unwrap();

Command::new(cargo_bin("scarb"))
.env("SCARB_TARGET_DIR", t.path())
.arg("cairo-run")
.arg("--no-build")
.current_dir(example)
.assert()
.failure()
.stderr_eq(indoc! {r#"
Error: package has not been compiled, file does not exist: hello_world.sierra.json
help: run `scarb build` to compile the package
"#});
}

#[test]
fn can_limit_gas() {
let example = manifest_dir()
.parent()
.unwrap()
Expand All @@ -55,7 +72,7 @@ fn scarb_build_is_called() {
.env("SCARB_TARGET_DIR", t.path())
.arg("cairo-run")
.arg("--available-gas")
.arg("2000000")
.arg("100000")
.current_dir(example)
.assert()
.success()
Expand All @@ -64,12 +81,12 @@ fn scarb_build_is_called() {
Finished release target(s) in [..]
Running hello_world
Run completed successfully, returning [987]
Remaining gas: 1953640
Remaining gas: 53640
"#});
}

#[test]
fn build_can_be_skipped() {
fn can_disable_gas() {
let example = manifest_dir()
.parent()
.unwrap()
Expand All @@ -84,14 +101,11 @@ fn build_can_be_skipped() {
.env("SCARB_TARGET_DIR", t.path())
.arg("cairo-run")
.arg("--available-gas")
.arg("2000000")
.arg("--no-build")
.arg("0")
.current_dir(example)
.assert()
.failure()
.stderr_eq(indoc! {r#"
Error: package has not been compiled, file does not exist: hello_world.sierra.json
help: run `scarb build` to compile the package
Error: program requires gas counter, please provide `--available-gas` argument
"#});
}

0 comments on commit eecdba1

Please sign in to comment.