Skip to content

Commit

Permalink
Add default gas limit value to cairo-run (#1003)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciektr authored Dec 18, 2023
1 parent eefa7ad commit d798ec2
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 27 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
"#});
}
5 changes: 4 additions & 1 deletion website/docs/extensions/cairo-run.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ The `scarb cairo-run` command executes the `main` function of a local package.
It does automatically compile the cairo code within the package so using `scarb build` beforehand is not necessary.
This automatically called build can be skipped with the `--no-build` flag.
The extension accepts two optional parameters: `--available-gas` and `--print-full-memory`.
The first one is used to set the available gas for the execution, and the second one prints the full memory after the execution.
The first one is used to set the available gas for the execution.
If not provided, a gas usage is not limited.
Gas usage can be disallowed by setting the value to `0`.
The second one prints the full memory after the execution.

0 comments on commit d798ec2

Please sign in to comment.