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

refactor(types): define errors for vote package #1503

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 17 additions & 17 deletions consensus/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (*changeProposer) checkCPValue(vte *vote.Vote, allowedValues ...vote.CPValu
}
}

return invalidJustificationError{
return InvalidJustificationError{
JustType: vte.CPJust().Type(),
Reason: fmt.Sprintf("invalid value: %v", vte.CPValue()),
}
Expand All @@ -39,15 +39,15 @@ func (*changeProposer) checkCPValue(vte *vote.Vote, allowedValues ...vote.CPValu
func (cp *changeProposer) checkJustInitZero(just vote.Just, blockHash hash.Hash) error {
j, ok := just.(*vote.JustInitNo)
if !ok {
return invalidJustificationError{
return InvalidJustificationError{
JustType: just.Type(),
Reason: "invalid just data",
}
}

err := j.QCert.ValidatePrepare(cp.validators, blockHash)
if err != nil {
return invalidJustificationError{
return InvalidJustificationError{
JustType: j.Type(),
Reason: err.Error(),
}
Expand All @@ -59,7 +59,7 @@ func (cp *changeProposer) checkJustInitZero(just vote.Just, blockHash hash.Hash)
func (*changeProposer) checkJustInitOne(just vote.Just) error {
_, ok := just.(*vote.JustInitYes)
if !ok {
return invalidJustificationError{
return InvalidJustificationError{
JustType: just.Type(),
Reason: "invalid just data",
}
Expand All @@ -73,7 +73,7 @@ func (cp *changeProposer) checkJustPreVoteHard(just vote.Just,
) error {
j, ok := just.(*vote.JustPreVoteHard)
if !ok {
return invalidJustificationError{
return InvalidJustificationError{
JustType: just.Type(),
Reason: "invalid just data",
}
Expand All @@ -82,7 +82,7 @@ func (cp *changeProposer) checkJustPreVoteHard(just vote.Just,
err := j.QCert.ValidateCPPreVote(cp.validators,
blockHash, cpRound-1, byte(cpValue))
if err != nil {
return invalidJustificationError{
return InvalidJustificationError{
JustType: just.Type(),
Reason: err.Error(),
}
Expand All @@ -96,7 +96,7 @@ func (cp *changeProposer) checkJustPreVoteSoft(just vote.Just,
) error {
j, ok := just.(*vote.JustPreVoteSoft)
if !ok {
return invalidJustificationError{
return InvalidJustificationError{
JustType: just.Type(),
Reason: "invalid just data",
}
Expand All @@ -105,7 +105,7 @@ func (cp *changeProposer) checkJustPreVoteSoft(just vote.Just,
err := j.QCert.ValidateCPMainVote(cp.validators,
blockHash, cpRound-1, byte(vote.CPValueAbstain))
if err != nil {
return invalidJustificationError{
return InvalidJustificationError{
JustType: just.Type(),
Reason: err.Error(),
}
Expand All @@ -119,7 +119,7 @@ func (cp *changeProposer) checkJustMainVoteNoConflict(just vote.Just,
) error {
j, ok := just.(*vote.JustMainVoteNoConflict)
if !ok {
return invalidJustificationError{
return InvalidJustificationError{
JustType: just.Type(),
Reason: "invalid just data",
}
Expand All @@ -128,7 +128,7 @@ func (cp *changeProposer) checkJustMainVoteNoConflict(just vote.Just,
err := j.QCert.ValidateCPPreVote(cp.validators,
blockHash, cpRound, byte(cpValue))
if err != nil {
return invalidJustificationError{
return InvalidJustificationError{
JustType: j.Type(),
Reason: err.Error(),
}
Expand All @@ -143,7 +143,7 @@ func (cp *changeProposer) checkJustMainVoteConflict(just vote.Just,
) error {
j, ok := just.(*vote.JustMainVoteConflict)
if !ok {
return invalidJustificationError{
return InvalidJustificationError{
JustType: just.Type(),
Reason: "invalid just data",
}
Expand Down Expand Up @@ -176,7 +176,7 @@ func (cp *changeProposer) checkJustMainVoteConflict(just vote.Just,
return err
}
default:
return invalidJustificationError{
return InvalidJustificationError{
JustType: just.Type(),
Reason: fmt.Sprintf("unexpected justification: %s", j.JustNo.Type()),
}
Expand Down Expand Up @@ -211,7 +211,7 @@ func (cp *changeProposer) checkJustPreVote(v *vote.Vote) error {

return cp.checkJustInitOne(just)
default:
return invalidJustificationError{
return InvalidJustificationError{
JustType: just.Type(),
Reason: "invalid pre-vote justification",
}
Expand All @@ -235,7 +235,7 @@ func (cp *changeProposer) checkJustPreVote(v *vote.Vote) error {
return cp.checkJustPreVoteHard(just, v.BlockHash(), v.CPRound(), v.CPValue())

default:
return invalidJustificationError{
return InvalidJustificationError{
JustType: just.Type(),
Reason: "invalid pre-vote justification",
}
Expand Down Expand Up @@ -264,7 +264,7 @@ func (cp *changeProposer) checkJustMainVote(v *vote.Vote) error {
return cp.checkJustMainVoteConflict(just, v.BlockHash(), v.CPRound())

default:
return invalidJustificationError{
return InvalidJustificationError{
JustType: just.Type(),
Reason: "invalid main-vote justification",
}
Expand All @@ -278,7 +278,7 @@ func (cp *changeProposer) checkJustDecide(v *vote.Vote) error {
}
j, ok := v.CPJust().(*vote.JustDecided)
if !ok {
return invalidJustificationError{
return InvalidJustificationError{
JustType: j.Type(),
Reason: "invalid just data",
}
Expand All @@ -287,7 +287,7 @@ func (cp *changeProposer) checkJustDecide(v *vote.Vote) error {
err = j.QCert.ValidateCPMainVote(cp.validators,
v.BlockHash(), v.CPRound(), byte(v.CPValue()))
if err != nil {
return invalidJustificationError{
return InvalidJustificationError{
JustType: j.Type(),
Reason: err.Error(),
}
Expand Down
44 changes: 22 additions & 22 deletions consensus/cp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func TestInvalidJustInitOne(t *testing.T) {
v := vote.NewCPPreVote(hash.UndefHash, h, r, 0, vote.CPValueNo, just, td.consB.valKey.Address())

err := td.consX.changeProposer.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
assert.ErrorIs(t, err, InvalidJustificationError{
JustType: just.Type(),
Reason: "invalid value: no",
})
Expand All @@ -152,7 +152,7 @@ func TestInvalidJustInitOne(t *testing.T) {
v := vote.NewCPPreVote(hash.UndefHash, h, r, 1, vote.CPValueYes, just, td.consB.valKey.Address())

err := td.consX.changeProposer.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
assert.ErrorIs(t, err, InvalidJustificationError{
JustType: just.Type(),
Reason: "invalid pre-vote justification",
})
Expand All @@ -163,7 +163,7 @@ func TestInvalidJustInitOne(t *testing.T) {
v := vote.NewCPPreVote(td.RandHash(), h, r, 0, vote.CPValueYes, invJust, td.consB.valKey.Address())

err := td.consX.changeProposer.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
assert.ErrorIs(t, err, InvalidJustificationError{
JustType: invJust.Type(),
Reason: "invalid pre-vote justification",
})
Expand All @@ -184,7 +184,7 @@ func TestInvalidJustInitZero(t *testing.T) {
v := vote.NewCPPreVote(td.RandHash(), h, r, 0, vote.CPValueYes, just, td.consB.valKey.Address())

err := td.consX.changeProposer.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
assert.ErrorIs(t, err, InvalidJustificationError{
JustType: just.Type(),
Reason: "invalid value: yes",
})
Expand All @@ -194,7 +194,7 @@ func TestInvalidJustInitZero(t *testing.T) {
v := vote.NewCPPreVote(td.RandHash(), h, r, 1, vote.CPValueNo, just, td.consB.valKey.Address())

err := td.consX.changeProposer.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
assert.ErrorIs(t, err, InvalidJustificationError{
JustType: just.Type(),
Reason: "invalid pre-vote justification",
})
Expand All @@ -204,7 +204,7 @@ func TestInvalidJustInitZero(t *testing.T) {
v := vote.NewCPPreVote(td.RandHash(), h, r, 0, vote.CPValueNo, just, td.consB.valKey.Address())

err := td.consX.changeProposer.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
assert.ErrorIs(t, err, InvalidJustificationError{
JustType: just.Type(),
Reason: fmt.Sprintf("certificate has an unexpected committers: %v", just.QCert.Committers()),
})
Expand All @@ -225,7 +225,7 @@ func TestInvalidJustPreVoteHard(t *testing.T) {
v := vote.NewCPPreVote(td.RandHash(), h, r, 1, vote.CPValueAbstain, just, td.consB.valKey.Address())

err := td.consX.changeProposer.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
assert.ErrorIs(t, err, InvalidJustificationError{
JustType: just.Type(),
Reason: "invalid value: abstain",
})
Expand All @@ -235,7 +235,7 @@ func TestInvalidJustPreVoteHard(t *testing.T) {
v := vote.NewCPPreVote(td.RandHash(), h, r, 0, vote.CPValueNo, just, td.consB.valKey.Address())

err := td.consX.changeProposer.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
assert.ErrorIs(t, err, InvalidJustificationError{
JustType: just.Type(),
Reason: "invalid pre-vote justification",
})
Expand All @@ -245,7 +245,7 @@ func TestInvalidJustPreVoteHard(t *testing.T) {
v := vote.NewCPPreVote(td.RandHash(), h, r, 1, vote.CPValueNo, just, td.consB.valKey.Address())

err := td.consX.changeProposer.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
assert.ErrorIs(t, err, InvalidJustificationError{
JustType: just.Type(),
Reason: fmt.Sprintf("certificate has an unexpected committers: %v", just.QCert.Committers()),
})
Expand All @@ -266,7 +266,7 @@ func TestInvalidJustPreVoteSoft(t *testing.T) {
v := vote.NewCPPreVote(td.RandHash(), h, r, 1, vote.CPValueAbstain, just, td.consB.valKey.Address())

err := td.consX.changeProposer.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
assert.ErrorIs(t, err, InvalidJustificationError{
JustType: just.Type(),
Reason: "invalid value: abstain",
})
Expand All @@ -276,7 +276,7 @@ func TestInvalidJustPreVoteSoft(t *testing.T) {
v := vote.NewCPPreVote(td.RandHash(), h, r, 0, vote.CPValueNo, just, td.consB.valKey.Address())

err := td.consX.changeProposer.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
assert.ErrorIs(t, err, InvalidJustificationError{
JustType: just.Type(),
Reason: "invalid pre-vote justification",
})
Expand All @@ -286,7 +286,7 @@ func TestInvalidJustPreVoteSoft(t *testing.T) {
v := vote.NewCPPreVote(td.RandHash(), h, r, 1, vote.CPValueNo, just, td.consB.valKey.Address())

err := td.consX.changeProposer.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
assert.ErrorIs(t, err, InvalidJustificationError{
JustType: just.Type(),
Reason: fmt.Sprintf("certificate has an unexpected committers: %v", just.QCert.Committers()),
})
Expand All @@ -307,7 +307,7 @@ func TestInvalidJustMainVoteNoConflict(t *testing.T) {
v := vote.NewCPMainVote(td.RandHash(), h, r, 1, vote.CPValueAbstain, just, td.consB.valKey.Address())

err := td.consX.changeProposer.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
assert.ErrorIs(t, err, InvalidJustificationError{
JustType: just.Type(),
Reason: "invalid value: abstain",
})
Expand All @@ -317,7 +317,7 @@ func TestInvalidJustMainVoteNoConflict(t *testing.T) {
v := vote.NewCPMainVote(td.RandHash(), h, r, 1, vote.CPValueNo, just, td.consB.valKey.Address())

err := td.consX.changeProposer.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
assert.ErrorIs(t, err, InvalidJustificationError{
JustType: just.Type(),
Reason: fmt.Sprintf("certificate has an unexpected committers: %v", just.QCert.Committers()),
})
Expand All @@ -341,7 +341,7 @@ func TestInvalidJustMainVoteConflict(t *testing.T) {
v := vote.NewCPMainVote(td.RandHash(), h, r, 0, vote.CPValueNo, just, td.consB.valKey.Address())

err := td.consX.changeProposer.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
assert.ErrorIs(t, err, InvalidJustificationError{
JustType: just.Type(),
Reason: "invalid value: no",
})
Expand All @@ -357,7 +357,7 @@ func TestInvalidJustMainVoteConflict(t *testing.T) {
v := vote.NewCPMainVote(td.RandHash(), h, r, 0, vote.CPValueYes, just, td.consB.valKey.Address())

err := td.consX.changeProposer.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
assert.ErrorIs(t, err, InvalidJustificationError{
JustType: just.Type(),
Reason: "invalid value: yes",
})
Expand All @@ -373,7 +373,7 @@ func TestInvalidJustMainVoteConflict(t *testing.T) {
v := vote.NewCPMainVote(td.RandHash(), h, r, 0, vote.CPValueAbstain, just, td.consB.valKey.Address())

err := td.consX.changeProposer.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
assert.ErrorIs(t, err, InvalidJustificationError{
JustType: vote.JustTypePreVoteSoft,
Reason: "invalid just data",
})
Expand All @@ -391,7 +391,7 @@ func TestInvalidJustMainVoteConflict(t *testing.T) {
v := vote.NewCPMainVote(td.RandHash(), h, r, 1, vote.CPValueAbstain, just, td.consB.valKey.Address())

err := td.consX.changeProposer.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
assert.ErrorIs(t, err, InvalidJustificationError{
JustType: just.Type(),
Reason: "unexpected justification: JustInitNo",
})
Expand All @@ -408,7 +408,7 @@ func TestInvalidJustMainVoteConflict(t *testing.T) {
v := vote.NewCPMainVote(td.RandHash(), h, r, 0, vote.CPValueAbstain, just, td.consB.valKey.Address())

err := td.consX.changeProposer.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
assert.ErrorIs(t, err, InvalidJustificationError{
JustType: just0.Type(),
Reason: fmt.Sprintf("certificate has an unexpected committers: %v", just0.QCert.Committers()),
})
Expand All @@ -427,7 +427,7 @@ func TestInvalidJustMainVoteConflict(t *testing.T) {
v := vote.NewCPMainVote(td.RandHash(), h, r, 1, vote.CPValueAbstain, just, td.consB.valKey.Address())

err := td.consX.changeProposer.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
assert.ErrorIs(t, err, InvalidJustificationError{
JustType: just0.Type(),
Reason: fmt.Sprintf("certificate has an unexpected committers: %v", just0.QCert.Committers()),
})
Expand All @@ -448,7 +448,7 @@ func TestInvalidJustDecided(t *testing.T) {
v := vote.NewCPDecidedVote(td.RandHash(), h, r, 0, vote.CPValueAbstain, just, td.consB.valKey.Address())

err := td.consX.changeProposer.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
assert.ErrorIs(t, err, InvalidJustificationError{
JustType: just.Type(),
Reason: "invalid value: abstain",
})
Expand All @@ -458,7 +458,7 @@ func TestInvalidJustDecided(t *testing.T) {
v := vote.NewCPDecidedVote(td.RandHash(), h, r, 0, vote.CPValueYes, just, td.consB.valKey.Address())

err := td.consX.changeProposer.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
assert.ErrorIs(t, err, InvalidJustificationError{
JustType: just.Type(),
Reason: fmt.Sprintf("certificate has an unexpected committers: %v", just.QCert.Committers()),
})
Expand Down
6 changes: 3 additions & 3 deletions consensus/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"github.com/pactus-project/pactus/types/vote"
)

// invalidJustificationError is returned when the justification for a change-proposer
// InvalidJustificationError is returned when the justification for a change-proposer
// vote is invalid.
type invalidJustificationError struct {
type InvalidJustificationError struct {
JustType vote.JustType
Reason string
}

func (e invalidJustificationError) Error() string {
func (e InvalidJustificationError) Error() string {
return fmt.Sprintf("invalid justification: %s, reason: %s",
e.JustType.String(), e.Reason)
}
Expand Down
3 changes: 1 addition & 2 deletions consensus/voteset/binary_voteset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/types/validator"
"github.com/pactus-project/pactus/types/vote"
"github.com/pactus-project/pactus/util/errors"
)

type roundVotes struct {
Expand Down Expand Up @@ -105,7 +104,7 @@ func (vs *BinaryVoteSet) AddVote(v *vote.Vote) (bool, error) {
}

// It is a duplicated vote
err = errors.Error(errors.ErrDuplicateVote)
err = ErrDuplicatedVote
} else {
roundVotes.allVotes[v.Signer()] = v
roundVotes.votedPower += power
Expand Down
Loading
Loading