Skip to content

Commit

Permalink
fail metadata encoding/decoding if subnets length is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Oct 22, 2024
1 parent e3f903b commit b2300bf
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions network/records/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package records

import (
"encoding/json"
"fmt"

"github.com/ssvlabs/ssv/network/commons"
)

const subnetsLength = int(commons.SubnetsCount) / 4 // each char in the string encodes status of 4 subnets

// NodeMetadata holds node's general information
type NodeMetadata struct {
// NodeVersion is the ssv-node version, it is a required field
Expand All @@ -18,6 +23,10 @@ type NodeMetadata struct {

// Encode encodes the metadata into bytes
func (nm *NodeMetadata) Encode() ([]byte, error) {
if len(nm.Subnets) != subnetsLength {
return nil, fmt.Errorf("invalid subnets length %d", len(nm.Subnets))
}

return json.Marshal(nm)
}

Expand All @@ -26,6 +35,9 @@ func (nm *NodeMetadata) Decode(data []byte) error {
if err := json.Unmarshal(data, nm); err != nil {
return err
}
if len(nm.Subnets) != subnetsLength {
return fmt.Errorf("invalid subnets length %d", len(nm.Subnets))
}
return nil
}

Expand Down

0 comments on commit b2300bf

Please sign in to comment.