public void Can_stop()
        {
            SyncPeerMock peerA = new SyncPeerMock("A");

            peerA.AddBlocksUpTo(QueueBasedSyncManager.MaxBatchSize, 0, 0);

            When.Syncing
            .Stop();
        }
        public void Can_sync_with_one_peer_straight()
        {
            SyncPeerMock peerA = new SyncPeerMock("A");

            When.Syncing
            .AfterProcessingGenesis()
            .AfterPeerIsAdded(peerA)
            .BestSuggested.BlockIsSameAsGenesis().Stop();
        }
        public void Will_remove_peer_when_init_fails()
        {
            SyncPeerMock peerA = new SyncPeerMock("A", true, true);

            peerA.AddBlocksUpTo(1);

            When.Syncing
            .AfterProcessingGenesis()
            .AfterPeerIsAdded(peerA)
            .WaitAMoment()
            .PeerCountIs(0).Stop();
        }
        public void Can_sync_more_than_a_batch()
        {
            SyncPeerMock peerA = new SyncPeerMock("A");

            peerA.AddBlocksUpTo(QueueBasedSyncManager.MaxBatchSize * 3, 0, 0);

            When.Syncing
            .AfterProcessingGenesis()
            .AfterPeerIsAdded(peerA)
            .Wait()
            .BestSuggested.HeaderIs(peerA.HeadHeader).Stop();
        }
        public void Can_sync_with_one_peer_straight_and_extend_chain()
        {
            SyncPeerMock peerA = new SyncPeerMock("A");

            peerA.AddBlocksUpTo(3);

            When.Syncing
            .AfterProcessingGenesis()
            .AfterPeerIsAdded(peerA)
            .Wait()
            .BestSuggested.HeaderIs(peerA.HeadHeader).Stop();
        }
        public void Will_not_add_same_peer_twice()
        {
            SyncPeerMock peerA = new SyncPeerMock("A");

            peerA.AddBlocksUpTo(1);

            When.Syncing
            .AfterProcessingGenesis()
            .AfterPeerIsAdded(peerA)
            .AfterPeerIsAdded(peerA)
            .WaitAMoment()
            .PeerCountIs(1)
            .BestSuggested.BlockHasNumber(1).Stop();
        }
        public void Can_extend_chain_by_one_on_new_block_message()
        {
            SyncPeerMock peerA = new SyncPeerMock("A");

            peerA.AddBlocksUpTo(1);

            When.Syncing
            .AfterProcessingGenesis()
            .AfterPeerIsAdded(peerA)
            .Wait()
            .After(() => peerA.AddBlocksUpTo(2))
            .AfterNewBlockMessage(peerA.HeadBlock, peerA)
            .BestSuggested.HeaderIs(peerA.HeadHeader).Stop();
        }
        public void Can_sync_when_best_peer_is_timing_out()
        {
            SyncPeerMock peerA = new SyncPeerMock("A");

            peerA.AddBlocksUpTo(1);

            When.Syncing
            .AfterProcessingGenesis()
            .AfterPeerIsAdded(peerA)
            .Wait()
            .After(() => peerA.AddBlocksUpTo(8))
            .AfterNewBlockMessage(peerA.HeadBlock, peerA)
            .Wait()
            .BestSuggested.HeaderIs(peerA.HeadHeader).Stop();
        }
        public void Will_ignore_blocks_it_does_not_know_about()
        {
            // testing the test framework here
            SyncPeerMock peerA = new SyncPeerMock("A");

            peerA.AddBlocksUpTo(1);

            When.Syncing
            .AfterProcessingGenesis()
            .AfterPeerIsAdded(peerA)
            .Wait()
            .After(() => peerA.AddBlocksUpTo(2))
            .Wait()
            .BestSuggested.BlockHasNumber(1).Stop();
        }
        public void Will_ignore_new_block_that_is_far_ahead()
        {
            SyncPeerMock peerA = new SyncPeerMock("A");

            peerA.AddBlocksUpTo(1);

            SyncPeerMock badPeer = new SyncPeerMock("B", false, true);

            badPeer.AddBlocksUpTo(20);

            When.Syncing
            .AfterProcessingGenesis()
            .AfterPeerIsAdded(badPeer)
            .Wait()
            .AfterPeerIsAdded(peerA)
            .Wait()
            .BestSuggested.BlockHasNumber(1).Stop();
        }
        public void Can_remove_peers()
        {
            SyncPeerMock peerA = new SyncPeerMock("A");
            SyncPeerMock peerB = new SyncPeerMock("B");

            When.Syncing
            .AfterProcessingGenesis()
            .AfterPeerIsAdded(peerA)
            .AfterPeerIsAdded(peerB)
            .WaitAMoment()
            .PeerCountIs(2)
            .AfterPeerIsRemoved(peerB)
            .WaitAMoment()
            .PeerCountIs(1)
            .AfterPeerIsRemoved(peerA)
            .WaitAMoment()
            .PeerCountIs(0).Stop();
        }
        public void Will_not_reorganize_more_than_max_reorg_length()
        {
            SyncPeerMock peerA = new SyncPeerMock("A");

            peerA.AddBlocksUpTo(QueueBasedSyncManager.MaxReorganizationLength + 1, 0, 0);

            SyncPeerMock peerB = new SyncPeerMock("B");

            peerB.AddBlocksUpTo(QueueBasedSyncManager.MaxReorganizationLength + 2, 0, 1);

            When.Syncing
            .AfterProcessingGenesis()
            .AfterPeerIsAdded(peerA)
            .Wait()
            .BestSuggested.HeaderIs(peerA.HeadHeader)
            .AfterPeerIsAdded(peerB)
            .Wait()
            .BestSuggested.HeaderIs(peerA.HeadHeader).Stop();
        }
        public void Will_not_reorganize_on_same_chain_length()
        {
            SyncPeerMock peerA = new SyncPeerMock("A");

            peerA.AddBlocksUpTo(10, 0, 0);

            SyncPeerMock peerB = new SyncPeerMock("B");

            peerB.AddBlocksUpTo(10, 0, 1);

            When.Syncing
            .AfterProcessingGenesis()
            .AfterPeerIsAdded(peerA)
            .Wait()
            .BestSuggested.HeaderIs(peerA.HeadHeader)
            .AfterPeerIsAdded(peerB)
            .Wait()
            .BestSuggested.HeaderIs(peerA.HeadHeader).Stop();
        }
        public void Can_reorg_based_on_total_difficulty()
        {
            SyncPeerMock peerA = new SyncPeerMock("A");

            peerA.AddBlocksUpTo(10, 0, 0);

            SyncPeerMock peerB = new SyncPeerMock("B");

            peerB.AddHighDifficultyBlocksUpTo(6, 0, 1);

            When.Syncing
            .AfterProcessingGenesis()
            .AfterPeerIsAdded(peerA)
            .Wait()
            .BestSuggested.HeaderIs(peerA.HeadHeader)
            .AfterPeerIsAdded(peerB)
            .Wait()
            .BestSuggested.HeaderIs(peerB.HeadHeader).Stop();
        }
        public void Can_reorg_on_add_peer()
        {
            SyncPeerMock peerA = new SyncPeerMock("A");

            peerA.AddBlocksUpTo(QueueBasedSyncManager.MaxBatchSize, 0, 0);

            SyncPeerMock peerB = new SyncPeerMock("B");

            peerB.AddBlocksUpTo(QueueBasedSyncManager.MaxBatchSize * 2, 0, 1);

            When.Syncing
            .AfterProcessingGenesis()
            .AfterPeerIsAdded(peerA)
            .Wait()
            .BestSuggested.HeaderIs(peerA.HeadHeader)
            .AfterPeerIsAdded(peerB)
            .Wait()
            .BestSuggested.HeaderIs(peerB.HeadHeader).Stop();
        }
        public void Can_reorg_on_hint_block_message()
        {
            SyncPeerMock peerA = new SyncPeerMock("A");

            peerA.AddBlocksUpTo(3);

            SyncPeerMock peerB = new SyncPeerMock("B");

            peerB.AddBlocksUpTo(3);

            When.Syncing
            .AfterProcessingGenesis()
            .AfterPeerIsAdded(peerA)
            .AfterPeerIsAdded(peerB)
            .Wait()
            .After(() => peerB.AddBlocksUpTo(6))
            .AfterHintBlockMessage(peerB.HeadBlock, peerB)
            .Wait()
            .BestSuggested.HeaderIs(peerB.HeadHeader).Stop();
        }
        public void Can_extend_chain_on_new_block_when_high_difficulty_low_number()
        {
            SyncPeerMock peerA = new SyncPeerMock("A");

            peerA.AddBlocksUpTo(10, 0, 0);

            SyncPeerMock peerB = new SyncPeerMock("B");

            peerB.AddHighDifficultyBlocksUpTo(6, 0, 1);

            When.Syncing
            .AfterProcessingGenesis()
            .AfterPeerIsAdded(peerA)
            .Wait()
            .AfterPeerIsAdded(peerB)
            .Wait()
            .After(() => peerB.AddHighDifficultyBlocksUpTo(6, 0, 1))
            .AfterNewBlockMessage(peerB.HeadBlock, peerB)
            .BestSuggested.HeaderIs(peerB.HeadHeader).Stop();
        }
        public void Will_inform_connecting_peer_about_the_alternative_branch_with_same_difficulty()
        {
            SyncPeerMock peerA = new SyncPeerMock("A");

            peerA.AddBlocksUpTo(2);

            SyncPeerMock peerB = new SyncPeerMock("B");

            peerB.AddBlocksUpTo(2, 0, 1);

            When.Syncing
            .AfterProcessingGenesis()
            .AfterPeerIsAdded(peerA)
            .Wait()
            .AfterPeerIsAdded(peerB)
            .Wait()
            .BestSuggested.BlockHasNumber(2).Stop();

            Assert.AreNotEqual(peerB.HeadBlock.Hash, peerA.HeadBlock.Hash);
            Assert.AreEqual(peerB.ReceivedBlocks.Peek().Hash, peerA.HeadBlock.Hash);
        }
