Repository: NethermindEth/cairo-vm-go
Pull request #654 (2024-09-05 18:01:23)

feat: reorganize `runner` package
This is a proposition to reorganize the runner folder

Until now, we had this separation of Zero / Cairo1 , but actually most of the logic in zero.go  (all the checks, endRun etc) is common to both Zero and Cairo.

Feel free to leave comment and share your thoughts about it 


Patch:
diff --git a/cmd/cli/main.go b/cmd/cli/main.go
index d6d7af8..7450f9a 100644
--- a/cmd/cli/main.go
+++ b/cmd/cli/main.go
@@ -11,7 +11,7 @@ import (
 	cairoversion "github.com/NethermindEth/cairo-vm-go/pkg/parsers/cairo_version"
 	"github.com/NethermindEth/cairo-vm-go/pkg/parsers/starknet"
 	zero "github.com/NethermindEth/cairo-vm-go/pkg/parsers/zero"
-	runnerzero "github.com/NethermindEth/cairo-vm-go/pkg/runners/zero"
+	"github.com/NethermindEth/cairo-vm-go/pkg/runner"
 	"github.com/urfave/cli/v2"
 )
 
@@ -120,12 +120,12 @@ func main() {
 							return fmt.Errorf("cannot create hints: %w", err)
 						}
 					}
-					program, err := runnerzero.LoadCairoZeroProgram(zeroProgram)
+					program, err := runner.LoadCairoZeroProgram(zeroProgram)
 					if err != nil {
 						return fmt.Errorf("cannot load program: %w", err)
 					}
 					fmt.Println("Running....")
-					runner, err := runnerzero.NewRunner(program, hints, proofmode, collectTrace, maxsteps, layoutName)
+					runner, err := runner.NewRunner(program, hints, proofmode, collectTrace, maxsteps, layoutName)
 					if err != nil {
 						return fmt.Errorf("cannot create runner: %w", err)
 					}
