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); }
public void Deletes_everything_after_the_missing_level() { MemDb blocksDb = new MemDb(); MemDb blockInfosDb = new MemDb(); MemDb headersDb = new MemDb(); BlockTree tree = new BlockTree(blocksDb, headersDb, blockInfosDb, new ChainLevelInfoRepository(blockInfosDb), MainnetSpecProvider.Instance, NullBloomStorage.Instance, LimboLogs.Instance); Block block0 = Build.A.Block.WithNumber(0).WithDifficulty(1).TestObject; Block block1 = Build.A.Block.WithNumber(1).WithDifficulty(2).WithParent(block0).TestObject; Block block2 = Build.A.Block.WithNumber(2).WithDifficulty(3).WithParent(block1).TestObject; Block block3 = Build.A.Block.WithNumber(3).WithDifficulty(4).WithParent(block2).TestObject; Block block4 = Build.A.Block.WithNumber(4).WithDifficulty(5).WithParent(block3).TestObject; Block block5 = Build.A.Block.WithNumber(5).WithDifficulty(6).WithParent(block4).TestObject; tree.SuggestBlock(block0); tree.SuggestBlock(block1); tree.SuggestBlock(block2); tree.SuggestBlock(block3); tree.SuggestBlock(block4); tree.SuggestHeader(block5.Header); tree.UpdateMainChain(block0); tree.UpdateMainChain(block1); tree.UpdateMainChain(block2); blockInfosDb.Delete(3); tree = new BlockTree(blocksDb, headersDb, blockInfosDb, new ChainLevelInfoRepository(blockInfosDb), MainnetSpecProvider.Instance, NullBloomStorage.Instance, LimboLogs.Instance); StartupBlockTreeFixer fixer = new StartupBlockTreeFixer(new SyncConfig(), tree, LimboNoErrorLogger.Instance); tree.Accept(fixer, CancellationToken.None); Assert.Null(blockInfosDb.Get(3), "level 3"); Assert.Null(blockInfosDb.Get(4), "level 4"); Assert.Null(blockInfosDb.Get(5), "level 5"); tree.Head.Header.Should().BeEquivalentTo(block2.Header, options => options.Excluding(t => t.MaybeParent)); tree.BestSuggestedHeader.Should().BeEquivalentTo(block2.Header, options => options.Excluding(t => t.MaybeParent)); tree.BestSuggestedBody.Should().BeEquivalentTo(block2.Body); tree.BestKnownNumber.Should().Be(2); }