Skip to content

Commit 4158e5b

Browse files
committed
migrate x/sync to p2p
Signed-off-by: Joshua Kim <20001595+joshua-kim@users.noreply.github.com>
1 parent 8974ed8 commit 4158e5b

17 files changed

+1618
-3451
lines changed

network/p2p/p2ptest/client.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package p2ptest
5+
6+
import (
7+
"context"
8+
"testing"
9+
"time"
10+
11+
"github.com/prometheus/client_golang/prometheus"
12+
"github.com/stretchr/testify/require"
13+
14+
"github.com/ava-labs/avalanchego/ids"
15+
"github.com/ava-labs/avalanchego/network/p2p"
16+
"github.com/ava-labs/avalanchego/snow/engine/common"
17+
"github.com/ava-labs/avalanchego/snow/engine/enginetest"
18+
"github.com/ava-labs/avalanchego/utils/logging"
19+
"github.com/ava-labs/avalanchego/utils/set"
20+
)
21+
22+
// NewClient generates a client-server pair and returns the client used to
23+
// communicate with a server with the specified handler
24+
func NewClient(t *testing.T, rootCtx context.Context, handler p2p.Handler) *p2p.Client {
25+
clientSender := &enginetest.Sender{}
26+
serverSender := &enginetest.Sender{}
27+
28+
clientNodeID := ids.GenerateTestNodeID()
29+
clientNetwork, err := p2p.NewNetwork(logging.NoLog{}, clientSender, prometheus.NewRegistry(), "")
30+
require.NoError(t, err)
31+
32+
serverNodeID := ids.GenerateTestNodeID()
33+
serverNetwork, err := p2p.NewNetwork(logging.NoLog{}, serverSender, prometheus.NewRegistry(), "")
34+
require.NoError(t, err)
35+
36+
clientSender.SendAppGossipF = func(ctx context.Context, _ common.SendConfig, gossipBytes []byte) error {
37+
go func() {
38+
require.NoError(t, serverNetwork.AppGossip(ctx, clientNodeID, gossipBytes))
39+
}()
40+
41+
return nil
42+
}
43+
44+
clientSender.SendAppRequestF = func(ctx context.Context, _ set.Set[ids.NodeID], requestID uint32, requestBytes []byte) error {
45+
// Send the request asynchronously to avoid deadlock when the server
46+
// sends the response back to the client
47+
go func() {
48+
require.NoError(t, serverNetwork.AppRequest(ctx, clientNodeID, requestID, time.Time{}, requestBytes))
49+
}()
50+
51+
return nil
52+
}
53+
54+
serverSender.SendAppResponseF = func(ctx context.Context, _ ids.NodeID, requestID uint32, responseBytes []byte) error {
55+
go func() {
56+
require.NoError(t, clientNetwork.AppResponse(ctx, serverNodeID, requestID, responseBytes))
57+
}()
58+
59+
return nil
60+
}
61+
62+
serverSender.SendAppErrorF = func(ctx context.Context, _ ids.NodeID, requestID uint32, errorCode int32, errorMessage string) error {
63+
go func() {
64+
require.NoError(t, clientNetwork.AppRequestFailed(ctx, serverNodeID, requestID, &common.AppError{
65+
Code: errorCode,
66+
Message: errorMessage,
67+
}))
68+
}()
69+
70+
return nil
71+
}
72+
73+
require.NoError(t, clientNetwork.Connected(rootCtx, clientNodeID, nil))
74+
require.NoError(t, clientNetwork.Connected(rootCtx, serverNodeID, nil))
75+
require.NoError(t, serverNetwork.Connected(rootCtx, clientNodeID, nil))
76+
require.NoError(t, serverNetwork.Connected(rootCtx, serverNodeID, nil))
77+
78+
require.NoError(t, serverNetwork.AddHandler(0, handler))
79+
return clientNetwork.NewClient(0)
80+
}

