当前位置: 首页>>代码示例>>Python>>正文


Python MerkleDatabase.update方法代码示例

本文整理汇总了Python中sawtooth_validator.state.merkle.MerkleDatabase.update方法的典型用法代码示例。如果您正苦于以下问题:Python MerkleDatabase.update方法的具体用法?Python MerkleDatabase.update怎么用?Python MerkleDatabase.update使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sawtooth_validator.state.merkle.MerkleDatabase的用法示例。


在下文中一共展示了MerkleDatabase.update方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: compute_state_hashes_wo_scheduler

# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import update [as 别名]
    def compute_state_hashes_wo_scheduler(self):
        """Creates a state hash from the state updates from each txn in a
        valid batch.

        Returns state_hashes (list of str): The merkle roots from state
            changes in 1 or more blocks in the yaml file.

        """

        tree = MerkleDatabase(database=DictDatabase())
        state_hashes = []
        updates = {}
        for batch in self._batches:
            b_id = batch.header_signature
            result = self._batch_results[b_id]
            if result.is_valid:
                for txn in batch.transactions:
                    txn_id = txn.header_signature
                    _, address_values = self._txn_execution[txn_id]
                    batch_updates = {}
                    for pair in address_values:
                        batch_updates.update({a: pair[a] for a in pair.keys()})
                    # since this is entirely serial, any overwrite
                    # of an address is expected and desirable.
                    updates.update(batch_updates)
            # This handles yaml files that have state roots in them
            if result.state_hash is not None:
                s_h = tree.update(set_items=updates, virtual=False)
                tree.set_merkle_root(merkle_root=s_h)
                state_hashes.append(s_h)
        if len(state_hashes) == 0:
            state_hashes.append(tree.update(set_items=updates))
        return state_hashes
开发者ID:feihujiang,项目名称:sawtooth-core,代码行数:35,代码来源:yaml_scheduler_tester.py

示例2: test_squash

# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import update [as 别名]
    def test_squash(self):
        """Tests that squashing a context based on state from other
        contexts will result in the same merkle hash as updating the
        merkle tree with the same data.

        Notes:
            Set up the context

            Test:
                1) Make set calls on several of the addresses.
                2) Squash the context to get a new state hash.
                3) Apply all of the aggregate sets from all
                of the contexts, to another database with a merkle tree.
                4) Assert that the state hashes are the same.
                5) Assert that the state deltas have been stored
        """
        # 1)
        context_id = self._setup_context()
        self.context_manager.set(
            context_id,
            [{self._create_address(a): v} for a, v in
             [('yyyy', b'2'),
              ('tttt', b'4')]])

        # 2)
        squash = self.context_manager.get_squash_handler()
        resulting_state_hash = squash(self.first_state_hash, [context_id],
                                      persist=True, clean_up=True)

        # 3)
        final_state_to_update = {self._create_address(a): v for a, v in
                                 [('llaa', b'1'),
                                  ('aall', b'2'),
                                  ('nnnn', b'3'),
                                  ('zzzz', b'9'),
                                  ('yyyy', b'2'),
                                  ('tttt', b'4'),
                                  ('qqqq', b'13'),
                                  ('oooo', b'25'),
                                  ('oozz', b'26'),
                                  ('zzoo', b'27'),
                                  ('ppoo', b'28'),
                                  ('aeio', b'29')]}

        test_merkle_tree = MerkleDatabase(self.database_results)
        test_resulting_state_hash = test_merkle_tree.update(
            final_state_to_update, virtual=False)
        # 4)
        self.assertEqual(resulting_state_hash, test_resulting_state_hash)
        state_changes = self.state_delta_store.get_state_deltas(
            resulting_state_hash)

        # 5)
        for addr, value in final_state_to_update.items():
            expected_state_change = StateChange(
                address=addr,
                value=value,
                type=StateChange.SET)

            self.assertTrue(expected_state_change in state_changes)
开发者ID:feihujiang,项目名称:sawtooth-core,代码行数:62,代码来源:tests.py

示例3: make_db_and_store

# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import update [as 别名]
def make_db_and_store(size=3, start='a'):
    """
    Creates and returns three related objects for testing:
        * database - dict database with evolving state
        * store - blocks with root hashes corresponding to that state
        * roots - list of root hashes used in order
    With defaults, the values at the three roots look like this:
        * 0 - {'a': b'1'}
        * 1 - {'a': b'2', 'b': b'4'}
        * 2 - {'a': b'3', 'b': b'5', 'c': b'7'}
    """
    database = DictDatabase()
    store = MockBlockStore(size=0);
    roots = []

    merkle = MerkleDatabase(database)
    data = {}

    for i in range(size):
        for k, v in data.items():
            data[k] = str(int(v) + 1).encode()
        data[_increment_key(start, i)] = str(i * size + 1).encode()

        root = merkle.update(data, virtual=False)
        roots.append(root)
        store.add_block(str(i), root)

    return database, store, roots
开发者ID:feihujiang,项目名称:sawtooth-core,代码行数:30,代码来源:mocks.py

示例4: compute_state_hashes_wo_scheduler

# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import update [as 别名]
    def compute_state_hashes_wo_scheduler(self, base_dir):
        """Creates a state hash from the state updates from each txn in a
        valid batch.

        Returns state_hashes (list of str): The merkle roots from state
            changes in 1 or more blocks in the yaml file.

        """

        database = NativeLmdbDatabase(
            os.path.join(base_dir, 'compute_state_hashes_wo_scheduler.lmdb'),
            indexes=MerkleDatabase.create_index_configuration(),
            _size=10 * 1024 * 1024)

        tree = MerkleDatabase(database=database)
        state_hashes = []
        updates = {}
        for batch in self._batches:
            b_id = batch.header_signature
            result = self._batch_results[b_id]
            if result.is_valid:
                for txn in batch.transactions:
                    txn_id = txn.header_signature
                    _, address_values, deletes = self._txn_execution[txn_id]
                    batch_updates = {}
                    for pair in address_values:
                        batch_updates.update({a: pair[a] for a in pair.keys()})

                    # since this is entirely serial, any overwrite
                    # of an address is expected and desirable.
                    updates.update(batch_updates)

                    for address in deletes:
                        if address in updates:
                            del updates[address]

            # This handles yaml files that have state roots in them
            if result.state_hash is not None:
                s_h = tree.update(set_items=updates, virtual=False)
                tree.set_merkle_root(merkle_root=s_h)
                state_hashes.append(s_h)
        if not state_hashes:
            state_hashes.append(tree.update(set_items=updates))
        return state_hashes
开发者ID:Whiteblock,项目名称:sawtooth-core,代码行数:46,代码来源:yaml_scheduler_tester.py

示例5: _squash

# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import update [as 别名]
        def _squash(state_root, context_ids, persist, clean_up):
            contexts_in_chain = deque()
            contexts_in_chain.extend(context_ids)
            context_ids_already_searched = []
            context_ids_already_searched.extend(context_ids)

            # There is only one exit condition and that is when all the
            # contexts have been accessed once.
            updates = dict()
            deletes = set()
            while contexts_in_chain:
                current_c_id = contexts_in_chain.popleft()
                current_context = self._contexts[current_c_id]
                if not current_context.is_read_only():
                    current_context.make_read_only()

                addresses_w_values = current_context.get_all_if_set()
                for add, val in addresses_w_values.items():
                    # Since we are moving backwards through the graph of
                    # contexts, only update if the address hasn't been set
                    # or deleted
                    if add not in updates and add not in deletes:
                        updates[add] = val

                addresses_w_values = current_context.get_all_if_deleted()
                for add, _ in addresses_w_values.items():
                    # Since we are moving backwards through the graph of
                    # contexts, only add to deletes if the address hasn't been
                    # previously deleted or set in the graph
                    if add not in updates and add not in deletes:
                        deletes.add(add)

                for c_id in current_context.base_contexts:
                    if c_id not in context_ids_already_searched:
                        contexts_in_chain.append(c_id)
                        context_ids_already_searched.append(c_id)

            tree = MerkleDatabase(self._database, state_root)

            # filter the delete list to just those items in the tree
            deletes = [addr for addr in deletes if addr in tree]

            if not updates and not deletes:
                state_hash = state_root
            else:
                virtual = not persist
                state_hash = tree.update(updates, deletes, virtual=virtual)

            if clean_up:
                self.delete_contexts(context_ids_already_searched)
            return state_hash
开发者ID:cianx,项目名称:sawtooth-core,代码行数:53,代码来源:context_manager.py

示例6: setUp

# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import update [as 别名]
    def setUp(self):
        database = DictDatabase()
        state_view_factory = StateViewFactory(database)
        self._settings_view_factory = SettingsViewFactory(state_view_factory)

        merkle_db = MerkleDatabase(database)
        self._current_root_hash = merkle_db.update({
            TestSettingsView._address('my.setting'):
                TestSettingsView._setting_entry('my.setting', '10'),
            TestSettingsView._address('my.setting.list'):
                TestSettingsView._setting_entry('my.setting.list', '10,11,12'),
            TestSettingsView._address('my.other.list'):
                TestSettingsView._setting_entry('my.other.list', '13;14;15')
        }, virtual=False)
开发者ID:feihujiang,项目名称:sawtooth-core,代码行数:16,代码来源:tests.py

示例7: _squash

# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import update [as 别名]
 def _squash(state_root, context_ids):
     tree = MerkleDatabase(self._database, state_root)
     updates = dict()
     for c_id in context_ids:
         with self._shared_lock:
             context = self._contexts[c_id]
         for add in context.get_address_value_dict().keys():
             if add in updates:
                 raise SquashException(
                     "Duplicate address {} in context {}".format(
                         add, c_id))
         updates.update({k: v.result() for k, v in
                         context.get_address_value_dict().items()})
     state_hash = tree.update(updates, virtual=False)
     return state_hash
开发者ID:jsmitchell,项目名称:sawtooth-core,代码行数:17,代码来源:context_manager.py

示例8: setUp

# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import update [as 别名]
    def setUp(self):
        self._temp_dir = tempfile.mkdtemp()
        database = NativeLmdbDatabase(
            os.path.join(self._temp_dir, 'test_config_view.lmdb'),
            indexes=MerkleDatabase.create_index_configuration(),
            _size=10 * 1024 * 1024)
        state_view_factory = StateViewFactory(database)
        self._settings_view_factory = SettingsViewFactory(state_view_factory)

        merkle_db = MerkleDatabase(database)
        self._current_root_hash = merkle_db.update({
            TestSettingsView._address('my.setting'):
                TestSettingsView._setting_entry('my.setting', '10'),
            TestSettingsView._address('my.setting.list'):
                TestSettingsView._setting_entry('my.setting.list', '10,11,12'),
            TestSettingsView._address('my.other.list'):
                TestSettingsView._setting_entry('my.other.list', '13;14;15')
        }, virtual=False)
开发者ID:Whiteblock,项目名称:sawtooth-core,代码行数:20,代码来源:tests.py

示例9: _squash

# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import update [as 别名]
        def _squash(state_root, context_ids, persist, clean_up):
            contexts_in_chain = deque()
            contexts_in_chain.extend(context_ids)
            context_ids_already_searched = []
            context_ids_already_searched.extend(context_ids)

            # There is only one exit condition and that is when all the
            # contexts have been accessed once.
            updates = dict()
            while contexts_in_chain:
                current_c_id = contexts_in_chain.popleft()
                current_context = self._contexts[current_c_id]
                if not current_context.is_read_only():
                    current_context.make_read_only()

                addresses_w_values = current_context.get_all_if_set()
                for add, val in addresses_w_values.items():
                    # Since we are moving backwards through the graph of
                    # contexts, only update if the address hasn't been set
                    if add not in updates:
                        updates[add] = val

                for c_id in current_context.base_contexts:
                    if c_id not in context_ids_already_searched:
                        contexts_in_chain.append(c_id)
                        context_ids_already_searched.append(c_id)

            if not updates:
                return state_root

            tree = MerkleDatabase(self._database, state_root)
            virtual = not persist
            state_hash = tree.update(updates, virtual=virtual)
            if persist:
                # save the state changes to the state_delta_store
                changes = [StateChange(address=addr,
                                       value=value,
                                       type=StateChange.SET)
                           for addr, value in updates.items()]
                self._state_delta_store.save_state_deltas(state_hash, changes)
            if clean_up:
                self.delete_contexts(context_ids_already_searched)
            return state_hash
开发者ID:feihujiang,项目名称:sawtooth-core,代码行数:45,代码来源:context_manager.py

示例10: commit_context

# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import update [as 别名]
    def commit_context(self, context_id_list, virtual):
        """
        Part of the interface to the Executor
        Args:
            context_id_list:

        Returns:
            state_hash (str): the new state hash after the context_id_list
                              has been committed

        """

        if any([c_id not in self._contexts for c_id in context_id_list]):
            raise CommitException("Context Id not in contexts")
        first_id = context_id_list[0]

        if not all([self._contexts[first_id].merkle_root ==
                    self._contexts[c_id].merkle_root
                    for c_id in context_id_list]):
            raise CommitException(
                "MerkleRoots not all equal, yet asking to merge")

        merkle_root = self._contexts[first_id].merkle_root
        tree = MerkleDatabase(self._database, merkle_root)

        merged_updates = {}
        for c_id in context_id_list:
            with self._shared_lock:
                context = self._contexts[c_id]
                del self._contexts[c_id]
            for k in context.get_writable_address_value_dict().keys():
                if k in merged_updates:
                    raise CommitException(
                        "Duplicate address {} in context {}".format(k, c_id))
            merged_updates.update(context.get_writable_address_value_dict())

        new_root = merkle_root
        add_value_dict = {address: value.result()
                          for address, value in merged_updates.items()}
        new_root = tree.update(set_items=add_value_dict, virtual=virtual)

        return new_root
开发者ID:jsmitchell,项目名称:sawtooth-core,代码行数:44,代码来源:context_manager.py

示例11: make_db_and_store

# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import update [as 别名]
def make_db_and_store(base_dir, size=3):
    """
    Creates and returns three related objects for testing:
        * database - dict database with evolving state
        * store - blocks with root hashes corresponding to that state
        * roots - list of root hashes used in order
    With defaults, the values at the three roots look like this:
        * 0 - {'000...1': b'1'}
        * 1 - {'000...1': b'2', '000...2': b'4'}
        * 2 - {'000...1': b'3', '000...2': b'5', '000...3': b'7'}
        * 3 - {'000...1': b'4', '000...2': b'6',
               '000...3': b'8', '000...4': b'10'}
    """
    database = NativeLmdbDatabase(
        os.path.join(base_dir, 'client_handlers_mock_db.lmdb'),
        indexes=MerkleDatabase.create_index_configuration(),
        _size=10 * 1024 * 1024)
    store = MockBlockStore(size=0)
    roots = []

    merkle = MerkleDatabase(database)
    data = {}

    # Create all the keys that will be used. Keys are zero-padded hex strings
    # starting with '1'.
    keys = [format(i, 'x').zfill(70) for i in range(1, size + 1)]

    for i in range(1, size + 1):
        # Construct the state for this root
        data = {}
        for key_idx in range(i):
            key = keys[key_idx]
            # Calculate unique values based on the key and root
            val = i + (2 * key_idx)
            data[key] = str(val).encode()

        root = merkle.update(data, virtual=False)
        roots.append(root)
        store.add_block(str(i), root)

    return database, store, roots
开发者ID:cianx,项目名称:sawtooth-core,代码行数:43,代码来源:mocks.py

示例12: test_state_view

# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import update [as 别名]
    def test_state_view(self):
        """Tests the StateViewFactory and its creation of StateViews

        This test exercises the following:

        1. Create an empty merkle database.
        2. Create a view into the database, asserting its emptiness.
        3. Update the database with a value, creating a new root.
        4. Create a view into the database with the new root.
        5. Verify the view does not match the previous view and contains the
           new item.
        """

        merkle_db = MerkleDatabase(self.database)

        state_view_factory = StateViewFactory(self.database)

        initial_state_view = state_view_factory.create_view(
            merkle_db.get_merkle_root())

        # test that the initial state view returns empty values
        self.assertEqual([], initial_state_view.addresses())
        self.assertEqual({}, {k: v for k, v in initial_state_view.leaves('')})
        with self.assertRaises(KeyError):
            initial_state_view.get('abcd')

        next_root = merkle_db.update({'abcd': 'hello'.encode()},
                                     virtual=False)

        next_state_view = state_view_factory.create_view(next_root)

        # Prove that the initial state view is not effected by the change
        self.assertEqual([], initial_state_view.addresses())
        self.assertEqual(['abcd'], next_state_view.addresses())

        # Check that the values can be properly read back
        self.assertEqual('hello', next_state_view.get('abcd').decode())
        self.assertEqual({'abcd': 'hello'.encode()},
                         {k: v for k, v in next_state_view.leaves('')})
