public async Task Can_activate_peer_on_verack_receiving()
        {
            // Arrange
            var blockchain = new NullBlockchain();

            AutoMockContainer.Register <IBlockchain>(blockchain);

            var verAckMessage = new VerAckMessage();
            var version       = new VersionPayload();
            var peerMock      = AutoMockContainer.GetMock <IPeer>();

            peerMock.SetupProperty(x => x.IsReady, false);
            peerMock.SetupProperty(x => x.Version, version);

            blockchain.LastBlockHeader.Index = 1;
            version.CurrentBlockIndex        = 1;

            var messageHandler = AutoMockContainer.Get <VerAckMessageHandler>();

            // Act
            await messageHandler.Handle(verAckMessage, peerMock.Object);

            // Assert
            peerMock.Object.IsReady.Should().BeTrue();
        }
        public async Task Can_send_get_block_headers_message_if_peer_block_header_height_is_different()
        {
            // Arrange
            var blockHeader = new BlockHeader()
            {
                Index = 1,
                Hash  = UInt256.Zero
            };

            var blockchainMock = this.AutoMockContainer.GetMock <IBlockchain>();

            blockchainMock
            .SetupGet(x => x.LastBlockHeader)
            .Returns(blockHeader);

            var verAckMessage = new VerAckMessage();
            var version       = new VersionPayload();
            var peerMock      = AutoMockContainer.GetMock <IPeer>();

            peerMock.SetupProperty(x => x.Version, version);
            version.CurrentBlockIndex = 2;

            var messageHandler = AutoMockContainer.Get <VerAckMessageHandler>();

            // Act
            await messageHandler.Handle(verAckMessage, peerMock.Object);

            // Assert
            // TODO: This need to evaluate correctly that the right GetBlockHeadersMessage is been generated.
            peerMock.Verify(x => x.Send(It.IsAny <GetBlockHeadersMessage>()), Times.Once);
        }
Beispiel #3
0
        public async Task Handle_VerAckMessageReceived_PeerIsReady()
        {
            // Arrange
            var verAckMessage = new VerAckMessage();
            var version       = new VersionPayload
            {
                CurrentBlockIndex = 1
            };
            var peerMock = AutoMockContainer.GetMock <IPeer>();

            peerMock.SetupProperty(x => x.IsReady, false);
            peerMock.SetupProperty(x => x.Version, version);

            var expectedLastBlockHeader = new BlockHeader(HeaderType.Header)
            {
                Index = 1
            };

            this.AutoMockContainer
            .GetMock <IBlockchainContext>()
            .SetupGet(x => x.LastBlockHeader)
            .Returns(expectedLastBlockHeader);

            var messageHandler = AutoMockContainer.Get <VerAckMessageHandler>();

            // Act
            await messageHandler.Handle(verAckMessage, peerMock.Object);

            // Assert
            peerMock.Object.IsReady
            .Should()
            .BeTrue();
        }
Beispiel #4
0
        public async Task Can_send_acknowledgement_on_version_receiving()
        {
            // Arrange
            var server = new NullServer();

            AutoMockContainer.Register <IServer>(server);

            var messageHandler = AutoMockContainer.Get <VersionMessageHandler>();
            var versionMessage = GetPeerVersionMessage(server);
            var peerMock       = AutoMockContainer.GetMock <IPeer>();

            peerMock.SetupProperty(x => x.Version);

            // Act
            await messageHandler.Handle(versionMessage, peerMock.Object);

            // Assert
            peerMock.Verify(x => x.Send <VerAckMessage>(), Times.Once);
        }
        public void Throw_on_the_same_nonce_on_version_receiving()
        {
            // Arrange
            var serverContextMock = AutoMockContainer
                                    .GetMock <IServerContext>()
                                    .SetupDefaultServerContext();

            var versionMessage = new VersionMessage(serverContextMock.Object.Version);

            var peerMock = AutoMockContainer.GetMock <IPeer>();

            peerMock.SetupProperty(x => x.Version);

            var messageHandler = AutoMockContainer.Get <VersionMessageHandler>();

            // Act
            Action a = () => messageHandler.Handle(versionMessage, peerMock.Object).Wait();

            // Assert
            a.Should().Throw <InvalidOperationException>();
        }
        public async Task Can_send_acknowledgement_on_version_receiving()
        {
            // Arrange
            var serverContextMock = AutoMockContainer
                                    .GetMock <IServerContext>()
                                    .SetupDefaultServerContext();

            var versionMessage = new VersionMessage(serverContextMock.Object.Version);

            var peerMock = AutoMockContainer.GetMock <IPeer>();

            peerMock
            .SetupGet(x => x.Version)
            .Returns(new VersionPayload());

            var messageHandler = AutoMockContainer.Get <VersionMessageHandler>();

            // Act
            await messageHandler.Handle(versionMessage, peerMock.Object);

            // Assert
            peerMock.Verify(x => x.Send <VerAckMessage>(), Times.Once);
        }
        public async Task Can_downgrade_protocol_on_version_receiving()
        {
            // Arrange
            var serverContextMock = AutoMockContainer
                                    .GetMock <IServerContext>()
                                    .SetupDefaultServerContext();

            var versionMessage = new VersionMessage(serverContextMock.Object.Version);

            var peerMock = AutoMockContainer.GetMock <IPeer>();

            peerMock
            .Setup(x => x.ChangeProtocol(peerMock.Object.Version))
            .Returns(false);

            var messageHandler = AutoMockContainer.Get <VersionMessageHandler>();

            // Act
            await messageHandler.Handle(versionMessage, peerMock.Object);

            // Assert
            peerMock.Verify(x => x.ChangeProtocol(It.IsAny <VersionPayload>()), Times.Once);
        }