network/p2p/p2ptest/client_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package p2ptest
5+
6+
import (
7+
"context"
8+
"testing"
9+
"time"
10+
11+
"github.com/stretchr/testify/require"
12+
13+
"github.com/ava-labs/avalanchego/ids"
14+
"github.com/ava-labs/avalanchego/network/p2p"
15+
"github.com/ava-labs/avalanchego/snow/engine/common"
16+
"github.com/ava-labs/avalanchego/utils/set"
17+
)
18+
19+
func TestNewClient_AppGossip(t *testing.T) {
20+
require := require.New(t)
21+
ctx := context.Background()
22+
23+
appGossipChan := make(chan struct{})
24+
testHandler := p2p.TestHandler{
25+
AppGossipF: func(context.Context, ids.NodeID, []byte) {
26+
close(appGossipChan)
27+
},
28+
}
29+
30+
client := NewClient(t, ctx, testHandler)
31+
require.NoError(client.AppGossip(ctx, common.SendConfig{}, []byte("foobar")))
32+
<-appGossipChan
33+
}
34+
35+
func TestNewClient_AppRequest(t *testing.T) {
36+
tests := []struct {
37+
name string
38+
appResponse []byte
39+
appErr error
40+
appRequestF func(ctx context.Context, client *p2p.Client, onResponse p2p.AppResponseCallback) error
41+
}{
42+
{
43+
name: "AppRequest - response",
44+
appResponse: []byte("foobar"),
45+
appRequestF: func(ctx context.Context, client *p2p.Client, onResponse p2p.AppResponseCallback) error {
46+
return client.AppRequest(ctx, set.Of(ids.GenerateTestNodeID()), []byte("foo"), onResponse)
47+
},
48+
},
49+
{
50+
name: "AppRequest - error",
51+
appErr: &common.AppError{
52+
Code: 123,
53+
Message: "foobar",
54+
},
55+
appRequestF: func(ctx context.Context, client *p2p.Client, onResponse p2p.AppResponseCallback) error {
56+
return client.AppRequest(ctx, set.Of(ids.GenerateTestNodeID()), []byte("foo"), onResponse)
57+
},
58+
},
59+
{
60+
name: "AppRequestAny - response",
61+
appResponse: []byte("foobar"),
62+
appRequestF: func(ctx context.Context, client *p2p.Client, onResponse p2p.AppResponseCallback) error {
63+
return client.AppRequestAny(ctx, []byte("foo"), onResponse)
64+
},
65+
},
66+
{
67+
name: "AppRequestAny - error",
68+
appErr: &common.AppError{
69+
Code: 123,
70+
Message: "foobar",
71+
},
72+
appRequestF: func(ctx context.Context, client *p2p.Client, onResponse p2p.AppResponseCallback) error {
73+
return client.AppRequestAny(ctx, []byte("foo"), onResponse)
74+
},
75+
},
76+
}
77+
78+
for _, tt := range tests {
79+
t.Run(tt.name, func(t *testing.T) {
80+
require := require.New(t)
81+
ctx := context.Background()
82+
83+
appRequestChan := make(chan struct{})
84+
testHandler := p2p.TestHandler{
85+
AppRequestF: func(context.Context, ids.NodeID, time.Time, []byte) ([]byte, *common.AppError) {
86+
if tt.appErr != nil {
87+
return nil, &common.AppError{
88+
Code: 123,
89+
Message: tt.appErr.Error(),
90+
}
91+
}
92+
93+
return tt.appResponse, nil
94+
},
95+
}
96+
97+
client := NewClient(t, ctx, testHandler)
98+
require.NoError(tt.appRequestF(
99+
ctx,
100+
client,
101+
func(_ context.Context, _ ids.NodeID, responseBytes []byte, err error) {
102+
require.ErrorIs(err, tt.appErr)
103+
require.Equal(tt.appResponse, responseBytes)
104+
close(appRequestChan)
105+
},
106+
))
107+
<-appRequestChan
108+
})
109+
}
110+
}

0 commit comments

Comments
 (0)