Skip to content

Commit

Permalink
fix: handle stack traces with deferred function (Consensys#521)
Browse files Browse the repository at this point in the history
* feat: add callDeferred method to test engine

* fix: return error when deferred function fails

* fix: always indent witness doctest output

witness.ToJSON indents output when run in debug mode and not if in normal mode.
But the doctest always expected non-indented output.

* fix: further specify skipped stack frames in normal mode

The function `callDeferred` is in `gnark/frontend`. We do not want to skip it
as it is a break condition.
  • Loading branch information
ivokub authored Mar 6, 2023
1 parent db76d9e commit 41cd258
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 15 deletions.
14 changes: 11 additions & 3 deletions backend/witness/witness_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package witness_test

import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"testing"
Expand Down Expand Up @@ -47,11 +49,17 @@ func ExampleWitness() {

// first get the circuit expected schema
schema, _ := frontend.NewSchema(assignment)
json, _ := reconstructed.ToJSON(schema)
ret, _ := reconstructed.ToJSON(schema)

fmt.Println(string(json))
var b bytes.Buffer
json.Indent(&b, ret, "", "\t")
fmt.Println(b.String())
// Output:
// {"X":42,"Y":8000,"E":1}
// {
// "X": 42,
// "Y": 8000,
// "E": 1
// }

}

Expand Down
2 changes: 1 addition & 1 deletion debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func writeStack(sbb *strings.Builder, forceClean ...bool) {
if strings.Contains(frame.File, "test/engine.go") {
continue
}
if strings.Contains(frame.File, "gnark/frontend") {
if strings.Contains(frame.File, "gnark/frontend/cs") {
continue
}
file = filepath.Base(file)
Expand Down
2 changes: 1 addition & 1 deletion debug/symbol_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (st *SymbolTable) CollectStack() []int {
if strings.Contains(frame.File, "test/engine.go") {
continue
}
if strings.Contains(frame.File, "gnark/frontend") {
if strings.Contains(frame.File, "gnark/frontend/cs") {
continue
}
frame.File = filepath.Base(frame.File)
Expand Down
2 changes: 1 addition & 1 deletion frontend/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func parseCircuit(builder Builder, circuit Circuit) (err error) {
return fmt.Errorf("define circuit: %w", err)
}
if err = callDeferred(builder); err != nil {
return fmt.Errorf("")
return fmt.Errorf("deferred: %w", err)
}

return
Expand Down
15 changes: 11 additions & 4 deletions test/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,8 @@ func IsSolved(circuit, witness frontend.Circuit, field *big.Int, opts ...TestEng
if err = c.Define(e); err != nil {
return fmt.Errorf("define: %w", err)
}
for i, cb := range circuitdefer.GetAll[func(frontend.API) error](e) {
if err = cb(e); err != nil {
return fmt.Errorf("defer %d: %w", i, err)
}
if err = callDeferred(e); err != nil {
return fmt.Errorf("deferred: %w", err)
}

log.Debug().Uint64("add", cptAdd).
Expand All @@ -141,6 +139,15 @@ func IsSolved(circuit, witness frontend.Circuit, field *big.Int, opts ...TestEng
return
}

func callDeferred(builder *engine) error {
for i, cb := range circuitdefer.GetAll[func(frontend.API) error](builder) {
if err := cb(builder); err != nil {
return fmt.Errorf("defer fn %d: %w", i, err)
}
}
return nil
}

var cptAdd, cptMul, cptSub, cptToBinary, cptFromBinary, cptAssertIsEqual uint64

func (e *engine) Add(i1, i2 frontend.Variable, in ...frontend.Variable) frontend.Variable {
Expand Down
7 changes: 2 additions & 5 deletions test/solver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/consensys/gnark/frontend/cs/scs"
"github.com/consensys/gnark/frontend/schema"
"github.com/consensys/gnark/internal/backend/circuits"
"github.com/consensys/gnark/internal/circuitdefer"
"github.com/consensys/gnark/internal/kvstore"
"github.com/consensys/gnark/internal/tinyfield"
"github.com/consensys/gnark/internal/utils"
Expand Down Expand Up @@ -209,10 +208,8 @@ func isSolvedEngine(c frontend.Circuit, field *big.Int, opts ...TestEngineOption
if err = c.Define(e); err != nil {
return fmt.Errorf("define: %w", err)
}
for i, cb := range circuitdefer.GetAll[func(frontend.API) error](e) {
if err = cb(e); err != nil {
return fmt.Errorf("defer %d: %w", i, err)
}
if err = callDeferred(e); err != nil {
return fmt.Errorf("")
}

return
Expand Down

0 comments on commit 41cd258

Please sign in to comment.