Beispiel #8
0
        public async Task Can_downgrade_protocol_on_version_receiving()
        {
            // Arrange
            var server = new NullServer();

            AutoMockContainer.Register <IServer>(server);

            var messageHandler = AutoMockContainer.Get <VersionMessageHandler>();
            var versionMessage = GetPeerVersionMessage(server);

            server.ProtocolVersion         = 2;
            versionMessage.Payload.Version = 1;

            var peerMock = AutoMockContainer.GetMock <IPeer>();

            peerMock.SetupProperty(x => x.Version);

            // Act
            await messageHandler.Handle(versionMessage, peerMock.Object);

            // Assert
            peerMock.Verify(x => x.DowngradeProtocol(It.IsAny <uint>()), Times.Once);
        }
Beispiel #9
0
        public void Throw_on_the_same_nonce_on_version_receiving()
        {
            // Arrange
            var server = new NullServer();

            AutoMockContainer.Register <IServer>(server);

            var versionMessage = GetPeerVersionMessage(server);

            versionMessage.Payload.Nonce = server.Nonce;

            var peerMock = AutoMockContainer.GetMock <IPeer>();

            peerMock.SetupProperty(x => x.Version);

            var messageHandler = AutoMockContainer.Get <VersionMessageHandler>();

            // Act
            Action a = () => messageHandler.Handle(versionMessage, peerMock.Object).Wait();

            // Assert
            a.Should().Throw <InvalidOperationException>();
        }
Beispiel #10
0
        public async Task Handle_LastLocalBlockIndex1LastPeerBlockIndex2_GetBlockMessageSend()
        {
            // Arrange
            const uint currentBlockIndex = 2;

            var blockHeader = new BlockHeader
            {
                Index = 1,
                Hash  = UInt256.Zero
            };

            var blockchainContextMock = this.AutoMockContainer.GetMock <IBlockchainContext>();

            blockchainContextMock
            .SetupGet(x => x.LastBlockHeader)
            .Returns(blockHeader);
            blockchainContextMock
            .SetupGet(x => x.NeedPeerSync)
            .Returns(true);

            var verAckMessage = new VerAckMessage();
            var version       = new VersionPayload();
            var peerMock      = AutoMockContainer.GetMock <IPeer>();

            peerMock.SetupProperty(x => x.Version, version);
            version.CurrentBlockIndex = currentBlockIndex;

            var messageHandler = AutoMockContainer.Get <VerAckMessageHandler>();

            // Act
            await messageHandler.Handle(verAckMessage, peerMock.Object);

            // Assert
            blockchainContextMock.Verify(x => x.SetPeerCurrentBlockIndex(currentBlockIndex));
            // TODO #410: This need to evaluate correctly that the right GetBlockHeadersMessage is been generated.
            peerMock.Verify(x => x.Send(It.IsAny <GetBlockHeadersMessage>()), Times.Once);
        }
Beispiel #11
0
        public async Task Can_send_get_block_headers_message_if_peer_block_header_height_is_different()
        {
            // Arrange
            var blockchain = new NullBlockchain();

            AutoMockContainer.Register <IBlockchain>(blockchain);

            var verAckMessage = new VerAckMessage();
            var version       = new VersionPayload();
            var peerMock      = AutoMockContainer.GetMock <IPeer>();

            peerMock.SetupProperty(x => x.Version, version);

            blockchain.LastBlockHeader.Index = 1;
            version.CurrentBlockIndex        = 2;

            var messageHandler = AutoMockContainer.Get <VerAckMessageHandler>();

            // Act
            await messageHandler.Handle(verAckMessage, peerMock.Object);

            // Assert
            peerMock.Verify(x => x.Send(It.IsAny <GetBlockHeadersMessage>()), Times.Once);
        }