diff --git a/validator/tests/test_client_request_handlers/mocks.py b/validator/tests/test_client_request_handlers/mocks.py index 5da229acf2..e2cf996526 100644 --- a/validator/tests/test_client_request_handlers/mocks.py +++ b/validator/tests/test_client_request_handlers/mocks.py @@ -17,6 +17,7 @@ import logging import os +import tempfile from sawtooth_validator.protobuf.block_pb2 import Block from sawtooth_validator.protobuf.block_pb2 import BlockHeader @@ -24,10 +25,8 @@ from sawtooth_validator.protobuf.batch_pb2 import BatchHeader from sawtooth_validator.protobuf.transaction_pb2 import Transaction from sawtooth_validator.protobuf.transaction_pb2 import TransactionHeader -from sawtooth_validator.journal.block_wrapper import BlockWrapper from sawtooth_validator.journal.block_store import BlockStore from sawtooth_validator.database.native_lmdb import NativeLmdbDatabase -from sawtooth_validator.database.dict_database import DictDatabase from sawtooth_validator.state.merkle import MerkleDatabase from sawtooth_validator.state.batch_tracker import BatchTracker @@ -83,15 +82,17 @@ class MockBlockStore(BlockStore): """ def __init__(self, size=3, start='0'): - super().__init__(DictDatabase( - indexes=BlockStore.create_index_configuration())) + self.dir = tempfile.mkdtemp() + self.block_db = NativeLmdbDatabase( + os.path.join(self.dir, 'block.lmdb'), + BlockStore.create_index_configuration()) + super().__init__(self.block_db) for i in range(size): self.add_block(_increment_key(start, i)) def clear(self): - self._block_store = DictDatabase( - indexes=BlockStore.create_index_configuration()) + self.__init__(size=0) def add_block(self, base_id, root='merkle_root'): block_id = 'b' * (128 - len(base_id)) + base_id @@ -116,7 +117,7 @@ def add_block(self, base_id, root='merkle_root'): header_signature=block_id, batches=[make_mock_batch(base_id)]) - self.update_chain([BlockWrapper(block)], []) + self.put_blocks([block]) def make_db_and_store(base_dir, size=3): diff --git a/validator/tests/test_client_request_handlers/test_block_handlers.py b/validator/tests/test_client_request_handlers/test_block_handlers.py index 2ffda18042..32588bff8a 100644 --- a/validator/tests/test_client_request_handlers/test_block_handlers.py +++ b/validator/tests/test_client_request_handlers/test_block_handlers.py @@ -14,7 +14,6 @@ # ------------------------------------------------------------------------------ import sawtooth_validator.state.client_handlers as handlers -from sawtooth_validator.journal.block_store import BlockStore from sawtooth_validator.protobuf import client_block_pb2 from sawtooth_validator.protobuf.block_pb2 import Block from test_client_request_handlers.base_case import ClientHandlerTestCase @@ -283,7 +282,7 @@ def test_block_list_paginated(self): response, '0x0000000000000002', 2, - next_id=BlockStore.block_num_to_hex(0)) + next_id=handlers.block_num_to_hex(0)) def test_block_list_paginated_by_start(self): """Verifies block list requests work paginated by limit and start. @@ -305,7 +304,7 @@ def test_block_list_paginated_by_start(self): - that item has a header_signature of 'bbb...1' """ response = self.make_paged_request( - limit=1, start=BlockStore.block_num_to_hex(1)) + limit=1, start=handlers.block_num_to_hex(1)) self.assertEqual(self.status.OK, response.status) self.assertEqual(B_2, response.head_id) @@ -313,7 +312,7 @@ def test_block_list_paginated_by_start(self): response, '0x0000000000000001', 1, - next_id=BlockStore.block_num_to_hex(0)) + next_id=handlers.block_num_to_hex(0)) self.assertEqual(1, len(response.blocks)) self.assert_all_instances(response.blocks, Block) self.assertEqual(B_1, response.blocks[0].header_signature) diff --git a/validator/tests/test_completer/test.py b/validator/tests/test_completer/test.py index c49e48b659..850b8d552a 100644 --- a/validator/tests/test_completer/test.py +++ b/validator/tests/test_completer/test.py @@ -18,13 +18,15 @@ import unittest import random import hashlib +import os +import tempfile import cbor from sawtooth_signing import create_context from sawtooth_signing import CryptoFactory from sawtooth_validator.journal.completer import Completer -from sawtooth_validator.database.dict_database import DictDatabase +from sawtooth_validator.database.native_lmdb import NativeLmdbDatabase from sawtooth_validator.journal.block_store import BlockStore from sawtooth_validator.journal.block_manager import BlockManager from sawtooth_validator.journal.block_wrapper import NULL_BLOCK_IDENTIFIER @@ -37,10 +39,13 @@ class TestCompleter(unittest.TestCase): def setUp(self): - self.block_store = BlockStore(DictDatabase( - indexes=BlockStore.create_index_configuration())) + self.dir = tempfile.mkdtemp() + self.block_db = NativeLmdbDatabase( + os.path.join(self.dir, 'block.lmdb'), + BlockStore.create_index_configuration()) + self.block_store = BlockStore(self.block_db) self.block_manager = BlockManager() - self.block_manager.add_store("commit_store", self.block_store) + self.block_manager.add_commit_store(self.block_store) self.gossip = MockGossip() self.completer = Completer( block_manager=self.block_manager, diff --git a/validator/tests/test_events/tests.py b/validator/tests/test_events/tests.py index fbea005e67..a224723700 100644 --- a/validator/tests/test_events/tests.py +++ b/validator/tests/test_events/tests.py @@ -15,11 +15,14 @@ # pylint: disable=protected-access +import os +import tempfile import unittest from unittest.mock import Mock from uuid import uuid4 from sawtooth_validator.database.dict_database import DictDatabase +from sawtooth_validator.database.native_lmdb import NativeLmdbDatabase from sawtooth_validator.journal.block_store import BlockStore from sawtooth_validator.journal.block_wrapper import BlockWrapper from sawtooth_validator.journal.event_extractors \ @@ -272,11 +275,15 @@ def test_unsubscribe(self): class ClientEventsGetRequestHandlerTest(unittest.TestCase): def setUp(self): - self.block_store = BlockStore(DictDatabase()) + self.dir = tempfile.mkdtemp() + self.block_db = NativeLmdbDatabase( + os.path.join(self.dir, 'block.lmdb'), + BlockStore.create_index_configuration()) + self.block_store = BlockStore(self.block_db) self.receipt_store = TransactionReceiptStore(DictDatabase()) self._txn_ids_by_block_id = {} for block_id, blk_w, txn_ids in create_chain(): - self.block_store[block_id] = blk_w + self.block_store.put_blocks([blk_w.block]) self._txn_ids_by_block_id[block_id] = txn_ids for txn_id in txn_ids: receipt = create_receipt(txn_id=txn_id, diff --git a/validator/tests/test_genesis/tests.py b/validator/tests/test_genesis/tests.py index 982e7a6189..dd923e13a4 100644 --- a/validator/tests/test_genesis/tests.py +++ b/validator/tests/test_genesis/tests.py @@ -25,7 +25,6 @@ from sawtooth_signing import create_context from sawtooth_signing import CryptoFactory from sawtooth_validator.database.native_lmdb import NativeLmdbDatabase -from sawtooth_validator.database.dict_database import DictDatabase from sawtooth_validator.protobuf.block_pb2 import Block from sawtooth_validator.protobuf.block_pb2 import BlockHeader from sawtooth_validator.protobuf.genesis_pb2 import GenesisData @@ -57,17 +56,22 @@ def tearDown(self): shutil.rmtree(self._temp_dir) @staticmethod - def make_block_store(data=None): - return BlockStore( - DictDatabase( - data, indexes=BlockStore.create_index_configuration())) + def make_block_store(blocks=None): + block_dir = tempfile.mkdtemp() + block_db = NativeLmdbDatabase( + os.path.join(block_dir, 'block.lmdb'), + BlockStore.create_index_configuration()) + block_store = BlockStore(block_db) + if blocks is not None: + block_store.put_blocks(blocks) + return block_store def test_requires_genesis(self): self._with_empty_batch_file() block_store = self.make_block_store() block_manager = BlockManager() - block_manager.add_store("commit_store", block_store) + block_manager.add_commit_store(block_store) genesis_ctrl = GenesisController( context_manager=Mock('context_manager'), @@ -86,11 +90,9 @@ def test_requires_genesis(self): def test_does_not_require_genesis_block_exists(self): block = self._create_block() - block_store = self.make_block_store({ - block.header_signature: block - }) + block_store = self.make_block_store([block.block]) block_manager = BlockManager() - block_manager.add_store("commit_store", block_store) + block_manager.add_commit_store(block_store) genesis_ctrl = GenesisController( context_manager=Mock('context_manager'), @@ -112,7 +114,7 @@ def test_does_not_require_genesis_join_network(self): block_store = self.make_block_store() block_manager = BlockManager() - block_manager.add_store("commit_store", block_store) + block_manager.add_commit_store(block_store) genesis_ctrl = GenesisController( context_manager=Mock('context_manager'), @@ -139,7 +141,7 @@ def test_does_not_require_genesis_with_no_file_no_network(self): """ block_store = self.make_block_store() block_manager = BlockManager() - block_manager.add_store("commit_store", block_store) + block_manager.add_commit_store(block_store) genesis_ctrl = GenesisController( context_manager=Mock('context_manager'), @@ -167,9 +169,9 @@ def test_requires_genesis_fails_if_block_exists(self): self._with_empty_batch_file() block = self._create_block() - block_store = self.make_block_store({block.header_signature: block}) + block_store = self.make_block_store([block.block]) block_manager = BlockManager() - block_manager.add_store("commit_store", block_store) + block_manager.add_commit_store(block_store) genesis_ctrl = GenesisController( context_manager=Mock('context_manager'), @@ -200,7 +202,7 @@ def test_requires_genesis_fails_if_joins_network_with_file(self): block_store = self.make_block_store() block_manager = BlockManager() - block_manager.add_store("commit_store", block_store) + block_manager.add_commit_store(block_store) genesis_ctrl = GenesisController( context_manager=Mock('context_manager'), @@ -233,7 +235,7 @@ def test_empty_batch_file_should_produce_block( genesis_file = self._with_empty_batch_file() block_store = self.make_block_store() block_manager = BlockManager() - block_manager.add_store("commit_store", block_store) + block_manager.add_commit_store(block_store) state_database = NativeLmdbDatabase( os.path.join(self._temp_dir, 'test_genesis.lmdb'), diff --git a/validator/tests/test_journal/block_tree_manager.py b/validator/tests/test_journal/block_tree_manager.py index d9f4357f63..640e1608eb 100644 --- a/validator/tests/test_journal/block_tree_manager.py +++ b/validator/tests/test_journal/block_tree_manager.py @@ -27,7 +27,6 @@ from sawtooth_signing import create_context from sawtooth_signing import CryptoFactory -from sawtooth_validator.database.dict_database import DictDatabase from sawtooth_validator.database.native_lmdb import NativeLmdbDatabase from sawtooth_validator.journal.block_builder import BlockBuilder @@ -97,10 +96,12 @@ def __repr__(self): def __init__(self, with_genesis=True): self.block_sender = MockBlockSender() self.batch_sender = MockBatchSender() - self.block_store = BlockStore(DictDatabase( - indexes=BlockStore.create_index_configuration())) - self.block_cache = BlockCache(self.block_store) self.dir = tempfile.mkdtemp() + self.block_db = NativeLmdbDatabase( + os.path.join(self.dir, 'block.lmdb'), + BlockStore.create_index_configuration()) + self.block_store = BlockStore(self.block_db) + self.block_cache = BlockCache(self.block_store) self.state_db = NativeLmdbDatabase( os.path.join(self.dir, "merkle.lmdb"), MerkleDatabase.create_index_configuration()) @@ -108,7 +109,7 @@ def __init__(self, with_genesis=True): self.state_view_factory = NativeStateViewFactory(self.state_db) self.block_manager = BlockManager() - self.block_manager.add_store("commit_store", self.block_store) + self.block_manager.add_commit_store(self.block_store) context = create_context('secp256k1') private_key = context.new_random_private_key() @@ -215,7 +216,7 @@ def generate_block(self, previous_block=None, self.block_cache[block_wrapper.identifier] = block_wrapper if add_to_store: - self.block_store[block_wrapper.identifier] = block_wrapper + self.block_store.put_blocks([block_wrapper.block]) LOGGER.debug("Generated %s", dumps_block(block_wrapper)) return block_wrapper @@ -231,7 +232,7 @@ def generate_chain(self, root_block, blocks, params=None, if root_block is None: previous = self.generate_genesis_block() - self.block_store[previous.identifier] = previous + self.block_store.put_blocks([previous.block]) else: previous = self._get_block(root_block)