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

Type test: share #337

Merged
merged 6 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
12 changes: 10 additions & 2 deletions types/spectest/all_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
"github.com/bloxapp/ssv-spec/types/spectest/tests/beacon"
"github.com/bloxapp/ssv-spec/types/spectest/tests/consensusdata"
consensusdataproposer "github.com/bloxapp/ssv-spec/types/spectest/tests/consensusdata/proposer"
"github.com/bloxapp/ssv-spec/types/spectest/tests/ssz"

"github.com/bloxapp/ssv-spec/types/spectest/tests/encryption"
"github.com/bloxapp/ssv-spec/types/spectest/tests/partialsigmessage"
"github.com/bloxapp/ssv-spec/types/spectest/tests/share"
"github.com/bloxapp/ssv-spec/types/spectest/tests/ssvmsg"
"github.com/bloxapp/ssv-spec/types/spectest/tests/ssz"
)

type SpecTest interface {
Expand Down Expand Up @@ -64,4 +63,13 @@ var AllTests = []SpecTest{
ssz.SSZWithdrawalsMarshaling(),

beacon.DepositData(),

share.HasPartialQuorumButNoQuorum(),
share.HasQuorum(),
share.HasQuorum3f1(),
share.NoPartialQuorumDuplicate(),
share.NoPartialQuorum(),
share.NoQuorumDuplicate(),
share.PartialQuorumWithDuplicate(),
share.QuorumWithDuplicate(),
}
2 changes: 1 addition & 1 deletion types/spectest/generate/tests.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions types/spectest/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ func TestJson(t *testing.T) {
typedTest := &beacon.DepositDataSpecTest{}
require.NoError(t, json.Unmarshal(byts, &typedTest))
typedTest.Run(t)
case reflect.TypeOf(&share.ShareTest{}).String():
byts, err := json.Marshal(test)
Copy link
Contributor

Choose a reason for hiding this comment

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

typo?

require.NoError(t, err)
typedTest := &share.ShareTest{}
require.NoError(t, json.Unmarshal(byts, &typedTest))
typedTest.Run(t)
default:
t.Fatalf("unknown test")
}
Expand Down
6 changes: 0 additions & 6 deletions types/spectest/tests/share/has_partial_quorum.go

This file was deleted.

6 changes: 0 additions & 6 deletions types/spectest/tests/share/has_partial_quorum_2f1.go

This file was deleted.

22 changes: 20 additions & 2 deletions types/spectest/tests/share/has_quorum.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
package share

import (
"github.com/bloxapp/ssv-spec/types"
"github.com/bloxapp/ssv-spec/types/testingutils"
"github.com/herumi/bls-eth-go-binary/bls"
)

// HasQuorum tests msg with unique 2f+1 signers
func HasQuorum() *SpecTest {
panic("implement")
func HasQuorum() *ShareTest {
ks := testingutils.Testing4SharesSet()
share := testingutils.TestingShare(ks)

msg := testingutils.TestingCommitMultiSignerMessage([]*bls.SecretKey{ks.Shares[1], ks.Shares[2], ks.Shares[3]}, []types.OperatorID{1, 2, 3})

return &ShareTest{
Name: "has quorum",
Share: *share,
Message: *msg,
ExpectedHasPartialQuorum: true,
ExpectedHasQuorum: true,
ExpectedFullCommittee: false,
}
}
22 changes: 20 additions & 2 deletions types/spectest/tests/share/has_quorum_3f1.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
package share

import (
"github.com/bloxapp/ssv-spec/types"
"github.com/bloxapp/ssv-spec/types/testingutils"
"github.com/herumi/bls-eth-go-binary/bls"
)

// HasQuorum3f1 tests msg with unique 3f+1 signers
func HasQuorum3f1() *SpecTest {
panic("implement")
func HasQuorum3f1() *ShareTest {
ks := testingutils.Testing4SharesSet()
share := testingutils.TestingShare(ks)

msg := testingutils.TestingCommitMultiSignerMessage([]*bls.SecretKey{ks.Shares[1], ks.Shares[2], ks.Shares[3], ks.Shares[4]}, []types.OperatorID{1, 2, 3, 4})

return &ShareTest{
Name: "has quorum 3f1",
Share: *share,
Message: *msg,
ExpectedHasPartialQuorum: true,
ExpectedHasQuorum: true,
ExpectedFullCommittee: true,
}
}
20 changes: 18 additions & 2 deletions types/spectest/tests/share/no_partial_quorum.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
package share

import (
"github.com/bloxapp/ssv-spec/types/testingutils"
)

// NoPartialQuorum tests msg with < unique f+1 signers
func NoPartialQuorum() *SpecTest {
panic("implement")
func NoPartialQuorum() *ShareTest {
ks := testingutils.Testing4SharesSet()
share := testingutils.TestingShare(ks)

msg := testingutils.TestingCommitMessage(ks.Shares[1], 1)

return &ShareTest{
Name: "no partial quorum",
Share: *share,
Message: *msg,
ExpectedHasPartialQuorum: false,
ExpectedHasQuorum: false,
ExpectedFullCommittee: false,
}
}
32 changes: 30 additions & 2 deletions types/spectest/tests/share/no_partial_quorum_duplicate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
package share

import (
"github.com/bloxapp/ssv-spec/qbft"
"github.com/bloxapp/ssv-spec/types"
"github.com/bloxapp/ssv-spec/types/testingutils"
)

// NoPartialQuorumDuplicate tests msg with < unique f+1 signers (but f+1 signers including duplicates)
func NoPartialQuorumDuplicate() *SpecTest {
panic("implement")
func NoPartialQuorumDuplicate() *ShareTest {
ks := testingutils.Testing4SharesSet()
share := testingutils.TestingShare(ks)

msg := &qbft.SignedMessage{
Message: qbft.Message{
MsgType: qbft.CommitMsgType,
Height: qbft.FirstHeight,
Round: qbft.FirstRound,
Identifier: testingutils.TestingIdentifier,
Root: testingutils.TestingQBFTRootData,
},
Signers: []types.OperatorID{1, 1},
}

return &ShareTest{
Name: "no partial quorum duplicate",
Share: *share,
Message: *msg,
ExpectedHasPartialQuorum: false,
ExpectedHasQuorum: false,
ExpectedFullCommittee: false,
ExpectedError: "non unique signer",
}
}
6 changes: 0 additions & 6 deletions types/spectest/tests/share/no_quorum.go

This file was deleted.

32 changes: 30 additions & 2 deletions types/spectest/tests/share/no_quorum_duplicate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
package share

import (
"github.com/bloxapp/ssv-spec/qbft"
"github.com/bloxapp/ssv-spec/types"
"github.com/bloxapp/ssv-spec/types/testingutils"
)

// NoQuorumDuplicate tests msg with < unique 2f+1 signers (but 2f+1 signers including duplicates)
func NoQuorumDuplicate() *SpecTest {
panic("implement")
func NoQuorumDuplicate() *ShareTest {
ks := testingutils.Testing4SharesSet()
share := testingutils.TestingShare(ks)

msg := &qbft.SignedMessage{
Message: qbft.Message{
MsgType: qbft.CommitMsgType,
Height: qbft.FirstHeight,
Round: qbft.FirstRound,
Identifier: testingutils.TestingIdentifier,
Root: testingutils.TestingQBFTRootData,
},
Signers: []types.OperatorID{1, 1, 2},
}

return &ShareTest{
Name: "no quorum duplicate",
Share: *share,
Message: *msg,
ExpectedHasPartialQuorum: true,
ExpectedHasQuorum: false,
ExpectedFullCommittee: false,
ExpectedError: "non unique signer",
}
}
24 changes: 24 additions & 0 deletions types/spectest/tests/share/partial_quorum_no_quorum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package share

import (
"github.com/bloxapp/ssv-spec/types"
"github.com/bloxapp/ssv-spec/types/testingutils"
"github.com/herumi/bls-eth-go-binary/bls"
)

// HasPartialQuorumButNoQuorum tests msg with unique f+1 signers
func HasPartialQuorumButNoQuorum() *ShareTest {
ks := testingutils.Testing4SharesSet()
share := testingutils.TestingShare(ks)

msg := testingutils.TestingCommitMultiSignerMessage([]*bls.SecretKey{ks.Shares[1], ks.Shares[2]}, []types.OperatorID{1, 2})

return &ShareTest{
Name: "has partial quorum but no quorum",
Share: *share,
Message: *msg,
ExpectedHasPartialQuorum: true,
ExpectedHasQuorum: false,
ExpectedFullCommittee: false,
}
}
32 changes: 30 additions & 2 deletions types/spectest/tests/share/partial_quorum_with_duplicate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
package share

import (
"github.com/bloxapp/ssv-spec/qbft"
"github.com/bloxapp/ssv-spec/types"
"github.com/bloxapp/ssv-spec/types/testingutils"
)

// PartialQuorumWithDuplicate tests msg with unique f+1 signers (but also including duplicates)
func PartialQuorumWithDuplicate() *SpecTest {
panic("implement")
func PartialQuorumWithDuplicate() *ShareTest {
ks := testingutils.Testing4SharesSet()
share := testingutils.TestingShare(ks)

msg := &qbft.SignedMessage{
Message: qbft.Message{
MsgType: qbft.CommitMsgType,
Height: qbft.FirstHeight,
Round: qbft.FirstRound,
Identifier: testingutils.TestingIdentifier,
Root: testingutils.TestingQBFTRootData,
},
Signers: []types.OperatorID{1, 1, 2},
}

return &ShareTest{
Name: "partial quorum with duplicate",
Share: *share,
Message: *msg,
ExpectedHasPartialQuorum: true,
ExpectedHasQuorum: false,
ExpectedFullCommittee: false,
ExpectedError: "non unique signer",
}
}
32 changes: 30 additions & 2 deletions types/spectest/tests/share/quorum_with_duplicate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
package share

import (
"github.com/bloxapp/ssv-spec/qbft"
"github.com/bloxapp/ssv-spec/types"
"github.com/bloxapp/ssv-spec/types/testingutils"
)

// QuorumWithDuplicate tests msg with unique 2f+1 signers (but also including duplicates)
func QuorumWithDuplicate() *SpecTest {
panic("implement")
func QuorumWithDuplicate() *ShareTest {
ks := testingutils.Testing4SharesSet()
share := testingutils.TestingShare(ks)

msg := &qbft.SignedMessage{
Message: qbft.Message{
MsgType: qbft.CommitMsgType,
Height: qbft.FirstHeight,
Round: qbft.FirstRound,
Identifier: testingutils.TestingIdentifier,
Root: testingutils.TestingQBFTRootData,
},
Signers: []types.OperatorID{1, 1, 2, 3},
}

return &ShareTest{
Name: "quorum with duplicate",
Share: *share,
Message: *msg,
ExpectedHasPartialQuorum: true,
ExpectedHasQuorum: true,
ExpectedFullCommittee: false,
ExpectedError: "non unique signer",
}
}
50 changes: 43 additions & 7 deletions types/spectest/tests/share/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,53 @@ package share

import (
"testing"

"github.com/bloxapp/ssv-spec/types"
"github.com/stretchr/testify/require"

"github.com/bloxapp/ssv-spec/qbft"
)

type SpecTest struct {
Name string
Data []byte
type ShareTest struct {
Name string
Share types.Share
Message qbft.SignedMessage
ExpectedHasPartialQuorum bool
ExpectedHasQuorum bool
ExpectedFullCommittee bool
ExpectedError string
}

func (test *SpecTest) TestName() string {
return test.Name
func (test *ShareTest) TestName() string {
return "share " + test.Name
}

func (test *SpecTest) Run(t *testing.T) {
panic("implement")
// Returns the number of unique signers in the message signers list
func (test *ShareTest) GetUniqueMessageSignersCount() int {
uniqueSigners := make(map[uint64]bool)

for _, element := range test.Message.Signers {
uniqueSigners[element] = true
}

return len(uniqueSigners)
}
Comment on lines +27 to +35
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func (test *ShareTest) GetUniqueMessageSignersCount() int {
uniqueSigners := make(map[uint64]bool)
for _, element := range test.Message.Signers {
uniqueSigners[element] = true
}
return len(uniqueSigners)
}
func (test *ShareTest) GetUniqueMessageSignersCount() int {
uniqueSigners := make(map[uint64]struct{})
for _, signer := range test.Message.Signers {
uniqueSigners[signer] = struct{}{}
}
return len(uniqueSigners)
}

not anything crucial, but this is more memory-efficient since it doesn't actually store any data


func (test *ShareTest) Run(t *testing.T) {

// Validate message
err := test.Message.Validate()
if len(test.ExpectedError) != 0 {
require.EqualError(t, err, test.ExpectedError)
} else {
require.NoError(t, err)
}

// Get unique signers
numSigners := test.GetUniqueMessageSignersCount()

// Test expected thresholds results
require.Equal(t, test.ExpectedHasPartialQuorum, test.Share.HasPartialQuorum(numSigners))
require.Equal(t, test.ExpectedHasQuorum, test.Share.HasQuorum(numSigners))
require.Equal(t, test.ExpectedFullCommittee, (len(test.Share.Committee) == numSigners))
}