2019-08-22 23:02:46 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-10-07 07:24:51 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/network"
|
2023-03-17 18:52:56 +00:00
|
|
|
testp2p "github.com/prysmaticlabs/prysm/v4/beacon-chain/p2p/testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/config/params"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/util"
|
2021-05-17 18:32:04 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2019-08-22 23:02:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestService_Send(t *testing.T) {
|
2022-05-20 07:16:53 +00:00
|
|
|
params.SetupTestConfigCleanup(t)
|
2019-08-22 23:02:46 +00:00
|
|
|
p1 := testp2p.NewTestP2P(t)
|
|
|
|
p2 := testp2p.NewTestP2P(t)
|
|
|
|
p1.Connect(p2)
|
|
|
|
|
|
|
|
svc := &Service{
|
2020-06-18 03:53:46 +00:00
|
|
|
host: p1.BHost,
|
2020-07-03 03:24:30 +00:00
|
|
|
cfg: &Config{},
|
2019-08-22 23:02:46 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 21:45:17 +00:00
|
|
|
msg := ðpb.Fork{
|
2020-10-14 07:55:28 +00:00
|
|
|
CurrentVersion: []byte("fooo"),
|
|
|
|
PreviousVersion: []byte("barr"),
|
|
|
|
Epoch: 55,
|
2019-08-22 23:02:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register external listener which will repeat the message back.
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
2020-07-13 01:20:53 +00:00
|
|
|
topic := "/testing/1"
|
2021-07-29 21:45:17 +00:00
|
|
|
RPCTopicMappings[topic] = new(ethpb.Fork)
|
2020-07-13 01:20:53 +00:00
|
|
|
defer func() {
|
|
|
|
delete(RPCTopicMappings, topic)
|
|
|
|
}()
|
|
|
|
p2.SetStreamHandler(topic+"/ssz_snappy", func(stream network.Stream) {
|
2021-07-29 21:45:17 +00:00
|
|
|
rcvd := ðpb.Fork{}
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, svc.Encoding().DecodeWithMaxLength(stream, rcvd))
|
|
|
|
_, err := svc.Encoding().EncodeWithMaxLength(stream, rcvd)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NoError(t, stream.Close())
|
2020-04-14 20:27:03 +00:00
|
|
|
wg.Done()
|
|
|
|
})
|
|
|
|
|
2020-06-18 03:53:46 +00:00
|
|
|
stream, err := svc.Send(context.Background(), msg, "/testing/1", p2.BHost.ID())
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2019-08-22 23:02:46 +00:00
|
|
|
|
2021-09-23 18:53:46 +00:00
|
|
|
util.WaitTimeout(&wg, 1*time.Second)
|
2019-08-22 23:02:46 +00:00
|
|
|
|
2021-07-29 21:45:17 +00:00
|
|
|
rcvd := ðpb.Fork{}
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, svc.Encoding().DecodeWithMaxLength(stream, rcvd))
|
2019-08-22 23:02:46 +00:00
|
|
|
if !proto.Equal(rcvd, msg) {
|
|
|
|
t.Errorf("Expected identical message to be received. got %v want %v", rcvd, msg)
|
|
|
|
}
|
|
|
|
}
|