Skip to content

Commit

Permalink
Add checks for the matching args size, rename files, modify the integ…
Browse files Browse the repository at this point in the history
…ration tests pipeline
  • Loading branch information
MaksymMalicki committed Jan 11, 2025
1 parent 31aeeed commit 086011a
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 27 deletions.
10 changes: 5 additions & 5 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,18 +217,18 @@ func main() {
if err != nil {
return fmt.Errorf("cannot load program: %w", err)
}
program, hints, err := runner.AssembleProgram(cairoProgram)
userArgs, err := starknet.ParseCairoProgramArgs(args)
if err != nil {
return fmt.Errorf("cannot parse args: %w", err)
}
program, hints, err := runner.AssembleProgram(cairoProgram, userArgs)
if err != nil {
return fmt.Errorf("cannot assemble program: %w", err)
}
runnerMode := runner.ExecutionModeCairo
if proofmode {
runnerMode = runner.ProofModeCairo
}
userArgs, err := starknet.ParseCairoProgramArgs(args)
if err != nil {
return fmt.Errorf("cannot parse args: %w", err)
}
if availableGas > 0 {
// The first argument is the available gas
availableGasArg := starknet.CairoFuncArgs{
Expand Down
48 changes: 30 additions & 18 deletions integration_tests/cairo_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (f *Filter) filtered(testFile string) bool {
return false
}

func runAndTestFile(t *testing.T, path string, name string, benchmarkMap map[string][3]int, benchmark bool, errorExpected bool, zero bool) {
func runAndTestFile(t *testing.T, path string, name string, benchmarkMap map[string][3]int, benchmark bool, errorExpected bool, zero bool, inputArgs string) {
t.Logf("testing: %s\n", path)
compiledOutput, err := compileCairoCode(path, zero)
if err != nil {
Expand All @@ -75,7 +75,7 @@ func runAndTestFile(t *testing.T, path string, name string, benchmarkMap map[str
}
layout := getLayoutFromFileName(path)

elapsedGo, traceFile, memoryFile, _, err := runVm(compiledOutput, layout, zero)
elapsedGo, traceFile, memoryFile, _, err := runVm(compiledOutput, layout, zero, inputArgs)
if errorExpected {
assert.Error(t, err, path)
writeToFile(path)
Expand All @@ -92,7 +92,7 @@ func runAndTestFile(t *testing.T, path string, name string, benchmarkMap map[str
if zero {
rustVmFilePath = compiledOutput
}
elapsedRs, rsTraceFile, rsMemoryFile, err := runRustVm(rustVmFilePath, layout, zero)
elapsedRs, rsTraceFile, rsMemoryFile, err := runRustVm(rustVmFilePath, layout, zero, inputArgs)
if errorExpected {
// we let the code go on so that we can check if the go vm also raises an error
assert.Error(t, err, path)
Expand Down Expand Up @@ -171,16 +171,27 @@ func TestCairoFiles(t *testing.T) {
if err != nil {
panic(fmt.Errorf("failed to open file: %w", err))
}

file.Close()
roots := []struct {
type TestCase struct {
path string
zero bool
}{
{"./cairo_zero_hint_tests/", true},
{"./cairo_zero_file_tests/", true},
{"./builtin_tests/", true},
}
roots := []TestCase{
// {"./cairo_zero_hint_tests/", true},
// {"./cairo_zero_file_tests/", true},
// {"./builtin_tests/", true},
// {"./cairo_1_programs/", false},
// {"./cairo_1_programs/dict_non_squashed", false},
{"./cairo_1_programs/with_input", false},
}

inputArgsMap := map[string]string{
"cairo_1_programs/with_input/array_input_sum__small.cairo": "2 [111 222 333] 1 [444 555 666 777]",
"cairo_1_programs/with_input/array_length__small.cairo": "[1 2 3 4 5 6] [7 8 9 10]",
"cairo_1_programs/with_input/branching.cairo": "123",
"cairo_1_programs/with_input/dict_with_input__small.cairo": "[1 2 3 4]",
"cairo_1_programs/with_input/tensor__small.cairo": "[1 4] [1 5]",
}

// filter is for debugging purposes
Expand Down Expand Up @@ -211,22 +222,19 @@ func TestCairoFiles(t *testing.T) {
if !filter.filtered(name) {
continue
}

inputArgs := inputArgsMap[path]
// we run tests concurrently if we don't need benchmarks
if !*zerobench {
sem <- struct{}{} // acquire a semaphore slot
wg.Add(1)

go func(path, name string, root struct {
path string
zero bool
}) {
go func(path, name string, root TestCase, inputArgs string) {
defer wg.Done()
defer func() { <-sem }() // release the semaphore slot when done
runAndTestFile(t, path, name, benchmarkMap, *zerobench, errorExpected, root.zero)
}(path, name, root)
runAndTestFile(t, path, name, benchmarkMap, *zerobench, errorExpected, root.zero, inputArgs)
}(path, name, root, inputArgs)
} else {
runAndTestFile(t, path, name, benchmarkMap, *zerobench, errorExpected, root.zero)
runAndTestFile(t, path, name, benchmarkMap, *zerobench, errorExpected, root.zero, inputArgs)
}
}
}
Expand Down Expand Up @@ -400,7 +408,7 @@ func runPythonVm(path, layout string) (time.Duration, string, string, error) {

// given a path to a compiled cairo zero file, execute it using the
// rust vm and return the trace and memory files location
func runRustVm(path, layout string, zero bool) (time.Duration, string, string, error) {
func runRustVm(path, layout string, zero bool, inputArgs string) (time.Duration, string, string, error) {
traceOutput := swapExtenstion(path, rsTraceSuffix)
memoryOutput := swapExtenstion(path, rsMemorySuffix)

Expand All @@ -412,6 +420,8 @@ func runRustVm(path, layout string, zero bool) (time.Duration, string, string, e
memoryOutput,
"--layout",
layout,
"--args",
inputArgs,
}

if zero {
Expand Down Expand Up @@ -441,7 +451,7 @@ func runRustVm(path, layout string, zero bool) (time.Duration, string, string, e

// given a path to a compiled cairo zero file, execute
// it using our vm
func runVm(path, layout string, zero bool) (time.Duration, string, string, string, error) {
func runVm(path, layout string, zero bool, inputArgs string) (time.Duration, string, string, string, error) {
traceOutput := swapExtenstion(path, traceSuffix)
memoryOutput := swapExtenstion(path, memorySuffix)

Expand Down Expand Up @@ -471,6 +481,8 @@ func runVm(path, layout string, zero bool) (time.Duration, string, string, strin
layout,
"--available_gas",
"9999999",
"--args",
inputArgs,
}
}
args = append(args, path)
Expand Down
21 changes: 17 additions & 4 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,25 @@ func NewRunner(program *Program, hints map[uint64][]hinter.Hinter, runnerMode Ru
}, nil
}

func AssembleProgram(cairoProgram *starknet.StarknetProgram) (Program, map[uint64][]hinter.Hinter, error) {
func AssembleProgram(cairoProgram *starknet.StarknetProgram, userArgs []starknet.CairoFuncArgs) (Program, map[uint64][]hinter.Hinter, error) {
mainFunc, ok := cairoProgram.EntryPointsByFunction["main"]
if !ok {
return Program{}, nil, fmt.Errorf("cannot find main function")
}
expectedArgsSize, actualArgsSize := 0, 0
for _, arg := range mainFunc.InputArgs {
expectedArgsSize += arg.Size
}
for _, arg := range userArgs {
if arg.Single != nil {
actualArgsSize += 1
} else {
actualArgsSize += 2
}
}
if expectedArgsSize != actualArgsSize {
return Program{}, nil, fmt.Errorf("missing arguments for main function")
}
program, err := LoadCairoProgram(cairoProgram)
if err != nil {
return Program{}, nil, fmt.Errorf("cannot load program: %w", err)
Expand Down Expand Up @@ -603,16 +617,15 @@ func GetEntryCodeInstructions(function starknet.EntryPointByFunction, finalizeFo
usedArgs += 1
}
}
offset := apOffset - usedArgs
for _, param := range paramTypes {
offset := apOffset - usedArgs
for i := 0; i < param.Size; i++ {
ctx.AddInlineCASM(
fmt.Sprintf("[ap + 0] = [ap - %d], ap++;", offset),
)
apOffset += param.Size
usedArgs += param.Size
}
}

_, endInstructionsSize, err := assembler.CasmToBytecode("call rel 0; ret;")
if err != nil {
return nil, nil, err
Expand Down

0 comments on commit 086011a

Please sign in to comment.