Esempio n. 1
0
    def test_get_receipts(self):
        """Tests that the TransactionReceiptGetRequestHandler will return a
        response with the receipt for the transaction requested.
        """
        receipt_store = TransactionReceiptStore(DictDatabase())

        receipt = TransactionReceipt(
            data=[TransactionReceipt.Data(
                data_type="dead", data="beef".encode())])

        receipt_store.put("deadbeef", receipt)

        handler = ClientReceiptGetRequestHandler(receipt_store)

        request = ClientReceiptGetRequest(
            transaction_ids=['deadbeef']).SerializeToString()

        response = handler.handle('test_conn_id', request)
        self.assertEqual(HandlerStatus.RETURN, response.status)
        self.assertEqual(ClientReceiptGetResponse.OK,
                         response.message_out.status)

        self.assertEqual([receipt], [r for r in response.message_out.receipts])


        request = ClientReceiptGetRequest(
            transaction_ids=['unknown']).SerializeToString()

        response = handler.handle('test_conn_id', request)
        self.assertEqual(HandlerStatus.RETURN, response.status)
        self.assertEqual(ClientReceiptGetResponse.NO_RESOURCE,
                         response.message_out.status)
Esempio n. 2
0
 def _make_receipts(self, results):
     receipts = []
     for result in results:
         receipt = TransactionReceipt()
         receipt.data.extend([
             TransactionReceipt.Data(data_type=data_type, data=data)
             for data_type, data in result.data
         ])
         receipt.state_changes.extend(result.state_changes)
         receipt.events.extend(result.events)
         receipt.transaction_id = result.signature
         receipts.append(receipt)
     return receipts
Esempio n. 3
0
    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"))
Esempio n. 4
0
    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)
Esempio n. 5
0
    def get(self, txn_id):
        """Returns the TransactionReceipt

        Args:
            txn_id (str): the id of the transaction for which the receipt
                should be retrieved.

        Returns:
            TransactionReceipt: The receipt for the given transaction id.

        Raises:
            KeyError: if the transaction id is unknown.
        """
        if txn_id not in self._receipt_db:
            raise KeyError('Unknown transaction id {}'.format(txn_id))

        txn_receipt_bytes = self._receipt_db[txn_id]
        txn_receipt = TransactionReceipt()
        txn_receipt.ParseFromString(txn_receipt_bytes)
        return txn_receipt