Skip to content

Commit 3d0611c

Browse files
Remove error from SDK AppGossip handler (#2252)
Signed-off-by: Joshua Kim <20001595+joshua-kim@users.noreply.github.com> Co-authored-by: Stephen Buttolph <stephen@avalabs.org>
1 parent 01a1bbe commit 3d0611c

File tree

8 files changed

+91
-50
lines changed

8 files changed

+91
-50
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/DataDog/zstd v1.5.2
1212
github.com/Microsoft/go-winio v0.5.2
1313
github.com/NYTimes/gziphandler v1.1.1
14-
github.com/ava-labs/coreth v0.12.8-rc.1
14+
github.com/ava-labs/coreth v0.12.9-rc.0
1515
github.com/ava-labs/ledger-avalanche/go v0.0.0-20231102202641-ae2ebdaeac34
1616
github.com/btcsuite/btcd/btcutil v1.1.3
1717
github.com/cockroachdb/pebble v0.0.0-20230209160836-829675f94811

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah
6666
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
6767
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
6868
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
69-
github.com/ava-labs/coreth v0.12.8-rc.1 h1:tvJcxQTQzxIQqx8TnrxdyMhZYbdsMaiy6AEiOyjvaa4=
70-
github.com/ava-labs/coreth v0.12.8-rc.1/go.mod h1:GBH5SxHZdScSp95IijDs9+Gxw/QDIWvfoLKiJMNYLsE=
69+
github.com/ava-labs/coreth v0.12.9-rc.0 h1:Xvk/iJTY2MSBkkiOs9Eo92nxd67VXzRjaC/WmQXRIb0=
70+
github.com/ava-labs/coreth v0.12.9-rc.0/go.mod h1:rECKQfGFDeodrwGPlJSvFUJDbVr30jSMIVjQLi6pNX4=
7171
github.com/ava-labs/ledger-avalanche/go v0.0.0-20231102202641-ae2ebdaeac34 h1:mg9Uw6oZFJKytJxgxnl3uxZOs/SB8CVHg6Io4Tf99Zc=
7272
github.com/ava-labs/ledger-avalanche/go v0.0.0-20231102202641-ae2ebdaeac34/go.mod h1:pJxaT9bUgeRNVmNRgtCHb7sFDIRKy7CzTQVi8gGNT6g=
7373
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=

network/p2p/handler.go

+10-21
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type Handler interface {
3030
ctx context.Context,
3131
nodeID ids.NodeID,
3232
gossipBytes []byte,
33-
) error
33+
)
3434
// AppRequest is called when handling an AppRequest message.
3535
// Returns the bytes for the response corresponding to [requestBytes]
3636
AppRequest(
@@ -53,9 +53,7 @@ type Handler interface {
5353
// NoOpHandler drops all messages
5454
type NoOpHandler struct{}
5555

56-
func (NoOpHandler) AppGossip(context.Context, ids.NodeID, []byte) error {
57-
return nil
58-
}
56+
func (NoOpHandler) AppGossip(context.Context, ids.NodeID, []byte) {}
5957

6058
func (NoOpHandler) AppRequest(context.Context, ids.NodeID, time.Time, []byte) ([]byte, error) {
6159
return nil, nil
@@ -69,14 +67,16 @@ func (NoOpHandler) CrossChainAppRequest(context.Context, ids.ID, time.Time, []by
6967
type ValidatorHandler struct {
7068
Handler
7169
ValidatorSet ValidatorSet
70+
Log logging.Logger
7271
}
7372

74-
func (v ValidatorHandler) AppGossip(ctx context.Context, nodeID ids.NodeID, gossipBytes []byte) error {
73+
func (v ValidatorHandler) AppGossip(ctx context.Context, nodeID ids.NodeID, gossipBytes []byte) {
7574
if !v.ValidatorSet.Has(ctx, nodeID) {
76-
return ErrNotValidator
75+
v.Log.Debug("dropping message", zap.Stringer("nodeID", nodeID))
76+
return
7777
}
7878

79-
return v.Handler.AppGossip(ctx, nodeID, gossipBytes)
79+
v.Handler.AppGossip(ctx, nodeID, gossipBytes)
8080
}
8181

8282
func (v ValidatorHandler) AppRequest(ctx context.Context, nodeID ids.NodeID, deadline time.Time, requestBytes []byte) ([]byte, error) {
@@ -89,15 +89,15 @@ func (v ValidatorHandler) AppRequest(ctx context.Context, nodeID ids.NodeID, dea
8989

9090
// responder automatically sends the response for a given request
9191
type responder struct {
92+
Handler
9293
handlerID uint64
93-
handler Handler
9494
log logging.Logger
9595
sender common.AppSender
9696
}
9797

9898
// AppRequest calls the underlying handler and sends back the response to nodeID
9999
func (r *responder) AppRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, deadline time.Time, request []byte) error {
100-
appResponse, err := r.handler.AppRequest(ctx, nodeID, deadline, request)
100+
appResponse, err := r.Handler.AppRequest(ctx, nodeID, deadline, request)
101101
if err != nil {
102102
r.log.Debug("failed to handle message",
103103
zap.Stringer("messageOp", message.AppRequestOp),
@@ -113,21 +113,10 @@ func (r *responder) AppRequest(ctx context.Context, nodeID ids.NodeID, requestID
113113
return r.sender.SendAppResponse(ctx, nodeID, requestID, appResponse)
114114
}
115115

116-
func (r *responder) AppGossip(ctx context.Context, nodeID ids.NodeID, msg []byte) {
117-
if err := r.handler.AppGossip(ctx, nodeID, msg); err != nil {
118-
r.log.Debug("failed to handle message",
119-
zap.Stringer("messageOp", message.AppGossipOp),
120-
zap.Stringer("nodeID", nodeID),
121-
zap.Uint64("handlerID", r.handlerID),
122-
zap.Binary("message", msg),
123-
)
124-
}
125-
}
126-
127116
// CrossChainAppRequest calls the underlying handler and sends back the response
128117
// to chainID
129118
func (r *responder) CrossChainAppRequest(ctx context.Context, chainID ids.ID, requestID uint32, deadline time.Time, request []byte) error {
130-
appResponse, err := r.handler.CrossChainAppRequest(ctx, chainID, deadline, request)
119+
appResponse, err := r.Handler.CrossChainAppRequest(ctx, chainID, deadline, request)
131120
if err != nil {
132121
r.log.Debug("failed to handle message",
133122
zap.Stringer("messageOp", message.CrossChainAppRequestOp),

network/p2p/handler_test.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/stretchr/testify/require"
1212

1313
"github.com/ava-labs/avalanchego/ids"
14+
"github.com/ava-labs/avalanchego/utils/logging"
1415
"github.com/ava-labs/avalanchego/utils/set"
1516
)
1617

@@ -32,34 +33,40 @@ func TestValidatorHandlerAppGossip(t *testing.T) {
3233
name string
3334
validatorSet ValidatorSet
3435
nodeID ids.NodeID
35-
expected error
36+
expected bool
3637
}{
3738
{
3839
name: "message dropped",
3940
validatorSet: testValidatorSet{},
4041
nodeID: nodeID,
41-
expected: ErrNotValidator,
4242
},
4343
{
4444
name: "message handled",
4545
validatorSet: testValidatorSet{
4646
validators: validatorSet,
4747
},
48-
nodeID: nodeID,
48+
nodeID: nodeID,
49+
expected: true,
4950
},
5051
}
5152

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

57+
called := false
5658
handler := ValidatorHandler{
57-
Handler: NoOpHandler{},
59+
Handler: testHandler{
60+
appGossipF: func(context.Context, ids.NodeID, []byte) {
61+
called = true
62+
},
63+
},
5864
ValidatorSet: tt.validatorSet,
65+
Log: logging.NoLog{},
5966
}
6067

61-
err := handler.AppGossip(context.Background(), tt.nodeID, []byte("foobar"))
62-
require.ErrorIs(err, tt.expected)
68+
handler.AppGossip(context.Background(), tt.nodeID, []byte("foobar"))
69+
require.Equal(tt.expected, called)
6370
})
6471
}
6572
}
@@ -96,6 +103,7 @@ func TestValidatorHandlerAppRequest(t *testing.T) {
96103
handler := ValidatorHandler{
97104
Handler: NoOpHandler{},
98105
ValidatorSet: tt.validatorSet,
106+
Log: logging.NoLog{},
99107
}
100108

101109
_, err := handler.AppRequest(context.Background(), tt.nodeID, time.Time{}, []byte("foobar"))

network/p2p/mocks/mock_handler.go

+2-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

network/p2p/router.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ func (r *Router) RegisterAppProtocol(handlerID uint64, handler Handler, nodeSamp
174174

175175
r.handlers[handlerID] = &meteredHandler{
176176
responder: &responder{
177+
Handler: handler,
177178
handlerID: handlerID,
178-
handler: handler,
179179
log: r.log,
180180
sender: r.sender,
181181
},

network/p2p/throttler_handler.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import (
99
"fmt"
1010
"time"
1111

12+
"go.uber.org/zap"
13+
1214
"github.com/ava-labs/avalanchego/ids"
15+
"github.com/ava-labs/avalanchego/utils/logging"
1316
)
1417

1518
var (
@@ -20,14 +23,16 @@ var (
2023
type ThrottlerHandler struct {
2124
Handler
2225
Throttler Throttler
26+
Log logging.Logger
2327
}
2428

25-
func (t ThrottlerHandler) AppGossip(ctx context.Context, nodeID ids.NodeID, gossipBytes []byte) error {
29+
func (t ThrottlerHandler) AppGossip(ctx context.Context, nodeID ids.NodeID, gossipBytes []byte) {
2630
if !t.Throttler.Handle(nodeID) {
27-
return fmt.Errorf("dropping message from %s: %w", nodeID, ErrThrottled)
31+
t.Log.Debug("dropping message", zap.Stringer("nodeID", nodeID))
32+
return
2833
}
2934

30-
return t.Handler.AppGossip(ctx, nodeID, gossipBytes)
35+
t.Handler.AppGossip(ctx, nodeID, gossipBytes)
3136
}
3237

3338
func (t ThrottlerHandler) AppRequest(ctx context.Context, nodeID ids.NodeID, deadline time.Time, requestBytes []byte) ([]byte, error) {

network/p2p/throttler_handler_test.go

+53-12
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,44 @@ import (
1111
"github.com/stretchr/testify/require"
1212

1313
"github.com/ava-labs/avalanchego/ids"
14+
"github.com/ava-labs/avalanchego/utils/logging"
1415
)
1516

17+
var _ Handler = (*testHandler)(nil)
18+
1619
func TestThrottlerHandlerAppGossip(t *testing.T) {
1720
tests := []struct {
18-
name string
19-
Throttler Throttler
20-
expectedErr error
21+
name string
22+
Throttler Throttler
23+
expected bool
2124
}{
2225
{
23-
name: "throttled",
26+
name: "not throttled",
2427
Throttler: NewSlidingWindowThrottler(time.Second, 1),
28+
expected: true,
2529
},
2630
{
27-
name: "throttler errors",
28-
Throttler: NewSlidingWindowThrottler(time.Second, 0),
29-
expectedErr: ErrThrottled,
31+
name: "throttled",
32+
Throttler: NewSlidingWindowThrottler(time.Second, 0),
3033
},
3134
}
3235
for _, tt := range tests {
3336
t.Run(tt.name, func(t *testing.T) {
3437
require := require.New(t)
3538

39+
called := false
3640
handler := ThrottlerHandler{
37-
Handler: NoOpHandler{},
41+
Handler: testHandler{
42+
appGossipF: func(context.Context, ids.NodeID, []byte) {
43+
called = true
44+
},
45+
},
3846
Throttler: tt.Throttler,
47+
Log: logging.NoLog{},
3948
}
40-
err := handler.AppGossip(context.Background(), ids.GenerateTestNodeID(), []byte("foobar"))
41-
require.ErrorIs(err, tt.expectedErr)
49+
50+
handler.AppGossip(context.Background(), ids.GenerateTestNodeID(), []byte("foobar"))
51+
require.Equal(tt.expected, called)
4252
})
4353
}
4454
}
@@ -50,11 +60,11 @@ func TestThrottlerHandlerAppRequest(t *testing.T) {
5060
expectedErr error
5161
}{
5262
{
53-
name: "throttled",
63+
name: "not throttled",
5464
Throttler: NewSlidingWindowThrottler(time.Second, 1),
5565
},
5666
{
57-
name: "throttler errors",
67+
name: "throttled",
5868
Throttler: NewSlidingWindowThrottler(time.Second, 0),
5969
expectedErr: ErrThrottled,
6070
},
@@ -66,9 +76,40 @@ func TestThrottlerHandlerAppRequest(t *testing.T) {
6676
handler := ThrottlerHandler{
6777
Handler: NoOpHandler{},
6878
Throttler: tt.Throttler,
79+
Log: logging.NoLog{},
6980
}
7081
_, err := handler.AppRequest(context.Background(), ids.GenerateTestNodeID(), time.Time{}, []byte("foobar"))
7182
require.ErrorIs(err, tt.expectedErr)
7283
})
7384
}
7485
}
86+
87+
type testHandler struct {
88+
appGossipF func(ctx context.Context, nodeID ids.NodeID, gossipBytes []byte)
89+
appRequestF func(ctx context.Context, nodeID ids.NodeID, deadline time.Time, requestBytes []byte) ([]byte, error)
90+
crossChainAppRequestF func(ctx context.Context, chainID ids.ID, deadline time.Time, requestBytes []byte) ([]byte, error)
91+
}
92+
93+
func (t testHandler) AppGossip(ctx context.Context, nodeID ids.NodeID, gossipBytes []byte) {
94+
if t.appGossipF == nil {
95+
return
96+
}
97+
98+
t.appGossipF(ctx, nodeID, gossipBytes)
99+
}
100+
101+
func (t testHandler) AppRequest(ctx context.Context, nodeID ids.NodeID, deadline time.Time, requestBytes []byte) ([]byte, error) {
102+
if t.appRequestF == nil {
103+
return nil, nil
104+
}
105+
106+
return t.appRequestF(ctx, nodeID, deadline, requestBytes)
107+
}
108+
109+
func (t testHandler) CrossChainAppRequest(ctx context.Context, chainID ids.ID, deadline time.Time, requestBytes []byte) ([]byte, error) {
110+
if t.crossChainAppRequestF == nil {
111+
return nil, nil
112+
}
113+
114+
return t.crossChainAppRequestF(ctx, chainID, deadline, requestBytes)
115+
}

0 commit comments

Comments
 (0)