Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cleanup] Initial documentation improvements and code cleanup #8

Merged
merged 35 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
11458c2
Update readme and godoc comments
h5law May 24, 2023
8b80a0c
Update README
h5law May 24, 2023
f315383
Update README.md
h5law May 25, 2023
0e5d4d0
Update workflow file
h5law May 25, 2023
db8d51b
Update fuzz workflow
h5law May 25, 2023
0cc5f71
Fix workflow
h5law May 25, 2023
3a72bcf
Update workflow name
h5law May 25, 2023
eb1d684
Address linter errors
h5law May 25, 2023
ad3f7c1
Add coverage file to codecov upload
h5law May 25, 2023
de15fba
Merge branch 'main' into documentation_cleanup
h5law May 25, 2023
671b17e
Update reviewpad workflows to remove unneeded jobs
h5law May 25, 2023
c3bb4f2
Remove executable permissions as for remote build on oss-fuzz
h5law May 25, 2023
5d396be
Change if statements in test workflow jobs
h5law May 25, 2023
1951c99
Revert if statement changes
h5law May 25, 2023
d663d97
Add missing env var to workflow
h5law May 25, 2023
c2882dc
Panic on errors in fuzz tests
h5law May 25, 2023
da85b29
Check for unknown errors in fuzzes only
h5law May 25, 2023
32a3085
Remove uneeded field call
h5law May 25, 2023
22c938b
Remove legacy fuzzes with go native fuzz, remove OSS-Fuzz build scrip…
h5law May 25, 2023
1d2fb80
Add comments to fuzz test and check root hash is behaving appropriate…
h5law May 26, 2023
ed8f9d7
Fix codecov upload file format
h5law May 28, 2023
353c30f
Revert to nil pointer interface implementation as this is the go idio…
h5law May 28, 2023
887969c
Update fuzz_test.go
h5law May 30, 2023
6e6d543
Update smt.go
h5law May 30, 2023
2f16a8b
Update fuzz_test.go
h5law May 30, 2023
b16f0f9
Update fuzz_test.go
h5law May 30, 2023
9538bba
Clarify comment
h5law May 30, 2023
cfada84
Add more explanations aroud node types, paths, hashers and digests
h5law May 30, 2023
7403924
Add line break between note
h5law May 30, 2023
5581c04
Clarify node storage
h5law May 30, 2023
91efe8f
Update workflow to use go 1.18
h5law May 31, 2023
0965b0a
Add PR Template file
h5law May 31, 2023
0931fa5
Add go report card and change workflow title
h5law May 31, 2023
51acc15
Update README.md
h5law Jun 1, 2023
86b4235
Add path visualisation
h5law Jun 1, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
- name: Create coverage report and run tests
run: |
set -euo pipefail
GODEBUG=netdns=cgo go test -p 1 -json ./... -covermode=count -coverprofile=coverage.out 2>&1 | tee test_results.json
GODEBUG=netdns=cgo go test -p 1 -json ./... -mod=readonly -timeout 8m -race -coverprofile=coverage.txt -covermode=atomic 2>&1 | tee test_results.json
- name: Sanitize test results
# We're utilizing `tee` above which can capture non-json stdout output so we need to remove non-json lines before additional parsing and submitting it to the external github action.
if: ${{ always() && env.TARGET_GOLANG_VERSION == matrix.go }}
Expand All @@ -62,12 +62,11 @@ jobs:
uses: guyarb/[email protected]
with:
test-results: test_results.json
- name: Prepare code coverage report
if: ${{ always() && env.TARGET_GOLANG_VERSION == matrix.go }}
run: go tool cover -func=coverage.out -o=coverage.out
- name: Upload coverage to Codecov
if: ${{ always() && env.TARGET_GOLANG_VERSION == matrix.go }}
uses: codecov/codecov-action@v3
with:
files: ./coverage.txt
- name: golangci-lint
if: ${{ always() && env.TARGET_GOLANG_VERSION == matrix.go }}
uses: golangci/golangci-lint-action@v3
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testdata/
34 changes: 25 additions & 9 deletions fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"github.com/stretchr/testify/require"
)

