Exemple #1
0
        public void Single_leaf_and_keep_for_multiple_dispatches_then_delete()
        {
            MemDb        memDb        = new MemDb();
            TrieStore    trieStore    = new TrieStore(memDb, Prune.WhenCacheReaches(1.MB()), new ConstantInterval(4), LimboLogs.Instance);
            PatriciaTree patriciaTree = new PatriciaTree(trieStore, _logManager);

            patriciaTree.Commit(0);
            patriciaTree.Commit(1);
            patriciaTree.Commit(2);
            patriciaTree.Set(_keyA, _longLeaf1);
            patriciaTree.Commit(3);
            patriciaTree.Commit(4);
            patriciaTree.Set(_keyA, Array.Empty <byte>());
            patriciaTree.Commit(5);
            patriciaTree.Set(_keyB, _longLeaf2);
            patriciaTree.Commit(6);
            patriciaTree.Commit(7);
            patriciaTree.Commit(8);
            patriciaTree.Commit(9);
            patriciaTree.Commit(10);
            patriciaTree.Commit(11);
            patriciaTree.Set(_keyB, Array.Empty <byte>());
            patriciaTree.Commit(12);
            patriciaTree.Commit(13);
            patriciaTree.UpdateRootHash();

            // leaf (root)
            memDb.Keys.Should().HaveCount(2);

            PatriciaTree checkTree = CreateCheckTree(memDb, patriciaTree);

            checkTree.Get(_keyA).Should().BeNull();
            checkTree.Get(_keyB).Should().BeNull();
        }
Exemple #2
0
        public async Task Can_ask_about_storage_in_parallel()
        {
            StorageCell     storageCell     = new StorageCell(_address1, UInt256.One);
            IReleaseSpec    spec            = MuirGlacier.Instance;
            MemDb           stateDb         = new MemDb();
            TrieStore       trieStore       = new TrieStore(stateDb, Logger);
            StateProvider   provider        = new StateProvider(trieStore, new MemDb(), Logger);
            StorageProvider storageProvider = new StorageProvider(trieStore, provider, Logger);

            void UpdateStorageValue(byte[] newValue)
            {
                storageProvider.Set(storageCell, newValue);
            }

            void AddOneToBalance()
            {
                provider.AddToBalance(_address1, 1, spec);
            }

            void CommitEverything()
            {
                storageProvider.Commit();
                storageProvider.CommitTrees(0);
                provider.Commit(spec);
                provider.CommitTree(0);
            }

            provider.CreateAccount(_address1, 1);
            CommitEverything();

            AddOneToBalance();
            UpdateStorageValue(new byte[] { 1 });
            CommitEverything();
            Keccak stateRoot0 = provider.StateRoot;

            AddOneToBalance();
            UpdateStorageValue(new byte[] { 2 });
            CommitEverything();
            Keccak stateRoot1 = provider.StateRoot;

            AddOneToBalance();
            UpdateStorageValue(new byte[] { 3 });
            CommitEverything();
            Keccak stateRoot2 = provider.StateRoot;

            AddOneToBalance();
            UpdateStorageValue(new byte[] { 4 });
            CommitEverything();
            Keccak stateRoot3 = provider.StateRoot;

            StateReader reader =
                new StateReader(new TrieStore(stateDb, LimboLogs.Instance), Substitute.For <IDb>(), Logger);

            Task a = StartStorageTask(reader, stateRoot0, storageCell, new byte[] { 1 });
            Task b = StartStorageTask(reader, stateRoot1, storageCell, new byte[] { 2 });
            Task c = StartStorageTask(reader, stateRoot2, storageCell, new byte[] { 3 });
            Task d = StartStorageTask(reader, stateRoot3, storageCell, new byte[] { 4 });

            await Task.WhenAll(a, b, c, d);
        }
