Example #1
0
        public async Task CanGetBlock()
        {
            Swarm swarmA = _swarms[0];
            Swarm swarmB = _swarms[1];

            Blockchain <BaseAction> chainA = _blockchains[0];
            Blockchain <BaseAction> chainB = _blockchains[1];

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

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

            Block <BaseAction> block1 = chainA.MineBlock(_fx1.Address1);
            Block <BaseAction> block2 = chainA.MineBlock(_fx1.Address1);

            try
            {
                await Task.WhenAll(
                    swarmA.InitContextAsync(),
                    swarmB.InitContextAsync());

                var at = Task.Run(
                    async() => await swarmA.RunAsync(chainA, 250));
                var bt = Task.Run(
                    async() => await swarmB.RunAsync(chainB, 250));

                await Assert.ThrowsAsync <PeerNotFoundException>(
                    async() => await swarmB.GetBlocksAsync(
                        swarmA.AsPeer, new[] { genesis.Hash }, null));

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

                IEnumerable <HashDigest <SHA256> > inventories1 =
                    await swarmB.GetBlocksAsync(
                        swarmA.AsPeer, new[] { genesis.Hash }, null);

                Assert.Equal(new[] { block1.Hash, block2.Hash }, inventories1);

                IEnumerable <HashDigest <SHA256> > inventories2 =
                    await swarmB.GetBlocksAsync(
                        swarmA.AsPeer, new[] { genesis.Hash }, block1.Hash);

                Assert.Equal(new[] { block1.Hash }, inventories2);

                List <Block <BaseAction> > receivedBlocks =
                    await swarmB.GetDataAsync <BaseAction>(
                        swarmA.AsPeer, inventories1
                        ).ToListAsync();

                Assert.Equal(new[] { block1, block2 }, receivedBlocks);
            }
            finally
            {
                await Task.WhenAll(
                    swarmA.DisposeAsync(),
                    swarmB.DisposeAsync());
            }
        }
Example #2
0
        public async Task CanBroadcastTx()
        {
            Swarm swarmA = _swarms[0];
            Swarm swarmB = _swarms[1];
            Swarm swarmC = _swarms[2];

            Blockchain <BaseAction> chainA = _blockchains[0];
            Blockchain <BaseAction> chainB = _blockchains[1];
            Blockchain <BaseAction> chainC = _blockchains[2];

            Transaction <BaseAction> tx = Transaction <BaseAction> .Make(
                new PrivateKey(),
                new PrivateKey().PublicKey.ToAddress(),
                new BaseAction[] { },
                DateTime.Now);

            chainA.StageTransactions(new[] { tx }.ToHashSet());
            chainA.MineBlock(_fx1.Address1);

            try
            {
                await Task.WhenAll(
                    swarmA.InitContextAsync(),
                    swarmB.InitContextAsync(),
                    swarmC.InitContextAsync());

                #pragma warning disable CS4014
                Task.Run(async() => await swarmA.RunAsync(chainA, 250));
                Task.Run(async() => await swarmB.RunAsync(chainB, 250));
                Task.Run(async() => await swarmC.RunAsync(chainC, 250));
                #pragma warning restore CS4014

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

                await swarmA.AddPeersAsync(new[] { swarmC.AsPeer });

                await EnsureExchange(swarmA, swarmB);
                await EnsureExchange(swarmA, swarmC);

                await swarmA.BroadcastTxsAsync(new[] { tx });

                await swarmC.TxReceived.WaitAsync();

                await swarmB.TxReceived.WaitAsync();

                Assert.Equal(tx, chainB.Transactions[tx.Id]);
                Assert.Equal(tx, chainC.Transactions[tx.Id]);
            }
            finally
            {
                await Task.WhenAll(
                    swarmA.DisposeAsync(),
                    swarmB.DisposeAsync(),
                    swarmC.DisposeAsync());
            }
        }
