def setUp(self): self.headers = Mock() self.blocks = Mock() self.cache = Mock(cache_name=b'cache_prefix') self.sut = Repository(self.headers, self.blocks, keep_blocks=5) self.sut.set_cache(self.cache) self.sut.ldb = Mock() self.loop = asyncio.get_event_loop()
def builder(ctx: Context): # pragma: no cover from spruned.application.cache import CacheAgent from spruned.repositories.repository import Repository from spruned.daemon.tasks.blocks_reactor import BlocksReactor from spruned.daemon.tasks.headers_reactor import HeadersReactor from spruned.application import spruned_vo_service from spruned.application.jsonrpc_server import JSONRPCServer from spruned.daemon.electrod import build as electrod_builder from spruned.daemon.bitcoin_p2p import build as p2p_builder electrod_connectionpool, electrod_interface = electrod_builder(ctx) p2p_connectionpool, p2p_interface = p2p_builder(ctx.get_network()) repository = Repository.instance() cache = CacheAgent(repository, int(ctx.cache_size)) repository.set_cache(cache) service = spruned_vo_service.SprunedVOService(electrod_interface, p2p_interface, repository=repository, cache=cache) jsonrpc_server = JSONRPCServer(ctx.rpcbind, ctx.rpcport, ctx.rpcuser, ctx.rpcpassword) jsonrpc_server.set_vo_service(service) headers_reactor = HeadersReactor(repository.headers, electrod_interface) blocks_reactor = BlocksReactor(repository, p2p_interface, prune=int(ctx.keep_blocks)) return jsonrpc_server, headers_reactor, blocks_reactor, repository, cache
def builder(ctx: Context): # pragma: no cover from spruned.application.cache import CacheAgent from spruned.repositories.repository import Repository from spruned.daemon.tasks.blocks_reactor import BlocksReactor from spruned.daemon.tasks.headers_reactor import HeadersReactor from spruned.application import spruned_vo_service from spruned.application.jsonrpc_server import JSONRPCServer from spruned.daemon.electrod import build as electrod_builder from spruned.daemon.bitcoin_p2p import build as p2p_builder electrod_connectionpool, electrod_interface = electrod_builder(ctx) p2p_connectionpool, p2p_interface = p2p_builder(ctx) repository = Repository.instance() cache = CacheAgent(repository, int(ctx.cache_size)) repository.set_cache(cache) service = spruned_vo_service.SprunedVOService( electrod_interface, p2p_interface, repository=repository, cache_agent=cache, context=ctx, fallback_non_segwit_blocks=True) jsonrpc_server = JSONRPCServer(ctx.rpcbind, ctx.rpcport, ctx.rpcuser, ctx.rpcpassword) jsonrpc_server.set_vo_service(service) headers_reactor = HeadersReactor(repository.headers, electrod_interface) if ctx.mempool_size: from spruned.application.mempool_observer import MempoolObserver mempool_observer = MempoolObserver(repository, p2p_interface) headers_reactor.add_on_new_header_callback( mempool_observer.on_block_header) p2p_interface.mempool = repository.mempool p2p_connectionpool.add_on_transaction_callback( mempool_observer.on_transaction) p2p_connectionpool.add_on_transaction_hash_callback( mempool_observer.on_transaction_hash) else: mempool_observer = None zmq_context = zmq_observer = None if ctx.is_zmq_enabled(): zmq_context, zmq_observer = build_zmq(ctx, mempool_observer, headers_reactor, ctx.mempool_size, service) blocks_reactor = BlocksReactor(repository, p2p_interface, keep_blocks=int(ctx.keep_blocks)) headers_reactor.add_on_best_height_hit_persistent_callbacks( p2p_connectionpool.set_best_header) return jsonrpc_server, headers_reactor, blocks_reactor, repository, \ cache, zmq_context, zmq_observer, p2p_interface
class TestRepository(TestCase): def setUp(self): self.headers = Mock() self.blocks = Mock() self.mempool = Mock() self.cache = Mock(cache_name=b'cache_prefix') self.sut = Repository(self.headers, self.blocks, self.mempool, keep_blocks=5) self.sut.set_cache(self.cache) self.sut.ldb = Mock() self.loop = asyncio.get_event_loop() def tearDown(self): self.headers.reset_mock() self.blocks.reset_mock() self.cache.reset_mock() self.sut.ldb.reset_mock() def test_check_stales(self): self.headers.get_best_header.return_value = { 'block_height': 16, 'block_hash': 'block10' } self.headers.get_headers_since_height.return_value = [{ 'block_height': 12, 'block_hash': 'block12' }, { 'block_height': 13, 'block_hash': 'block13' }, { 'block_height': 14, 'block_hash': 'block14' }, { 'block_height': 15, 'block_hash': 'block15' }, { 'block_height': 16, 'block_hash': 'block16' }] self.blocks.storage_name = b'block_prefix' self.blocks.get_key.side_effect = lambda x, y: b'block_prefix.' + BLOCK_INDEX_PREFIX + b'.' + x.encode( ) self.cache.get_index.return_value = { 'keys': { b'block12': {}, b'block13': {}, b'block14': {}, b'block15': {} } } iterator = [ b'block_prefix.block12', b'block_prefix.block13', b'block_prefix.block14', b'block_prefix.block15', b'block_prefix.block16', ] self.sut.ldb.iterator.return_value = iterator self.loop.run_until_complete(self.sut.ensure_integrity()) Mock.assert_called_once_with(self.blocks.remove_block, b'block_prefix.block16') def test_check_stales_no_index(self): self.headers.get_best_header.return_value = { 'block_height': 16, 'block_hash': 'block10' } self.cache.get_index.return_value = None self.headers.get_headers_since_height.return_value = [{ 'block_height': 12, 'block_hash': 'block12' }, { 'block_height': 13, 'block_hash': 'block13' }, { 'block_height': 14, 'block_hash': 'block14' }, { 'block_height': 15, 'block_hash': 'block15' }, { 'block_height': 16, 'block_hash': 'block16' }] self.blocks.storage_name = b'block_prefix' self.blocks.get_key.side_effect = lambda x, y: b'block_prefix.' + BLOCK_INDEX_PREFIX + b'.' + x.encode( ) self.cache.get_index.return_value = None Mock.assert_not_called(self.blocks.remove_block)