开发者ID:Whiteblock,项目名称:sawtooth-core,代码行数:41,代码来源:tests.py

示例13: TestSawtoothMerkleTrie

# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import update [as 别名]
class TestSawtoothMerkleTrie(unittest.TestCase):
    def setUp(self):
        self.lmdb = lmdb_nolock_database.LMDBNoLockDatabase(
            "/home/vagrant/merkle.lmdb",
            'n')

        self.trie = MerkleDatabase(self.lmdb)

    def tearDown(self):
        self.trie.close()

    def test_merkle_trie_root_advance(self):
        value = {"name": "foo", "value": 1}

        orig_root = self.trie.get_merkle_root()
        new_root = self.trie.set(MerkleDatabase.hash("foo"),
                                 value)

        with self.assertRaises(KeyError):
            self.trie.get(MerkleDatabase.hash("foo"))

        self.trie.set_merkle_root(new_root)

        self.assertEqual(self.trie.get(MerkleDatabase.hash("foo")),
                         value)

    def test_merkle_trie_delete(self):
        value = {"name": "bar", "value": 1}

        new_root = self.trie.set(MerkleDatabase.hash("bar"), value)

        self.trie.set_merkle_root(new_root)

        self.assertEqual(self.trie.get(MerkleDatabase.hash("bar")),
                         value)

        del_root = self.trie.delete(MerkleDatabase.hash("bar"))

        self.trie.set_merkle_root(del_root)

        with self.assertRaises(KeyError):
            self.trie.get(MerkleDatabase.hash("bar"))

    def test_merkle_trie_update(self):
        value = ''.join(random.choice(string.ascii_lowercase)
                        for _ in range(512))
        keys = []
        for i in range(1000):
            key = ''.join(random.choice(string.ascii_lowercase)
                          for _ in range(10))
            keys.append(key)
            hash = MerkleDatabase.hash(key)
            new_root = self.trie.set(hash, {key: value})
            self.trie.set_merkle_root(new_root)

        set_items = {}
        for key in random.sample(keys, 50):
            hash = MerkleDatabase.hash(key)
            thing = {key: 5.0}
            set_items[hash] = thing

        update_root = self.trie.update(set_items)
        self.trie.set_merkle_root(update_root)

        for address in set_items:
            self.assertEqual(self.trie.get(address),
                             set_items[address])
开发者ID:jsmitchell,项目名称:sawtooth-core,代码行数:69,代码来源:test_merkle_trie.py

示例14: TestIdentityView

# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import update [as 别名]
class TestIdentityView(unittest.TestCase):
    def __init__(self, test_name):
        super().__init__(test_name)
        self._temp_dir = None

    def setUp(self):
        self._temp_dir = tempfile.mkdtemp()

        self._database = NativeLmdbDatabase(
            os.path.join(self._temp_dir, 'test_identity_view.lmdb'),
            indexes=MerkleDatabase.create_index_configuration(),
            _size=10 * 1024 * 1024)
        self._tree = MerkleDatabase(self._database)

    def tearDown(self):
        shutil.rmtree(self._temp_dir)

    def test_identityview_roles(self):
        """Tests get_role and get_roles get the correct Roles and the
        IdentityViewFactory produces the correct view of the database.

        Notes:
            1. Create an empty MerkleDatabase and update it with one
            serialized RoleList.
            2. Assert that get_role returns that named Role.
            3. Assert that get_role returns None for a name that doesn't
               correspond to a Role.
            4. Assert that all the Roles are returned by get_roles.
            5. Update the MerkleDatabase with another serialized RoleList with
               a different name.
            6. Repeat 2.
            7. Repeat 3.
            8. Repeat 4.

        """

        state_view_factory = StateViewFactory(self._database)

        identity_view_factory = identity_view.IdentityViewFactory(
            state_view_factory=state_view_factory)

        # 1.
        role_list = identity_pb2.RoleList()
        role1 = role_list.roles.add()
        role1_name = "sawtooth.test.example1"

        role1.name = role1_name
        role1.policy_name = "this_is_an_example"

        state_root1 = self._tree.update(
            set_items={
                _get_role_address(role1_name): role_list.SerializeToString()
            },
            virtual=False)

        # 2.
        identity_view1 = identity_view_factory.create_identity_view(
            state_hash=state_root1)
        self.assertEqual(
            identity_view1.get_role(role1_name),
            role1,
            "IdentityView().get_role returns the correct Role by name.")

        # 3.
        self.assertIsNone(
            identity_view1.get_role("Not-a-Role"),
            "IdentityView().get_role returns None if there is "
            "no Role with that name.")

        # 4.
        self.assertEqual(identity_view1.get_roles(),
                         [role1],
                         "IdentityView().get_roles returns all the roles in"
                         " State.")

        # 5.
        role_list2 = identity_pb2.RoleList()
        role2 = role_list2.roles.add()
        role2_name = "sawtooth.test.example2"

        role2.name = role2_name
        role2.policy_name = "this_is_another_example"

        self._tree.set_merkle_root(merkle_root=state_root1)

        state_root2 = self._tree.update(
            {
                _get_role_address(role2_name): role_list2.SerializeToString()
            },
            virtual=False)

        # 6.
        identity_view2 = identity_view_factory.create_identity_view(
            state_hash=state_root2)

        self.assertEqual(
            identity_view2.get_role(role2_name),
            role2,
            "IdentityView().get_role returns the correct Role by name.")

