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_length_is_as_expected()
        {
            StateTree tree = new StateTree();

            byte[]  code     = new byte[] { 1, 2, 3 };
            Account account1 = Build.An.Account.WithBalance(1).WithStorageRoot(TestItem.KeccakA).TestObject;
            Account account2 = Build.An.Account.WithBalance(2).TestObject;

            tree.Set(TestItem.AddressA, account1);
            tree.Set(TestItem.AddressB, account2);
            tree.Commit();

            AccountProofCollector accountProofCollector = new AccountProofCollector(TestItem.AddressA, new[] { Bytes.FromHexString("0x0000000000000000000000000000000000000000000000000000000000000000"), Bytes.FromHexString("0x0000000000000000000000000000000000000000000000000000000000000001") });

            tree.Accept(accountProofCollector, tree.RootHash, true);
            AccountProof proof = accountProofCollector.BuildResult();

            Assert.AreEqual(2, proof.StorageProofs.Length);
        }
        public void Proof_path_is_filled()
        {
            StateTree tree = new StateTree();

            byte[]  code     = new byte[] { 1, 2, 3 };
            Account account1 = Build.An.Account.WithBalance(1).WithStorageRoot(TestItem.KeccakA).TestObject;
            Account account2 = Build.An.Account.WithBalance(2).TestObject;

            tree.Set(TestItem.AddressA, account1);
            tree.Set(TestItem.AddressB, account2);
            tree.Commit();

            AccountProofCollector accountProofCollector = new AccountProofCollector(TestItem.AddressA);

            tree.Accept(accountProofCollector, tree.RootHash, true);
            AccountProof proof = accountProofCollector.BuildResult();

            Assert.AreEqual(3, proof.Proof.Length);
        }
        public void Non_existing_account_is_valid_even_when_leaf_is_the_last_part_of_the_proof()
        {
            StateTree tree = new StateTree();

            Account account1 = Build.An.Account.WithBalance(1).TestObject;

            tree.Set(TestItem.AddressA, account1);
            tree.Commit(0);

            AccountProofCollector accountProofCollector = new AccountProofCollector(TestItem.AddressC, new UInt256[] { 1, 2, 3 });

            tree.Accept(accountProofCollector, tree.RootHash, true);
            AccountProof proof = accountProofCollector.BuildResult();

            proof.Proof.Should().HaveCount(1);
            Assert.AreEqual(TestItem.AddressC, proof.Address);
            Assert.AreEqual(Keccak.OfAnEmptyString, proof.CodeHash);
            Assert.AreEqual(Keccak.EmptyTreeHash, proof.StorageRoot);
            Assert.AreEqual(UInt256.Zero, proof.Balance);
            Assert.AreEqual(new byte[] { 0 }, proof.StorageProofs[0].Value);
            Assert.AreEqual(new byte[] { 0 }, proof.StorageProofs[1].Value);
            Assert.AreEqual(new byte[] { 0 }, proof.StorageProofs[2].Value);
        }
Exemplo n.º 5
0
        // https://github.com/ethereum/EIPs/issues/1186
        public ResultWrapper <AccountProof> eth_getProof(Address accountAddress, byte[][] storageKeys, BlockParameter blockParameter)
        {
            BlockHeader header;

            try
            {
                header = _blockchainBridge.FindHeader(blockParameter);
                if (header == null)
                {
                    return(ResultWrapper <AccountProof> .Fail($"{blockParameter} block not found", ErrorCodes.ResourceNotFound, null));
                }
            }
            catch (Exception ex)
            {
                return(ResultWrapper <AccountProof> .Fail(ex.Message, ErrorCodes.InternalError, null));
            }

            AccountProofCollector accountProofCollector = new AccountProofCollector(accountAddress, storageKeys);

            _blockchainBridge.RunTreeVisitor(accountProofCollector, header.StateRoot);

            return(ResultWrapper <AccountProof> .Success(accountProofCollector.BuildResult()));
        }
        public void Chaotic_test()
        {
            const int accountsCount = 100;

            CryptoRandom random = new CryptoRandom();
            List <AddressWithStorage> addressesWithStorage = new List <AddressWithStorage>();

            for (int i = 0; i < accountsCount; i++)
            {
                AddressWithStorage addressWithStorage = new AddressWithStorage();
                addressWithStorage.StorageCells = new StorageCell[i];
                byte[] addressBytes = random.GenerateRandomBytes(20);
                addressWithStorage.Address = new Address(addressBytes);

                for (int j = 0; j < i; j++)
                {
                    byte[]      storageIndex = random.GenerateRandomBytes(32);
                    UInt256     index        = new UInt256(storageIndex);
                    StorageCell storageCell  = new StorageCell(addressWithStorage.Address, index);
                    addressWithStorage.StorageCells[j] = storageCell;
                }

                addressesWithStorage.Add(addressWithStorage);
            }

            IDb       memDb     = new MemDb();
            TrieStore trieStore = new TrieStore(memDb, LimboLogs.Instance);
            StateTree tree      = new StateTree(trieStore, LimboLogs.Instance);

            for (int i = 0; i < accountsCount; i++)
            {
                Account     account     = Build.An.Account.WithBalance((UInt256)i).TestObject;
                StorageTree storageTree = new StorageTree(trieStore, Keccak.EmptyTreeHash, LimboLogs.Instance);
                for (int j = 0; j < i; j++)
                {
                    storageTree.Set(addressesWithStorage[i].StorageCells[j].Index, new byte[1] {
                        1
                    });
                }

                storageTree.UpdateRootHash();
                storageTree.Commit(0);

                account = account.WithChangedStorageRoot(storageTree.RootHash);
                tree.Set(addressesWithStorage[i].Address, account);
            }

            tree.UpdateRootHash();
            tree.Commit(0);

            for (int i = 0; i < accountsCount; i++)
            {
                AccountProofCollector collector = new AccountProofCollector(addressesWithStorage[i].Address, addressesWithStorage[i].StorageCells.Select(sc => sc.Index).ToArray());
                tree.Accept(collector, tree.RootHash, true);

                AccountProof accountProof = collector.BuildResult();
                accountProof.Address.Should().Be(addressesWithStorage[i].Address);
                accountProof.Balance.Should().Be((UInt256)i);
                accountProof.Nonce.Should().Be(0);
                accountProof.CodeHash.Should().Be(Keccak.OfAnEmptyString);
                if (i != 0)
                {
                    accountProof.StorageRoot.Should().NotBe(Keccak.EmptyTreeHash);
                }
                accountProof.StorageProofs.Length.Should().Be(i);

                for (int j = 0; j < i; j++)
                {
                    byte[] indexBytes = new byte[32];
                    addressesWithStorage[i].StorageCells[j].Index.ToBigEndian(indexBytes.AsSpan());
                    accountProof.StorageProofs[j].Key.ToHexString().Should().Be(indexBytes.ToHexString(), $"{i} {j}");

                    TrieNode node = new TrieNode(NodeType.Unknown, accountProof.StorageProofs[j].Proof.Last());
                    node.ResolveNode(null);
                    // TestContext.Write($"|[{i},{j}]");
                    if (node.Value.Length != 1)
                    {
                        TestContext.WriteLine();
                        TestContext.WriteLine(addressesWithStorage[i].Address);
                        TestContext.WriteLine(i);
                        foreach (StorageCell storageCell in addressesWithStorage[i].StorageCells)
                        {
                            TestContext.WriteLine("storage: " + storageCell.Index);
                        }
                    }

                    node.Value.Should().BeEquivalentTo(new byte[] { 1 });
                }
            }
        }
        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}");
                }
            }
        }