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

Improve test coverage #350

Merged
merged 23 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
15177a2
test: add test for precomp generation
jsign May 1, 2023
3f4ff44
test: add more coverage for empty
jsign May 1, 2023
b6d636a
encoding: add more test coverage
jsign May 1, 2023
8f3a304
coalesce StemFromBytes with equivalent method
jsign May 1, 2023
b67ec5f
remove unused method
jsign May 1, 2023
e0cbcce
test: cover internal node walking in ToJSON()
jsign May 1, 2023
80bd36f
ci: use the race detector in tests
jsign May 1, 2023
c128b1d
tree: avoid logic duplication making Get(...) call GetStem(...) & fix…
jsign May 1, 2023
4f3a405
tests: add tests for leaf node children manipulation
jsign May 1, 2023
a7ccf53
test: add test case coverage for value deletion at index greater than…
jsign May 1, 2023
4a0191b
test: add test that insert multiple values touching both c1 and c2
jsign May 1, 2023
2850e31
tests: add new test to cover unconvered leaf node apis
jsign May 1, 2023
f70c9c0
test: add extra cases for hashed node
jsign May 1, 2023
5817a34
test: add tests for unknown node
jsign May 1, 2023
da8af24
ipa: add test to improve coverage and remove unused method
jsign May 1, 2023
e5ea6ad
lint
jsign May 1, 2023
74b0b33
remove extra files
jsign May 2, 2023
5594557
improve comments
jsign May 2, 2023
2fe89f6
recover MergeTrees method
jsign May 2, 2023
61c36ba
change api of insertMultiple(...)
jsign May 2, 2023
67a8b49
rebuild StemFromBytes with extra check calling FromLEBytes
jsign May 2, 2023
8b70e8f
tree: in GetStem(...) wrap errors
jsign May 3, 2023
1b366f3
rebase fix
jsign May 10, 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
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ jobs:
with:
go-version: 1.18
- name: Test
run: go test -v ./...
run: go test -v -race ./...
jsign marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion config_ipa.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (ipac *IPAConfig) CommitToPoly(poly []Fr, _ int) *Point {

var cfg *Config

const precompFileName = "precomp"
var precompFileName = "precomp"
jsign marked this conversation as resolved.
Show resolved Hide resolved

func GetConfig() *Config {
if cfg == nil {
Expand Down
31 changes: 31 additions & 0 deletions config_ipa_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package verkle

import (
"os"
"testing"
)

func TestGeneratePrecompFile(t *testing.T) {
jsign marked this conversation as resolved.
Show resolved Hide resolved
if testing.Short() {
t.Skip("skipping test in short mode")
}
cfg = nil // Side-effects of the package having globals, yuck.

// Restore the default after this test.
defer func(originalPath string) {
precompFileName = originalPath
cfg = nil
}(precompFileName)

precompFileName = t.TempDir() + "/precomp"
if conf := GetConfig(); conf == nil {
t.Fatal("GetConfig() returned nil")
}
stat, err := os.Stat(precompFileName)
if err != nil {
t.Fatalf("file stat failed: %s", err)
}
if stat.Size() == 0 {
t.Fatal("precomp file is empty")
}
}
1 change: 1 addition & 0 deletions debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestJSON(t *testing.T) {
root := New()
root.Insert(zeroKeyTest, fourtyKeyTest, nil)
root.Insert(oneKeyTest, zeroKeyTest, nil)
root.Insert(forkOneKeyTest, zeroKeyTest, nil) // Force an internal node in the first layer.
jsign marked this conversation as resolved.
Show resolved Hide resolved
root.Insert(fourtyKeyTest, oneKeyTest, nil)
root.(*InternalNode).children[152] = &HashedNode{commitment: []byte{1, 2, 3, 4}}
root.Commit()
Expand Down
14 changes: 7 additions & 7 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ package verkle
import "errors"

var (
errInsertIntoHash = errors.New("trying to insert into hashed node")
errDeleteHash = errors.New("trying to delete from a hashed subtree")
errReadFromInvalid = errors.New("trying to read from an invalid child")
errSerializeHashedNode = errors.New("trying to serialize a hashed internal node")
errInsertIntoOtherStem = errors.New("insert splits a stem where it should not happen")
errStatelessAndStatefulMix = errors.New("a stateless node should not be found in a stateful tree")
errMissingNodeInStateless = errors.New("trying to access a node that is missing from the stateless view")
errInsertIntoHash = errors.New("trying to insert into hashed node")
errDeleteHash = errors.New("trying to delete from a hashed subtree")
errReadFromInvalid = errors.New("trying to read from an invalid child")
errSerializeHashedNode = errors.New("trying to serialize a hashed internal node")
errInsertIntoOtherStem = errors.New("insert splits a stem where it should not happen")
errUnknownNodeType = errors.New("unknown node type detected")
errMissingNodeInStateless = errors.New("trying to access a node that is missing from the stateless view")
)

const (
Expand Down
16 changes: 16 additions & 0 deletions empty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,20 @@ func TestEmptyFuncs(t *testing.T) {
if v != nil {
t.Fatal("non-nil get from empty")
}

if !e.Commitment().Equal(e.Commit()) {
t.Fatal("commitment and commit mismatch")
}

if _, _, _, err := e.GetProofItems(nil); err == nil {
t.Fatal("get proof items should error")
}

if _, err := e.Serialize(); err == nil {
t.Fatal("serialize should error")
}

if !e.Hash().Equal(&FrZero) {
t.Fatal("hash should be the zero element")
}
}
20 changes: 20 additions & 0 deletions encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,23 @@ func TestLeafStemLength(t *testing.T) {
t.Fatalf("invalid serialization when the stem is longer than 31 bytes: %x (%d bytes)", ser, len(ser))
}
}

func TestInvalidNodeEncoding(t *testing.T) {
jsign marked this conversation as resolved.
Show resolved Hide resolved
// Test a short payload.
if _, err := ParseNode([]byte{leafRLPType}, 0, SerializedPointCompressed{}); err != errSerializedPayloadTooShort {
t.Fatalf("invalid error, got %v, expected %v", err, errSerializedPayloadTooShort)
}

// Test an invalid node type.
values := make([][]byte, NodeWidth)
values[42] = testValue
ln := NewLeafNode(ffx32KeyTest, values)
lnbytes, err := ln.Serialize()
if err != nil {
t.Fatalf("serializing leaf node: %v", err)
}
lnbytes[0] = leafRLPType + internalRLPType // Change the type of the node to something invalid.
if _, err := ParseNode(lnbytes, 0, SerializedPointCompressed{}); err != ErrInvalidNodeEncoding {
t.Fatalf("invalid error, got %v, expected %v", err, ErrInvalidNodeEncoding)
}
}
14 changes: 12 additions & 2 deletions hashednode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ package verkle
import "testing"

func TestHashedNodeFuncs(t *testing.T) {
e := HashedNode{}
fakeCommitment := EmptyCodeHashPoint.Bytes()
e := HashedNode{commitment: fakeCommitment[:]}
err := e.Insert(zeroKeyTest, zeroKeyTest, nil)
if err == nil {
if err != errInsertIntoHash {
t.Fatal("got nil error when inserting into a hashed node")
}
err = e.Delete(zeroKeyTest, nil)
Expand All @@ -44,4 +45,13 @@ func TestHashedNodeFuncs(t *testing.T) {
if v != nil {
t.Fatal("non-nil get from a hashed node")
}
if _, _, _, err := e.GetProofItems(nil); err == nil {
t.Fatal("got nil error when getting proof items from a hashed node")
}
if _, err := e.Serialize(); err != errSerializeHashedNode {
t.Fatal("got nil error when serializing a hashed node")
}
if hash := e.Hash(); hash == nil {
t.Fatal("got nil hash when hashing a hashed node")
}
}
22 changes: 11 additions & 11 deletions ipa.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,27 @@ func toFrMultiple(res []*Fr, ps []*Point) {
}

func FromLEBytes(fr *Fr, data []byte) {
if len(data) > 32 {
panic("data is too long")
}
var aligned [32]byte
copy(aligned[:len(data)], data)
copy(aligned[:], data)
fr.SetBytesLE(aligned[:])
}
jsign marked this conversation as resolved.
Show resolved Hide resolved

func StemFromBytes(fr *Fr, data []byte) {
if len(data) != StemSize {
panic("data length must be StemSize")
}
FromLEBytes(fr, data)
}

func FromBytes(fr *Fr, data []byte) {
var aligned [32]byte
copy(aligned[32-len(data):], data)
fr.SetBytes(aligned[:])
}

func StemFromBytes(fr *Fr, data []byte) {
bytes := make([]byte, len(data))
copy(bytes, data)
fr.SetBytesLE(bytes)
}
jsign marked this conversation as resolved.
Show resolved Hide resolved

func Equal(self *Point, other *Point) bool {
return other.Equal(self)
}

func Generator() *Point {
return new(Point).Identity()
}
jsign marked this conversation as resolved.
Show resolved Hide resolved
22 changes: 22 additions & 0 deletions ipa_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package verkle

import (
"encoding/binary"
"math/big"
"testing"
)

func TestFromBytes(t *testing.T) {
var fr Fr

var beFortyTwo [8]byte
binary.BigEndian.PutUint64(beFortyTwo[:], 42)

FromBytes(&fr, beFortyTwo[:])
jsign marked this conversation as resolved.
Show resolved Hide resolved

bi := big.NewInt(0)
if fr.ToBigIntRegular(bi).Int64() != 42 {
t.Fatalf("got %v, want 42", bi)
}

}
Loading
Loading