Ejemplo n.º 1
0
        public TrieStats CollectStats()
        {
            TrieStatsCollector collector = new TrieStatsCollector(_codeDb, _logManager);

            _tree.Accept(collector, _tree.RootHash, true);
            return(collector.Stats);
        }
        public static TrieStats CollectStats(this IStateProvider stateProvider, IKeyValueStore codeStorage, ILogManager logManager)
        {
            TrieStatsCollector collector = new TrieStatsCollector(codeStorage, logManager);

            stateProvider.Accept(collector, stateProvider.StateRoot);
            return(collector.Stats);
        }
Ejemplo n.º 3
0
        public void Can_collect_stats()
        {
            MemDb           memDb           = new MemDb();
            IDb             stateDb         = memDb;
            TrieStore       trieStore       = new TrieStore(stateDb, new MemoryLimit(0.MB()), Persist.EveryBlock, LimboLogs.Instance);
            StateProvider   stateProvider   = new StateProvider(trieStore, stateDb, LimboLogs.Instance);
            StorageProvider storageProvider = new StorageProvider(trieStore, stateProvider, LimboLogs.Instance);

            stateProvider.CreateAccount(TestItem.AddressA, 1);
            Keccak codeHash = stateProvider.UpdateCode(new byte[] { 1, 2, 3 });

            stateProvider.UpdateCodeHash(TestItem.AddressA, codeHash, Istanbul.Instance);

            stateProvider.CreateAccount(TestItem.AddressB, 1);
            Keccak codeHash2 = stateProvider.UpdateCode(new byte[] { 1, 2, 3, 4 });

            stateProvider.UpdateCodeHash(TestItem.AddressB, codeHash2, Istanbul.Instance);

            for (int i = 0; i < 1000; i++)
            {
                StorageCell storageCell = new StorageCell(TestItem.AddressA, (UInt256)i);
                storageProvider.Set(storageCell, new byte[] { (byte)i });
            }

            storageProvider.Commit();
            stateProvider.Commit(Istanbul.Instance);

            storageProvider.CommitTrees(0);
            stateProvider.CommitTree(0);
            storageProvider.CommitTrees(1);
            stateProvider.CommitTree(1);

            memDb.Delete(codeHash2); // missing code
            Keccak storageKey = new Keccak("0x345e54154080bfa9e8f20c99d7a0139773926479bc59e5b4f830ad94b6425332");

            memDb.Delete(storageKey); // deletes some storage
            trieStore.ClearCache();

            TrieStatsCollector statsCollector = new TrieStatsCollector(stateDb, LimboLogs.Instance);

            stateProvider.Accept(statsCollector, stateProvider.StateRoot);
            var stats = statsCollector.Stats;

            stats.CodeCount.Should().Be(1);
            stats.MissingCode.Should().Be(1);

            stats.NodesCount.Should().Be(1348);

            stats.StateBranchCount.Should().Be(1);
            stats.StateExtensionCount.Should().Be(1);
            stats.AccountCount.Should().Be(2);

            stats.StorageCount.Should().Be(1343);
            stats.StorageBranchCount.Should().Be(337);
            stats.StorageExtensionCount.Should().Be(12);
            stats.StorageLeafCount.Should().Be(994);
            stats.MissingStorage.Should().Be(1);
        }
Ejemplo n.º 4
0
        public void Can_accepts_visitors()
        {
            StateProvider provider = new StateProvider(new StateDb(new MemDb()), Substitute.For <IDb>(), Logger);

            provider.CreateAccount(TestItem.AddressA, 1.Ether());
            provider.Commit(MuirGlacier.Instance);
            provider.CommitTree();

            TrieStatsCollector visitor = new TrieStatsCollector(new MemDb(), LimboLogs.Instance);

            provider.Accept(visitor, provider.StateRoot);
        }
Ejemplo n.º 5
0
            public void CompareTrees(string stage, bool skipLogs = false)
            {
                DbContext dbContext = new DbContext(_logger);

                if (!skipLogs)
                {
                    _logger.Info($"==================== {stage} ====================");
                }
                dbContext.LocalStateTree.RootHash = dbContext.RemoteStateTree.RootHash;

                if (!skipLogs)
                {
                    _logger.Info("-------------------- REMOTE --------------------");
                }
                TreeDumper dumper = new TreeDumper();

                dbContext.RemoteStateTree.Accept(dumper, dbContext.RemoteStateTree.RootHash, true);
                string remote = dumper.ToString();

                if (!skipLogs)
                {
                    _logger.Info(remote);
                }
                if (!skipLogs)
                {
                    _logger.Info("-------------------- LOCAL --------------------");
                }
                dumper.Reset();
                dbContext.LocalStateTree.Accept(dumper, dbContext.LocalStateTree.RootHash, true);
                string local = dumper.ToString();

                if (!skipLogs)
                {
                    _logger.Info(local);
                }

                if (stage == "END")
                {
                    Assert.AreEqual(remote, local, $"{remote}{Environment.NewLine}{local}");
                    TrieStatsCollector collector = new TrieStatsCollector(dbContext.LocalCodeDb, new OneLoggerLogManager(_logger));
                    dbContext.LocalStateTree.Accept(collector, dbContext.LocalStateTree.RootHash, true);
                    Assert.AreEqual(0, collector.Stats.MissingCode);
                }

                //            Assert.AreEqual(dbContext._remoteCodeDb.Keys.OrderBy(k => k, Bytes.Comparer).ToArray(), dbContext._localCodeDb.Keys.OrderBy(k => k, Bytes.Comparer).ToArray(), "keys");
                //            Assert.AreEqual(dbContext._remoteCodeDb.Values.OrderBy(k => k, Bytes.Comparer).ToArray(), dbContext._localCodeDb.Values.OrderBy(k => k, Bytes.Comparer).ToArray(), "values");
                //
                //            Assert.AreEqual(dbContext._remoteDb.Keys.OrderBy(k => k, Bytes.Comparer).ToArray(), _localDb.Keys.OrderBy(k => k, Bytes.Comparer).ToArray(), "keys");
                //            Assert.AreEqual(dbContext._remoteDb.Values.OrderBy(k => k, Bytes.Comparer).ToArray(), _localDb.Values.OrderBy(k => k, Bytes.Comparer).ToArray(), "values");
            }