コード例 #1
0
ファイル: PeerSyncTest.cs プロジェクト: wyk125/AElf
        public void SyncNextAnnouncement_WithSyncingAndEmptyAnnoucements_EndsSync()
        {
            Mock <IMessageReader> reader         = new Mock <IMessageReader>();
            Mock <IMessageWriter> messageWritter = new Mock <IMessageWriter>();

            ECKeyPair kp = new KeyPairGenerator().Generate();
            Peer      p  = new Peer(new TcpClient(), reader.Object, messageWritter.Object, 1234, kp, 0);

            var(_, handshake) = NetworkTestHelpers.CreateKeyPairAndHandshake(1235);
            p.AuthentifyWith(handshake); // set "other peer as authentified"

            var annoucement = new Announce {
                Height = 10, Id = ByteString.CopyFromUtf8("FakeHash")
            };

            p.StashAnnouncement(annoucement);

            // trigger sync
            p.SyncNextAnnouncement();

            // end sync
            var movedToNext = p.SyncNextAnnouncement();

            // Check that sync ended
            Assert.False(movedToNext);

            Assert.Null(p.SyncedAnnouncement);
            Assert.False(p.AnyStashed);
        }
コード例 #2
0
        public void Start_AuthentificationNoTimout_ShouldThrowEvent()
        {
            int localPort  = 1234;
            int remotePort = 1235;

            Mock <IMessageReader> reader         = new Mock <IMessageReader>();
            Mock <IMessageWriter> messageWritter = new Mock <IMessageWriter>();

            ECKeyPair key = new KeyPairGenerator().Generate();
            Peer      p   = new Peer(new TcpClient(), reader.Object, messageWritter.Object, localPort, key, 0);

            p.AuthTimeout = 100;

            AuthFinishedArgs authFinishedArgs = null;

            p.AuthFinished += (sender, args) => {
                authFinishedArgs = args as AuthFinishedArgs;
            };

            p.Start();

            var(_, handshake) = NetworkTestHelpers.CreateKeyPairAndHandshake(remotePort);
            p.AuthentifyWith(handshake);

            Task.Delay(200).Wait();

            Assert.Null(authFinishedArgs);
            Assert.True(p.IsAuthentified);
        }
コード例 #3
0
        public void Start_Disposed_ThrowsInvalidOperationException()
        {
            int port = 1234;

            Peer p = new Peer(new TcpClient(), null, null, port, null, 0);

            var(_, handshake) = NetworkTestHelpers.CreateKeyPairAndHandshake(port);

            p.AuthentifyWith(handshake);

            var ex = Assert.Throws <InvalidOperationException>(() => p.Start());

            Assert.Equal("Cannot start an already authentified peer.", ex.Message);
        }
コード例 #4
0
ファイル: PeerSyncTest.cs プロジェクト: wyk125/AElf
        public void SyncNextAnnouncement_WithAnnouncement_TriggersSync()
        {
            Mock <IMessageReader> reader         = new Mock <IMessageReader>();
            Mock <IMessageWriter> messageWritter = new Mock <IMessageWriter>();

            ECKeyPair kp = new KeyPairGenerator().Generate();
            Peer      p  = new Peer(new TcpClient(), reader.Object, messageWritter.Object, 1234, kp, 0);

            var(_, handshake) = NetworkTestHelpers.CreateKeyPairAndHandshake(1235);
            p.AuthentifyWith(handshake); // set "other peer as authentified"

            BlockRequest req = null;

            messageWritter
            .Setup(w => w.EnqueueMessage(It.Is <Message>(m => m.Type == (int)AElfProtocolMsgType.RequestBlock), It.IsAny <Action <Message> >()))
            .Callback <Message, Action <Message> >((m, a) =>
            {
                if (m != null)
                {
                    req = BlockRequest.Parser.ParseFrom(m.Payload);
                }
            });

            var annoucement = new Announce {
                Height = 10, Id = ByteString.CopyFromUtf8("FakeHash")
            };

            p.StashAnnouncement(annoucement);

            // trigger sync
            var movedToNext = p.SyncNextAnnouncement();

            Assert.NotNull(req?.Id);

            // Should effectively request the block
            Assert.True(movedToNext);
            Assert.Equal(req.Height, 10);
            Assert.Equal(req.Id.ToStringUtf8(), "FakeHash");

            Assert.Equal(p.SyncedAnnouncement, annoucement);
            Assert.False(p.AnyStashed);

            Assert.Equal(p.SyncTarget, 0);
            Assert.Equal(p.CurrentlyRequestedHeight, 0);
        }
コード例 #5
0
ファイル: PeerSyncTest.cs プロジェクト: wyk125/AElf
        public void SyncToHeight_WithValidTarget_TriggersSync()
        {
            // Mutiple things to verify: (1) state is correct, (2) a request is created for startHeight and (3) that
            // the corresponding message is sent to the peer.

            int startHeight  = 2;
            int targetHeight = 3;

            int peerPort = 1234;

            Mock <IMessageReader> reader         = new Mock <IMessageReader>();
            Mock <IMessageWriter> messageWritter = new Mock <IMessageWriter>();

            ECKeyPair kp = new KeyPairGenerator().Generate();
            Peer      p  = new Peer(new TcpClient(), reader.Object, messageWritter.Object, peerPort, kp, 0);

            var(_, handshake) = NetworkTestHelpers.CreateKeyPairAndHandshake(1235);
            p.AuthentifyWith(handshake); // set "other peer as authentified"

            BlockRequest req = null;

            messageWritter
            .Setup(w => w.EnqueueMessage(It.Is <Message>(m => m.Type == (int)AElfProtocolMsgType.RequestBlock), It.IsAny <Action <Message> >()))
            .Callback <Message, Action <Message> >((m, a) =>
            {
                if (m != null)
                {
                    req = BlockRequest.Parser.ParseFrom(m.Payload);
                }
            });

            p.SyncToHeight(startHeight, targetHeight);

            // Check state
            Assert.Equal(p.SyncTarget, targetHeight);
            Assert.Equal(p.CurrentlyRequestedHeight, startHeight);

            // Check block request
            Assert.NotEmpty(p.BlockRequests);
            Assert.NotEmpty(p.BlockRequests);

            // Check request sent
            Assert.NotNull(req);
            Assert.Equal(req.Height, startHeight);
        }
コード例 #6
0
ファイル: PeerSyncTest.cs プロジェクト: wyk125/AElf
        public void SyncNextHistory_ReceiveTarget_EndsSync()
        {
            int startHeight  = 2;
            int targetHeight = 2;

            int peerPort = 1234;

            Mock <IMessageReader> reader         = new Mock <IMessageReader>();
            Mock <IMessageWriter> messageWritter = new Mock <IMessageWriter>();

            ECKeyPair kp = new KeyPairGenerator().Generate();
            Peer      p  = new Peer(new TcpClient(), reader.Object, messageWritter.Object, peerPort, kp, 0);

            var(_, handshake) = NetworkTestHelpers.CreateKeyPairAndHandshake(1235);
            p.AuthentifyWith(handshake); // set "other peer as authentified"

            p.SyncToHeight(startHeight, targetHeight);
            var movedToNext = p.SyncNextHistory();

            Assert.False(movedToNext);

            Assert.Equal(p.SyncTarget, 0);
            Assert.Equal(p.CurrentlyRequestedHeight, 0);
        }