Skip to content

Commit

Permalink
State comparison json (#275)
Browse files Browse the repository at this point in the history
* json state comparison for qbft msg processing

* prev

* qbft controller spec json state comparison

* ssv based json state comparison

* msg processing state comparison

* ssv multi msg processing test json state comparison

* update jsons after pull

* fix json tests

* require hardcoded post root to equal json loaded SC root

* naming refactor

* use log (#2)

---------

Co-authored-by: Gal Rogozinski <galrogogit@gmail.com>
  • Loading branch information
alonmuroch and GalRogozinski committed Jun 21, 2023
1 parent 2f33257 commit 9393cbb
Show file tree
Hide file tree
Showing 228 changed files with 264,494 additions and 52 deletions.
62 changes: 60 additions & 2 deletions qbft/spectest/generate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/bloxapp/ssv-spec/qbft/spectest/tests"
"log"
"os"
"path/filepath"
"reflect"
Expand All @@ -16,9 +17,13 @@ import (
//go:generate go run main.go

func main() {
clearStateComparisonFolder()

all := map[string]tests.SpecTest{}
for _, testF := range spectest.AllTests {
test := testF()

// write json test
n := reflect.TypeOf(test).String() + "_" + test.TestName()
if all[n] != nil {
panic(fmt.Sprintf("duplicate test: %s\n", n))
Expand All @@ -35,8 +40,61 @@ func main() {
panic("did not generate all tests\n")
}

fmt.Printf("found %d tests\n", len(all))
log.Printf("found %d tests\n", len(all))
writeJson(byts)

for _, testF := range spectest.AllTests {
test := testF()
// generate post state comparison
post, err := test.GetPostState()
if err != nil {
panic(err.Error())
}
writeJsonStateComparison(test.TestName(), reflect.TypeOf(test).String(), post)
}
}

func clearStateComparisonFolder() {
_, basedir, _, ok := runtime.Caller(0)
if !ok {
panic("no caller info")
}
dir := filepath.Join(strings.TrimSuffix(basedir, "main.go"), "state_comparison")

if err := os.RemoveAll(dir); err != nil {
panic(err.Error())
}

if err := os.Mkdir(dir, 0700); err != nil {
panic(err.Error())
}
}

func writeJsonStateComparison(name, testType string, post interface{}) {
if post == nil { // If nil, test not supporting post state comparison yet
log.Printf("skipping state comparison json, not supported: %s\n", name)
return
}
log.Printf("writing state comparison json: %s\n", name)

byts, err := json.MarshalIndent(post, "", " ")
if err != nil {
panic(err.Error())
}

_, basedir, _, ok := runtime.Caller(0)
if !ok {
panic("no caller info")
}
basedir = filepath.Join(strings.TrimSuffix(basedir, "main.go"), "state_comparison", testType)

// try to create directory if it doesn't exist
_ = os.Mkdir(basedir, 0700)
file := filepath.Join(basedir, fmt.Sprintf("%s.json", name))

if err := os.WriteFile(file, byts, 0644); err != nil {
panic(err.Error())
}
}

func writeJson(data []byte) {
Expand All @@ -50,7 +108,7 @@ func writeJson(data []byte) {
_ = os.Mkdir(basedir, os.ModeDir)

file := filepath.Join(basedir, "tests.json")
fmt.Printf("writing spec tests json to: %s\n", file)
log.Printf("writing spec tests json to: %s\n", file)
if err := os.WriteFile(file, data, 0644); err != nil {
panic(err.Error())
}
Expand Down
Loading

0 comments on commit 9393cbb

Please sign in to comment.