Skip to content
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 2 additions & 1 deletion p2p/validation/msg_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package validation

import (
"context"
"fmt"

pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/peer"
Expand Down Expand Up @@ -112,7 +113,7 @@ func validateFutureMsg(

// verify signature
if err := types.Verify(msg, committeeMember.Committee); err != nil {
return errors.Wrap(err, "msg signature invalid")
return types.WrapError(types.MessageSignatureInvalidErrorCode, fmt.Errorf("msg signature invalid: %w", err))
}

return nil
Expand Down
20 changes: 11 additions & 9 deletions qbft/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package qbft

import (
"bytes"
"fmt"
"sort"

"github.com/pkg/errors"

"github.com/ssvlabs/ssv-spec/types"
)

Expand Down Expand Up @@ -45,7 +47,7 @@ func commitQuorumForRoundRoot(state *State, commitMsgContainer *MsgContainer, ro

func aggregateCommitMsgs(msgs []*ProcessingMessage, fullData []byte) (*types.SignedSSVMessage, error) {
if len(msgs) == 0 {
return nil, errors.New("can't aggregate zero commit msgs")
return nil, types.NewError(types.ZeroCommitMessagesErrorCode, "can't aggregate zero commit msgs")
}

var ret *types.SignedSSVMessage
Expand Down Expand Up @@ -120,18 +122,18 @@ func baseCommitValidationIgnoreSignature(
) error {

if err := msg.Validate(); err != nil {
return errors.Wrap(err, "signed commit invalid")
return types.WrapError(types.CommitMessageInvalidErrorCode, fmt.Errorf("signed commit invalid: %w", err))
}

if msg.QBFTMessage.MsgType != CommitMsgType {
return errors.New("commit msg type is wrong")
return types.NewError(types.CommitMessageTypeWrongErrorCode, "commit msg type is wrong")
}
if msg.QBFTMessage.Height != height {
return errors.New("wrong msg height")
return types.NewError(types.WrongMessageHeightErrorCode, "wrong msg height")
}

if !msg.SignedMessage.CheckSignersInCommittee(operators) {
return errors.New("signer not in committee")
return types.NewError(types.SignerIsNotInCommitteeErrorCode, "signer not in committee")
}

return nil
Expand All @@ -148,7 +150,7 @@ func baseCommitValidationVerifySignature(

// verify signature
if err := types.Verify(msg.SignedMessage, operators); err != nil {
return errors.Wrap(err, "msg signature invalid")
return types.WrapError(types.MessageSignatureInvalidErrorCode, fmt.Errorf("msg signature invalid: %w", err))
}

return nil
Expand All @@ -166,15 +168,15 @@ func validateCommit(
}

if len(msg.SignedMessage.OperatorIDs) != 1 {
return errors.New("msg allows 1 signer")
return types.NewError(types.MessageAllowsOneSignerOnlyErrorCode, "msg allows 1 signer")
}

if msg.QBFTMessage.Round != round {
return errors.New("wrong msg round")
return types.NewError(types.WrongMessageRoundErrorCode, "wrong msg round")
}

if !bytes.Equal(proposedMsg.QBFTMessage.Root[:], msg.QBFTMessage.Root[:]) {
return errors.New("proposed data mismatch")
return types.NewError(types.ProposedDataMismatchErrorCode, "proposed data mismatch")
}

return nil
Expand Down
10 changes: 5 additions & 5 deletions qbft/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ func (c *Controller) StartNewInstance(height Height, value []byte) error {

// can't use <= because of height == 0 case
if height < c.Height {
return errors.New("attempting to start an instance with a past height")
return types.NewError(types.StartInstanceErrorCode, "attempting to start an instance with a past height")
}

// covers height == 0 case
if c.StoredInstances.FindInstance(height) != nil {
return errors.New("instance already running")
return types.NewError(types.InstanceAlreadyRunningErrorCode, "instance already running")
}

c.Height = height
Expand Down Expand Up @@ -89,7 +89,7 @@ func (c *Controller) ProcessMsg(signedMessage *types.SignedSSVMessage) (*types.S
return nil, err
}
if isFuture {
return nil, fmt.Errorf("future msg from height, could not process")
return nil, types.NewError(types.FutureMessageErrorCode, "future msg from height, could not process")
}

return c.UponExistingInstanceMsg(msg)
Expand All @@ -107,7 +107,7 @@ func (c *Controller) UponExistingInstanceMsg(msg *ProcessingMessage) (*types.Sig

// if previously decided, we don't process more messages
if prevDecided {
return nil, errors.New("not processing consensus message since instance is already decided")
return nil, types.NewError(types.SkipConsensusMessageAsInstanceIsDecidedErrorCode, "not processing consensus message since instance is already decided")
}

decided, _, decidedMsg, err := inst.ProcessMsg(msg)
Expand All @@ -131,7 +131,7 @@ func (c *Controller) UponExistingInstanceMsg(msg *ProcessingMessage) (*types.Sig
func (c *Controller) BaseMsgValidation(msg *ProcessingMessage) error {
// verify msg belongs to controller
if !bytes.Equal(c.Identifier, msg.QBFTMessage.Identifier) {
return errors.New("message doesn't belong to Identifier")
return types.NewError(types.MessageIdentifierInvalidErrorCode, "message doesn't belong to Identifier")
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion qbft/decided.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func ValidateDecided(
return errors.Wrap(err, "could not hash input data")
}
if !bytes.Equal(r[:], msg.QBFTMessage.Root[:]) {
return errors.New("H(data) != root")
return types.NewError(types.RootHashInvalidErrorCode, "H(data) != root")
}

return nil
Expand Down
10 changes: 5 additions & 5 deletions qbft/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (i *Instance) Start(value []byte, height Height) {

func (i *Instance) Broadcast(msg *types.SignedSSVMessage) error {
if !i.CanProcessMessages() {
return errors.New("instance stopped processing messages")
return types.NewError(types.InstanceStoppedProcessingMessagesErrorCode, "instance stopped processing messages")
}

return i.GetConfig().GetNetwork().Broadcast(msg.SSVMessage.GetID(), msg)
Expand All @@ -88,7 +88,7 @@ func (i *Instance) Broadcast(msg *types.SignedSSVMessage) error {
// ProcessMsg processes a new QBFT msg, returns non nil error on msg processing error
func (i *Instance) ProcessMsg(msg *ProcessingMessage) (decided bool, decidedValue []byte, aggregatedCommit *types.SignedSSVMessage, err error) {
if !i.CanProcessMessages() {
return false, nil, nil, errors.New("instance stopped processing messages")
return false, nil, nil, types.NewError(types.InstanceStoppedProcessingMessagesErrorCode, "instance stopped processing messages")
}

if err := i.BaseMsgValidation(msg); err != nil {
Expand Down Expand Up @@ -126,7 +126,7 @@ func (i *Instance) BaseMsgValidation(msg *ProcessingMessage) error {
}

if msg.QBFTMessage.Round < i.State.Round {
return errors.New("past round")
return types.NewError(types.PastRoundErrorCode, "past round")
}

switch msg.QBFTMessage.MsgType {
Expand All @@ -140,7 +140,7 @@ func (i *Instance) BaseMsgValidation(msg *ProcessingMessage) error {
case PrepareMsgType:
proposedMsg := i.State.ProposalAcceptedForCurrentRound
if proposedMsg == nil {
return errors.New("did not receive proposal for this round")
return types.NewError(types.NoProposalForCurrentRoundErrorCode, "did not receive proposal for this round")
}
return validSignedPrepareForHeightRoundAndRootIgnoreSignature(
msg,
Expand All @@ -152,7 +152,7 @@ func (i *Instance) BaseMsgValidation(msg *ProcessingMessage) error {
case CommitMsgType:
proposedMsg := i.State.ProposalAcceptedForCurrentRound
if proposedMsg == nil {
return errors.New("did not receive proposal for this round")
return types.NewError(types.NoProposalForCurrentRoundErrorCode, "did not receive proposal for this round")
}
return validateCommit(
msg,
Expand Down
10 changes: 6 additions & 4 deletions qbft/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package qbft

import (
"crypto/sha256"
"fmt"

"github.com/pkg/errors"

"github.com/ssvlabs/ssv-spec/types"
)

Expand Down Expand Up @@ -91,7 +93,7 @@ func (msg *Message) GetRoot() ([32]byte, error) {
// Msg validation checks the msg, it's variables for validity.
func (msg *Message) Validate() error {
if len(msg.Identifier) != 56 {
return errors.New("message identifier is invalid")
return types.NewError(types.MessageIdentifierInvalidErrorCode, "message identifier is invalid")
}
if _, err := msg.GetRoundChangeJustifications(); err != nil {
return err
Expand All @@ -100,10 +102,10 @@ func (msg *Message) Validate() error {
return err
}
if msg.MsgType > RoundChangeMsgType {
return errors.New("message type is invalid")
return types.NewError(types.MessageTypeInvalidErrorCode, "message type is invalid")
}
if msg.Round == NoRound {
return errors.New("message round is invalid")
return types.NewError(types.MessageRoundInvalidErrorCode, "message round is invalid")
}
return nil
}
Expand All @@ -121,7 +123,7 @@ func unmarshalJustifications(data [][]byte) ([]*types.SignedSSVMessage, error) {
for i, d := range data {
sMsg := &types.SignedSSVMessage{}
if err := sMsg.UnmarshalSSZ(d); err != nil {
return nil, err
return nil, types.WrapError(types.UnmarshalSSZErrorCode, fmt.Errorf("unmarshal justification: %w", err))
}
ret[i] = sMsg
}
Expand Down
2 changes: 1 addition & 1 deletion qbft/messages_encoding.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions qbft/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package qbft

import (
"bytes"
"fmt"

"github.com/pkg/errors"
"github.com/ssvlabs/ssv-spec/types"
Expand Down Expand Up @@ -90,26 +91,26 @@ func validSignedPrepareForHeightRoundAndRootIgnoreSignature(
return errors.New("prepare msg type is wrong")
}
if msg.QBFTMessage.Height != height {
return errors.New("wrong msg height")
return types.NewError(types.WrongMessageHeightErrorCode, "wrong msg height")
}
if msg.QBFTMessage.Round != round {
return errors.New("wrong msg round")
return types.NewError(types.WrongMessageRoundErrorCode, "wrong msg round")
}

if err := msg.Validate(); err != nil {
return errors.Wrap(err, "prepareData invalid")
}

if !bytes.Equal(msg.QBFTMessage.Root[:], root[:]) {
return errors.New("proposed data mismatch")
return types.NewError(types.ProposedDataMismatchErrorCode, "proposed data mismatch")
}

if len(msg.SignedMessage.OperatorIDs) != 1 {
return errors.New("msg allows 1 signer")
return types.NewError(types.MessageAllowsOneSignerOnlyErrorCode, "msg allows 1 signer")
}

if !msg.SignedMessage.CheckSignersInCommittee(operators) {
return errors.New("signer not in committee")
return types.NewError(types.SignerIsNotInCommitteeErrorCode, "signer not in committee")
}

return nil
Expand All @@ -128,7 +129,7 @@ func validSignedPrepareForHeightRoundAndRootVerifySignature(

// Verify signature
if err := types.Verify(msg.SignedMessage, operators); err != nil {
return errors.Wrap(err, "msg signature invalid")
return types.WrapError(types.MessageSignatureInvalidErrorCode, fmt.Errorf("msg signature invalid: %w", err))
}

return nil
Expand Down
19 changes: 10 additions & 9 deletions qbft/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"

"github.com/pkg/errors"

"github.com/ssvlabs/ssv-spec/types"
)

Expand Down Expand Up @@ -56,22 +57,22 @@ func isValidProposal(
return errors.New("msg type is not proposal")
}
if msg.QBFTMessage.Height != state.Height {
return errors.New("wrong msg height")
return types.NewError(types.WrongMessageHeightErrorCode, "wrong msg height")
}
if len(msg.SignedMessage.OperatorIDs) != 1 {
return errors.New("msg allows 1 signer")
return types.NewError(types.MessageAllowsOneSignerOnlyErrorCode, "msg allows 1 signer")
}

if !msg.SignedMessage.CheckSignersInCommittee(state.CommitteeMember.Committee) {
return errors.New("signer not in committee")
return types.NewError(types.SignerIsNotInCommitteeErrorCode, "signer not in committee")
}

if !msg.SignedMessage.MatchedSigners([]types.OperatorID{proposer(state, config, msg.QBFTMessage.Round)}) {
return errors.New("proposal leader invalid")
return types.NewError(types.ProposalLeaderInvalidErrorCode, "proposal leader invalid")
}

if err := msg.Validate(); err != nil {
return errors.Wrap(err, "proposal invalid")
return types.NewError(types.ProposalInvalidErrorCode, "proposal invalid")
}

// verify full data integrity
Expand All @@ -80,7 +81,7 @@ func isValidProposal(
return errors.Wrap(err, "could not hash input data")
}
if !bytes.Equal(msg.QBFTMessage.Root[:], r[:]) {
return errors.New("H(data) != root")
return types.NewError(types.RootHashInvalidErrorCode, "H(data) != root")
}

// get justifications
Expand Down Expand Up @@ -121,7 +122,7 @@ func isValidProposal(
msg.QBFTMessage.Round > state.Round {
return nil
}
return errors.New("proposal is not valid with current state")
return types.NewError(types.ProposalInvalidErrorCode, "proposal is not valid with current state")
}

// isProposalJustification returns nil if the proposal and round change messages are valid and justify a proposal message for the provided round, value and leader
Expand Down Expand Up @@ -153,7 +154,7 @@ func isProposalJustification(

// check there is a quorum
if !HasQuorum(state.CommitteeMember, roundChangeMsgs) {
return errors.New("change round has no quorum")
return types.NewError(types.RoundChangeNoQuorumErrorCode, "change round has no quorum")
}

// previouslyPreparedF returns true if any on the round change messages have a prepared round and fullData
Expand Down Expand Up @@ -206,7 +207,7 @@ func isProposalJustification(
rcMsg.QBFTMessage.Root,
state.CommitteeMember.Committee,
); err != nil {
return errors.New("signed prepare not valid")
return types.NewError(types.PrepareMessageInvalidErrorCode, "signed prepare not valid")
}
}
return nil
Expand Down
Loading