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

Validate params in RPC server #268

Merged
merged 10 commits into from
Jul 4, 2024
1 change: 1 addition & 0 deletions aggregator/database/mocks/database.go

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

1 change: 1 addition & 0 deletions aggregator/gen.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aggregator

//go:generate mockgen -destination=./mocks/rest_aggregator.go -package=mocks github.com/NethermindEth/near-sffl/aggregator RestAggregatorer
//go:generate mockgen -destination=./mocks/rpc_aggregator.go -package=mocks github.com/NethermindEth/near-sffl/aggregator RpcAggregatorer
//go:generate mockgen -destination=./mocks/message_blsagg.go -package=mocks github.com/NethermindEth/near-sffl/aggregator MessageBlsAggregationService
//go:generate mockgen -destination=./mocks/rollup_broadcaster.go -package=mocks github.com/NethermindEth/near-sffl/aggregator RollupBroadcasterer
//go:generate mockgen -destination=./mocks/eth_client.go -package=mocks github.com/Layr-Labs/eigensdk-go/chainio/clients/eth Client
1 change: 1 addition & 0 deletions aggregator/mocks/eth_client.go

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

1 change: 1 addition & 0 deletions aggregator/mocks/message_blsagg.go

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

1 change: 1 addition & 0 deletions aggregator/mocks/rest_aggregator.go

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

1 change: 1 addition & 0 deletions aggregator/mocks/rollup_broadcaster.go

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

111 changes: 111 additions & 0 deletions aggregator/mocks/rpc_aggregator.go

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