Exemple #3
0
        public void Test_update_many(int i)
        {
            MemDb        memDb        = new MemDb();
            TrieStore    trieStore    = new TrieStore(memDb, new MemoryLimit(128.MB()), Persist.EveryBlock, _logManager);
            PatriciaTree patriciaTree = new PatriciaTree(trieStore, _logManager);

            for (int j = 0; j < i; j++)
            {
                Keccak key   = TestItem.Keccaks[j];
                byte[] value = GenerateIndexedAccountRlp(j);
                patriciaTree.Set(key.Bytes, value);
            }

            for (int j = 0; j < i; j++)
            {
                Keccak key   = TestItem.Keccaks[j];
                byte[] value = GenerateIndexedAccountRlp(j + 1);
                patriciaTree.Set(key.Bytes, value);
            }

            patriciaTree.Commit(0);
            patriciaTree.UpdateRootHash();

            PatriciaTree checkTree = CreateCheckTree(memDb, patriciaTree);

            for (int j = 0; j < i; j++)
            {
                Keccak key   = TestItem.Keccaks[j];
                byte[] value = GenerateIndexedAccountRlp(j + 1);
                checkTree.Get(key.Bytes).Should().BeEquivalentTo(value, $@"{i} {j}");
            }
        }
        public void Dispatcher_will_try_to_clear_memory_the_soonest_possible()
        {
            TrieNode trieNode1 = new TrieNode(NodeType.Leaf, new byte[0]);

            trieNode1.ResolveKey(null !, true);
            TrieNode trieNode2 = new TrieNode(NodeType.Leaf, new byte[1]);

            trieNode2.ResolveKey(null !, true);

            TrieNode trieNode3 = new TrieNode(NodeType.Leaf, new byte[2]);

            trieNode3.ResolveKey(null !, true);

            TrieNode trieNode4 = new TrieNode(NodeType.Leaf, new byte[3]);

            trieNode4.ResolveKey(null !, true);

            TrieStore trieStore = new TrieStore(new MemDb(), new MemoryLimit(512), No.Persistence, _logManager);

            trieStore.CommitNode(1234, new NodeCommitInfo(trieNode1));
            trieStore.CommitNode(1234, new NodeCommitInfo(trieNode2));
            trieStore.FinishBlockCommit(TrieType.State, 1234, trieNode2);
            trieStore.CommitNode(1235, new NodeCommitInfo(trieNode3));
            trieStore.CommitNode(1235, new NodeCommitInfo(trieNode4));
            trieStore.MemoryUsedByDirtyCache.Should().Be(
                trieNode1.GetMemorySize(false) +
                trieNode2.GetMemorySize(false) +
                trieNode3.GetMemorySize(false) +
                trieNode4.GetMemorySize(false));
        }
        public void Storage_proofs_have_keys_set()
        {
            IDb         memDb       = new MemDb();
            ITrieStore  trieStore   = new TrieStore(memDb, LimboLogs.Instance);
            StateTree   tree        = new StateTree(trieStore, LimboLogs.Instance);
            StorageTree storageTree = new StorageTree(trieStore, Keccak.EmptyTreeHash, LimboLogs.Instance);

            storageTree.Set(UInt256.Zero, Bytes.FromHexString("0xab12000000000000000000000000000000000000000000000000000000000000000000000000000000"));
            storageTree.Set(UInt256.One, Bytes.FromHexString("0xab34000000000000000000000000000000000000000000000000000000000000000000000000000000"));
            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);

            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("0x0000000000000000000000000000000000000000000000000000000000000000", proof.StorageProofs[0].Key.ToHexString(true));
            Assert.AreEqual("0x0000000000000000000000000000000000000000000000000000000000000001", proof.StorageProofs[1].Key.ToHexString(true));
        }
Exemple #6
0
        public void Setup()
        {
            IDbProvider              memDbProvider            = TestMemDbProvider.Init();
            TrieStore                trieStore                = new TrieStore(new MemDb(), LimboLogs.Instance);
            StateProvider            stateProvider            = new StateProvider(trieStore, new MemDb(), LimboLogs.Instance);
            StorageProvider          storageProvider          = new StorageProvider(trieStore, stateProvider, LimboLogs.Instance);
            ChainLevelInfoRepository chainLevelInfoRepository = new ChainLevelInfoRepository(memDbProvider);
            ISpecProvider            specProvider             = MainnetSpecProvider.Instance;
            IBloomStorage            bloomStorage             = NullBloomStorage.Instance;
            EthereumEcdsa            ecdsa = new EthereumEcdsa(1, LimboLogs.Instance);

            TxPool.TxPool txPool = new TxPool.TxPool(
                NullTxStorage.Instance,
                ecdsa,
                specProvider,
                new TxPoolConfig(),
                stateProvider,
                LimboLogs.Instance);
            _blockTree = new BlockTree(
                memDbProvider,
                chainLevelInfoRepository,
                specProvider,
                bloomStorage,
                new SyncConfig(),
                LimboLogs.Instance);
            BlockhashProvider blockhashProvider = new BlockhashProvider(_blockTree, LimboLogs.Instance);
            VirtualMachine    virtualMachine    = new VirtualMachine(
                stateProvider,
                storageProvider,
                blockhashProvider,
                specProvider,
                LimboLogs.Instance);
            TransactionProcessor transactionProcessor = new TransactionProcessor(
                specProvider,
                stateProvider,
                storageProvider,
                virtualMachine,
                LimboLogs.Instance);
            BlockProcessor blockProcessor = new BlockProcessor(
                MainnetSpecProvider.Instance,
                Always.Valid,
                new RewardCalculator(specProvider),
                transactionProcessor,
                stateProvider,
                storageProvider,
                txPool,
                NullReceiptStorage.Instance,
                new WitnessCollector(memDbProvider.StateDb, LimboLogs.Instance),
                LimboLogs.Instance);

            _blockchainProcessor = new BlockchainProcessor(
                _blockTree,
                blockProcessor,
                new RecoverSignatures(
                    ecdsa,
                    txPool,
                    specProvider,
                    LimboLogs.Instance),
                LimboLogs.Instance, BlockchainProcessor.Options.Default);
        }
