Skip to content

Commit

Permalink
test marshaling and gen random values
Browse files Browse the repository at this point in the history
  • Loading branch information
Loong committed May 15, 2020
1 parent ae3f197 commit 623f2eb
Show file tree
Hide file tree
Showing 10 changed files with 769 additions and 103 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ require (
github.com/ethereum/go-ethereum v1.9.5
github.com/onsi/ginkgo v1.11.0
github.com/onsi/gomega v1.8.1
github.com/renproject/abi v0.4.1 // indirect
github.com/renproject/id v0.3.3
github.com/renproject/surge v1.1.3
github.com/renproject/surge v1.1.5
github.com/sirupsen/logrus v1.4.2
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ github.com/renproject/surge v1.1.2 h1:Yy3pTlRyaMJGLfn64JHgCnWs3cWbRJjE+aFxZXRGfW
github.com/renproject/surge v1.1.2/go.mod h1:UnnFYpLSD0T9MzCcyHjbNdmxiQsDVyBDCuqcbhcaLCY=
github.com/renproject/surge v1.1.3 h1:nCN3yWUbIbSDWyMaU6aCIidCE15yEcZb8Bcuziog/wU=
github.com/renproject/surge v1.1.3/go.mod h1:UnnFYpLSD0T9MzCcyHjbNdmxiQsDVyBDCuqcbhcaLCY=
github.com/renproject/surge v1.1.5 h1:uoup398vYr7NYYWFwes4Hmsa6AMFujfUEhYZKZ0nuCs=
github.com/renproject/surge v1.1.5/go.mod h1:UnnFYpLSD0T9MzCcyHjbNdmxiQsDVyBDCuqcbhcaLCY=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
Expand Down
206 changes: 205 additions & 1 deletion process/message_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,205 @@
package process_test
package process_test

import (
"testing/quick"

"github.com/renproject/hyperdrive/process"
"github.com/renproject/id"
"github.com/renproject/surge"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Propose", func() {
Context("when unmarshaling fuzz", func() {
It("should not panic", func() {
f := func(fuzz []byte) bool {
msg := process.Propose{}
Expect(surge.FromBinary(fuzz, &msg)).ToNot(Succeed())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})

Context("when marshaling and then unmarshaling", func() {
It("should equal itself", func() {
f := func(height process.Height, round, validRound process.Round, value process.Value, from id.Signatory, signature id.Signature) bool {
expected := process.Propose{
Height: height,
Round: round,
ValidRound: validRound,
Value: value,
From: from,
Signature: signature,
}
data, err := surge.ToBinary(expected)
Expect(err).ToNot(HaveOccurred())
got := process.Propose{}
err = surge.FromBinary(data, &got)
Expect(err).ToNot(HaveOccurred())
Expect(got.Equal(&expected)).To(BeTrue())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})

Context("when compute the hash", func() {
It("should not be random", func() {
f := func(height process.Height, round, validRound process.Round, value process.Value) bool {
expected, err := process.NewProposeHash(height, round, validRound, value)
Expect(err).ToNot(HaveOccurred())
got, err := process.NewProposeHash(height, round, validRound, value)
Expect(err).ToNot(HaveOccurred())
Expect(got.Equal(&expected)).To(BeTrue())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})

It("should the expected signatory", func() {
f := func(height process.Height, round, validRound process.Round, value process.Value) bool {
privKey := id.NewPrivKey()
hash, err := process.NewProposeHash(height, round, validRound, value)
Expect(err).ToNot(HaveOccurred())
signature, err := privKey.Sign(&hash)
Expect(err).ToNot(HaveOccurred())
signatory, err := signature.Signatory(&hash)
Expect(err).ToNot(HaveOccurred())
Expect(privKey.Signatory().Equal(&signatory)).To(BeTrue())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
})

var _ = Describe("Prevote", func() {
Context("when unmarshaling fuzz", func() {
It("should not panic", func() {
f := func(fuzz []byte) bool {
msg := process.Prevote{}
Expect(surge.FromBinary(fuzz, &msg)).ToNot(Succeed())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})

Context("when marshaling and then unmarshaling", func() {
It("should equal itself", func() {
f := func(height process.Height, round process.Round, value process.Value, from id.Signatory, signature id.Signature) bool {
expected := process.Prevote{
Height: height,
Round: round,
Value: value,
From: from,
Signature: signature,
}
data, err := surge.ToBinary(expected)
Expect(err).ToNot(HaveOccurred())
got := process.Prevote{}
err = surge.FromBinary(data, &got)
Expect(err).ToNot(HaveOccurred())
Expect(got.Equal(&expected)).To(BeTrue())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})

Context("when compute the hash", func() {
It("should not be random", func() {
f := func(height process.Height, round process.Round, value process.Value) bool {
expected, err := process.NewPrevoteHash(height, round, value)
Expect(err).ToNot(HaveOccurred())
got, err := process.NewPrevoteHash(height, round, value)
Expect(err).ToNot(HaveOccurred())
Expect(got.Equal(&expected)).To(BeTrue())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})

It("should the expected signatory", func() {
f := func(height process.Height, round process.Round, value process.Value) bool {
privKey := id.NewPrivKey()
hash, err := process.NewPrevoteHash(height, round, value)
Expect(err).ToNot(HaveOccurred())
signature, err := privKey.Sign(&hash)
Expect(err).ToNot(HaveOccurred())
signatory, err := signature.Signatory(&hash)
Expect(err).ToNot(HaveOccurred())
Expect(privKey.Signatory().Equal(&signatory)).To(BeTrue())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
})

var _ = Describe("Precommit", func() {
Context("when unmarshaling fuzz", func() {
It("should not panic", func() {
f := func(fuzz []byte) bool {
msg := process.Precommit{}
Expect(surge.FromBinary(fuzz, &msg)).ToNot(Succeed())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})

Context("when marshaling and then unmarshaling", func() {
It("should equal itself", func() {
f := func(height process.Height, round process.Round, value process.Value, from id.Signatory, signature id.Signature) bool {
expected := process.Precommit{
Height: height,
Round: round,
Value: value,
From: from,
Signature: signature,
}
data, err := surge.ToBinary(expected)
Expect(err).ToNot(HaveOccurred())
got := process.Precommit{}
err = surge.FromBinary(data, &got)
Expect(err).ToNot(HaveOccurred())
Expect(got.Equal(&expected)).To(BeTrue())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})

Context("when compute the hash", func() {
It("should not be random", func() {
f := func(height process.Height, round process.Round, value process.Value) bool {
expected, err := process.NewPrecommitHash(height, round, value)
Expect(err).ToNot(HaveOccurred())
got, err := process.NewPrecommitHash(height, round, value)
Expect(err).ToNot(HaveOccurred())
Expect(got.Equal(&expected)).To(BeTrue())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})

It("should the expected signatory", func() {
f := func(height process.Height, round process.Round, value process.Value) bool {
privKey := id.NewPrivKey()
hash, err := process.NewPrecommitHash(height, round, value)
Expect(err).ToNot(HaveOccurred())
signature, err := privKey.Sign(&hash)
Expect(err).ToNot(HaveOccurred())
signatory, err := signature.Signatory(&hash)
Expect(err).ToNot(HaveOccurred())
Expect(privKey.Signatory().Equal(&signatory)).To(BeTrue())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
})
52 changes: 2 additions & 50 deletions process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,6 @@ type Process struct {

// State of the Process.
State `json:"state"`
// ProposeLogs store the Proposes for all Rounds.
ProposeLogs map[Round]Propose `json:"proposeLogs"`
// PrevoteLogs store the Prevotes for all Processes in all Rounds.
PrevoteLogs map[Round]map[id.Signatory]Prevote `json:"prevoteLogs"`
// PrecommitLogs store the Precommits for all Processes in all Rounds.
PrecommitLogs map[Round]map[id.Signatory]Precommit `json:"precommitLogs"`
// OnceFlags prevents events from happening more than once.
OnceFlags map[Round]OnceFlag `json:"onceFlags"`
}

// New returns a new Process that is in the default State with empty message
Expand Down Expand Up @@ -151,22 +143,14 @@ func New(
committer: committer,
catcher: catcher,

State: DefaultState(),
ProposeLogs: make(map[Round]Propose),
PrevoteLogs: make(map[Round]map[id.Signatory]Prevote),
PrecommitLogs: make(map[Round]map[id.Signatory]Precommit),
OnceFlags: make(map[Round]OnceFlag),
State: DefaultState(),
}
}

// SizeHint returns the number of bytes required to represent this Process in
// binary.
func (p Process) SizeHint() int {
return surge.SizeHint(p.State) +
surge.SizeHint(p.ProposeLogs) +
surge.SizeHint(p.PrevoteLogs) +
surge.SizeHint(p.PrecommitLogs) +
surge.SizeHint(p.OnceFlags)
return surge.SizeHint(p.State)
}

// Marshal this Process into binary.
Expand All @@ -175,22 +159,6 @@ func (p Process) Marshal(w io.Writer, m int) (int, error) {
if err != nil {
return m, fmt.Errorf("marshaling state: %v", err)
}
m, err = surge.Marshal(w, p.ProposeLogs, m)
if err != nil {
return m, fmt.Errorf("marshaling propose logs: %v", err)
}
m, err = surge.Marshal(w, p.PrevoteLogs, m)
if err != nil {
return m, fmt.Errorf("marshaling prevote logs: %v", err)
}
m, err = surge.Marshal(w, p.PrecommitLogs, m)
if err != nil {
return m, fmt.Errorf("marshaling precommit logs: %v", err)
}
m, err = surge.Marshal(w, p.OnceFlags, m)
if err != nil {
return m, fmt.Errorf("marshaling once flags: %v", err)
}
return m, nil
}

Expand All @@ -200,22 +168,6 @@ func (p *Process) Unmarshal(r io.Reader, m int) (int, error) {
if err != nil {
return m, fmt.Errorf("unmarshaling state: %v", err)
}
m, err = surge.Unmarshal(r, &p.ProposeLogs, m)
if err != nil {
return m, fmt.Errorf("unmarshaling propose logs: %v", err)
}
m, err = surge.Unmarshal(r, &p.PrevoteLogs, m)
if err != nil {
return m, fmt.Errorf("unmarshaling prevote logs: %v", err)
}
m, err = surge.Unmarshal(r, &p.PrecommitLogs, m)
if err != nil {
return m, fmt.Errorf("unmarshaling precommit logs: %v", err)
}
m, err = surge.Unmarshal(r, &p.OnceFlags, m)
if err != nil {
return m, fmt.Errorf("unmarshaling once flags: %v", err)
}
return m, nil
}

Expand Down
50 changes: 50 additions & 0 deletions process/process_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,51 @@
package process_test

import (
"math/rand"
"testing/quick"
"time"

"github.com/renproject/hyperdrive/process"
"github.com/renproject/hyperdrive/process/processutil"
"github.com/renproject/id"
"github.com/renproject/surge"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Process", func() {
Context("when unmarshaling fuzz", func() {
It("should not panic", func() {
f := func(fuzz []byte) bool {
msg := process.Process{}
Expect(surge.FromBinary(fuzz, &msg)).ToNot(Succeed())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})

Context("when marshaling and then unmarshaling", func() {
It("should equal itself", func() {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
f := func(proposeLogs map[process.Round]process.Propose, prevoteLogs map[process.Round]map[id.Signatory]process.Prevote, precommitLogs map[process.Round]map[id.Signatory]process.Precommit, onceFlags map[process.Round]process.OnceFlag) bool {
expected := process.Process{
State: processutil.RandomState(r),
}
expected.ProposeLogs = proposeLogs
expected.PrevoteLogs = prevoteLogs
expected.PrecommitLogs = precommitLogs
expected.OnceFlags = onceFlags
data, err := surge.ToBinary(expected)
Expect(err).ToNot(HaveOccurred())
got := process.Process{}
err = surge.FromBinary(data, &got)
Expect(err).ToNot(HaveOccurred())
Expect(got.State.Equal(&expected.State)).To(BeTrue())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
})
Loading

0 comments on commit 623f2eb

Please sign in to comment.