Example #1
0
 public SwarmBenchmark()
 {
     _policy      = new NullBlockPolicy <DumbAction>();
     _stagePolicy = new VolatileStagePolicy <DumbAction>();
     _miner       = TestUtils.ChainPrivateKey;
     _blocks      = new List <Block <DumbAction> >
     {
         TestUtils.MineGenesisBlock <DumbAction>(_policy.GetHashAlgorithm, _miner),
     };
     _appProtocolVersion = AppProtocolVersion.Sign(new PrivateKey(), 1);
     _blocks.Add(TestUtils.MineNextBlock(_blocks[0], _policy.GetHashAlgorithm, _miner));
     _blocks.Add(TestUtils.MineNextBlock(_blocks[1], _policy.GetHashAlgorithm, _miner));
     _blocks.Add(TestUtils.MineNextBlock(_blocks[2], _policy.GetHashAlgorithm, _miner));
     _blocks.Add(TestUtils.MineNextBlock(_blocks[3], _policy.GetHashAlgorithm, _miner));
 }
Example #2
0
        public async Task Copy()
        {
            using (StoreFixture fx = FxConstructor())
                using (StoreFixture fx2 = FxConstructor())
                {
                    IStore s1 = fx.Store, s2 = fx2.Store;
                    var    policy = new NullBlockPolicy <DumbAction>();
                    var    blocks = new BlockChain <DumbAction>(
                        policy,
                        new VolatileStagePolicy <DumbAction>(),
                        s1,
                        fx.StateStore,
                        MineGenesis <DumbAction>(policy.GetHashAlgorithm, GenesisMiner.PublicKey)
                        .Evaluate(GenesisMiner, policy.BlockAction, fx.StateStore)
                        );

                    // FIXME: Need to add more complex blocks/transactions.
                    var key = new PrivateKey();
                    await blocks.MineBlock(key);

                    await blocks.MineBlock(key);

                    await blocks.MineBlock(key);

                    s1.Copy(to: Fx.Store);
                    Fx.Store.Copy(to: s2);

                    Assert.Equal(s1.ListChainIds().ToHashSet(), s2.ListChainIds().ToHashSet());
                    Assert.Equal(s1.GetCanonicalChainId(), s2.GetCanonicalChainId());
                    foreach (Guid chainId in s1.ListChainIds())
                    {
                        Assert.Equal(s1.IterateIndexes(chainId), s2.IterateIndexes(chainId));
                        foreach (BlockHash blockHash in s1.IterateIndexes(chainId))
                        {
                            Assert.Equal(
                                s1.GetBlock <DumbAction>(fx.GetHashAlgorithm, blockHash),
                                s2.GetBlock <DumbAction>(fx2.GetHashAlgorithm, blockHash)
                                );
                        }
                    }

                    // ArgumentException is thrown if the destination store is not empty.
                    Assert.Throws <ArgumentException>(() => Fx.Store.Copy(fx2.Store));
                }
        }
        public async Task BroadcastBlockToReconnectedPeer()
        {
            var miner      = new PrivateKey();
            var policy     = new NullBlockPolicy <DumbAction>();
            var fx         = new MemoryStoreFixture(policy.BlockAction);
            var minerChain = MakeBlockChain(policy, fx.Store, fx.StateStore);

            foreach (int i in Enumerable.Range(0, 10))
            {
                await minerChain.MineBlock(miner);
            }

            Swarm <DumbAction> seed = CreateSwarm(
                miner,
                policy: policy,
                genesis: minerChain.Genesis
                );
            BlockChain <DumbAction> seedChain = seed.BlockChain;

            var privateKey            = new PrivateKey();
            Swarm <DumbAction> swarmA = CreateSwarm(
                privateKey: privateKey,
                policy: policy,
                genesis: minerChain.Genesis
                );
            Swarm <DumbAction> swarmB = CreateSwarm(
                privateKey: privateKey,
                policy: policy,
                genesis: minerChain.Genesis
                );

            foreach (BlockHash blockHash in minerChain.BlockHashes.Skip(1).Take(4))
            {
                seedChain.Append(minerChain[blockHash]);
            }

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

                Assert.Equal(swarmA.AsPeer, swarmB.AsPeer);

                await swarmA.AddPeersAsync(new[] { seed.AsPeer }, null);
                await StopAsync(swarmA);

                await seed.PeerDiscovery.RefreshTableAsync(
                    TimeSpan.Zero,
                    default(CancellationToken));

                Assert.DoesNotContain(swarmA.AsPeer, seed.Peers);

                foreach (BlockHash blockHash in minerChain.BlockHashes.Skip(5))
                {
                    seedChain.Append(minerChain[blockHash]);
                }

                await swarmB.AddPeersAsync(new[] { seed.AsPeer }, null);

                // This is added for context switching.
                await Task.Delay(100);

                Assert.Contains(swarmB.AsPeer, seed.Peers);
                Assert.Contains(seed.AsPeer, swarmB.Peers);

                seed.BroadcastBlock(seedChain.Tip);

                await swarmB.BlockAppended.WaitAsync();

                Assert.NotEqual(seedChain.BlockHashes, swarmA.BlockChain.BlockHashes);
                Assert.Equal(seedChain.BlockHashes, swarmB.BlockChain.BlockHashes);
            }
            finally
            {
                await StopAsync(seed);
                await StopAsync(swarmA);
                await StopAsync(swarmB);

                seed.Dispose();
                swarmA.Dispose();
                swarmB.Dispose();
            }
        }