def _make_state_delta_events(self, subscriptions): gen = False for subscription in subscriptions: if subscription.event_type == "state_delta": gen = True if not gen: return [] addresses = set() attributes = [] squashed_changes = [] for receipt in reversed(self._receipts): for state_change in reversed(receipt.state_changes): address = state_change.address if address not in addresses: addresses.add(address) attributes.append( Event.Attribute(key="address", value=address)) squashed_changes.append(state_change) state_delta_set = StateDeltaSet() state_delta_set.state_changes.extend(squashed_changes) event = Event( event_type="state_delta", attributes=attributes, data=state_delta_set.SerializeToString()) for subscription in subscriptions: if event in subscription: return [event]
def test_chain_update(self): """ Test that if there is no fork and only one value is udpated, only that value is in validated in the catch. """ # Set up cache so it does not fork block1 = self.create_block() self._identity_obsever.chain_update(block1, []) self._identity_cache.get_role("network", "state_root") self._identity_cache.get_policy("policy1", "state_root") self.assertNotEqual(self._identity_cache["network"], None) self.assertNotEqual(self._identity_cache["policy1"], None) # Add next block and event that says network was updated. block2 = self.create_block("abcdef1234567890") event = Event( event_type="identity_update", attributes=[Event.Attribute(key="updated", value="network")]) receipts = TransactionReceipt(events=[event]) self._identity_obsever.chain_update(block2, [receipts]) # Check that only "network" was invalidated self.assertEquals(self._identity_cache["network"], None) self.assertNotEqual(self._identity_cache["policy1"], None) # check that the correct values can be fetched from state. identity_view = \ self._identity_view_factory.create_identity_view("state_root") self.assertEquals( self._identity_cache.get_role("network", "state_root"), identity_view.get_role("network")) self.assertEquals( self._identity_cache.get_policy("policy1", "state_root"), identity_view.get_policy("policy1"))
def do_chain_update(): block1 = testIdentityObserver.create_block() testIdentityObserver._identity_obsever.chain_update(block1, []) testIdentityObserver._identity_cache.get_role("network", "state_root") testIdentityObserver._identity_cache.get_policy( "policy1", "state_root") assert testIdentityObserver._identity_cache["network"] != None assert testIdentityObserver._identity_cache["policy1"] != None # Add next block and event that says network was updated. block2 = testIdentityObserver.create_block("abcdef1234567890") event = Event( event_type="identity/update", attributes=[Event.Attribute(key="updated", value="network")]) receipts = TransactionReceipt(events=[event]) testIdentityObserver._identity_obsever.chain_update(block2, [receipts]) # Check that only "network" was invalidated assert testIdentityObserver._identity_cache["network"] == None assert testIdentityObserver._identity_cache["policy1"] != None # check that the correct values can be fetched from state. identity_view = \ testIdentityObserver._identity_view_factory.create_identity_view("state_root") assert \ testIdentityObserver._identity_cache.get_role("network", "state_root") == \ identity_view.get_role("network") assert \ testIdentityObserver._identity_cache.get_policy("policy1", "state_root") == \ identity_view.get_policy("policy1")
def test_chain_update(): """ Test that if there is no fork and only one value is udpated, only that value is in validated in the catch. """ # Set up cache so it does not fork testIdentityObserver = TestIdentityObserver() block1 = testIdentityObserver.create_block() testIdentityObserver._identity_obsever.chain_update(block1, []) testIdentityObserver._identity_cache.get_role("network", "state_root") testIdentityObserver._identity_cache.get_policy( "policy1", "state_root") # Add next block and event that says network was updated. block2 = testIdentityObserver.create_block("abcdef1234567890") event = Event( event_type="identity/update", attributes=[Event.Attribute(key="updated", value="network")]) receipts = TransactionReceipt(events=[event]) testIdentityObserver._identity_obsever.chain_update(block2, [receipts]) identity_view = \ testIdentityObserver._identity_view_factory.create_identity_view( "state_root")
def _make_event(self): block = self._block attributes = [ Event.Attribute(key="block_id", value=block.identifier), Event.Attribute(key="block_num", value=str(block.block_num)), Event.Attribute( key="state_root_hash", value=block.state_root_hash), Event.Attribute( key="previous_block_id", value=block.previous_block_id)] return Event(event_type="block_commit", attributes=attributes)
def test_receipt_store_get_and_set(self): """Tests that we correctly get and set state changes to a ReceiptStore. This test sets a list of receipts and then gets them back, ensuring that the data is the same. """ receipt_store = TransactionReceiptStore(DictDatabase()) receipts = [] for i in range(10): state_changes = [] events = [] data = [] for j in range(10): string = str(j) byte = string.encode() state_changes.append( StateChange(address='a100000' + string, value=byte, type=StateChange.SET)) events.append( Event( event_type="test", data=byte, attributes=[Event.Attribute(key=string, value=string)])) data.append( TransactionReceipt.Data(data_type="test", data=byte)) receipts.append( TransactionReceipt(state_changes=state_changes, events=events, data=data)) for i in range(len(receipts)): receipt_store.put(str(i), receipts[i]) for i in range(len(receipts)): stored_receipt = receipt_store.get(str(i)) self.assertEqual(stored_receipt.state_changes, receipts[i].state_changes) self.assertEqual(stored_receipt.events, receipts[i].events) self.assertEqual(stored_receipt.data, receipts[i].data)
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 do_block_event_extractor(): block_header = BlockHeader(block_num=85, state_root_hash="0987654321fedcba", previous_block_id="0000000000000000") block = BlockWrapper( Block(header_signature="abcdef1234567890", header=block_header.SerializeToString())) extractor = BlockEventExtractor(block) events = extractor.extract( [EventSubscription(event_type="sawtooth/block-commit")]) assert events == [ Event(event_type="sawtooth/block-commit", attributes=[ Event.Attribute(key="block_id", value="abcdef1234567890"), Event.Attribute(key="block_num", value="85"), Event.Attribute(key="state_root_hash", value="0987654321fedcba"), Event.Attribute(key="previous_block_id", value="0000000000000000") ]) ]
def test_block_event_extractor(self): """Test that a sawtooth/block-commit event is generated correctly.""" block_header = BlockHeader(block_num=85, state_root_hash="0987654321fedcba", previous_block_id="0000000000000000") block = BlockWrapper( Block(header_signature="abcdef1234567890", header=block_header.SerializeToString())) extractor = BlockEventExtractor(block) events = extractor.extract( [EventSubscription(event_type="sawtooth/block-commit")]) self.assertEqual(events, [ Event(event_type="sawtooth/block-commit", attributes=[ Event.Attribute(key="block_id", value="abcdef1234567890"), Event.Attribute(key="block_num", value="85"), Event.Attribute(key="state_root_hash", value="0987654321fedcba"), Event.Attribute(key="previous_block_id", value="0000000000000000") ]) ])
def do_receipt_store_get_and_set(): receipt_store = TransactionReceiptStore(DictDatabase()) receipts = [] for i in range(10): state_changes = [] events = [] data = [] for j in range(10): string = str(j) byte = string.encode() state_changes.append( StateChange( address='a100000' + string, value=byte, type=StateChange.SET)) events.append( Event( event_type="test", data=byte, attributes=[Event.Attribute(key=string, value=string)])) data.append(byte) receipts.append( TransactionReceipt( state_changes=state_changes, events=events, data=data)) for i, receipt in enumerate(receipts): receipt_store.put(str(i), receipt) for i, receipt in enumerate(receipts): stored_receipt = receipt_store.get(str(i)) assert stored_receipt.state_changes == receipt.state_changes
def do_tf_events(): 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([]) assert [] == events events = extractor.extract([ EventSubscription(event_type="test1"), EventSubscription(event_type="test5"), ]) assert events == [event_sets[0][0], event_sets[2][1]]
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]])