Example #3
0
        public async Task CanGetTx()
        {
            Swarm swarmA = _swarms[0];
            Swarm swarmB = _swarms[1];

            Blockchain <BaseAction> chainA = _blockchains[0];
            Blockchain <BaseAction> chainB = _blockchains[1];

            Transaction <BaseAction> tx = Transaction <BaseAction> .Make(
                new PrivateKey(),
                new PrivateKey().PublicKey.ToAddress(),
                new BaseAction[] { },
                DateTime.Now
                );

            chainB.StageTransactions(new[] { tx }.ToHashSet());
            chainB.MineBlock(_fx1.Address1);

            try
            {
                await Task.WhenAll(
                    swarmA.InitContextAsync(),
                    swarmB.InitContextAsync());

                #pragma warning disable CS4014
                Task.Run(async() => await swarmA.RunAsync(chainA, 250));
                Task.Run(async() => await swarmB.RunAsync(chainB, 250));
                #pragma warning restore CS4014

                Assert.Throws <PeerNotFoundException>(
                    () => swarmB.GetTxsAsync <BaseAction>(
                        swarmA.AsPeer, new[] { tx.Id }));

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

                List <Transaction <BaseAction> > txs =
                    await swarmA.GetTxsAsync <BaseAction>(
                        swarmB.AsPeer, new[] { tx.Id }
                        ).ToListAsync();

                Assert.Equal(new[] { tx }, txs);
            }
            finally
            {
                await Task.WhenAll(
                    swarmA.DisposeAsync(),
                    swarmB.DisposeAsync());
            }
        }
Example #4
0
        public async Task CanBeCancelled()
        {
            Swarm swarm = _swarms[0];
            Blockchain <BaseAction> chain = _blockchains[0];
            var cts = new CancellationTokenSource();

            try
            {
                await swarm.InitContextAsync();

                var at = Task.Run(
                    async() => await swarm.RunAsync(chain, 250, cts.Token));

                cts.Cancel();
                await Assert.ThrowsAsync <TaskCanceledException>(async() => await at);
            }
            finally
            {
                await swarm.DisposeAsync();
            }
        }
Example #5
0
        public async Task WorksAsExpected()
        {
            Swarm a = _swarms[0];
            Swarm b = _swarms[1];
            Swarm c = _swarms[2];

            Blockchain <BaseAction> chain = _blockchains[0];

            DateTime lastDistA;
            Peer     aAsPeer;

            try
            {
                await b.InitContextAsync();

                await c.InitContextAsync();

                try
                {
                    await a.InitContextAsync();

                    var at = Task.Run(async() => await a.RunAsync(chain, 250));
                    await b.AddPeersAsync(new[] { a.AsPeer });

                    var bt = Task.Run(async() => await b.RunAsync(chain, 250));
                    await EnsureExchange(a, b);

                    Assert.Equal(new[] { b.AsPeer }.ToImmutableHashSet(), a.ToImmutableHashSet());
                    Assert.Equal(new[] { a.AsPeer }.ToImmutableHashSet(), b.ToImmutableHashSet());

                    await c.AddPeersAsync(new[] { a.AsPeer });

                    var ct = Task.Run(async() => await c.RunAsync(chain, 250));
                    await EnsureExchange(a, c);
                    await EnsureExchange(a, b);

                    Assert.Equal(new[] { b.AsPeer, c.AsPeer }.ToImmutableHashSet(), a.ToImmutableHashSet());
                    Assert.Equal(new[] { a.AsPeer, c.AsPeer }.ToImmutableHashSet(), b.ToImmutableHashSet());
                    Assert.Equal(new[] { a.AsPeer, b.AsPeer }.ToImmutableHashSet(), c.ToImmutableHashSet());

                    lastDistA = a.LastDistributed;
                    aAsPeer   = a.AsPeer;
                }
                finally
                {
                    await a.DisposeAsync();
                }

                Assert.True(lastDistA < a.LastDistributed);

                await EnsureRecvAsync(b, aAsPeer, a.LastDistributed);
                await EnsureRecvAsync(c, aAsPeer, a.LastDistributed);
                await EnsureExchange(b, c);

                Assert.Equal(new[] { c.AsPeer }.ToImmutableHashSet(), b.ToImmutableHashSet());
                Assert.Equal(new[] { b.AsPeer }.ToImmutableHashSet(), c.ToImmutableHashSet());
            }
            finally
            {
                await b.DisposeAsync();

                await c.DisposeAsync();
            }
        }