Exemple #1
0
        public void SendReceiveMultipartMessage()
        {
            async Task SendReceiveAsync()
            {
                using (var server = new RouterSocket("inproc://async"))
                    using (var client = new DealerSocket("inproc://async"))
                    {
                        var req = new NetMQMessage();
                        req.Append("Hello");
                        req.Append(new byte[] { 0x00, 0x01, 0x02, 0x03 });

                        await client.SendMultipartMessageAsync(req);

                        NetMQMessage received = await server.ReceiveMultipartMessageAsync();

                        Assert.Equal("Hello", received[1].ConvertToString());
                        Assert.Equal(new byte[] { 0x00, 0x01, 0x02, 0x03 }, received[2].Buffer);
                    }
            }

            using (var runtime = new NetMQRuntime())
            {
                var t = SendReceiveAsync();
                runtime.Run(t);

                if (t.IsFaulted && t.Exception is AggregateException exc)
                {
                    throw exc.GetBaseException();
                }
            }
        }
Exemple #2
0
        internal IAsyncEnumerable <Transaction <T> > GetTxsAsync <T>(
            DealerSocket socket,
            IEnumerable <TxId> txIds,
            CancellationToken cancellationToken = default(CancellationToken))
            where T : IAction
        {
            return(new AsyncEnumerable <Transaction <T> >(async yield =>
            {
                var request = new GetTxs(txIds);
                await socket.SendMultipartMessageAsync(
                    request.ToNetMQMessage(_privateKey),
                    cancellationToken: cancellationToken);

                int hashCount = txIds.Count();
                while (hashCount > 0)
                {
                    NetMQMessage response =
                        await socket.ReceiveMultipartMessageAsync(
                            cancellationToken: cancellationToken);
                    Message parsedMessage = Message.Parse(response, true);
                    if (parsedMessage is Messages.Tx parsed)
                    {
                        Transaction <T> tx = Transaction <T> .FromBencodex(
                            parsed.Payload);
                        await yield.ReturnAsync(tx);
                        hashCount--;
                    }
                    else
                    {
                        throw new InvalidMessageException(
                            $"The response of getdata isn't block. " +
                            $"but {parsedMessage}");
                    }
                }
            }));
        }
Exemple #3
0
        internal IAsyncEnumerable <Block <T> > GetBlocksAsync <T>(
            DealerSocket sock,
            IEnumerable <HashDigest <SHA256> > blockHashes,
            CancellationToken cancellationToken)
            where T : IAction
        {
            return(new AsyncEnumerable <Block <T> >(async yield =>
            {
                var request = new GetBlocks(blockHashes);
                await sock.SendMultipartMessageAsync(
                    request.ToNetMQMessage(_privateKey),
                    cancellationToken: cancellationToken);

                int hashCount = blockHashes.Count();
                while (hashCount > 0)
                {
                    NetMQMessage response =
                        await sock.ReceiveMultipartMessageAsync(
                            cancellationToken: cancellationToken);
                    Message parsedMessage = Message.Parse(response, true);
                    if (parsedMessage is Block blockMessage)
                    {
                        Block <T> block = Block <T> .FromBencodex(
                            blockMessage.Payload);
                        await yield.ReturnAsync(block);
                        hashCount--;
                    }
                    else
                    {
                        throw new InvalidMessageException(
                            $"The response of getdata isn't block. " +
                            $"but {parsedMessage}");
                    }
                }
            }));
        }
Exemple #4
0
        internal async Task <IEnumerable <HashDigest <SHA256> > > GetBlockHashesAsync(
            DealerSocket sock,
            BlockLocator locator,
            HashDigest <SHA256>?stop,
            CancellationToken cancellationToken)
        {
            var request = new GetBlockHashes(locator, stop);
            await sock.SendMultipartMessageAsync(
                request.ToNetMQMessage(_privateKey),
                cancellationToken : cancellationToken);

            NetMQMessage response = await sock.ReceiveMultipartMessageAsync();

            Message parsedMessage = Message.Parse(response, reply: true);

            if (parsedMessage is BlockHashes blockHashes)
            {
                return(blockHashes.Hashes);
            }

            throw new InvalidMessageException(
                      $"The response of GetBlockHashes isn't BlockHashes. " +
                      $"but {parsedMessage}");
        }
Exemple #5
0
        public async Task GetMultipleBlocksAtOnce()
        {
            var privateKey = new PrivateKey();

            BlockChain <DumbAction> chainA = _blockchains[0];
            BlockChain <DumbAction> chainB = _blockchains[1];

            Swarm <DumbAction> swarmA = _swarms[0];
            Swarm <DumbAction> swarmB = new Swarm <DumbAction>(
                chainB,
                privateKey,
                1,
                host: IPAddress.Loopback.ToString());

            Block <DumbAction> genesis = chainA.MineBlock(_fx1.Address1);

            chainB.Append(genesis); // chainA and chainB shares genesis block.
            chainA.MineBlock(_fx1.Address1);
            chainA.MineBlock(_fx1.Address1);

            try
            {
                await StartAsync(swarmA);
                await StartAsync(swarmB);

                var peer = swarmA.AsPeer;

                await swarmB.AddPeersAsync(new[] { peer });

                IEnumerable <HashDigest <SHA256> > hashes =
                    await swarmB.GetBlockHashesAsync(
                        peer,
                        new BlockLocator(new[] { genesis.Hash }),
                        null);

                var netMQAddress = $"tcp://{peer.EndPoint.Host}:{peer.EndPoint.Port}";
                using (var socket = new DealerSocket(netMQAddress))
                {
                    var request = new GetBlocks(hashes, 2);
                    await socket.SendMultipartMessageAsync(
                        request.ToNetMQMessage(privateKey));

                    NetMQMessage response = await socket.ReceiveMultipartMessageAsync();

                    Message parsedMessage = Message.Parse(response, true);
                    Libplanet.Net.Messages.Blocks blockMessage =
                        (Libplanet.Net.Messages.Blocks)parsedMessage;

                    Assert.Equal(2, blockMessage.Payloads.Count);

                    response = await socket.ReceiveMultipartMessageAsync();

                    parsedMessage = Message.Parse(response, true);
                    blockMessage  = (Libplanet.Net.Messages.Blocks)parsedMessage;

                    Assert.Single(blockMessage.Payloads);
                }
            }
            finally
            {
                await Task.WhenAll(
                    swarmA.StopAsync(),
                    swarmB.StopAsync());
            }
        }