Example #1
0
    def _record_finalize(self, state, originator, data):
        txn_data = RecordFinalizePayload()
        txn_data.ParseFromString(data)

        record_addr = Addressing.record_address(txn_data.identifier)

        state_items = self._get(state, [record_addr])

        records = state_items.get(record_addr, RecordContainer())

        # check that the record does not exists
        record = self._find_record(records, txn_data.identifier)
        if record is None:
            raise InvalidTransaction("Record does not exists.")

        owner = record.owners[len(record.owners) - 1]
        if owner.agent_identifier != originator:
            raise InvalidTransaction("Record can only be finalized by owner.")

        custodian = record.custodians[len(record.custodians) - 1]
        if custodian.agent_identifier != originator:
            raise InvalidTransaction(
                "Application can only be finalized when the owner is the " +
                "custodian.")

        # Mark the record as final
        record.final = True

        # send back the updated agents list
        self._set(state, [(record_addr, records)])
Example #2
0
    def _record_create(self, state, originator, data):
        txn_data = RecordCreatePayload()
        txn_data.ParseFromString(data)

        agent_addr = Addressing.agent_address(originator)
        record_addr = Addressing.record_address(txn_data.identifier)

        state_items = self._get(state, [agent_addr, record_addr])

        agents = state_items.get(agent_addr, None)
        records = state_items.get(record_addr, RecordContainer())

        # check that the agent exists
        if self._find_agent(agents, originator) is None:
            raise InvalidTransaction("Agent is not registered.")

        # check that the record does not exists
        record = self._find_record(records, txn_data.identifier)
        if record is not None:
            raise InvalidTransaction("Record already exists.")

        # create the new record
        record = records.entries.add()
        record.identifier = txn_data.identifier
        record.creation_time = txn_data.creation_time
        owner = record.owners.add()
        owner.agent_identifier = originator
        owner.start_time = txn_data.creation_time
        custodian = record.custodians.add()
        custodian.agent_identifier = originator
        custodian.start_time = txn_data.creation_time
        record.final = False

        # send back the updated agents list
        self._set(state, [(record_addr, records)])
Example #3
0
 def _containerize(value):
     if isinstance(value, Agent):
         return AgentContainer(entries=[value])
     elif isinstance(value, Application):
         return ApplicationContainer(entries=[value])
     elif isinstance(value, Record):
         return RecordContainer(entries=[value])
     raise Exception("Unknown data type")
Example #4
0
    def _apply_record_change(cur, block_num, record_state_change):
        record_container = RecordContainer()
        record_container.ParseFromString(record_state_change.value)

        for record in record_container.entries:
            cur.execute(
                'UPDATE record SET end_block_num = %s '
                'WHERE end_block_num = %s AND identifier = %s',
                [block_num, MAX_BLOCK_NUMBER, record.identifier])

            if record_state_change.type == StateChange.SET:
                cur.execute(
                    'INSERT INTO record '
                    '(id, start_block_num, end_block_num,'
                    ' identifier, '
                    ' creation_time, '
                    ' finalize) '
                    'VALUES (DEFAULT, %s,  %s, %s, %s, %s) RETURNING id', [
                        block_num, MAX_BLOCK_NUMBER, record.identifier,
                        record.creation_time, record.final
                    ])

                (record_id, ) = cur.fetchone()

                insert_record_agent_sql = \
                    """INSERT INTO record_agent
                    (record_id, agent_identifier, start_time, agent_type)
                    VALUES (%s, %s, %s, %s)
                    """
                for owner in record.owners:
                    cur.execute(insert_record_agent_sql, [
                        record_id, owner.agent_identifier, owner.start_time,
                        Application.OWNER
                    ])
                for custodian in record.custodians:
                    cur.execute(insert_record_agent_sql, [
                        record_id, custodian.agent_identifier,
                        custodian.start_time, Application.CUSTODIAN
                    ])
Example #5
0
 def _get(state, addresses):
     entries = state.get(addresses)
     if entries:
         out = {}
         for e in entries:
             addr = e.address
             if e.data:
                 if addr.startswith(Addressing.agent_namespace()):
                     container = AgentContainer()
                 elif addr.startswith(Addressing.application_namespace()):
                     container = ApplicationContainer()
                 elif addr.startswith(Addressing.record_namespace()):
                     container = RecordContainer()
                 else:
                     raise InvalidTransaction("Unknown namespaces.")
             else:
                 container = None
             container.ParseFromString(e.data)
             out[addr] = container
         return out
     return {}
Example #6
0
def _decode_record_container(encoded):
    return _pb_loads(RecordContainer(), encoded)