Exemple #19
0
        public async Task HealTreeWithoutBoundaryProofs()
        {
            DbContext dbContext = new DbContext(_logger, _logManager);

            TestItem.Tree.FillStateTreeWithTestAccounts(dbContext.RemoteStateTree);

            Keccak rootHash = dbContext.RemoteStateTree.RootHash;

            ProcessAccountRange(dbContext.RemoteStateTree, dbContext.LocalStateTree, 1, rootHash, TestItem.Tree.AccountsWithPaths);

            SyncPeerMock mock = new SyncPeerMock(dbContext.RemoteStateDb, dbContext.RemoteCodeDb);

            SafeContext ctx = PrepareDownloader(dbContext, mock);

            await ActivateAndWait(ctx, dbContext, 1024);

            DetailedProgress data = ctx.TreeFeed.GetDetailedProgress();


            dbContext.CompareTrees("END");
            Assert.AreEqual(dbContext.RemoteStateTree.RootHash, dbContext.LocalStateTree.RootHash);
            Assert.AreEqual(0, data.RequestedNodesCount);   // 4 boundary proof nodes stitched together => 0
        }
Exemple #20
0
        public async Task HealBigSquezedRandomTree()
        {
            DbContext dbContext = new DbContext(_logger, _logManager);

            int pathPoolCount = 100_000;

            Keccak[] pathPool = new Keccak[pathPoolCount];
            SortedDictionary <Keccak, Account> accounts = new();
            int updatesCount   = 0;
            int deletionsCount = 0;

            for (int i = 0; i < pathPoolCount; i++)
            {
                byte[] key = new byte[32];
                ((UInt256)i).ToBigEndian(key);
                Keccak keccak = new Keccak(key);
                pathPool[i] = keccak;
            }

            // generate Remote Tree
            for (int accountIndex = 0; accountIndex < 10000; accountIndex++)
            {
                Account account = TestItem.GenerateRandomAccount();
                Keccak  path    = pathPool[TestItem.Random.Next(pathPool.Length - 1)];

                dbContext.RemoteStateTree.Set(path, account);
                accounts[path] = account;
            }

            dbContext.RemoteStateTree.Commit(0);

            int startingHashIndex = 0;
            int endHashIndex      = 0;
            int blockJumps        = 5;

            for (int blockNumber = 1; blockNumber <= blockJumps; blockNumber++)
            {
                for (int i = 0; i < 19; i++)
                {
                    endHashIndex = startingHashIndex + 1000;

                    ProcessAccountRange(dbContext.RemoteStateTree, dbContext.LocalStateTree, blockNumber, dbContext.RemoteStateTree.RootHash,
                                        accounts.Where(a => a.Key >= pathPool[startingHashIndex] && a.Key <= pathPool[endHashIndex]).Select(a => new PathWithAccount(a.Key, a.Value)).ToArray());

                    startingHashIndex = endHashIndex + 1;
                }

                for (int accountIndex = 0; accountIndex < 1000; accountIndex++)
                {
                    Account account = TestItem.GenerateRandomAccount();
                    Keccak  path    = pathPool[TestItem.Random.Next(pathPool.Length - 1)];

                    if (accounts.ContainsKey(path))
                    {
                        if (TestItem.Random.NextSingle() > 0.5)
                        {
                            dbContext.RemoteStateTree.Set(path, account);
                            accounts[path] = account;
                            updatesCount++;
                        }
                        else
                        {
                            dbContext.RemoteStateTree.Set(path, null);
                            accounts.Remove(path);
                            deletionsCount++;
                        }
                    }
                    else
                    {
                        dbContext.RemoteStateTree.Set(path, account);
                        accounts[path] = account;
                    }
                }

                dbContext.RemoteStateTree.Commit(blockNumber);
            }

            endHashIndex = startingHashIndex + 1000;
            while (endHashIndex < pathPool.Length - 1)
            {
                endHashIndex = startingHashIndex + 1000;
                if (endHashIndex > pathPool.Length - 1)
                {
                    endHashIndex = pathPool.Length - 1;
                }

                ProcessAccountRange(dbContext.RemoteStateTree, dbContext.LocalStateTree, blockJumps, dbContext.RemoteStateTree.RootHash,
                                    accounts.Where(a => a.Key >= pathPool[startingHashIndex] && a.Key <= pathPool[endHashIndex]).Select(a => new PathWithAccount(a.Key, a.Value)).ToArray());


                startingHashIndex += 1000;
            }

            SyncPeerMock mock = new SyncPeerMock(dbContext.RemoteStateDb, dbContext.RemoteCodeDb);

            dbContext.LocalStateTree.RootHash = dbContext.RemoteStateTree.RootHash;
            SafeContext ctx = PrepareDownloader(dbContext, mock);

            await ActivateAndWait(ctx, dbContext, 9);

            DetailedProgress data = ctx.TreeFeed.GetDetailedProgress();

            dbContext.LocalStateTree.UpdateRootHash();
            dbContext.CompareTrees("END");
            _logger.Info($"REQUESTED NODES TO HEAL: {data.RequestedNodesCount}");
            Assert.IsTrue(data.RequestedNodesCount < accounts.Count / 2);
        }