Exemple #7
0
        public void Non_existing()
        {
            StorageCell  storageCell = new StorageCell(_address1, UInt256.One);
            IReleaseSpec spec        = MuirGlacier.Instance;

            MemDb           stateDb         = new MemDb();
            TrieStore       trieStore       = new TrieStore(stateDb, Logger);
            StateProvider   provider        = new StateProvider(trieStore, new MemDb(), Logger);
            StorageProvider storageProvider = new StorageProvider(trieStore, provider, Logger);

            void CommitEverything()
            {
                storageProvider.Commit();
                storageProvider.CommitTrees(0);
                provider.Commit(spec);
                provider.CommitTree(0);
            }

            provider.CreateAccount(_address1, 1);
            storageProvider.Set(storageCell, new byte[] { 1 });
            CommitEverything();
            Keccak stateRoot0 = provider.StateRoot;

            StateReader reader =
                new StateReader(new TrieStore(stateDb, LimboLogs.Instance), Substitute.For <IDb>(), Logger);
            Keccak storageRoot = reader.GetStorageRoot(stateRoot0, _address1);

            reader.GetStorage(storageRoot, storageCell.Index + 1).Should().BeEquivalentTo(new byte[] { 0 });
            reader.GetStorage(Keccak.EmptyTreeHash, storageCell.Index + 1).Should().BeEquivalentTo(new byte[] { 0 });
        }
        public void Prepared_block_contains_author_field()
        {
            IDb                   stateDb              = new MemDb();
            IDb                   codeDb               = new MemDb();
            TrieStore             trieStore            = new TrieStore(stateDb, LimboLogs.Instance);
            IStateProvider        stateProvider        = new StateProvider(trieStore, codeDb, LimboLogs.Instance);
            ITransactionProcessor transactionProcessor = Substitute.For <ITransactionProcessor>();
            BlockProcessor        processor            = new BlockProcessor(
                RinkebySpecProvider.Instance,
                TestBlockValidator.AlwaysValid,
                NoBlockRewards.Instance,
                transactionProcessor,
                stateProvider,
                new StorageProvider(trieStore, stateProvider, LimboLogs.Instance),
                NullTxPool.Instance,
                NullReceiptStorage.Instance,
                NullWitnessCollector.Instance,
                LimboLogs.Instance);

            BlockHeader header = Build.A.BlockHeader.WithAuthor(TestItem.AddressD).TestObject;
            Block       block  = Build.A.Block.WithHeader(header).TestObject;

            Block[] processedBlocks = processor.Process(
                Keccak.EmptyTreeHash,
                new List <Block> {
                block
            },
                ProcessingOptions.None,
                NullBlockTracer.Instance);
            Assert.AreEqual(1, processedBlocks.Length, "length");
            Assert.AreEqual(block.Author, processedBlocks[0].Author, "author");
        }
Exemple #9
0
        public void Extension_branch_extension_and_leaf_then_branch_leaf_leaf()
        {
            /* R
             * E - - - - - - - - - - - - - - -
             * B B B B B B B B B B B B B B B B
             * E L - - - - - - - - - - - - - -
             * E - - - - - - - - - - - - - - -
             * B B B B B B B B B B B B B B B B
             * L L - - - - - - - - - - - - - - */

            byte[] key1 = Bytes.FromHexString("000000100000000aa");
            byte[] key2 = Bytes.FromHexString("000000100000000bb");
            byte[] key3 = Bytes.FromHexString("000000200000000cc");

            MemDb        memDb        = new MemDb();
            TrieStore    trieStore    = new TrieStore(memDb, Prune.WhenCacheReaches(1.MB()), Persist.EveryBlock, _logManager);
            PatriciaTree patriciaTree = new PatriciaTree(trieStore, _logManager);

            patriciaTree.Set(key1, _longLeaf1);
            patriciaTree.Set(key2, _longLeaf1);
            patriciaTree.Set(key3, _longLeaf1);
            patriciaTree.UpdateRootHash();
            patriciaTree.Commit(0);

            memDb.Keys.Should().HaveCount(7);
            PatriciaTree checkTree = CreateCheckTree(memDb, patriciaTree);

            checkTree.Get(key1).Should().BeEquivalentTo(_longLeaf1);
            checkTree.Get(key2).Should().BeEquivalentTo(_longLeaf1);
            checkTree.Get(key3).Should().BeEquivalentTo(_longLeaf1);
        }
