def test_tf_events(self): """Test that tf events are generated correctly.""" gen_data = [ ["test1", "test2"], ["test3"], ["test4", "test5", "test6"], ] event_sets = [ [ Event(event_type=event_type) for event_type in events ] for events in gen_data ] receipts = [ TransactionReceipt(events=events) for events in event_sets ] extractor = ReceiptEventExtractor(receipts) events = extractor.extract([]) self.assertEqual([], events) events = extractor.extract([ EventSubscription(event_type="test1"), EventSubscription(event_type="test5"), ]) self.assertEqual(events, [event_sets[0][0], event_sets[2][1]])
def test_state_delta_events(self): """Test that sawtooth/state-delta events are generated correctly.""" gen_data = [ [("a", b"a", StateChange.SET), ("b", b"b", StateChange.DELETE)], [("a", b"a", StateChange.DELETE), ("d", b"d", StateChange.SET)], [("e", b"e", StateChange.SET)], ] change_sets = [ [ StateChange(address=address, value=value, type=change_type) for address, value, change_type in state_changes ] for state_changes in gen_data ] receipts = [ TransactionReceipt(state_changes=state_changes) for state_changes in change_sets ] extractor = ReceiptEventExtractor(receipts) factory = EventFilterFactory() events = extractor.extract([ EventSubscription( event_type="sawtooth/state-delta", filters=[factory.create("address", "a")]), EventSubscription( event_type="sawtooth/state-delta", filters=[factory.create( "address", "[ce]", EventFilter.REGEX_ANY)], ) ]) self.assertEqual(events, [Event( event_type="sawtooth/state-delta", attributes=[ Event.Attribute(key="address", value=address) for address in ["e", "d", "a", "b"] ], data=StateChangeList(state_changes=[ change_sets[2][0], change_sets[1][1], change_sets[1][0], change_sets[0][1], ]).SerializeToString(), )])
def get_events_for_block(self, blkw, subscriptions): receipts = [] for batch in blkw.block.batches: for txn in batch.transactions: try: receipts.append(self._receipt_store.get( txn.header_signature)) except KeyError: LOGGER.warning( "Transaction id %s not found in receipt store " " while looking" " up events for block id %s", txn.header_signature[:10], blkw.identifier[:10]) block_event_extractor = BlockEventExtractor(blkw) receipt_event_extractor = ReceiptEventExtractor(receipts=receipts) events = [] events.extend(block_event_extractor.extract(subscriptions)) events.extend(receipt_event_extractor.extract(subscriptions)) return events
def chain_update(self, block, receipts): extractors = [ BlockEventExtractor(block), ReceiptEventExtractor(receipts), ] subscriptions = [] for subscriber in self._subscribers.values(): for subscription in subscriber.subscriptions: if subscription not in subscriptions: subscriptions.append(subscription) events = [] for extractor in extractors: extracted_events = extractor.extract(subscriptions) if extracted_events: events.extend(extracted_events) if events: self.broadcast_events(events)
def chain_update(self, block, receipts): """ Handles both "sawtooth/block-commit" Events and "setting/update" Events. For "sawtooth/block-commit", the last_block_num is updated or a fork is detected. For "setting/update", the corresponding cache entry will be updated. """ block_events = BlockEventExtractor(block).extract( [EventSubscription(event_type="sawtooth/block-commit")]) receipt_events = ReceiptEventExtractor(receipts).extract( [EventSubscription(event_type="setting/update")]) for event in block_events: forked = self._handle_block_commit(event) if forked: return for event in receipt_events: if event.event_type == "setting/update": self._handle_txn_commit(event)
def get_events_for_block_ids(self, block_ids, subscriptions): """Get a list of events associated with all the block ids. Args: block_ids (list of str): The block ids to search for events that match each subscription. subscriptions (list of EventSubscriptions): EventFilter and event type to filter events. Returns (list of Events): The Events associated which each block id. Raises: KeyError A block id isn't found within the block store. """ events = [] extractors = [] for block_id in block_ids: blk_w = self._block_store[block_id] extractors.append(BlockEventExtractor(blk_w)) receipts = [] for batch in blk_w.block.batches: for txn in batch.transactions: try: receipts.append( self._receipt_store.get(txn.header_signature)) except KeyError: LOGGER.warning( "Transaction id %s not found in receipt store " " while looking" " up events for block id %s", txn.header_signature[:10], block_id[:10]) extractors.append(ReceiptEventExtractor(receipts=receipts)) for extractor in extractors: events.extend(extractor.extract(subscriptions)) return events