github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/validator/tests/test_state_view/tests.py (about)

     1  # Copyright 2017 Intel Corporation
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #     http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  # ------------------------------------------------------------------------------
    15  
    16  import os
    17  import shutil
    18  import tempfile
    19  import unittest
    20  
    21  from sawtooth_validator.database.native_lmdb import NativeLmdbDatabase
    22  from sawtooth_validator.state.merkle import MerkleDatabase
    23  
    24  from sawtooth_validator.state.state_view import StateViewFactory
    25  
    26  
    27  class StateViewTest(unittest.TestCase):
    28      def __init__(self, test_name):
    29          super().__init__(test_name)
    30          self._temp_dir = None
    31  
    32      def setUp(self):
    33          self._temp_dir = tempfile.mkdtemp()
    34  
    35          self.database = NativeLmdbDatabase(
    36              os.path.join(self._temp_dir, 'test_state_view.lmdb'),
    37              indexes=MerkleDatabase.create_index_configuration(),
    38              _size=10 * 1024 * 1024)
    39  
    40      def tearDown(self):
    41          shutil.rmtree(self._temp_dir)
    42  
    43      def test_state_view(self):
    44          """Tests the StateViewFactory and its creation of StateViews
    45  
    46          This test exercises the following:
    47  
    48          1. Create an empty merkle database.
    49          2. Create a view into the database, asserting its emptiness.
    50          3. Update the database with a value, creating a new root.
    51          4. Create a view into the database with the new root.
    52          5. Verify the view does not match the previous view and contains the
    53             new item.
    54          """
    55  
    56          merkle_db = MerkleDatabase(self.database)
    57  
    58          state_view_factory = StateViewFactory(self.database)
    59  
    60          initial_state_view = state_view_factory.create_view(
    61              merkle_db.get_merkle_root())
    62  
    63          # test that the initial state view returns empty values
    64          self.assertEqual([], initial_state_view.addresses())
    65          self.assertEqual({}, {k: v for k, v in initial_state_view.leaves('')})
    66          with self.assertRaises(KeyError):
    67              initial_state_view.get('abcd')
    68  
    69          next_root = merkle_db.update({'abcd': 'hello'.encode()},
    70                                       virtual=False)
    71  
    72          next_state_view = state_view_factory.create_view(next_root)
    73  
    74          # Prove that the initial state view is not effected by the change
    75          self.assertEqual([], initial_state_view.addresses())
    76          self.assertEqual(['abcd'], next_state_view.addresses())
    77  
    78          # Check that the values can be properly read back
    79          self.assertEqual('hello', next_state_view.get('abcd').decode())
    80          self.assertEqual({'abcd': 'hello'.encode()},
    81                           {k: v for k, v in next_state_view.leaves('')})