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

Move json utils to a testutils file #432

Merged
merged 2 commits into from
Jun 30, 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
33 changes: 0 additions & 33 deletions qbft/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package qbft

import (
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"

"github.com/pkg/errors"
Expand Down Expand Up @@ -180,37 +178,6 @@ func (c *Controller) forceStopAllInstanceExceptCurrent() {
}
}

// GetRoot returns the state's deterministic root
func (c *Controller) GetRoot() ([32]byte, error) {
marshaledRoot, err := json.Marshal(c)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not encode state")
}
ret := sha256.Sum256(marshaledRoot)
return ret, nil
}

// Encode implementation
func (c *Controller) Encode() ([]byte, error) {
return json.Marshal(c)
}

// Decode implementation
func (c *Controller) Decode(data []byte) error {
err := json.Unmarshal(data, &c)
if err != nil {
return errors.Wrap(err, "could not decode controller")
}

config := c.GetConfig()
for _, i := range c.StoredInstances {
if i != nil {
i.config = config
}
}
return nil
}

func (c *Controller) broadcastDecided(aggregatedCommit *types.SignedSSVMessage) error {

if err := c.GetConfig().GetNetwork().Broadcast(aggregatedCommit.SSVMessage.GetID(), aggregatedCommit); err != nil {
Expand Down
16 changes: 0 additions & 16 deletions qbft/instance.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package qbft

import (
"encoding/json"
"fmt"
"sync"

Expand Down Expand Up @@ -206,21 +205,6 @@ func (i *Instance) GetHeight() Height {
return i.State.Height
}

// GetRoot returns the state's deterministic root
func (i *Instance) GetRoot() ([32]byte, error) {
return i.State.GetRoot()
}

// Encode implementation
func (i *Instance) Encode() ([]byte, error) {
return json.Marshal(i)
}

// Decode implementation
func (i *Instance) Decode(data []byte) error {
return json.Unmarshal(data, &i)
}

// CanProcessMessages will return true if instance can process messages
func (i *Instance) CanProcessMessages() bool {
return !i.forceStop && i.State.Round < i.config.GetCutOffRound()
Expand Down
128 changes: 128 additions & 0 deletions qbft/json_testutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package qbft

import (
"crypto/sha256"
"encoding/json"

"github.com/pkg/errors"
)

// This file adds, as testing utils, the Encode, Decode and GetRoot methods
// so that structures follow the types.Encoder and types.Root interface

// Controller
func (c *Controller) Encode() ([]byte, error) {
return json.Marshal(c)
}

func (c *Controller) Decode(data []byte) error {
err := json.Unmarshal(data, &c)
if err != nil {
return errors.Wrap(err, "could not decode controller")
}

config := c.GetConfig()
for _, i := range c.StoredInstances {
if i != nil {
i.config = config
}
}
return nil
}

func (c *Controller) GetRoot() ([32]byte, error) {
marshaledRoot, err := json.Marshal(c)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not encode controller")
}
ret := sha256.Sum256(marshaledRoot)
return ret, nil
}

// Instance
func (i *Instance) Encode() ([]byte, error) {
return json.Marshal(i)
}

func (i *Instance) Decode(data []byte) error {
return json.Unmarshal(data, &i)
}

func (i *Instance) GetRoot() ([32]byte, error) {
return i.State.GetRoot()
}

// MarshalJSON is a custom JSON marshaller for Instance
func (i *Instance) MarshalJSON() ([]byte, error) {
type Alias Instance
if i.forceStop {
return json.Marshal(&struct {
ForceStop bool `json:"forceStop"`
*Alias
}{
ForceStop: i.forceStop,
Alias: (*Alias)(i),
})
} else {
return json.Marshal(&struct {
*Alias
}{
Alias: (*Alias)(i),
})
}
}

// UnmarshalJSON is a custom JSON unmarshaller for Instance
func (i *Instance) UnmarshalJSON(data []byte) error {
type Alias Instance
aux := &struct {
ForceStop *bool `json:"forceStop,omitempty"`
*Alias
}{
Alias: (*Alias)(i),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
if aux.ForceStop != nil {
i.forceStop = *aux.ForceStop
}
return nil
}

// State

func (s *State) Encode() ([]byte, error) {
return json.Marshal(s)
}

func (s *State) Decode(data []byte) error {
return json.Unmarshal(data, &s)
}

func (s *State) GetRoot() ([32]byte, error) {
marshaledRoot, err := s.Encode()
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not encode state")
}
ret := sha256.Sum256(marshaledRoot)
return ret, nil
}

// MsgContainer
func (c *MsgContainer) Encode() ([]byte, error) {
return json.Marshal(c)
}

func (c *MsgContainer) Decode(data []byte) error {
return json.Unmarshal(data, &c)
}

func (c *MsgContainer) GetRoot() ([32]byte, error) {
marshaledRoot, err := c.Encode()
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not encode msg container")
}
ret := sha256.Sum256(marshaledRoot)
return ret, nil
}
47 changes: 0 additions & 47 deletions qbft/marshalutils.go

This file was deleted.

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

import (
"bytes"
"encoding/json"

"github.com/ssvlabs/ssv-spec/types"
)
Expand Down Expand Up @@ -146,13 +145,3 @@ func (c *MsgContainer) AddMsg(signedMsg *types.SignedSSVMessage) error {

return nil
}

// Encode returns the encoded struct in bytes or error
func (c *MsgContainer) Encode() ([]byte, error) {
return json.Marshal(c)
}

// Decode returns error if decoding failed
func (c *MsgContainer) Decode(data []byte) error {
return json.Unmarshal(data, &c)
}
25 changes: 0 additions & 25 deletions qbft/state.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package qbft

import (
"crypto/sha256"
"encoding/json"

"github.com/pkg/errors"

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

Expand Down Expand Up @@ -102,23 +97,3 @@ type State struct {
CommitContainer *MsgContainer
RoundChangeContainer *MsgContainer
}

// GetRoot returns the state's deterministic root
func (s *State) GetRoot() ([32]byte, error) {
marshaledRoot, err := s.Encode()
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not encode state")
}
ret := sha256.Sum256(marshaledRoot)
return ret, nil
}

// Encode returns a msg encoded bytes or error
func (s *State) Encode() ([]byte, error) {
return json.Marshal(s)
}

// Decode returns error if decoding failed
func (s *State) Decode(data []byte) error {
return json.Unmarshal(data, &s)
}
23 changes: 0 additions & 23 deletions ssv/aggregator.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package ssv

import (
"crypto/sha256"
"encoding/json"

"github.com/attestantio/go-eth2-client/spec/phase0"
ssz "github.com/ferranbt/fastssz"
"github.com/pkg/errors"
Expand Down Expand Up @@ -283,23 +280,3 @@ func (r *AggregatorRunner) GetSigner() types.BeaconSigner {
func (r *AggregatorRunner) GetOperatorSigner() types.OperatorSigner {
return r.operatorSigner
}

// Encode returns the encoded struct in bytes or error
func (r *AggregatorRunner) Encode() ([]byte, error) {
return json.Marshal(r)
}

// Decode returns error if decoding failed
func (r *AggregatorRunner) Decode(data []byte) error {
return json.Unmarshal(data, &r)
}

// GetRoot returns the root used for signing and verification
func (r *AggregatorRunner) GetRoot() ([32]byte, error) {
marshaledRoot, err := r.Encode()
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not encode DutyRunnerState")
}
ret := sha256.Sum256(marshaledRoot)
return ret, nil
}
Loading