// FuzzSMT uses fuzzing to attempt to break the SMT implementation
// in its current state this fuzzing test does not confirm the SMT
h5law marked this conversation as resolved.
Show resolved Hide resolved
// functions correctly, it only trys to detect when it fails unexpectedly
func FuzzSMT(f *testing.F) {
h5law marked this conversation as resolved.
Show resolved Hide resolved
seeds := [][]byte{
[]byte(""),
Expand All @@ -22,15 +25,18 @@ func FuzzSMT(f *testing.F) {
f.Add(s)
}
f.Fuzz(func(t *testing.T, input []byte) {
t.Log(input)
smn := NewSimpleMap()
Olshansk marked this conversation as resolved.
Show resolved Hide resolved
tree := NewSparseMerkleTree(smn, sha256.New())

r := bytes.NewReader(input)
var keys [][]byte

// key returns a random byte to be used as a key, either generating a new
// one or using a previously generated one with a 50/50 chance of either
key := func() []byte {
h5law marked this conversation as resolved.
Show resolved Hide resolved
if readByte(r) < math.MaxUint8/2 {
k := make([]byte, readByte(r)/2)
b := readByte(r)
if b < math.MaxUint8/2 {
k := make([]byte, b/2)
if _, err := r.Read(k); err != nil {
return nil
}
Expand All @@ -42,39 +48,49 @@ func FuzzSMT(f *testing.F) {
return nil
}

return keys[int(readByte(r))%len(keys)]
return keys[int(b)%len(keys)]
}

for i := 0; r.Len() != 0; i++ {
Olshansk marked this conversation as resolved.
Show resolved Hide resolved
h5law marked this conversation as resolved.
Show resolved Hide resolved
originalRoot := tree.Root()
b, err := r.ReadByte()
if err != nil {
continue
}

op := op(int(b) % int(Noop))
// Randomly select an operation to perform
op := op(int(b) % int(NumOps))
switch op {
case Get:
_, err := tree.Get(key())
if err != nil {
Olshansk marked this conversation as resolved.
Show resolved Hide resolved
require.ErrorIsf(t, err, ErrKeyNotPresent, "unknown error occured while getting")
}
newRoot := tree.Root()
require.Equal(t, originalRoot, newRoot, "root changed while getting")
case Update:
value := make([]byte, 32)
binary.BigEndian.PutUint64(value, uint64(i))
err := tree.Update(key(), value)
if err != nil {
require.ErrorIsf(t, err, ErrKeyNotPresent, "unknown error occured while updating")
}
require.NoErrorf(t, err, "unknown error occured while updating")
newRoot := tree.Root()
require.NotEqual(t, originalRoot, newRoot, "root unchanged while updating")
case Delete:
err := tree.Delete(key())
if err != nil {
require.ErrorIsf(t, err, ErrKeyNotPresent, "unknown error occured while deleting")
continue
}
// If the key was present check root has changed
newRoot := tree.Root()
require.NotEqual(t, originalRoot, newRoot, "root unchanged while deleting")
case Prove:
_, err := tree.Prove(key())
if err != nil {
require.ErrorIsf(t, err, ErrKeyNotPresent, "unknown error occured while proving")
}
newRoot := tree.Root()
require.Equal(t, originalRoot, newRoot, "root changed while proving")
}

newRoot := tree.Root()
Expand All @@ -91,7 +107,7 @@ const (
Update
Delete
Prove
Noop
NumOps
)

func readByte(r *bytes.Reader) byte {
Expand Down
4 changes: 2 additions & 2 deletions hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ var (
)

var (
_ PathHasher = &pathHasher{}
_ ValueHasher = &valueHasher{}
_ PathHasher = (*pathHasher)(nil)
_ ValueHasher = (*valueHasher)(nil)
)

// PathHasher defines how key inputs are hashed to produce tree paths.
Expand Down
4 changes: 2 additions & 2 deletions smt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

var (
_ treeNode = &innerNode{}
_ treeNode = &leafNode{}
_ treeNode = (*innerNode)(nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this a recommendation of the linter?

I'm personally a bigger fan of the previous approach, or potentially using var _ treeNode = new(innerNode) instead

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually thought about this a lot and went into the golang discord and this is the idiomatic go way to do this.

There is potentially some issues around the other way linked here but it is unclear if this affects this specific use case.

_ treeNode = (*leafNode)(nil)
)

type treeNode interface {
Expand Down