Пример #1
0
        public void TestGetUncommittedBlockInfo()
        {
            var(_, inbox, outbox, chainController, c5, uncommittedBlockId) = TestOpenBlock();

            // C(0) -> C(1) -> C(2)   -> C(3) -> C(4)
            //             |                 \-> C(4-1)
            //             |                 \-> U(4-2)
            //             \-> C(2-1) -> C(3-1)

            var request = GetBlockInfoRequest.From(_r1, c5, _4_2.ConvertToBlockHandle(c5.Mask));

            inbox.TryWrite(request.Span);

            chainController.HandleRequest();

            outbox.Next();
            var response = new GetBlockInfoResponse(outbox.Peek().Span);

            Assert.Equal(MessageKind.BlockInfoResponse, response.MessageHeader.MessageKind);
            Assert.False(response.IsCommitted);

            Assert.Equal(_r1, response.MessageHeader.RequestId);
            Assert.Equal(c5, response.MessageHeader.ClientId);

            Assert.Equal(uncommittedBlockId, response.UncommittedBlockId);
            Assert.Equal(_4_2, response.Handle.ConvertToBlockAlias(c5.Mask));
            Assert.Equal(_3, response.Parent.ConvertToBlockAlias(c5.Mask));
            Assert.Equal(4, response.BlockHeight);
        }
Пример #2
0
        public Span <byte> GetBlockInfo(GetBlockInfoRequest request)
        {
            // the request is the same for committed and uncommitted blocks
            // which is why one needs to look for it among the committed and
            // uncommitted blocks of the blockchain.

            if (_store.TryGetCommittedBlock(request.BlockAlias, out var committedBlock))
            {
                return(new GetBlockInfoResponse(
                           ref request.MessageHeader,
                           committedBlock.BlockId,
                           committedBlock.Alias,
                           committedBlock.Parent,
                           committedBlock.BlockHeight,
                           request.Mask,
                           _pool).Span);
            }

            _store.TryGetUncommittedBlock(request.BlockAlias, out var uncommittedBlock);

            return(new GetBlockInfoResponse(
                       ref request.MessageHeader,
                       uncommittedBlock.BlockId,
                       uncommittedBlock.Alias,
                       uncommittedBlock.Parent,
                       uncommittedBlock.BlockHeight,
                       request.Mask,
                       _pool).Span);
        }
Пример #3
0
        public void terab_uxto_get_blockinfo_committed()
        {
            terab_utxo_commit_block();

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

                var header        = new MessageHeader(GetBlockInfoRequest.SizeInBytes, _r1, _c0, MessageKind.GetBlockInfo);
                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 >= GetBlockInfoRequest.SizeInBytes - MessageHeader.SizeInBytes);

                var request = GetBlockInfoRequest.From(_r1, _c0,
                                                       new BlockAlias(0, 0).ConvertToBlockHandle(_handleMask));
                request.Span.Slice(16).CopyTo(data);

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

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

            _socket.ExpectSend(data =>
            {
                Assert.Equal(GetBlockInfoResponse.SizeInBytes, data.Length);
                var response = new GetBlockInfoResponse(data);

                Assert.Equal(new BlockAlias(0, 0), response.Handle.ConvertToBlockAlias(_handleMask));
                Assert.Equal(BlockAlias.GenesisParent, response.Parent.ConvertToBlockAlias(_handleMask));
                Assert.Equal(0, response.BlockHeight);
                Assert.Equal(CommittedBlockId.Genesis, response.CommittedBlockId);

                return(GetBlockInfoResponse.SizeInBytes);
            });

            Assert.True(_clientConn.HandleRequest());

            Assert.True(_dispatcher.HandleRequest());

            Assert.True(_chainController.HandleRequest());

            Assert.True(_dispatcher.HandleRequest());

            Assert.True(_clientConn.HandleResponse());

            _socket.ExpectAllDone();
        }
Пример #4
0
        public void TestGetCommittedBlockInfo()
        {
            var(_, inbox, outbox, chainController, c5) = Setup();

            var request = GetBlockInfoRequest.From(_r1, c5, _1.ConvertToBlockHandle(c5.Mask));

            inbox.TryWrite(request.Span);

            chainController.HandleRequest();

            var response = new GetBlockInfoResponse(outbox.Peek().Span);

            Assert.Equal(MessageKind.BlockInfoResponse, response.MessageHeader.MessageKind);
            Assert.True(response.IsCommitted);

            Assert.Equal(_r1, response.MessageHeader.RequestId);
            Assert.Equal(c5, response.MessageHeader.ClientId);

            Assert.Equal(_id_1, response.CommittedBlockId);
            Assert.Equal(_1, response.Handle.ConvertToBlockAlias(c5.Mask));
            Assert.Equal(new BlockAlias(0, 0), response.Parent.ConvertToBlockAlias(c5.Mask));
            Assert.Equal(1, response.BlockHeight);
        }
Пример #5
0
        public unsafe void OpenCommit()
        {
            var instance = GetInstance();

            instance.Start();

            var localEndpoint = new IPEndPoint(IPAddress.Loopback, instance.Port);
            var rawSocket     = new Socket(localEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            rawSocket.Connect(localEndpoint);

            var socket = new SocketLikeAdapter(rawSocket);

            try
            {
                // Open block
                var openBlockRequest = OpenBlockRequest.ForGenesis(RequestId.MinRequestId);
                socket.Send(openBlockRequest.Span);

                var openBlockResponse = new OpenBlockResponse(new byte[OpenBlockResponse.SizeInBytes]);
                socket.Receive(openBlockResponse.Span);

                Assert.Equal(OpenBlockStatus.Success, openBlockResponse.Status);

                // Commit block
                var blockId = new CommittedBlockId();
                for (var i = 0; i < CommittedBlockId.SizeInBytes; i++)
                {
                    blockId.Data[i] = (byte)i;
                }

                var commitBlockRequest = CommitBlockRequest.From(
                    RequestId.MinRequestId,
                    ClientId.MinClientId,
                    openBlockResponse.Handle,
                    blockId);
                socket.Send(commitBlockRequest.Span);

                var commitBlockResponse = new CommitBlockResponse(new byte[CommitBlockResponse.SizeInBytes]);
                socket.Receive(commitBlockResponse.Span);

                Assert.Equal(CommitBlockStatus.Success, commitBlockResponse.Status);

                // Get block info
                var getBlockInfoRequest = GetBlockInfoRequest.From(
                    RequestId.MinRequestId,
                    ClientId.MinClientId,
                    openBlockResponse.Handle);
                socket.Send(getBlockInfoRequest.Span);

                var getBlockInfoResponse =
                    new GetBlockInfoResponse(new byte[GetBlockInfoResponse.SizeInBytes]);
                socket.Receive(getBlockInfoResponse.Span);

                Assert.True(getBlockInfoResponse.CommittedBlockId.Equals(blockId));
            }
            finally
            {
                socket.Close();
                instance.Stop();
            }
        }