Skip to content

Implement SDK handler to drop messages from non-validators #1917

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

Merged
merged 7 commits into from
Aug 28, 2023
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
30 changes: 29 additions & 1 deletion network/p2p/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package p2p

import (
"context"
"errors"
"time"

"go.uber.org/zap"
Expand All @@ -15,7 +16,12 @@ import (
"github.com/ava-labs/avalanchego/utils/logging"
)

var _ Handler = (*NoOpHandler)(nil)
var (
ErrNotValidator = errors.New("not a validator")

_ Handler = (*NoOpHandler)(nil)
_ Handler = (*ValidatorHandler)(nil)
)

// Handler is the server-side logic for virtual machine application protocols.
type Handler interface {
Expand Down Expand Up @@ -58,6 +64,28 @@ func (NoOpHandler) CrossChainAppRequest(context.Context, ids.ID, time.Time, []by
return nil, nil
}

// ValidatorHandler drops messages from non-validators
type ValidatorHandler struct {
Handler
ValidatorSet ValidatorSet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we don't do this very well in the rest of the repo... but should we make this a minimal interface? Doesn't seem like we need any of the sampling functionality here.

}

func (v ValidatorHandler) AppGossip(ctx context.Context, nodeID ids.NodeID, gossipBytes []byte) error {
if !v.ValidatorSet.Has(ctx, nodeID) {
return ErrNotValidator
}

return v.Handler.AppGossip(ctx, nodeID, gossipBytes)
}

func (v ValidatorHandler) AppRequest(ctx context.Context, nodeID ids.NodeID, deadline time.Time, requestBytes []byte) ([]byte, error) {
if !v.ValidatorSet.Has(ctx, nodeID) {
return nil, ErrNotValidator
}

return v.Handler.AppRequest(ctx, nodeID, deadline, requestBytes)
}

// responder automatically sends the response for a given request
type responder struct {
handlerID uint64
Expand Down
105 changes: 105 additions & 0 deletions network/p2p/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package p2p

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/set"
)

var _ ValidatorSet = (*testValidatorSet)(nil)

type testValidatorSet struct {
validators set.Set[ids.NodeID]
}

func (t testValidatorSet) Has(_ context.Context, nodeID ids.NodeID) bool {
return t.validators.Contains(nodeID)
}

func TestValidatorHandlerAppGossip(t *testing.T) {
nodeID := ids.GenerateTestNodeID()
validatorSet := set.Of(nodeID)

tests := []struct {
name string
validatorSet ValidatorSet
nodeID ids.NodeID
expected error
}{
{
name: "message dropped",
validatorSet: testValidatorSet{},
nodeID: nodeID,
expected: ErrNotValidator,
},
{
name: "message handled",
validatorSet: testValidatorSet{
validators: validatorSet,
},
nodeID: nodeID,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require := require.New(t)

handler := ValidatorHandler{
Handler: NoOpHandler{},
ValidatorSet: tt.validatorSet,
}

err := handler.AppGossip(context.Background(), tt.nodeID, []byte("foobar"))
require.ErrorIs(err, tt.expected)
})
}
}

func TestValidatorHandlerAppRequest(t *testing.T) {
nodeID := ids.GenerateTestNodeID()
validatorSet := set.Of(nodeID)

tests := []struct {
name string
validatorSet ValidatorSet
nodeID ids.NodeID
expected error
}{
{
name: "message dropped",
validatorSet: testValidatorSet{},
nodeID: nodeID,
expected: ErrNotValidator,
},
{
name: "message handled",
validatorSet: testValidatorSet{
validators: validatorSet,
},
nodeID: nodeID,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require := require.New(t)

handler := ValidatorHandler{
Handler: NoOpHandler{},
ValidatorSet: tt.validatorSet,
}

_, err := handler.AppRequest(context.Background(), tt.nodeID, time.Time{}, []byte("foobar"))
require.ErrorIs(err, tt.expected)
})
}
}
9 changes: 8 additions & 1 deletion network/p2p/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ import (
"github.com/ava-labs/avalanchego/utils/set"
)

var _ NodeSampler = (*Validators)(nil)
var (
_ ValidatorSet = (*Validators)(nil)
_ NodeSampler = (*Validators)(nil)
)

type ValidatorSet interface {
Has(ctx context.Context, nodeID ids.NodeID) bool
}

func NewValidators(log logging.Logger, subnetID ids.ID, validators validators.State, maxValidatorSetStaleness time.Duration) *Validators {
return &Validators{
Expand Down