Skip to content

Commit

Permalink
Problem: websocket fails with ELECTION transaction (#2482)
Browse files Browse the repository at this point in the history
Solution: have a more general approach to process transaction types. If
a transaction does *not* contain `asset.id`, then the `id` of the
`asset` is the `id` of the transaction.
  • Loading branch information
vrde authored and kansi committed Aug 27, 2018
1 parent 6fdcaf4 commit 41a2687
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
24 changes: 14 additions & 10 deletions bigchaindb/web/websocket_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ def _multiprocessing_to_asyncio(in_queue, out_queue, loop):
loop.call_soon_threadsafe(out_queue.put_nowait, value)


def eventify_block(block):
for tx in block['transactions']:
try:
asset_id = tx['asset']['id']
except KeyError:
asset_id = tx['id']
yield {'height': block['height'],
'asset_id': asset_id,
'transaction_id': tx['id']}


class Dispatcher:
"""Dispatch events to websockets.
Expand Down Expand Up @@ -99,17 +110,10 @@ def publish(self):
str_buffer.append(event)

elif event.type == EventTypes.BLOCK_VALID:
block = event.data

for tx in block['transactions']:
asset_id = tx['id'] if tx['operation'] == 'CREATE' else tx['asset']['id']
data = {'height': block['height'],
'asset_id': asset_id,
'transaction_id': tx['id']}
str_buffer.append(json.dumps(data))
str_buffer = map(json.dumps, eventify_block(event.data))

for _, websocket in self.subscribers.items():
for str_item in str_buffer:
for str_item in str_buffer:
for _, websocket in self.subscribers.items():
yield from websocket.send_str(str_item)


Expand Down
27 changes: 27 additions & 0 deletions tests/web/test_websocket_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,33 @@ def send_str(self, s):
self.received.append(s)


def test_eventify_block_works_with_any_transaction():
from bigchaindb.web.websocket_server import eventify_block

block = {
'height': 1,
'transactions': [{
'id': 1
}, {
'id': 2,
'asset': {'id': 1}
}]
}

expected_events = [{
'height': 1,
'asset_id': 1,
'transaction_id': 1
}, {
'height': 1,
'asset_id': 1,
'transaction_id': 2
}]

for event, expected in zip(eventify_block(block), expected_events):
assert event == expected


@asyncio.coroutine
def test_bridge_sync_async_queue(loop):
from bigchaindb.web.websocket_server import _multiprocessing_to_asyncio
Expand Down

0 comments on commit 41a2687

Please sign in to comment.