private void TransferTxs(GetTxs getTxs)
        {
            foreach (TxId txid in getTxs.TxIds)
            {
                try
                {
                    Transaction <T> tx = BlockChain.GetTransaction(txid);

                    if (tx is null)
                    {
                        continue;
                    }

                    Message response = new Messages.Tx(tx.Serialize(true))
                    {
                        Identity = getTxs.Identity,
                    };
                    Transport.ReplyMessage(response);
                }
                catch (KeyNotFoundException)
                {
                    _logger.Warning("Requested TxId {TxId} does not exist.", txid);
                }
            }
        }
        private void TransferTxs(GetTxs getTxs)
        {
            IEnumerable <Transaction <T> > txs = getTxs.TxIds
                                                 .Where(txId => _store.ContainsTransaction(txId))
                                                 .Select(BlockChain.GetTransaction);

            foreach (Transaction <T> tx in txs)
            {
                Message response = new Messages.Tx(tx.Serialize(true))
                {
                    Identity = getTxs.Identity,
                };
                Transport.ReplyMessage(response);
            }
        }
Beispiel #3
0
        private void TransferTxs(GetTxs getTxs)
        {
            foreach (TxId txid in getTxs.TxIds)
            {
                Transaction <T> tx = BlockChain.GetTransaction(txid);

                if (tx is null)
                {
                    continue;
                }

                Message response = new Messages.Tx(tx.Serialize(true))
                {
                    Identity = getTxs.Identity,
                };
                Transport.ReplyMessage(response);
            }
        }
Beispiel #4
0
        private async Task TransferTxs <T>(
            Blockchain <T> blockchain,
            GetTxs getTxs,
            CancellationToken cancellationToken)
            where T : IAction
        {
            IDictionary <TxId, Transaction <T> > txs = blockchain.Transactions;

            foreach (var txid in getTxs.TxIds)
            {
                if (txs.TryGetValue(txid, out Transaction <T> tx))
                {
                    Message response = new Messages.Tx(tx.ToBencodex(true))
                    {
                        Identity = getTxs.Identity,
                    };
                    await ReplyAsync(response, cancellationToken);
                }
            }
        }
Beispiel #5
0
        public async Task CanFillWithInvalidTransaction()
        {
            var privateKey = new PrivateKey();
            var address    = privateKey.ToAddress();
            var swarm1     = CreateSwarm();
            var swarm2     = CreateSwarm();

            var tx1 = swarm2.BlockChain.MakeTransaction(
                privateKey,
                new[] { new DumbAction(address, "foo") });

            var tx2 = swarm2.BlockChain.MakeTransaction(
                privateKey,
                new[] { new DumbAction(address, "bar") });

            var tx3 = swarm2.BlockChain.MakeTransaction(
                privateKey,
                new[] { new DumbAction(address, "quz") });

            var tx4 = Transaction <DumbAction> .Create(
                4,
                privateKey,
                swarm1.BlockChain.Genesis.Hash,
                new[] { new DumbAction(address, "qux") });

            try
            {
                await StartAsync(swarm1);
                await StartAsync(swarm2);

                var transport = swarm1.Transport;
                var msg       = new GetTxs(new[] { tx1.Id, tx2.Id, tx3.Id, tx4.Id });
                var replies   = (await transport.SendMessageWithReplyAsync(
                                     (BoundPeer)swarm2.AsPeer,
                                     msg,
                                     TimeSpan.FromSeconds(1),
                                     4,
                                     true,
Beispiel #6
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}");
                    }
                }
            }));
        }