Skip to content

Commit

Permalink
Adapt message validation
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusFranco99 committed Jan 10, 2024
1 parent 7965ba8 commit 1c3ace2
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions 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"

"github.com/bloxapp/ssv-spec/qbft"
"github.com/bloxapp/ssv-spec/ssv"
"github.com/bloxapp/ssv-spec/types"
Expand All @@ -15,11 +16,19 @@ type MsgValidatorFunc = func(ctx context.Context, p peer.ID, msg *pubsub.Message

func MsgValidation(runner ssv.Runner) MsgValidatorFunc {
return func(ctx context.Context, p peer.ID, msg *pubsub.Message) pubsub.ValidationResult {
ssvMsg, err := DecodePubsubMsg(msg)
signedSSVMsg, err := DecodePubsubMsg(msg)
if err != nil {
return pubsub.ValidationReject
}
if validateSSVMessage(runner, ssvMsg) != nil {

// Validate SignedSSVMessage
if validateSignedSSVMessage(runner, signedSSVMsg) != nil {
return pubsub.ValidationReject
}

// Get SSVMessage
ssvMsg, err := signedSSVMsg.GetSSVMessageFromData()
if err != nil {
return pubsub.ValidationReject
}

Expand All @@ -40,15 +49,29 @@ func MsgValidation(runner ssv.Runner) MsgValidatorFunc {
}
}

func DecodePubsubMsg(msg *pubsub.Message) (*types.SSVMessage, error) {
func DecodePubsubMsg(msg *pubsub.Message) (*types.SignedSSVMessage, error) {
byts := msg.GetData()
ret := &types.SSVMessage{}
ret := &types.SignedSSVMessage{}
if err := ret.Decode(byts); err != nil {
return nil, err
}
return ret, nil
}

func validateSignedSSVMessage(runner ssv.Runner, msg *types.SignedSSVMessage) error {

if err := msg.Validate(); err != nil {
return err
}

ssvMessage, err := msg.GetSSVMessageFromData()
if err != nil {
return err
}

return validateSSVMessage(runner, ssvMessage)
}

func validateSSVMessage(runner ssv.Runner, msg *types.SSVMessage) error {
if !runner.GetBaseRunner().Share.ValidatorPubKey.MessageIDBelongs(msg.GetID()) {
return errors.New("msg ID doesn't match validator ID")
Expand Down

0 comments on commit 1c3ace2

Please sign in to comment.