Skip to content

Commit

Permalink
Complete endRun method for Cairo runner (keep-starknet-strange#473)
Browse files Browse the repository at this point in the history
* Complete endRun method for Cairo runner

* fix endRun call

* fix conflicts

* fix conflicts

---------

Co-authored-by: lanaivina <[email protected]>
Co-authored-by: Nikita Orlov <[email protected]>
  • Loading branch information
3 people authored Mar 22, 2024
1 parent 4afa756 commit 02a8bbd
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/integration_tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -302,5 +302,5 @@ pub fn cairo_run(allocator: std.mem.Allocator, pathname: []const u8, layout: []c
// then
var hint_processor: HintProcessor = .{};
try runner.runUntilPC(end, extensive_hints, &hint_processor);
try runner.endRun();
try runner.endRun(allocator, true, false, &hint_processor);
}
2 changes: 1 addition & 1 deletion src/vm/cairo_run.zig
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub fn runConfig(allocator: Allocator, config: Config) !void {
// TODO: make flag for extensive_hints
var hint_processor: HintProcessor = .{};
try runner.runUntilPC(end, false, &hint_processor);
try runner.endRun();
try runner.endRun(allocator, true, false, &hint_processor);
// TODO readReturnValues necessary for builtins

if (config.output_trace != null or config.output_memory != null) {
Expand Down
77 changes: 72 additions & 5 deletions src/vm/runners/cairo_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -715,14 +715,28 @@ pub const CairoRunner = struct {
}
}

pub fn endRun(self: *Self) !void {
// TODO relocate memory
// TODO call end_run in vm for builtins
if (self.run_ended) {
pub fn endRun(
self: *Self,
allocator: Allocator,
disable_trace_padding: bool,
disable_finalize_all: bool,
hint_processor: *HintProcessor,
) !void {
if (self.run_ended)
return CairoRunnerError.EndRunAlreadyCalled;

try self.vm.segments.memory.relocateMemory();
try self.vm.endRun(allocator, &self.execution_scopes);

if (disable_finalize_all) return;

_ = try self.vm.computeSegmentsEffectiveSizes(false);

if (self.isProofMode() and !disable_trace_padding) {
// TODO : complete proof mode
_ = hint_processor;
}

// TODO handle proof_mode case
self.run_ended = true;
}

Expand Down Expand Up @@ -4342,3 +4356,56 @@ test "CairoRunner: checkDilutedCheckUsage with bitwise builtin" {
// Call checkDilutedCheckUsage method and expect no error.
try cairo_runner.checkDilutedCheckUsage(std.testing.allocator);
}

test "CairoRunner: endRun with a run that is already finished" {
// Create a CairoRunner instance for testing.
var cairo_runner = try CairoRunner.init(
std.testing.allocator,
try Program.initDefault(std.testing.allocator, true),
"plain",
ArrayList(MaybeRelocatable).init(std.testing.allocator),
try CairoVM.init(
std.testing.allocator,
.{},
),
false,
);
defer cairo_runner.deinit(std.testing.allocator);

cairo_runner.run_ended = true;

var hint_processor: HintProcessor = .{};

try expectError(
CairoRunnerError.EndRunAlreadyCalled,
cairo_runner.endRun(std.testing.allocator, true, false, &hint_processor),
);
}

test "CairoRunner: endRun with a simple test" {
// Create a CairoRunner instance for testing.
var cairo_runner = try CairoRunner.init(
std.testing.allocator,
try Program.initDefault(std.testing.allocator, true),
"plain",
ArrayList(MaybeRelocatable).init(std.testing.allocator),
try CairoVM.init(
std.testing.allocator,
.{},
),
false,
);
defer cairo_runner.deinit(std.testing.allocator);

var hint_processor: HintProcessor = .{};

try cairo_runner.endRun(std.testing.allocator, true, false, &hint_processor);

cairo_runner.run_ended = false;

cairo_runner.relocated_memory.clearAndFree();

try cairo_runner.endRun(std.testing.allocator, true, true, &hint_processor);

try expect(!cairo_runner.run_ended);
}

0 comments on commit 02a8bbd

Please sign in to comment.