Skip to content

Commit

Permalink
Cairo1-run compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kus committed Oct 14, 2024
1 parent d4e311c commit 246394e
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 67 deletions.
7 changes: 6 additions & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@ license-file = "LICENSE"

[workspace.dependencies]
cairo_test = "2.8.0"

shinigami_engine = { git = "https://github.com/keep-starknet-strange/shinigami.git", rev = "9e7692d" }

[profile.cairo1-run.cairo]
# https://github.com/lambdaclass/cairo-vm/issues/1745
enable-gas = false
# https://github.com/software-mansion/scarb/blob/4d91be7a7dbfeb24327e3ba21c62fbac43a2105a/scarb/src/compiler/compilers/lib.rs#L154
sierra-replace-ids = true
3 changes: 0 additions & 3 deletions packages/client/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
mod main;
// TODO: scarb cairo-run should support "features" argument
// so that we can conditionally compile this module
mod test;
33 changes: 0 additions & 33 deletions packages/client/src/main.cairo

This file was deleted.

40 changes: 16 additions & 24 deletions packages/client/src/test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -35,46 +35,38 @@ struct UtreexoArgs {
/// Receives arguments in a serialized format (Cairo serde).
/// Panics in case of a validation error or chain state mismatch.
/// Prints result to the stdout.
fn test(mut arguments: Span<felt252>, execute_script: bool) {
pub(crate) fn main(mut arguments: Span<felt252>, execute_script: bool) {
let mut gas_before = get_available_gas();

let Args { mut chain_state, blocks, expected_chain_state, utreexo_args } = Serde::deserialize(
ref arguments
)
.expect('Failed to deserialize');

let mut utxo_set: UtxoSet = Default::default();
let mut gas_before = get_available_gas();

for block in blocks {
let height = chain_state.block_height + 1;
match chain_state.validate_and_apply(block, ref utxo_set, execute_script) {
Result::Ok(new_chain_state) => {
chain_state = new_chain_state;
let gas_after = get_available_gas();
println!("OK: block={} gas_spent={}", height, gas_before - gas_after);
gas_before = gas_after;
},
Result::Ok(new_chain_state) => { chain_state = new_chain_state; },
Result::Err(err) => {
let gas_after = get_available_gas();
println!(
"FAIL: block={} gas_spent={} error='{}'", height, gas_before - gas_after, err
);
println!("FAIL: gas_spent={} error='{}'", gas_before - get_available_gas(), err);
panic!();
}
}
};

if chain_state != expected_chain_state {
println!(
"FAIL: block={} error='expected chain state {:?}, actual {:?}'",
chain_state.block_height,
"FAIL: gas_spent={} error='expected chain state {:?}, actual {:?}'",
gas_before - get_available_gas(),
expected_chain_state,
chain_state
);
panic!();
}

if let Result::Err(err) = utxo_set.finalize() {
println!("FAIL: error='{}'", err);
println!("FAIL: gas_spent={} error='{}'", gas_before - get_available_gas(), err);
panic!();
}

Expand All @@ -83,25 +75,25 @@ fn test(mut arguments: Span<felt252>, execute_script: bool) {
.validate_and_apply(
utxo_set.leaves_to_add.span(), utxo_set.leaves_to_delete.span(), proofs.span(),
) {
Result::Ok(new_state) => {
state = new_state;
let gas_after = get_available_gas();
println!("OK: gas_spent={}", gas_before - gas_after);
},
Result::Ok(new_state) => { state = new_state; },
Result::Err(err) => {
let gas_after = get_available_gas();
println!("FAIL: gas_spent={} error='{:?}'", gas_before - gas_after, err);
println!("FAIL: gas_spent={} error='{:?}'", gas_before - get_available_gas(), err);
panic!();
}
}

if state != expected_state {
println!(
"FAIL: error='expected utreexo state {:?}, actual {:?}'", expected_state, state
"FAIL: gas_spent={} error='expected utreexo state {:?}, actual {:?}'",
gas_before - get_available_gas(),
expected_state,
state
);
panic!();
}
}

println!("OK: gas_spent={}", gas_before - get_available_gas());
}

/// Workaround for handling missing `utreexo_args` field.
Expand Down
2 changes: 1 addition & 1 deletion scripts/data/client.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ run_client() {

echo -n "Running $mode client on blocks $first - $second "
python ../../scripts/data/format_args.py $batch_file > $arguments_file
output=$(scarb cairo-run --no-build --package client --function test --arguments-file $arguments_file)
output=$(scarb cairo-run --no-build --package client --function main --arguments-file $arguments_file)
if [[ $? -ne 0 || "$output" == *"FAIL"* || "$output" == *error* || "$output" == *panicked* ]]; then
echo "fail"
echo $output
Expand Down
16 changes: 13 additions & 3 deletions scripts/data/format_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,18 @@ def format_item(item):
return format_item(args)


def format_args(input_file, execute_script):
def format_args(input_file, execute_script, cairo1_run):
"""Reads arguments from JSON file and prints formatted result.
Expects a single CLI argument containing file path.
Output is compatible with the Scarb runner arguments format.
"""
args = json.loads(Path(input_file).read_text())
res = flatten_tuples(serialize(args))
print([res, 1 if execute_script else 0])
flag = 1 if execute_script else 0
if cairo1_run:
print(f'{format_cairo1_run(res)} {flag}')
else:
print([res, flag])


if __name__ == "__main__":
Expand All @@ -138,6 +142,12 @@ def format_args(input_file, execute_script):
help="Input file with arguments in JSON format",
)

parser.add_argument(
"--cairo1_run",
action=argparse.BooleanOptionalAction,
help="Whether to format args for cairo1-run or not",
)

args = parser.parse_args()

format_args(args.input_file, args.execute_script)
format_args(args.input_file, args.execute_script, args.cairo1_run)
4 changes: 2 additions & 2 deletions scripts/data/integration_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ for test_file in "${test_files[@]}"; do
else
arguments_file=".arguments-$(basename "$test_file")"
python ../../scripts/data/format_args.py --input_file ${test_file} $([[ $execute_scripts -eq 1 ]] && echo "--execute_script") > $arguments_file
output=$(scarb cairo-run --no-build --function test --arguments-file $arguments_file)
gas_spent=$(echo "$output" | grep -o 'gas_spent=[0-9]*' | awk -F= '{sum += $2} END {print sum}')
output=$(scarb cairo-run --no-build --function main --arguments-file $arguments_file)
gas_spent=$(echo $output | grep -o 'gas_spent=[0-9]*' | sed 's/gas_spent=//')

if [[ "$nocapture" -eq 1 ]]; then
echo -e "\n$output"
Expand Down

0 comments on commit 246394e

Please sign in to comment.