23 changes: 20 additions & 3 deletions aggregator/rpc_server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,16 @@ func mapErrors(err error) error {
// rpc framework forces a reply type to exist, so we put bool as a placeholder
func (s *RpcServer) ProcessSignedCheckpointTaskResponse(signedCheckpointTaskResponse *messages.SignedCheckpointTaskResponse, reply *bool) error {
s.logger.Info("Received signed task response", "response", signedCheckpointTaskResponse)

err := signedCheckpointTaskResponse.IsValid()
if err != nil {
return err
}

s.listener.IncTotalSignedCheckpointTaskResponse()
s.listener.ObserveLastMessageReceivedTime(signedCheckpointTaskResponse.OperatorId, CheckpointTaskResponseLabel)

err := s.app.ProcessSignedCheckpointTaskResponse(signedCheckpointTaskResponse)
err = s.app.ProcessSignedCheckpointTaskResponse(signedCheckpointTaskResponse)
if err != nil {
s.listener.IncSignedCheckpointTaskResponse(
signedCheckpointTaskResponse.OperatorId,
Expand All @@ -113,14 +119,20 @@ func (s *RpcServer) ProcessSignedCheckpointTaskResponse(signedCheckpointTaskResp

func (s *RpcServer) ProcessSignedStateRootUpdateMessage(signedStateRootUpdateMessage *messages.SignedStateRootUpdateMessage, reply *bool) error {
s.logger.Info("Received signed state root update message", "updateMessage", signedStateRootUpdateMessage)

err := signedStateRootUpdateMessage.IsValid()
if err != nil {
return err
}

s.listener.IncTotalSignedCheckpointTaskResponse()
s.listener.ObserveLastMessageReceivedTime(signedStateRootUpdateMessage.OperatorId, StateRootUpdateMessageLabel)

hasNearDaCommitment := signedStateRootUpdateMessage.Message.HasNearDaCommitment()
operatorId := signedStateRootUpdateMessage.OperatorId
rollupId := signedStateRootUpdateMessage.Message.RollupId

err := s.app.ProcessSignedStateRootUpdateMessage(signedStateRootUpdateMessage)
err = s.app.ProcessSignedStateRootUpdateMessage(signedStateRootUpdateMessage)
s.listener.IncSignedStateRootUpdateMessage(operatorId, rollupId, err != nil, hasNearDaCommitment)
if err != nil {
return mapErrors(err)
Expand All @@ -132,11 +144,16 @@ func (s *RpcServer) ProcessSignedStateRootUpdateMessage(signedStateRootUpdateMes
func (s *RpcServer) ProcessSignedOperatorSetUpdateMessage(signedOperatorSetUpdateMessage *messages.SignedOperatorSetUpdateMessage, reply *bool) error {
s.logger.Info("Received signed operator set update message", "message", signedOperatorSetUpdateMessage)

err := signedOperatorSetUpdateMessage.IsValid()
if err != nil {
return err
}

operatorId := signedOperatorSetUpdateMessage.OperatorId
s.listener.ObserveLastMessageReceivedTime(operatorId, OperatorSetUpdateMessageLabel)
s.listener.IncTotalSignedOperatorSetUpdateMessage()

err := s.app.ProcessSignedOperatorSetUpdateMessage(signedOperatorSetUpdateMessage)
err = s.app.ProcessSignedOperatorSetUpdateMessage(signedOperatorSetUpdateMessage)
s.listener.IncSignedOperatorSetUpdateMessage(operatorId, err != nil)
if err != nil {
return mapErrors(err)
Expand Down
110 changes: 110 additions & 0 deletions aggregator/rpc_server/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package rpc_server

import (
"math/big"
"testing"

"github.com/Layr-Labs/eigensdk-go/crypto/bls"
"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/NethermindEth/near-sffl/aggregator/mocks"
"github.com/NethermindEth/near-sffl/core/types"
"github.com/NethermindEth/near-sffl/core/types/messages"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)

func TestProcessSignedCheckpointTaskResponse_InvalidParams(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

agg := mocks.NewMockRpcAggregatorer(mockCtrl)
logger, _ := logging.NewZapLogger(logging.Development)

rpc := NewRpcServer("localhost:8080", agg, logger)

var ignore bool
t.Run("nil message", func(t *testing.T) {
err := rpc.ProcessSignedCheckpointTaskResponse(nil, &ignore)

assert.NotNil(t, err)
})

t.Run("nil signature", func(t *testing.T) {
err := rpc.ProcessSignedCheckpointTaskResponse(&messages.SignedCheckpointTaskResponse{
BlsSignature: bls.Signature{G1Point: nil},
}, &ignore)

assert.NotNil(t, err)
})
}

func TestProcessSignedStateRootUpdateMessage_InvalidParams(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

agg := mocks.NewMockRpcAggregatorer(mockCtrl)
logger, _ := logging.NewZapLogger(logging.Development)

rpc := NewRpcServer("localhost:8080", agg, logger)

var ignore bool
t.Run("nil message", func(t *testing.T) {
err := rpc.ProcessSignedStateRootUpdateMessage(nil, &ignore)

assert.NotNil(t, err)
})

t.Run("nil signature", func(t *testing.T) {
err := rpc.ProcessSignedStateRootUpdateMessage(&messages.SignedStateRootUpdateMessage{
BlsSignature: bls.Signature{G1Point: nil},
}, &ignore)

assert.NotNil(t, err)
})
}
func TestProcessSignedOperatorSetUpdateMessage_InvalidParams(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

agg := mocks.NewMockRpcAggregatorer(mockCtrl)
logger, _ := logging.NewZapLogger(logging.Development)

rpc := NewRpcServer("localhost:8080", agg, logger)

var ignore bool
t.Run("nil message", func(t *testing.T) {
err := rpc.ProcessSignedOperatorSetUpdateMessage(nil, &ignore)

assert.NotNil(t, err)
})

t.Run("nil signature", func(t *testing.T) {
err := rpc.ProcessSignedOperatorSetUpdateMessage(&messages.SignedOperatorSetUpdateMessage{
BlsSignature: bls.Signature{G1Point: nil},
}, &ignore)

assert.NotNil(t, err)
})

t.Run("nil operator pubkey", func(t *testing.T) {
err := rpc.ProcessSignedOperatorSetUpdateMessage(&messages.SignedOperatorSetUpdateMessage{
BlsSignature: *bls.NewZeroSignature(),
Message: messages.OperatorSetUpdateMessage{Operators: []types.RollupOperator{
{Pubkey: nil, Weight: big.NewInt(0)},
}},
}, &ignore)

assert.NotNil(t, err)
})

t.Run("nil operator weight", func(t *testing.T) {
err := rpc.ProcessSignedOperatorSetUpdateMessage(&messages.SignedOperatorSetUpdateMessage{
BlsSignature: *bls.NewZeroSignature(),
Message: messages.OperatorSetUpdateMessage{Operators: []types.RollupOperator{
{Pubkey: bls.NewZeroG1Point(), Weight: nil},
}},
}, &ignore)

assert.NotNil(t, err)
})
}
14 changes: 14 additions & 0 deletions core/types/messages/checkpoint_task.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package messages

import (
"errors"

"github.com/Layr-Labs/eigensdk-go/crypto/bls"
eigentypes "github.com/Layr-Labs/eigensdk-go/types"
"github.com/ethereum/go-ethereum/accounts/abi"
Expand All @@ -23,6 +25,18 @@ type SignedCheckpointTaskResponse struct {
OperatorId eigentypes.OperatorId
}

func (s *SignedCheckpointTaskResponse) IsValid() error {
if s == nil {
return errors.New("SignedCheckpointTaskResponse is nil")
}

if s.BlsSignature.G1Point == nil {
return errors.New("BlsSignature.G1Point is nil")
}

return nil
}

type CheckpointMessages struct {
StateRootUpdateMessages []StateRootUpdateMessage
StateRootUpdateMessageAggregations []MessageBlsAggregation
Expand Down
23 changes: 23 additions & 0 deletions core/types/messages/operator_set_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package messages

import (
"encoding/binary"
"errors"
"math/big"

"github.com/Layr-Labs/eigensdk-go/crypto/bls"
Expand All @@ -25,6 +26,28 @@ type SignedOperatorSetUpdateMessage struct {
OperatorId eigentypes.OperatorId
}

func (s *SignedOperatorSetUpdateMessage) IsValid() error {
if s == nil {
return errors.New("SignedOperatorSetUpdateMessage is nil")
}

if s.BlsSignature.G1Point == nil {
return errors.New("BlsSignature.G1Point is nil")
}

for _, operator := range s.Message.Operators {
if operator.Pubkey == nil {
return errors.New("Operator.Pubkey is nil")
}

if operator.Weight == nil {
return errors.New("Operator.Weight is nil")
}
}

return nil
}

func NewOperatorSetUpdateMessageFromBinding(binding registryrollup.OperatorSetUpdateMessage) OperatorSetUpdateMessage {
operators := make([]coretypes.RollupOperator, 0, len(binding.Operators))

Expand Down
Loading
Loading