#.........这里部分代码省略.........
开发者ID:Whiteblock,项目名称:sawtooth-core,代码行数:103,代码来源:tests.py

示例15: TestSawtoothMerkleTrie

# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import update [as 别名]
class TestSawtoothMerkleTrie(unittest.TestCase):
    def setUp(self):
        self.dir = tempfile.mkdtemp()
        self.file = os.path.join(self.dir, 'merkle.lmdb')

        self.lmdb = lmdb_nolock_database.LMDBNoLockDatabase(
            self.file,
            'n')

        self.trie = MerkleDatabase(self.lmdb)

    def tearDown(self):
        self.trie.close()

    def test_merkle_trie_root_advance(self):
        value = {'name': 'foo', 'value': 1}

        orig_root = self.get_merkle_root()
        new_root = self.set('foo', value)

        self.assert_root(orig_root)
        self.assert_no_key('foo')

        self.set_merkle_root(new_root)

        self.assert_root(new_root)
        self.assert_value_at_address('foo', value)

    def test_merkle_trie_delete(self):
        value = {'name': 'bar', 'value': 1}

        new_root = self.set('bar', value)
        self.set_merkle_root(new_root)

        self.assert_root(new_root)
        self.assert_value_at_address('bar', value)

        # deleting an invalid key should raise an error
        with self.assertRaises(KeyError):
            self.delete('barf')

        del_root = self.delete('bar')

        # del_root hasn't been set yet, so address should still have value
        self.assert_root(new_root)
        self.assert_value_at_address('bar', value)

        self.set_merkle_root(del_root)

        self.assert_root(del_root)
        self.assert_no_key('bar')

    def test_merkle_trie_update(self):
        init_root = self.get_merkle_root()

        values = {}
        key_hashes = {
            key: _hash(key)
            for key in (_random_string(10) for _ in range(1000))
        }

        for key, hashed in key_hashes.items():
            value = {key: _random_string(512)}
            new_root = self.set(hashed, value, ishash=True)
            values[hashed] = value
            self.set_merkle_root(new_root)

        self.assert_not_root(init_root)

        for address, value in values.items():
            self.assert_value_at_address(
                address, value, ishash=True)

        set_items = {
            hashed: {
                key: 5.0
            }
            for key, hashed in random.sample(key_hashes.items(), 50)
        }
        values.update(set_items)
        delete_items = {
            hashed
            for hashed in random.sample(list(key_hashes.values()), 50)
        }

        # make sure there are no sets and deletes of the same key
        delete_items = delete_items - set_items.keys()
        for addr in delete_items:
            del values[addr]

        virtual_root = self.update(set_items, delete_items, virtual=True)

        # virtual root shouldn't match actual contents of tree
        with self.assertRaises(KeyError):
            self.set_merkle_root(virtual_root)

        actual_root = self.update(set_items, delete_items, virtual=False)

        # the virtual root should be the same as the actual root
        self.assertEqual(virtual_root, actual_root)
#.........这里部分代码省略.........
开发者ID:jjason,项目名称:sawtooth-core,代码行数:103,代码来源:tests.py


注:本文中的sawtooth_validator.state.merkle.MerkleDatabase.update方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。