Example #1
0
    def _apply_application_change(cur, block_num, application_state_change):
        application_container = ApplicationContainer()
        application_container.ParseFromString(application_state_change.value)

        # Since this contains all the applications for a given record, we must
        # mark all the existing ones for the record as the previous block
        if application_container.entries:
            cur.execute(
                'UPDATE application SET end_block_num = %s '
                'WHERE end_block_num = %s AND '
                'record_identifier = %s', [
                    block_num, MAX_BLOCK_NUMBER,
                    application_container.entries[0].record_identifier
                ])

        for application in application_container.entries:
            if application_state_change.type == StateChange.SET:
                cur.execute(
                    'INSERT INTO application '
                    '(start_block_num, end_block_num,'
                    ' record_identifier, applicant,'
                    ' creation_time, type, status, terms) '
                    'VALUES (%s, %s, %s, %s, %s, %s, %s, %s)', [
                        block_num, MAX_BLOCK_NUMBER,
                        application.record_identifier, application.applicant,
                        application.creation_time, application.type,
                        application.status, application.terms
                    ])
Example #2
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 #3
0
    def _application_create(self, state, originator, data):
        txn_data = ApplicationCreatePayload()
        txn_data.ParseFromString(data)

        agent_addr = Addressing.agent_address(originator)
        record_addr = Addressing.record_address(txn_data.record_identifier)
        application_addr = Addressing.application_address(
            txn_data.record_identifier)
        state_items = self._get(state,
                                [agent_addr, application_addr, record_addr])

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

        records = state_items.get(record_addr, None)
        record = self._find_record(records, txn_data.record_identifier)
        if record is None:
            raise InvalidTransaction("Record does not exist.")

        if record.final:
            raise InvalidTransaction("Record is final, no updates can be " +
                                     "proposed.")

        # check that the application does not exists
        applications = state_items.get(application_addr,
                                       ApplicationContainer())
        application = self._find_application(applications, originator,
                                             txn_data.record_identifier,
                                             txn_data.type)
        if application is not None:
            raise InvalidTransaction("Application already exists.")

        # create the new application
        application = applications.entries.add()
        application.record_identifier = txn_data.record_identifier
        application.applicant = originator
        application.creation_time = txn_data.creation_time
        application.type = txn_data.type
        application.status = Application.OPEN
        application.terms = txn_data.terms

        # send back the updated application list
        self._set(state, [(application_addr, applications)])
Example #4
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 #5
0
def _decode_application_container(encoded):
    return _pb_loads(ApplicationContainer(), encoded)