Beispiel #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());
            }
        }
Beispiel #2
0
 public void CanAppendBlock()
 {
     Assert.Empty(_blockchain.Blocks);
     _blockchain.Append(_fx.Block1);
     Assert.Contains(_fx.Block1, _blockchain);
 }