Exemple #10
0
        public void When_two_branches_with_two_same_children_change_one_and_change_back_next_block()
        {
            MemDb        memDb        = new MemDb();
            TrieStore    trieStore    = new TrieStore(memDb, Prune.WhenCacheReaches(1.MB()), Persist.EveryBlock, _logManager);
            PatriciaTree patriciaTree = new PatriciaTree(trieStore, _logManager);

            patriciaTree.Set(_keyA, _longLeaf1);
            patriciaTree.Set(_keyB, _longLeaf1);
            patriciaTree.Set(_keyC, _longLeaf1);
            patriciaTree.Set(_keyD, _longLeaf1);
            patriciaTree.UpdateRootHash();
            patriciaTree.Commit(0);
            patriciaTree.Set(_keyA, _longLeaf3);
            patriciaTree.Set(_keyA, _longLeaf1);
            patriciaTree.UpdateRootHash();
            patriciaTree.Commit(1);

            memDb.Keys.Should().HaveCount(5);
            PatriciaTree checkTree = CreateCheckTree(memDb, patriciaTree);

            checkTree.Get(_keyA).Should().BeEquivalentTo(_longLeaf1);
            checkTree.Get(_keyB).Should().BeEquivalentTo(_longLeaf1);
            checkTree.Get(_keyC).Should().BeEquivalentTo(_longLeaf1);
            checkTree.Get(_keyD).Should().BeEquivalentTo(_longLeaf1);
        }
Exemple #11
0
        public void Test_add_and_delete_many_next_block(int i)
        {
            MemDb        memDb        = new MemDb();
            TrieStore    trieStore    = new TrieStore(memDb, new MemoryLimit(128.MB()), Persist.EveryBlock, _logManager);
            PatriciaTree patriciaTree = new PatriciaTree(trieStore, _logManager);

            for (int j = 0; j < i; j++)
            {
                Keccak key   = TestItem.Keccaks[j];
                byte[] value = GenerateIndexedAccountRlp(j);
                patriciaTree.Set(key.Bytes, value);
            }

            patriciaTree.Commit(0);

            for (int j = 0; j < i; j++)
            {
                Keccak key = TestItem.Keccaks[j];
                patriciaTree.Set(key.Bytes, Array.Empty <byte>());
            }

            patriciaTree.Commit(1);
            patriciaTree.UpdateRootHash();

            PatriciaTree checkTree = CreateCheckTree(memDb, patriciaTree);

            for (int j = 0; j < i; j++)
            {
                Keccak key = TestItem.Keccaks[j];
                checkTree.Get(key.Bytes).Should().BeNull($@"{i} {j}");
            }
        }
