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"); }
public void Shows_empty_values_when_account_is_missing() { IDb memDb = new MemDb(); StateTree tree = new StateTree(new TrieStore(memDb, LimboLogs.Instance), LimboLogs.Instance); byte[] code = new byte[] { 1, 2, 3 }; Account account2 = Build.An.Account.WithBalance(2).TestObject; tree.Set(TestItem.AddressB, account2); tree.Commit(0); TreeDumper dumper = new TreeDumper(); tree.Accept(dumper, tree.RootHash, true); Console.WriteLine(dumper.ToString()); AccountProofCollector accountProofCollector = new AccountProofCollector(TestItem.AddressA); tree.Accept(accountProofCollector, tree.RootHash, true); AccountProof proof = accountProofCollector.BuildResult(); Assert.AreEqual((UInt256)0, proof.Balance); Assert.AreEqual(UInt256.Zero, proof.Nonce); Assert.AreEqual(Keccak.OfAnEmptyString, proof.CodeHash); Assert.AreEqual(Keccak.EmptyTreeHash, proof.StorageRoot); }
public string DumpState() { TreeDumper dumper = new TreeDumper(); _tree.Accept(dumper, _tree.RootHash, true); return(dumper.ToString()); }
public static string DumpState(this IStateProvider stateProvider) { TreeDumper dumper = new TreeDumper(); stateProvider.Accept(dumper, stateProvider.StateRoot); return(dumper.ToString()); }
public void Storage_proofs_when_values_are_missing_setup() { byte[] a = Bytes.FromHexString("0x000000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaaaa"); byte[] b = Bytes.FromHexString("0x0000000000000000000000000bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); byte[] c = Bytes.FromHexString("0x00000000001ccccccccccccccccccccccccccccccccccccccccccccccccccccc"); byte[] d = Bytes.FromHexString("0x00000000001ddddddddddddddddddddddddddddddddddddddddddddddddddddd"); byte[] e = Bytes.FromHexString("0x00000000001eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"); IDb memDb = new MemDb(); TrieStore trieStore = new TrieStore(memDb, LimboLogs.Instance); StateTree tree = new StateTree(trieStore, LimboLogs.Instance); StorageTree storageTree = new StorageTree(trieStore, Keccak.EmptyTreeHash, LimboLogs.Instance); storageTree.Set(Keccak.Compute(a).Bytes, Rlp.Encode(Bytes.FromHexString("0xab12000000000000000000000000000000000000000000000000000000000000000000000000000000"))); storageTree.Set(Keccak.Compute(c).Bytes, Rlp.Encode(Bytes.FromHexString("0xab56000000000000000000000000000000000000000000000000000000000000000000000000000000"))); storageTree.Set(Keccak.Compute(e).Bytes, Rlp.Encode(Bytes.FromHexString("0xab9a000000000000000000000000000000000000000000000000000000000000000000000000000000"))); storageTree.Commit(0); byte[] code = new byte[] { 1, 2, 3 }; Account account1 = Build.An.Account.WithBalance(1).WithStorageRoot(storageTree.RootHash).TestObject; Account account2 = Build.An.Account.WithBalance(2).TestObject; tree.Set(TestItem.AddressA, account1); tree.Set(TestItem.AddressB, account2); tree.Commit(0); TreeDumper dumper = new TreeDumper(); tree.Accept(dumper, tree.RootHash, true); Console.WriteLine(dumper.ToString()); AccountProofCollector accountProofCollector = new AccountProofCollector(TestItem.AddressA, new byte[][] { a, b, c, d, e }); tree.Accept(accountProofCollector, tree.RootHash, true); AccountProof proof = accountProofCollector.BuildResult(); Assert.AreEqual("0xab12000000000000000000000000000000000000000000000000000000000000000000000000000000", proof.StorageProofs[0].Value?.ToHexString(true) ?? "0x"); Assert.AreEqual("0x00", proof.StorageProofs[1].Value?.ToHexString(true) ?? "0x"); Assert.AreEqual("0xab56000000000000000000000000000000000000000000000000000000000000000000000000000000", proof.StorageProofs[2].Value?.ToHexString(true) ?? "0x"); Assert.AreEqual("0x00", proof.StorageProofs[3].Value?.ToHexString(true) ?? "0x"); Assert.AreEqual("0xab9a000000000000000000000000000000000000000000000000000000000000000000000000000000", proof.StorageProofs[4].Value?.ToHexString(true) ?? "0x"); proof.StorageProofs[1].Proof.Should().HaveCount(3); proof.StorageProofs[3].Proof.Should().HaveCount(2); }
public void Storage_proofs_have_values_set_complex_3_setup() { byte[] a = Bytes.FromHexString("0x000000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaaaa"); byte[] b = Bytes.FromHexString("0x0000000000000000000000000bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); byte[] c = Bytes.FromHexString("0x00000000001ccccccccccccccccccccccccccccccccccccccccccccccccccccc"); byte[] d = Bytes.FromHexString("0x00000000001ddddddddddddddddddddddddddddddddddddddddddddddddddddd"); byte[] e = Bytes.FromHexString("0x00000000001eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"); IDb memDb = new MemDb(); StateTree tree = new StateTree(memDb); StorageTree storageTree = new StorageTree(memDb); storageTree.Set(Keccak.Compute(a).Bytes, Rlp.Encode(Bytes.FromHexString("0xab12000000000000000000000000000000000000000000000000000000000000000000000000000000"))); storageTree.Set(Keccak.Compute(b).Bytes, Rlp.Encode(Bytes.FromHexString("0xab34000000000000000000000000000000000000000000000000000000000000000000000000000000"))); storageTree.Set(Keccak.Compute(c).Bytes, Rlp.Encode(Bytes.FromHexString("0xab56000000000000000000000000000000000000000000000000000000000000000000000000000000"))); storageTree.Set(Keccak.Compute(d).Bytes, Rlp.Encode(Bytes.FromHexString("0xab78000000000000000000000000000000000000000000000000000000000000000000000000000000"))); storageTree.Set(Keccak.Compute(e).Bytes, Rlp.Encode(Bytes.FromHexString("0xab9a000000000000000000000000000000000000000000000000000000000000000000000000000000"))); storageTree.Commit(); byte[] code = new byte[] { 1, 2, 3 }; Account account1 = Build.An.Account.WithBalance(1).WithStorageRoot(storageTree.RootHash).TestObject; Account account2 = Build.An.Account.WithBalance(2).TestObject; tree.Set(TestItem.AddressA, account1); tree.Set(TestItem.AddressB, account2); tree.Commit(); TreeDumper dumper = new TreeDumper(); tree.Accept(dumper, tree.RootHash, true); Console.WriteLine(dumper.ToString()); AccountProofCollector accountProofCollector = new AccountProofCollector(TestItem.AddressA, new byte[][] { a, b, c, d, e }); tree.Accept(accountProofCollector, tree.RootHash, true); AccountProof proof = accountProofCollector.BuildResult(); Assert.AreEqual("0xab12000000000000000000000000000000000000000000000000000000000000000000000000000000", proof.StorageProofs[0].Value.ToHexString(true)); Assert.AreEqual("0xab34000000000000000000000000000000000000000000000000000000000000000000000000000000", proof.StorageProofs[1].Value.ToHexString(true)); Assert.AreEqual("0xab56000000000000000000000000000000000000000000000000000000000000000000000000000000", proof.StorageProofs[2].Value.ToHexString(true)); Assert.AreEqual("0xab78000000000000000000000000000000000000000000000000000000000000000000000000000000", proof.StorageProofs[3].Value.ToHexString(true)); Assert.AreEqual("0xab9a000000000000000000000000000000000000000000000000000000000000000000000000000000", proof.StorageProofs[4].Value.ToHexString(true)); }
public void Storage_proofs_have_values_set_complex_2_setup() { Keccak a = new Keccak("0x000000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaaaa"); Keccak b = new Keccak("0x0000000000000000000000000bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); Keccak c = new Keccak("0x0000000000cccccccccccccccccccccccccccccccccccccccccccccccccccccc"); Keccak d = new Keccak("0x0000000000dddddddddddddddddddddddddddddddddddddddddddddddddddddd"); Keccak e = new Keccak("0x0000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"); IDb memDb = new MemDb(); StateTree tree = new StateTree(memDb); StorageTree storageTree = new StorageTree(memDb); storageTree.Set(a.Bytes, Rlp.Encode(Bytes.FromHexString("0xab12000000000000000000000000000000000000000000000000000000000000000000000000000000"))); storageTree.Set(b.Bytes, Rlp.Encode(Bytes.FromHexString("0xab34000000000000000000000000000000000000000000000000000000000000000000000000000000"))); storageTree.Set(c.Bytes, Rlp.Encode(Bytes.FromHexString("0xab56000000000000000000000000000000000000000000000000000000000000000000000000000000"))); storageTree.Set(d.Bytes, Rlp.Encode(Bytes.FromHexString("0xab78000000000000000000000000000000000000000000000000000000000000000000000000000000"))); storageTree.Set(e.Bytes, Rlp.Encode(Bytes.FromHexString("0xab9a000000000000000000000000000000000000000000000000000000000000000000000000000000"))); storageTree.Commit(); byte[] code = new byte[] { 1, 2, 3 }; Account account1 = Build.An.Account.WithBalance(1).WithStorageRoot(storageTree.RootHash).TestObject; Account account2 = Build.An.Account.WithBalance(2).TestObject; tree.Set(TestItem.AddressA, account1); tree.Set(TestItem.AddressB, account2); tree.Commit(); TreeDumper dumper = new TreeDumper(); tree.Accept(dumper, memDb, tree.RootHash); Console.WriteLine(dumper.ToString()); ProofCollector proofCollector = new ProofCollector(TestItem.AddressA, new Keccak[] { a, b, c, d, e }); tree.Accept(proofCollector, memDb, tree.RootHash); AccountProof proof = proofCollector.BuildResult(); Assert.AreEqual("0xab12000000000000000000000000000000000000000000000000000000000000000000000000000000", proof.StorageProofs[0].Value.ToHexString(true)); Assert.AreEqual("0xab34000000000000000000000000000000000000000000000000000000000000000000000000000000", proof.StorageProofs[1].Value.ToHexString(true)); Assert.AreEqual("0xab56000000000000000000000000000000000000000000000000000000000000000000000000000000", proof.StorageProofs[2].Value.ToHexString(true)); Assert.AreEqual("0xab78000000000000000000000000000000000000000000000000000000000000000000000000000000", proof.StorageProofs[3].Value.ToHexString(true)); Assert.AreEqual("0xab9a000000000000000000000000000000000000000000000000000000000000000000000000000000", proof.StorageProofs[4].Value.ToHexString(true)); }
public void _Test_storage_failed_case(string historicallyFailingCase) { string[] lines = historicallyFailingCase.Split(Environment.NewLine); int storageCount = lines.Length - 2; IDb memDb = new MemDb(); TrieStore trieStore = new TrieStore(memDb, LimboLogs.Instance); StateTree tree = new StateTree(trieStore, LimboLogs.Instance); Address address = new Address(Bytes.FromHexString(lines[0])); int accountIndex = int.Parse(lines[1]); UInt256[] indexes = new UInt256[storageCount]; for (int j = 0; j < storageCount; j++) { indexes[j] = UInt256.Parse(lines[j + 2].Replace("storage: ", string.Empty)); } AddressWithStorage addressWithStorage = new AddressWithStorage(); addressWithStorage.StorageCells = new StorageCell[storageCount]; addressWithStorage.Address = address; StorageTree storageTree = new StorageTree(trieStore, Keccak.EmptyTreeHash, LimboLogs.Instance); for (int j = 0; j < storageCount; j++) { UInt256 index = UInt256.Parse(lines[j + 2].Replace("storage: ", string.Empty)); StorageCell storageCell = new StorageCell(address, index); addressWithStorage.StorageCells[j] = storageCell; byte[] rawKey = new byte[32]; addressWithStorage.StorageCells[j].Index.ToBigEndian(rawKey); TestContext.WriteLine($"Set {Keccak.Compute(rawKey).Bytes.ToHexString()}"); storageTree.Set(addressWithStorage.StorageCells[j].Index, new byte[] { 1 }); storageTree.UpdateRootHash(); storageTree.Commit(0); } Account account = Build.An.Account.WithBalance((UInt256)accountIndex).WithStorageRoot(storageTree.RootHash).TestObject; tree.Set(addressWithStorage.Address, account); tree.UpdateRootHash(); tree.Commit(0); TreeDumper treeDumper = new TreeDumper(); tree.Accept(treeDumper, tree.RootHash, true); TestContext.WriteLine(treeDumper.ToString()); AccountProofCollector collector = new AccountProofCollector(address, indexes); tree.Accept(collector, tree.RootHash, true); AccountProof accountProof = collector.BuildResult(); accountProof.Address.Should().Be(address); accountProof.Balance.Should().Be((UInt256)accountIndex); accountProof.Nonce.Should().Be(0); accountProof.CodeHash.Should().Be(Keccak.OfAnEmptyString); if (accountIndex != 0) { accountProof.StorageRoot.Should().NotBe(Keccak.EmptyTreeHash); } accountProof.StorageProofs.Length.Should().Be(accountIndex); for (int j = 0; j < accountProof.StorageProofs.Length; j++) { TrieNode node = new TrieNode(NodeType.Unknown, accountProof.StorageProofs[j].Proof.Last()); node.ResolveNode(new TrieStore(memDb, NullLogManager.Instance)); if (node.Value.Length != 1) { TestContext.WriteLine($"{j}"); // throw new InvalidDataException($"{j}"); } } }