Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
Fixup unit tests to work with Rust blockstore
Browse files Browse the repository at this point in the history
DictDatabase no long works with BlockStore, so we need to replace its usage.

Signed-off-by: Adam Ludvik <ludvik@bitwise.io>
  • Loading branch information
Adam Ludvik committed Sep 28, 2018
1 parent 04ddb2c commit 83d20c7
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 40 deletions.
15 changes: 8 additions & 7 deletions validator/tests/test_client_request_handlers/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@

import logging
import os
import tempfile

from sawtooth_validator.protobuf.block_pb2 import Block
from sawtooth_validator.protobuf.block_pb2 import BlockHeader
from sawtooth_validator.protobuf.batch_pb2 import Batch
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

Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -305,15 +304,15 @@ 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)
self.assert_valid_paging(
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)
Expand Down
13 changes: 9 additions & 4 deletions validator/tests/test_completer/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
11 changes: 9 additions & 2 deletions validator/tests/test_events/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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,
Expand Down
34 changes: 18 additions & 16 deletions validator/tests/test_genesis/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'),
Expand All @@ -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'),
Expand All @@ -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'),
Expand All @@ -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'),
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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'),
Expand Down
15 changes: 8 additions & 7 deletions validator/tests/test_journal/block_tree_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -97,18 +96,20 @@ 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())

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()
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down

0 comments on commit 83d20c7

Please sign in to comment.