Skip to content

Commit 43afaf4

Browse files
ceyonurCopilot
andauthored
remove request parse check in network forwarding (#977) (#1593)
Signed-off-by: Ceyhun Onur <[email protected]> Signed-off-by: Ceyhun Onur <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 5f742d0 commit 43afaf4

File tree

5 files changed

+41
-24
lines changed

5 files changed

+41
-24
lines changed

peer/network.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,8 @@ func (n *network) AppRequest(ctx context.Context, nodeID ids.NodeID, requestID u
228228

229229
log.Debug("received AppRequest from node", "nodeID", nodeID, "requestID", requestID, "requestLen", len(request))
230230

231-
var req message.Request
232-
if _, err := n.codec.Unmarshal(request, &req); err != nil {
233-
log.Debug("forwarding AppRequest to SDK network", "nodeID", nodeID, "requestID", requestID, "requestLen", len(request), "err", err)
231+
if !IsNetworkRequest(requestID) {
232+
log.Debug("forwarding AppRequest to SDK network", "nodeID", nodeID, "requestID", requestID, "requestLen", len(request))
234233
return n.p2pNetwork.AppRequest(ctx, nodeID, requestID, deadline, request)
235234
}
236235

@@ -240,6 +239,12 @@ func (n *network) AppRequest(ctx context.Context, nodeID ids.NodeID, requestID u
240239
return nil
241240
}
242241

242+
var req message.Request
243+
if _, err := n.codec.Unmarshal(request, &req); err != nil {
244+
log.Debug("failed to unmarshal AppRequest", "nodeID", nodeID, "requestID", requestID, "err", err)
245+
return nil
246+
}
247+
243248
log.Debug("processing incoming request", "nodeID", nodeID, "requestID", requestID, "req", req)
244249
// We make a new context here because we don't want to cancel the context
245250
// passed into n.AppSender.SendAppResponse below
@@ -435,3 +440,10 @@ func (n *network) nextRequestID() uint32 {
435440

436441
return next
437442
}
443+
444+
// IsNetworkRequest checks if the given requestID is a request for this network handler (even-numbered requestIDs)
445+
// SDK requests are odd-numbered requestIDs
446+
// (see invariant: https://github.com/ava-labs/avalanchego/blob/v1.13.0/network/p2p/router.go#L83)
447+
func IsNetworkRequest(requestID uint32) bool {
448+
return requestID%2 == 0
449+
}

peer/network_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/stretchr/testify/assert"
2323
"github.com/stretchr/testify/require"
2424

25+
"github.com/ava-labs/subnet-evm/peer/peertest"
2526
"github.com/ava-labs/subnet-evm/plugin/evm/message"
2627

2728
"github.com/ava-labs/avalanchego/codec"
@@ -477,8 +478,7 @@ func TestOnRequestHonoursDeadline(t *testing.T) {
477478

478479
requestHandler.response, err = marshalStruct(codecManager, TestMessage{Message: "hi there"})
479480
assert.NoError(t, err)
480-
err = net.AppRequest(context.Background(), nodeID, 1, time.Now().Add(1*time.Millisecond), requestBytes)
481-
assert.NoError(t, err)
481+
assert.NoError(t, net.AppRequest(context.Background(), nodeID, 0, time.Now().Add(1*time.Millisecond), requestBytes))
482482
// ensure the handler didn't get called (as peer.Network would've dropped the request)
483483
assert.EqualValues(t, requestHandler.calls, 0)
484484

@@ -492,7 +492,7 @@ func TestOnRequestHonoursDeadline(t *testing.T) {
492492
func TestHandleInvalidMessages(t *testing.T) {
493493
codecManager := buildCodec(t, HelloGossip{}, TestMessage{})
494494
nodeID := ids.GenerateTestNodeID()
495-
requestID := uint32(1)
495+
requestID := peertest.TestSDKRequestID
496496
sender := &enginetest.Sender{
497497
SendAppErrorF: func(context.Context, ids.NodeID, uint32, int32, string) error {
498498
return nil
@@ -544,7 +544,7 @@ func TestHandleInvalidMessages(t *testing.T) {
544544
func TestNetworkPropagatesRequestHandlerError(t *testing.T) {
545545
codecManager := buildCodec(t, TestMessage{})
546546
nodeID := ids.GenerateTestNodeID()
547-
requestID := uint32(1)
547+
requestID := peertest.TestPeerRequestID
548548
sender := testAppSender{}
549549

550550
p2pNetwork, err := p2p.NewNetwork(logging.NoLog{}, nil, prometheus.NewRegistry(), "")
@@ -585,6 +585,7 @@ func TestNetworkRouting(t *testing.T) {
585585
},
586586
}
587587
protocol := 0
588+
requestID := peertest.TestSDKRequestID
588589
handler := &testSDKHandler{}
589590
p2pNetwork, err := p2p.NewNetwork(logging.NoLog{}, sender, prometheus.NewRegistry(), "")
590591
require.NoError(err)
@@ -595,14 +596,14 @@ func TestNetworkRouting(t *testing.T) {
595596

596597
nodeID := ids.GenerateTestNodeID()
597598
foobar := append([]byte{byte(protocol)}, []byte("foobar")...)
598-
err = network.AppRequest(context.Background(), nodeID, 0, time.Time{}, foobar)
599-
require.NoError(err)
599+
// forward it to the sdk handler
600+
require.NoError(network.AppRequest(context.Background(), nodeID, requestID, time.Now().Add(5*time.Second), foobar))
600601
require.True(handler.appRequested)
601602

602-
err = network.AppResponse(context.Background(), ids.GenerateTestNodeID(), 0, foobar)
603+
err = network.AppResponse(context.Background(), ids.GenerateTestNodeID(), requestID, foobar)
603604
require.ErrorIs(err, p2p.ErrUnrequestedResponse)
604605

605-
err = network.AppRequestFailed(context.Background(), nodeID, 0, common.ErrTimeout)
606+
err = network.AppRequestFailed(context.Background(), nodeID, requestID, common.ErrTimeout)
606607
require.ErrorIs(err, p2p.ErrUnrequestedResponse)
607608
}
608609

peer/peertest/request_id.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// (c) 2019-2022, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package peertest
5+
6+
const (
7+
// TestSDKRequestID is the request ID for the SDK request, which is odd-numbered.
8+
// See peer.IsNetworkRequest for more details.
9+
TestSDKRequestID uint32 = 1
10+
11+
// TestPeerRequestID is the request ID for the peer request, which is even-numbered.
12+
// See peer.IsNetworkRequest for more details.
13+
TestPeerRequestID uint32 = 0
14+
)

plugin/evm/message/request.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,6 @@ type Request interface {
2121
Handle(ctx context.Context, nodeID ids.NodeID, requestID uint32, handler RequestHandler) ([]byte, error)
2222
}
2323

24-
// BytesToRequest unmarshals the given requestBytes into Request object
25-
func BytesToRequest(codec codec.Manager, requestBytes []byte) (Request, error) {
26-
var request Request
27-
if _, err := codec.Unmarshal(requestBytes, &request); err != nil {
28-
return nil, err
29-
}
30-
return request, nil
31-
}
32-
3324
// RequestToBytes marshals the given request object into bytes
3425
func RequestToBytes(codec codec.Manager, request Request) ([]byte, error) {
3526
return codec.Marshal(Version, &request)

plugin/evm/vm_warp_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/ava-labs/subnet-evm/eth/tracers"
3636
"github.com/ava-labs/subnet-evm/params"
3737
"github.com/ava-labs/subnet-evm/params/extras"
38+
"github.com/ava-labs/subnet-evm/peer/peertest"
3839
customheader "github.com/ava-labs/subnet-evm/plugin/evm/header"
3940
"github.com/ava-labs/subnet-evm/plugin/evm/message"
4041
"github.com/ava-labs/subnet-evm/precompile/contract"
@@ -798,8 +799,7 @@ func TestMessageSignatureRequestsToVM(t *testing.T) {
798799

799800
// Send the app request and make sure we called SendAppResponseFn
800801
deadline := time.Now().Add(60 * time.Second)
801-
err = vm.Network.AppRequest(context.Background(), ids.GenerateTestNodeID(), 1, deadline, requestBytes)
802-
require.NoError(t, err)
802+
require.NoError(t, vm.Network.AppRequest(context.Background(), ids.GenerateTestNodeID(), peertest.TestPeerRequestID, deadline, requestBytes))
803803
require.True(t, calledSendAppResponseFn)
804804
})
805805
}
@@ -856,8 +856,7 @@ func TestBlockSignatureRequestsToVM(t *testing.T) {
856856

857857
// Send the app request and make sure we called SendAppResponseFn
858858
deadline := time.Now().Add(60 * time.Second)
859-
err = vm.Network.AppRequest(context.Background(), ids.GenerateTestNodeID(), 1, deadline, requestBytes)
860-
require.NoError(t, err)
859+
require.NoError(t, vm.Network.AppRequest(context.Background(), ids.GenerateTestNodeID(), peertest.TestPeerRequestID, deadline, requestBytes))
861860
require.True(t, calledSendAppResponseFn)
862861
})
863862
}

0 commit comments

Comments
 (0)