示例#1
0
        public void terab_utxo_get_committed_block()
        {
            terab_utxo_commit_block();

            _socket.ExpectReceive(data =>
            {
                data.Clear();
                Assert.True(data.Length == MessageHeader.SizeInBytes);

                var header = new MessageHeader(GetBlockHandleRequest.SizeInBytes, _r1, _c0,
                                               MessageKind.GetBlockHandle);
                var headerStorage = new MessageHeader[1];
                headerStorage[0]  = header;
                var headerBytes   = MemoryMarshal.Cast <MessageHeader, byte>(headerStorage);
                headerBytes.CopyTo(data);

                return(MessageHeader.SizeInBytes);
            });

            _socket.ExpectReceive(data =>
            {
                data.Clear();
                Assert.True(data.Length >= GetBlockHandleRequest.SizeInBytes - MessageHeader.SizeInBytes);

                var request = GetBlockHandleRequest.From(_r1, _c0, CommittedBlockId.Genesis);
                request.Span.Slice(16).CopyTo(data);

                return(GetBlockHandleRequest.SizeInBytes - MessageHeader.SizeInBytes);
            });

            _socket.ExpectConnected(() => true);
            _socket.ExpectConnected(() => true);

            _socket.ExpectSend(data =>
            {
                Assert.Equal(GetBlockHandleResponse.SizeInBytes, data.Length);
                var response = new GetBlockHandleResponse(data);
                Assert.Equal(new BlockAlias(0, 0), response.Handle.ConvertToBlockAlias(_handleMask));
                return(GetBlockHandleResponse.SizeInBytes);
            });

            Assert.True(_clientConn.HandleRequest());

            Assert.True(_dispatcher.HandleRequest());

            Assert.True(_chainController.HandleRequest());

            Assert.True(_dispatcher.HandleRequest());

            Assert.True(_clientConn.HandleResponse());

            _socket.ExpectAllDone();
        }
示例#2
0
        public void QueueTooManyClients()
        {
            var socket          = new MockSocket();
            var dispatcherInbox = new BoundedInbox();
            var dispatcher      = new DispatchController(dispatcherInbox, new BoundedInbox(), new BoundedInbox[64],
                                                         new IdentityHash());

            dispatcher.OnConnectionAccepted = (_) => { };

            for (var i = 0; i < Constants.MaxActiveConnections; i++)
            {
                var client0 = new ConnectionController(dispatcherInbox, socket, ClientId.Next());
                client0.OnRequestReceived = () => { };
                dispatcher.AddConnection(client0);
                dispatcher.HandleNewConnection();
            }

            var client1 = new ConnectionController(dispatcherInbox, socket, ClientId.Next());

            client1.OnRequestReceived = () => { };

            socket.ExpectSend(data =>
            {
                Assert.Equal(ProtocolErrorResponse.SizeInBytes, data.Length);

                var errorMessage = new ProtocolErrorResponse(data);
                Assert.Equal(MessageKind.ProtocolErrorResponse, errorMessage.MessageHeader.MessageKind);
                Assert.Equal(ProtocolErrorStatus.TooManyActiveClients, errorMessage.Status);

                return(data.Length);
            });

            dispatcher.AddConnection(client1);
            dispatcher.HandleNewConnection();
            client1.HandleResponse();
            socket.ExpectAllDone();
        }
示例#3
0
        public void FillChainControllerInbox()
        {
            var socket1         = new MockSocket();
            var socket2         = new MockSocket();
            var dispatcherInbox = new BoundedInbox();
            var client1         = new ConnectionController(dispatcherInbox, socket1, ClientId.Next());
            var client2         = new ConnectionController(dispatcherInbox, socket2, ClientId.Next());

            client1.OnRequestReceived = () => { };
            client2.OnRequestReceived = () => { };

            Func <int, int, MockSocket.SpanToInt> func = (s1, s2) =>
            {
                return(data =>
                {
                    data.Clear();
                    BinaryPrimitives.TryWriteInt32LittleEndian(data, s1);
                    // TODO: [vermorel] PingChainController has been removed, logic need to be upgraded.
                    //MessageKind.PingChainController.WriteTo(data.Slice(MessageHeaderHelper.MessageKindStart));
                    return s2;
                });
            };

            socket2.ExpectReceive(func(LargeMessageSize, MessageHeader.SizeInBytes));
            socket2.ExpectReceive(func(LargeMessageSize, LargeMessageSize));
            socket1.ExpectReceive(func(LargeMessageSize, MessageHeader.SizeInBytes));
            socket1.ExpectReceive(func(LargeMessageSize, LargeMessageSize));
            // request too short
            var bodyStart = sizeof(int) + RequestId.SizeInBytes + ClientId.SizeInBytes + sizeof(MessageKind);

            socket2.ExpectReceive(func(bodyStart, bodyStart));


            socket2.ExpectSend(data =>
            {
                Assert.Equal(ProtocolErrorResponse.SizeInBytes, data.Length);
                var message = new ProtocolErrorResponse(data);
                Assert.Equal(MessageKind.ProtocolErrorResponse, message.MessageHeader.MessageKind);
                Assert.Equal(ProtocolErrorStatus.RequestTooShort, message.Status);

                return(ProtocolErrorResponse.SizeInBytes);
            });

            socket2.ExpectConnected(() => true);
            socket1.ExpectConnected(() => true);

            var dispatcher = new DispatchController(dispatcherInbox,
                                                    new BoundedInbox(Constants.MaxResponseSize),
                                                    Enumerable.Range(0, 32).Select(x => new BoundedInbox()).ToArray(),
                                                    new IdentityHash());

            // Nil handling of notifications
            dispatcher.OnBlockMessageDispatched = () => { };
            for (var i = 0; i < dispatcher.OnCoinMessageDispatched.Length; i++)
            {
                dispatcher.OnCoinMessageDispatched[i] = () => { }
            }
            ;

            dispatcher.OnConnectionAccepted = (_) => { };
            dispatcher.AddConnection(client1);
            dispatcher.AddConnection(client2);

            dispatcher.HandleNewConnection();
            dispatcher.HandleNewConnection();
            client1.HandleRequest();
            client2.HandleRequest();
            client2.HandleRequest();
            client2.HandleResponse();
            dispatcher.HandleRequest();
            dispatcher.HandleRequest();
            dispatcher.HandleRequest();

            socket1.ExpectAllDone();
            socket2.ExpectAllDone();
        }