Exemple #12
0
        public void Two_branches_exactly_same_leaf_then_one_removed()
        {
            MemDb     memDb     = new MemDb();
            TrieStore trieStore = new TrieStore(
                memDb,
                Prune.WhenCacheReaches(1.MB()),
                Persist.EveryBlock,
                LimboLogs.Instance);
            PatriciaTree patriciaTree = new PatriciaTree(trieStore, _logManager);

            patriciaTree.Set(_keyA, _longLeaf1);
            patriciaTree.Set(_keyB, _longLeaf1);
            patriciaTree.Set(_keyC, _longLeaf1);
            patriciaTree.Set(_keyD, _longLeaf1);
            patriciaTree.Set(_keyA, Array.Empty <byte>());
            patriciaTree.Commit(0);

            // leaf (root)
            memDb.Keys.Should().HaveCount(6);
            PatriciaTree checkTree = CreateCheckTree(memDb, patriciaTree);

            checkTree.Get(_keyA).Should().BeNull();
            checkTree.Get(_keyB).Should().BeEquivalentTo(_longLeaf1);
            checkTree.Get(_keyC).Should().BeEquivalentTo(_longLeaf1);
            checkTree.Get(_keyD).Should().BeEquivalentTo(_longLeaf1);
        }
        public void Can_store_a_witness()
        {
            IDb stateDb   = new MemDb();
            IDb codeDb    = new MemDb();
            var trieStore = new TrieStore(stateDb, LimboLogs.Instance);

            IStateProvider        stateProvider        = new StateProvider(trieStore, codeDb, LimboLogs.Instance);
            ITransactionProcessor transactionProcessor = Substitute.For <ITransactionProcessor>();
            IWitnessCollector     witnessCollector     = Substitute.For <IWitnessCollector>();
            BlockProcessor        processor            = new(
                RinkebySpecProvider.Instance,
                TestBlockValidator.AlwaysValid,
                NoBlockRewards.Instance,
                new BlockProcessor.BlockValidationTransactionsExecutor(transactionProcessor, stateProvider),
                stateProvider,
                new StorageProvider(trieStore, stateProvider, LimboLogs.Instance),
                NullReceiptStorage.Instance,
                witnessCollector,
                LimboLogs.Instance);

            BlockHeader header = Build.A.BlockHeader.WithAuthor(TestItem.AddressD).TestObject;
            Block       block  = Build.A.Block.WithHeader(header).TestObject;

            _ = processor.Process(
                Keccak.EmptyTreeHash,
                new List <Block> {
                block
            },
                ProcessingOptions.None,
                NullBlockTracer.Instance);

            witnessCollector.Received(1).Persist(block.Hash);
        }
        protected async Task Prepare()
        {
            _wallet          = new DevWallet(new WalletConfig(), _logManager);
            _feeAccount      = _wallet.GetAccounts()[0];
            _consumerAccount = _wallet.GetAccounts()[1];
            _providerAccount = _wallet.GetAccounts()[2];
            _ndmConfig       = new NdmConfig();

            IReleaseSpec  spec         = _releaseSpec;
            ISpecProvider specProvider = new SingleReleaseSpecProvider(spec, 99);
            MemDb         stateDb      = new MemDb();
            TrieStore     trieStore    = new TrieStore(stateDb, _logManager);

            _state = new StateProvider(trieStore, new MemDb(), _logManager);
            StorageProvider storageProvider = new StorageProvider(trieStore, _state, _logManager);

            _state.CreateAccount(_consumerAccount, 1000.Ether());
            _state.CreateAccount(_providerAccount, 1.Ether());
            _state.Commit(spec);
            _state.CommitTree(0);

            VirtualMachine machine = new VirtualMachine(_state, storageProvider, Substitute.For <IBlockhashProvider>(),
                                                        specProvider, _logManager);
            TransactionProcessor processor = new TransactionProcessor(specProvider, _state, storageProvider, machine, _logManager);

            _bridge = new BlockchainBridge(processor);

            TxReceipt receipt = await DeployContract(Bytes.FromHexString(ContractData.GetInitCode(_feeAccount)));

            ((NdmConfig)_ndmConfig).ContractAddress = receipt.ContractAddress.ToString();
            _contractAddress = receipt.ContractAddress;
            _txPool          = new TxPool.TxPool(new InMemoryTxStorage(), new EthereumEcdsa(specProvider.ChainId, _logManager),
                                                 new FixedBlockChainHeadSpecProvider(specProvider), new TxPoolConfig(), _state, new TxValidator(specProvider.ChainId), _logManager);
            _ndmBridge = new NdmBlockchainBridge(_bridge, _bridge, _bridge, _bridge);
        }
        public void Does_not_fail_when_proofs_are_longer_than_number_of_proofs_regression()
        {
            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(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(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);

            AccountProofCollector accountProofCollector = new AccountProofCollector(TestItem.AddressA, new byte[][] { a });

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

            TestToJson(proof, "{\"accountProof\":[\"0xe215a08c9a7cdf08d4425c138ef5ba3d1f6c2ae18786fe88d9a56230a00b3e83367b25\",\"0xf8518080808080a017934c1c90ce30ca48f32b4f449dab308cfba803fd6d142cab01eb1fad1b70038080808080808080a02352504a0cd6095829b18bae394d0c882d84eead7be5b6ad0a87daaff9d2fb4a8080\",\"0xf869a020227dead52ea912e013e7641ccd6b3b174498e55066b0c174a09c8c3cc4bf5eb846f8448001a0b2375a34ff2c8037d9ff04ebc16367b51d156d9a905ca54cef50bfad1a4c0711a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470\"],\"address\":\"0xb7705ae4c6f81b66cdb323c65f4e8133690fc099\",\"balance\":\"0x1\",\"codeHash\":\"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470\",\"nonce\":\"0x0\",\"storageHash\":\"0xb2375a34ff2c8037d9ff04ebc16367b51d156d9a905ca54cef50bfad1a4c0711\",\"storageProof\":[{\"key\":\"0x000000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaaaa\",\"proof\":[\"0xf8918080a02474007b0486d5e951fe3fbcdae3e63cadf9c85cb8f178d3c7ca972e2d77705a808080808080a059c4fa21a1e4c40dc3c87e925befbb78a5b6f729865a12f6f0490d9801bcbf22a06b3576bbd6c91ca7128f69f728f3e30bf8980c6381430a5e80186d0dfec89d4e8080a098cfc3bf071c19a2e230165f4152bb98a5d1ab0fee47c952de65da85fcbdfdb2808080\",\"0xf85180a0aec9a5fc3ba2ebedf137fbcf6987b303c9d8718f75253e6e2444b81a4049e5b980808080808080808080a0397f22cb0ad24543caffaad031e3a0a538e5d8ac106d8f6858a703d442c0e4d380808080\",\"0xf84ca02046d62176084b9d1eace1c8bcc2353228d10569a500ccfd1bdbd8c093f4b4e9aaa9ab12000000000000000000000000000000000000000000000000000000000000000000000000000000\"],\"value\":\"0xab12000000000000000000000000000000000000000000000000000000000000000000000000000000\"}]}");
        }
        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 Memory_with_one_node_is_288()
        {
            TrieNode trieNode = new TrieNode(NodeType.Leaf, Keccak.Zero); // 56B

            TrieStore trieStore = new TrieStore(new MemDb(), new TestPruningStrategy(true), No.Persistence, _logManager);

            trieStore.CommitNode(1234, new NodeCommitInfo(trieNode));
            trieStore.MemoryUsedByDirtyCache.Should().Be(
                trieNode.GetMemorySize(false));
        }
Exemple #18
0
        public void Connect_extension_with_extension()
        {
            /* to test this case we need something like this initially */

            /* R
             * E - - - - - - - - - - - - - - -
             * B B B B B B B B B B B B B B B B
             * E L - - - - - - - - - - - - - -
             * E - - - - - - - - - - - - - - -
             * B B B B B B B B B B B B B B B B
             * L L - - - - - - - - - - - - - - */

            /* then we delete the leaf (marked as X) */

            /* R
             * B B B B B B B B B B B B B B B B
             * E X - - - - - - - - - - - - - -
             * E - - - - - - - - - - - - - - -
             * B B B B B B B B B B B B B B B B
             * L L - - - - - - - - - - - - - - */

            /* and we end up with an extended extension replacing what was previously a top-level branch*/

            /* R
             * E
             * E
             * E - - - - - - - - - - - - - - -
             * B B B B B B B B B B B B B B B B
             * L L - - - - - - - - - - - - - - */

            byte[] key1 = Bytes.FromHexString("000000100000000aa");
            byte[] key2 = Bytes.FromHexString("000000100000000bb");
            byte[] key3 = Bytes.FromHexString("000000200000000cc");

            MemDb        memDb        = new MemDb();
            TrieStore    trieStore    = new TrieStore(memDb, Prune.WhenCacheReaches(1.MB()), Persist.EveryBlock, _logManager);
            PatriciaTree patriciaTree = new PatriciaTree(trieStore, _logManager);

            patriciaTree.Set(key1, _longLeaf1);
            patriciaTree.Set(key2, _longLeaf1);
            patriciaTree.Set(key3, _longLeaf1);
            patriciaTree.UpdateRootHash();
            patriciaTree.Commit(0);
            patriciaTree.Set(key3, Array.Empty <byte>());
            patriciaTree.UpdateRootHash();
            patriciaTree.Commit(1);

            memDb.Keys.Should().HaveCount(8);
            PatriciaTree checkTree = CreateCheckTree(memDb, patriciaTree);

            checkTree.Get(key1).Should().BeEquivalentTo(_longLeaf1);
            checkTree.Get(key2).Should().BeEquivalentTo(_longLeaf1);
            checkTree.Get(key3).Should().BeNull();
        }
Exemple #19
0
        public void Single_leaf()
        {
            MemDb        memDb        = new MemDb();
            TrieStore    trieStore    = new TrieStore(memDb, Prune.WhenCacheReaches(1.MB()), Persist.EveryBlock, _logManager);
            PatriciaTree patriciaTree = new PatriciaTree(trieStore, _logManager);

            patriciaTree.Set(_keyA, _longLeaf1);
            patriciaTree.Commit(0);

            // leaf (root)
            memDb.Keys.Should().HaveCount(1);
        }
        public void Prunning_off_cache_should_not_find_cached_or_unknown()
        {
            TrieStore trieStore     = new TrieStore(new MemDb(), No.Pruning, No.Persistence, _logManager);
            var       returnedNode  = trieStore.FindCachedOrUnknown(TestItem.KeccakA, true);
            var       returnedNode2 = trieStore.FindCachedOrUnknown(TestItem.KeccakB, true);
            var       returnedNode3 = trieStore.FindCachedOrUnknown(TestItem.KeccakC, true);

            Assert.AreEqual(NodeType.Unknown, returnedNode.NodeType);
            Assert.AreEqual(NodeType.Unknown, returnedNode2.NodeType);
            Assert.AreEqual(NodeType.Unknown, returnedNode3.NodeType);
            trieStore.MemoryUsedByDirtyCache.Should().Be(0);
        }
Exemple #21
0
 private PruningContext(IPruningStrategy pruningStrategy, IPersistenceStrategy persistenceStrategy)
 {
     _logManager          = new TestLogManager(LogLevel.Trace);
     _logger              = _logManager.GetClassLogger();
     _dbProvider          = TestMemDbProvider.Init();
     _persistenceStrategy = persistenceStrategy;
     _pruningStrategy     = pruningStrategy;
     _trieStore           = new TrieStore(_dbProvider.StateDb, _pruningStrategy, _persistenceStrategy, _logManager);
     _stateProvider       = new StateProvider(_trieStore, _dbProvider.CodeDb, _logManager);
     _storageProvider     = new StorageProvider(_trieStore, _stateProvider, _logManager);
     _stateReader         = new StateReader(_trieStore, _dbProvider.CodeDb, _logManager);
 }
Exemple #22
0
        public void Setup()
        {
            _stateDb           = new MemDb();
            _codeDb            = new MemDb();
            _blockhashProvider = new TestBlockhashProvider();
            _specProvider      = OlympicSpecProvider.Instance;;

            TrieStore trieStore = new TrieStore(_stateDb, _logManager);

            _stateProvider   = new StateProvider(trieStore, _codeDb, _logManager);
            _storageProvider = new StorageProvider(trieStore, _stateProvider, _logManager);
        }
        public void Memory_with_two_nodes_is_correct()
        {
            TrieNode trieNode1 = new TrieNode(NodeType.Leaf, TestItem.KeccakA);
            TrieNode trieNode2 = new TrieNode(NodeType.Leaf, TestItem.KeccakB);

            TrieStore trieStore = new TrieStore(new MemDb(), new TestPruningStrategy(true), No.Persistence, _logManager);

            trieStore.CommitNode(1234, new NodeCommitInfo(trieNode1));
            trieStore.CommitNode(1234, new NodeCommitInfo(trieNode2));
            trieStore.MemoryUsedByDirtyCache.Should().Be(
                trieNode1.GetMemorySize(false) +
                trieNode2.GetMemorySize(false));
        }
            public DbContext(ILogger logger, ILogManager logManager)
            {
                _logger         = logger;
                RemoteDb        = new MemDb();
                LocalDb         = new MemDb();
                RemoteStateDb   = RemoteDb;
                LocalStateDb    = LocalDb;
                LocalCodeDb     = new MemDb();
                RemoteCodeDb    = new MemDb();
                RemoteTrieStore = new TrieStore(RemoteStateDb, logManager);

                RemoteStateTree = new StateTree(RemoteTrieStore, logManager);
                LocalStateTree  = new StateTree(new TrieStore(LocalStateDb.Innermost, logManager), logManager);
            }
        public void Prunning_off_cache_should_not_change_commit_node()
        {
            TrieNode trieNode  = new TrieNode(NodeType.Leaf, Keccak.Zero);
            TrieNode trieNode2 = new TrieNode(NodeType.Branch, TestItem.KeccakA);
            TrieNode trieNode3 = new TrieNode(NodeType.Branch, TestItem.KeccakB);

            TrieStore trieStore = new TrieStore(new MemDb(), No.Pruning, No.Persistence, _logManager);

            trieStore.CommitNode(1234, new NodeCommitInfo(trieNode));
            trieStore.FinishBlockCommit(TrieType.State, 1234, trieNode);
            trieStore.CommitNode(124, new NodeCommitInfo(trieNode2));
            trieStore.CommitNode(11234, new NodeCommitInfo(trieNode3));
            trieStore.MemoryUsedByDirtyCache.Should().Be(0);
        }
Exemple #26
0
        public static INdmBlockchainBridge BuildABridge()
        {
            IDbProvider memDbProvider = TestMemDbProvider.Init();
            StateReader stateReader   = new StateReader(
                new TrieStore(memDbProvider.StateDb, LimboLogs.Instance), memDbProvider.CodeDb, LimboLogs.Instance);
            var                          trieStore     = new TrieStore(memDbProvider.StateDb, LimboLogs.Instance);
            StateProvider                stateProvider = new StateProvider(trieStore, memDbProvider.CodeDb, LimboLogs.Instance);
            IEthereumEcdsa               ecdsa         = new EthereumEcdsa(ChainId.Mainnet, LimboLogs.Instance);
            BlockTree                    blockTree     = Build.A.BlockTree().OfChainLength(1).TestObject;
            MainnetSpecProvider          specProvider  = MainnetSpecProvider.Instance;
            ITransactionComparerProvider transactionComparerProvider =
                new TransactionComparerProvider(MainnetSpecProvider.Instance, blockTree);
            ITxPool txPool = new TxPool.TxPool(
                new InMemoryTxStorage(), ecdsa,
                new ChainHeadInfoProvider(specProvider, blockTree, stateProvider),
                new TxPoolConfig(),
                new TxValidator(specProvider.ChainId),
                LimboLogs.Instance,
                transactionComparerProvider.GetDefaultComparer());
            IWallet          wallet           = new DevWallet(new WalletConfig(), LimboLogs.Instance);
            ReceiptsRecovery receiptsRecovery = new ReceiptsRecovery(ecdsa, specProvider);
            LogFinder        logFinder        = new LogFinder(blockTree, new InMemoryReceiptStorage(), NullBloomStorage.Instance,
                                                              LimboLogs.Instance, receiptsRecovery, 1024);

            ReadOnlyTxProcessingEnv processingEnv = new ReadOnlyTxProcessingEnv(
                new ReadOnlyDbProvider(memDbProvider, false),
                new TrieStore(memDbProvider.StateDb, LimboLogs.Instance).AsReadOnly(memDbProvider.StateDb),
                new ReadOnlyBlockTree(blockTree),
                specProvider, LimboLogs.Instance);
            BlockchainBridge blockchainBridge = new BlockchainBridge(
                processingEnv,
                txPool,
                new InMemoryReceiptStorage(),
                NullFilterStore.Instance,
                NullFilterManager.Instance,
                ecdsa,
                Timestamper.Default,
                logFinder,
                specProvider,
                false,
                false);

            WalletTxSigner txSigner  = new WalletTxSigner(wallet, ChainId.Mainnet);
            ITxSealer      txSealer0 = new TxSealer(txSigner, Timestamper.Default);
            ITxSealer      txSealer1 = new NonceReservingTxSealer(txSigner, Timestamper.Default, txPool);
            ITxSender      txSender  = new TxPoolSender(txPool, txSealer0, txSealer1);

            return(new NdmBlockchainBridge(blockchainBridge, blockTree, stateReader, txSender));
        }
        public TrieStore Trie_committer_with_one_node()
        {
            TrieNode trieNode = new TrieNode(NodeType.Unknown); // 56B

            ITrieNodeCache trieNodeCache = new TrieNodeCache(_logManager);
            TrieStore      treeStore     = new TrieStore(
                trieNodeCache,
                _whateverDb,
                new DepthAndMemoryBased(128, 1.MB()),
                No.Persistence,
                _logManager);

            treeStore.CommitOneNode(1234, new NodeCommitInfo(trieNode));
            return(treeStore);
        }
Exemple #28
0
        public void Extension_with_branch_with_two_same_children()
        {
            MemDb        memDb        = new MemDb();
            TrieStore    trieStore    = new TrieStore(memDb, Prune.WhenCacheReaches(1.MB()), Persist.EveryBlock, _logManager);
            PatriciaTree patriciaTree = new PatriciaTree(trieStore, _logManager);

            patriciaTree.Set(_keyA, _longLeaf1);
            patriciaTree.Set(_keyB, _longLeaf1);
            patriciaTree.Commit(0);
            memDb.Keys.Should().HaveCount(4);
            PatriciaTree checkTree = CreateCheckTree(memDb, patriciaTree);

            checkTree.Get(_keyA).Should().BeEquivalentTo(_longLeaf1);
            checkTree.Get(_keyB).Should().BeEquivalentTo(_longLeaf1);
        }
Exemple #29
0
        public void Single_leaf_delete_same_block()
        {
            MemDb        memDb        = new MemDb();
            TrieStore    trieStore    = new TrieStore(memDb, Prune.WhenCacheReaches(1.MB()), No.Persistence, _logManager);
            PatriciaTree patriciaTree = new PatriciaTree(trieStore, _logManager);

            patriciaTree.Set(_keyA, _longLeaf1);
            patriciaTree.Set(_keyA, Array.Empty <byte>());
            patriciaTree.Commit(0);

            // leaf (root)
            memDb.Keys.Should().HaveCount(0);

            PatriciaTree checkTree = CreateCheckTree(memDb, patriciaTree);

            checkTree.Get(_keyA).Should().BeNull();
        }
        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);
        }