From ea8ac4b80ef2ad61e2c2797f1a51db5eb6b93d96 Mon Sep 17 00:00:00 2001 From: Vanshdeep Singh Date: Tue, 3 Apr 2018 14:57:44 +0200 Subject: [PATCH] Problem: Event stream api crashing (#2173) Solution: Fix Exchange initialization --- bigchaindb/tendermint/commands.py | 5 +++-- bigchaindb/web/websocket_server.py | 4 ++-- tests/web/test_websocket_server.py | 12 +++++++++--- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/bigchaindb/tendermint/commands.py b/bigchaindb/tendermint/commands.py index 514ba1b7..8f1756ab 100644 --- a/bigchaindb/tendermint/commands.py +++ b/bigchaindb/tendermint/commands.py @@ -35,8 +35,6 @@ BANNER = """ def start(): # Exchange object for event stream api exchange = Exchange() - p_exchange = Process(name='exchange', target=exchange.run) - p_exchange.start() # start the web api app_server = server.create_server( @@ -61,6 +59,9 @@ def start(): args=(exchange.get_publisher_queue(),)) p_websocket_client.start() + p_exchange = Process(name='exchange', target=exchange.run) + p_exchange.start() + # We need to import this after spawning the web server # because import ABCIServer will monkeypatch all sockets # for gevent. diff --git a/bigchaindb/web/websocket_server.py b/bigchaindb/web/websocket_server.py index 930ebc94..e47ef67a 100644 --- a/bigchaindb/web/websocket_server.py +++ b/bigchaindb/web/websocket_server.py @@ -96,9 +96,9 @@ class Dispatcher: elif event.type == EventTypes.BLOCK_VALID: block = event.data - for tx in block['block']['transactions']: + for tx in block['transactions']: asset_id = tx['id'] if tx['operation'] == 'CREATE' else tx['asset']['id'] - data = {'block_id': block['id'], + data = {'height': block['height'], 'asset_id': asset_id, 'transaction_id': tx['id']} str_buffer.append(json.dumps(data)) diff --git a/tests/web/test_websocket_server.py b/tests/web/test_websocket_server.py index 4545fb59..e578bed7 100644 --- a/tests/web/test_websocket_server.py +++ b/tests/web/test_websocket_server.py @@ -121,23 +121,29 @@ def test_websocket_string_event(test_client, loop): def test_websocket_block_event(b, _block, test_client, loop): from bigchaindb import events from bigchaindb.web.websocket_server import init_app, POISON_PILL, EVENTS_ENDPOINT + from bigchaindb.models import Transaction + from bigchaindb.common import crypto + + user_priv, user_pub = crypto.generate_key_pair() + tx = Transaction.create([user_pub], [([user_pub], 1)]) + tx = tx.sign([user_priv]) event_source = asyncio.Queue(loop=loop) app = init_app(event_source, loop=loop) client = yield from test_client(app) ws = yield from client.ws_connect(EVENTS_ENDPOINT) - block = _block.to_dict() + block = {'height': 1, 'transactions': [tx.to_dict()]} block_event = events.Event(events.EventTypes.BLOCK_VALID, block) yield from event_source.put(block_event) - for tx in block['block']['transactions']: + for tx in block['transactions']: result = yield from ws.receive() json_result = json.loads(result.data) assert json_result['transaction_id'] == tx['id'] # Since the transactions are all CREATEs, asset id == transaction id assert json_result['asset_id'] == tx['id'] - assert json_result['block_id'] == block['id'] + assert json_result['height'] == block['height'] yield from event_source.put(POISON_PILL)