示例#1
0
        public async Task Get_storage()
        {
            IDbProvider dbProvider = await TestMemDbProvider.InitAsync();

            /* all testing will be touching just a single storage cell */
            StorageCell storageCell = new StorageCell(_address1, UInt256.One);

            TrieStore       trieStore = new TrieStore(dbProvider.StateDb, Logger);
            StateProvider   state     = new StateProvider(trieStore, dbProvider.CodeDb, Logger);
            StorageProvider storage   = new StorageProvider(trieStore, state, Logger);

            /* to start with we need to create an account that we will be setting storage at */
            state.CreateAccount(storageCell.Address, UInt256.One);
            state.Commit(MuirGlacier.Instance);
            state.CommitTree(1);

            /* at this stage we have an account with empty storage at the address that we want to test */

            byte[] initialValue = new byte[] { 1, 2, 3 };
            storage.Set(storageCell, initialValue);
            storage.Commit();
            storage.CommitTrees(2);
            state.Commit(MuirGlacier.Instance);
            state.CommitTree(2);

            StateReader reader = new StateReader(
                new TrieStore(dbProvider.StateDb, LimboLogs.Instance), dbProvider.CodeDb, Logger);

            var account   = reader.GetAccount(state.StateRoot, _address1);
            var retrieved = reader.GetStorage(account.StorageRoot, storageCell.Index);

            retrieved.Should().BeEquivalentTo(initialValue);

            /* at this stage we set the value in storage to 1,2,3 at the tested storage cell */

            /* Now we are testing scenario where the storage is being changed by the block processor.
            *  To do that we create some different storage / state access stack that represents the processor.
            *  It is a different stack of objects than the one that is used by the blockchain bridge. */

            byte[] newValue = new byte[] { 1, 2, 3, 4, 5 };

            StateProvider processorStateProvider =
                new StateProvider(trieStore, new MemDb(), LimboLogs.Instance);

            processorStateProvider.StateRoot = state.StateRoot;

            StorageProvider processorStorageProvider =
                new StorageProvider(trieStore, processorStateProvider, LimboLogs.Instance);

            processorStorageProvider.Set(storageCell, newValue);
            processorStorageProvider.Commit();
            processorStorageProvider.CommitTrees(3);
            processorStateProvider.Commit(MuirGlacier.Instance);
            processorStateProvider.CommitTree(3);

            /* At this stage the DB should have the storage value updated to 5.
             * We will try to retrieve the value by taking the state root from the processor.*/

            retrieved =
                reader.GetStorage(processorStateProvider.GetStorageRoot(storageCell.Address), storageCell.Index);
            retrieved.Should().BeEquivalentTo(newValue);

            /* If it failed then it means that the blockchain bridge cached the previous call value */
        }