diff --git a/pkg/runners/zero/program.go b/pkg/runner/program.go
similarity index 92%
rename from pkg/runners/zero/program.go
rename to pkg/runner/program.go
index 0221863..c84a0bc 100644
--- a/pkg/runners/zero/program.go
+++ b/pkg/runner/program.go
@@ -1,4 +1,4 @@
-package zero
+package runner
 
 import (
 	"fmt"
@@ -8,7 +8,7 @@ import (
 	"github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
 )
 
-type Program struct {
+type ZeroProgram struct {
 	// the bytecode in string format
 	Bytecode []*fp.Element
 	// given a string it returns the pc for that function call
@@ -19,7 +19,9 @@ type Program struct {
 	Builtins []sn.Builtin
 }
 
-func LoadCairoZeroProgram(cairoZeroJson *zero.ZeroProgram) (*Program, error) {
+type CairoProgram struct{}
+
+func LoadCairoZeroProgram(cairoZeroJson *zero.ZeroProgram) (*ZeroProgram, error) {
 	// bytecode
 	bytecode := make([]*fp.Element, len(cairoZeroJson.Data))
 	for i := range cairoZeroJson.Data {
@@ -43,7 +45,7 @@ func LoadCairoZeroProgram(cairoZeroJson *zero.ZeroProgram) (*Program, error) {
 		return nil, err
 	}
 
-	return &Program{
+	return &ZeroProgram{
 		Bytecode:    bytecode,
 		Entrypoints: entrypoints,
 		Labels:      labels,
diff --git a/pkg/runners/zero/zero.go b/pkg/runner/runner.go
similarity index 98%
rename from pkg/runners/zero/zero.go
rename to pkg/runner/runner.go
index b3e8c16..391c394 100644
--- a/pkg/runners/zero/zero.go
+++ b/pkg/runner/runner.go
@@ -1,4 +1,4 @@
-package zero
+package runner
 
 import (
 	"errors"
@@ -16,7 +16,7 @@ import (
 
 type ZeroRunner struct {
 	// core components
-	program    *Program
+	program    *ZeroProgram
 	vm         *vm.VirtualMachine
 	hintrunner hintrunner.HintRunner
 	// config
@@ -28,8 +28,10 @@ type ZeroRunner struct {
 	layout      builtins.Layout
 }
 
+type CairoRunner struct{}
+
 // Creates a new Runner of a Cairo Zero program
-func NewRunner(program *Program, hints map[uint64][]hinter.Hinter, proofmode bool, collectTrace bool, maxsteps uint64, layoutName string) (ZeroRunner, error) {
+func NewRunner(program *ZeroProgram, hints map[uint64][]hinter.Hinter, proofmode bool, collectTrace bool, maxsteps uint64, layoutName string) (ZeroRunner, error) {
 	hintrunner := hintrunner.NewHintRunner(hints)
 	layout, err := builtins.GetLayout(layoutName)
 	if err != nil {
diff --git a/pkg/runners/cairo/cairo.go b/pkg/runners/cairo/cairo.go
deleted file mode 100644
index fedf8a0..0000000
--- a/pkg/runners/cairo/cairo.go
+++ /dev/null
@@ -1,1 +0,0 @@
-package cairo
diff --git a/pkg/runners/cairo/program.go b/pkg/runners/cairo/program.go
deleted file mode 100644
index fedf8a0..0000000
--- a/pkg/runners/cairo/program.go
+++ /dev/null
@@ -1,1 +0,0 @@
-package cairo


Test patch:
diff --git a/pkg/runners/zero/program_test.go b/pkg/runner/program_test.go
similarity index 96%
rename from pkg/runners/zero/program_test.go
rename to pkg/runner/program_test.go
index 63f274e..599f8b1 100644
--- a/pkg/runners/zero/program_test.go
+++ b/pkg/runner/program_test.go
@@ -1,4 +1,4 @@
-package zero
+package runner
 
 import (
 	"testing"
@@ -50,7 +50,7 @@ func TestLoadCairoZeroProgram(t *testing.T) {
 	program, err := LoadCairoZeroProgram(cairoZeroJson)
 	require.NoError(t, err)
 
-	require.Equal(t, &Program{
+	require.Equal(t, &ZeroProgram{
 		Bytecode: []*fp.Element{
 			stringToFelt("0x01"),
 			stringToFelt("0x02"),
diff --git a/pkg/runners/zero/zero_benchmark_test.go b/pkg/runner/runner_benchmark_test.go
similarity index 99%
rename from pkg/runners/zero/zero_benchmark_test.go
rename to pkg/runner/runner_benchmark_test.go
index 647e47a..71668ff 100644
--- a/pkg/runners/zero/zero_benchmark_test.go
+++ b/pkg/runner/runner_benchmark_test.go
@@ -1,4 +1,4 @@
-package zero
+package runner
 
 import (
 	"math"
diff --git a/pkg/runners/zero/zero_test.go b/pkg/runner/runner_test.go
similarity index 99%
rename from pkg/runners/zero/zero_test.go
rename to pkg/runner/runner_test.go
index 25e62fd..45eb5a4 100644
--- a/pkg/runners/zero/zero_test.go
+++ b/pkg/runner/runner_test.go
@@ -1,4 +1,4 @@
-package zero
+package runner
 
 import (
 	"fmt"
@@ -414,13 +414,13 @@ func trimmedSegment(segment *memory.Segment) *memory.Segment {
 	return segment
 }
 
-func createProgram(code string) *Program {
+func createProgram(code string) *ZeroProgram {
 	bytecode, err := assembler.CasmToBytecode(code)
 	if err != nil {
 		panic(err)
 	}
 
-	program := Program{
+	program := ZeroProgram{
 		Bytecode: bytecode,
 		Entrypoints: map[string]uint64{
 			"main": 0,
@@ -430,7 +430,7 @@ func createProgram(code string) *Program {
 	return &program
 }
 
-func createProgramWithBuiltins(code string, builtins ...sn.Builtin) *Program {
+func createProgramWithBuiltins(code string, builtins ...sn.Builtin) *ZeroProgram {
 	program := createProgram(code)
 	program.Builtins = builtins
 	return program
diff --git a/pkg/runners/cairo/cairo_test.go b/pkg/runners/cairo/cairo_test.go
deleted file mode 100644
index fedf8a0..0000000
--- a/pkg/runners/cairo/cairo_test.go
+++ /dev/null
@@ -1,1 +0,0 @@
-package cairo
diff --git a/pkg/runners/cairo/program_test.go b/pkg/runners/cairo/program_test.go
deleted file mode 100644
index fedf8a0..0000000
--- a/pkg/runners/cairo/program_test.go
+++ /dev/null
@@ -1,1 +0,0 @@
-package cairo