Exemplo n.º 1
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)])
    def test_agent_create(self):
        """
        Test if the supplychain processor can create an agent.
        """
        try:
            validator = self.validator
            factory = self.factory

            agent_pub_key = factory.public_key
            agent_addr = Addressing.agent_address(agent_pub_key)

            # 1. -> Send a set transaction
            #    <- Expect a state get request
            validator.send(factory.create_agent_tp_process_request("agent"))

            # Expect test for agent existance
            received = self.expect_get([agent_addr])
            self.respond_get(received, [(agent_addr, None)])

            # Expect create agent
            agent = factory.create_agent(agent_pub_key, "agent")
            received = self.expect_set([(agent_addr, agent)])
            self.respond_set(received, [agent_addr])

            self.expect_ok()
        except Exception:
            LOGGER.exception("test_agent_create exception")
            raise
Exemplo n.º 3
0
    def test_agent_create(self):
        """
        Test if the supplychain processor can create an agent.
        """
        try:
            validator = self.validator
            factory = self.factory

            agent_pub_key = factory.public_key
            agent_addr = Addressing.agent_address(agent_pub_key)

            # 1. -> Send a set transaction
            #    <- Expect a state get request
            validator.send(factory.create_agent_tp_process_request("agent"))

            # Expect test for agent existance
            received = self.expect_get([agent_addr])
            self.respond_get(received, [(agent_addr, None)])

            # Expect create agent
            agent = factory.create_agent(agent_pub_key, "agent")
            received = self.expect_set([(agent_addr, agent)])
            self.respond_set(received, [agent_addr])

            self.expect_ok()
        except Exception:
            LOGGER.exception("test_agent_create exception")
            raise
Exemplo n.º 4
0
 def agent_create(self, name, wait=None):
     addrs = [Addressing.agent_address(self._public_key)]
     return self._send_txn(SupplyChainPayload.AGENT_CREATE,
                           AgentCreatePayload(name=name),
                           addrs,
                           addrs,
                           wait=wait)
Exemplo n.º 5
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)])
Exemplo n.º 6
0
 def agent_get(self, public_key):
     address = Addressing.agent_address(public_key)
     result = self._send_get("state/{}".format(address))
     try:
         data = self._get_result_data(result)
         agents = _decode_agent_container(data)
         return next((agent for agent in agents.entries
                      if agent.identifier == public_key), None)
     except BaseException:
         return None
Exemplo n.º 7
0
    def create_agent_tp_process_request(self, name):
        agent_pub_key = self.public_key
        agent_addr = Addressing.agent_address(agent_pub_key)
        inputs = [agent_addr]
        outputs = [agent_addr]

        return self.create_tp_process_request(
            SupplyChainPayload.AGENT_CREATE,
            AgentCreatePayload(name="agent"),
            inputs, outputs)
Exemplo n.º 8
0
 def agent_get(self, public_key):
     address = Addressing.agent_address(public_key)
     result = self._send_get("state/{}".format(address))
     try:
         data = self._get_result_data(result)
         agents = _decode_agent_container(data)
         return next((agent for agent in agents.entries
                      if agent.identifier == public_key), None)
     except BaseException:
         return None
Exemplo n.º 9
0
    def record_create(self, record_identifier, creation_time=None, wait=None):
        outputs = [Addressing.record_address(record_identifier)]
        inputs = outputs + \
            [Addressing.agent_address(self._public_key)]

        return self._send_txn(
            SupplyChainPayload.RECORD_CREATE,
            RecordCreatePayload(
                identifier=record_identifier,
                creation_time=creation_time or int(time.time())),
            inputs=inputs, outputs=outputs, wait=wait)
Exemplo n.º 10
0
    def finalize_record_tp_process_request(self,
                                           record_id):
        record_addr = Addressing.record_address(record_id)
        agent_pub_key = self.public_key
        agent_addr = Addressing.agent_address(agent_pub_key)
        inputs = [agent_addr, record_addr]
        outputs = [agent_addr, record_addr]

        return self.create_tp_process_request(
            SupplyChainPayload.RECORD_FINALIZE,
            RecordFinalizePayload(identifier=record_id),
            inputs, outputs)
Exemplo n.º 11
0
    def record_create(self, record_identifier, creation_time=None, wait=None):
        outputs = [Addressing.record_address(record_identifier)]
        inputs = outputs + \
            [Addressing.agent_address(self._public_key)]

        return self._send_txn(SupplyChainPayload.RECORD_CREATE,
                              RecordCreatePayload(identifier=record_identifier,
                                                  creation_time=creation_time
                                                  or int(time.time())),
                              inputs=inputs,
                              outputs=outputs,
                              wait=wait)
Exemplo n.º 12
0
 def application_create(self, record_identifier, application_type,
                        terms="", creation_time=None, wait=None):
     outputs = [Addressing.application_address(record_identifier)]
     inputs = outputs + [Addressing.agent_address(self.public_key),
                         Addressing.record_address(record_identifier)]
     return self._send_txn(
         SupplyChainPayload.APPLICATION_CREATE,
         ApplicationCreatePayload(
             record_identifier=record_identifier,
             creation_time=creation_time or int(time.time()),
             type=application_type,
             terms=terms),
         inputs=inputs, outputs=outputs, wait=wait)
Exemplo n.º 13
0
 def application_cancel(self, record_identifier, application_type,
                        wait=None):
     outputs = [Addressing.application_address(record_identifier),
                Addressing.record_address(record_identifier)]
     inputs = outputs + [Addressing.agent_address(self.public_key),
                         Addressing.record_address(record_identifier)]
     return self._send_txn(
         SupplyChainPayload.APPLICATION_CANCEL,
         ApplicationCancelPayload(
             record_identifier=record_identifier,
             applicant=self._public_key,
             type=application_type),
         inputs=inputs, outputs=outputs, wait=wait)
Exemplo n.º 14
0
    def test_create_application(self):
        """
        Test if the supplychain processor can create a application.
        """
        try:
            validator = self.validator
            factory = self.factory

            record_id = "serial number"
            record_addr = Addressing.record_address(record_id)
            agent_pub_key = factory.public_key
            agent_addr = Addressing.agent_address(agent_pub_key)
            application_addr = Addressing.application_address(record_id)
            timestamp = int(time.time())
            terms = "Please take this."

            # 1. -> Send a set transaction
            #    <- Expect a state get request
            validator.send(factory.create_application_tp_process_request(
                agent_pub_key,
                record_id,
                timestamp,
                Application.OWNER,
                terms))

            # test for record existance
            received = self.expect_get(
                [agent_addr, application_addr, record_addr])

            agent = factory.create_agent(agent_pub_key)
            record = factory.create_record(record_id)
            self.respond_get(received,
                             [(agent_addr, agent),
                              (application_addr, None),
                              (record_addr, record)])

            # Expect test for existance
            application = factory.create_application(
                record_id,
                agent_pub_key,
                Application.OWNER,
                Application.OPEN,
                timestamp,
                terms)
            received = self.expect_set([(application_addr, application)])
            self.respond_set(received, [application_addr])

            self.expect_ok()
        except Exception:
            LOGGER.exception("test_create_application exception")
            raise
Exemplo n.º 15
0
    def create_record_tp_process_request(self,
                                         identifier,
                                         timestamp):
        record_addr = Addressing.record_address(identifier)
        agent_pub_key = self.public_key
        agent_addr = Addressing.agent_address(agent_pub_key)
        inputs = [agent_addr, record_addr]
        outputs = [agent_addr, record_addr]

        return self.create_tp_process_request(
            SupplyChainPayload.RECORD_CREATE,
            RecordCreatePayload(identifier=identifier,
                                creation_time=timestamp),
            inputs, outputs)
Exemplo n.º 16
0
 def application_accept(self, record_identifier,
                        applicant, application_type,
                        timestamp=None, wait=None):
     outputs = [Addressing.application_address(record_identifier),
                Addressing.record_address(record_identifier)]
     inputs = outputs + [Addressing.agent_address(self.public_key),
                         Addressing.record_address(record_identifier)]
     return self._send_txn(
         SupplyChainPayload.APPLICATION_ACCEPT,
         ApplicationAcceptPayload(
             record_identifier=record_identifier,
             applicant=applicant,
             type=application_type,
             timestamp=timestamp or time.time()),
         inputs=inputs, outputs=outputs, wait=wait)
Exemplo n.º 17
0
    def create_application_cancel_tp_process_request(self,
                                                     record_id,
                                                     applicant_id,
                                                     application_type):
        record_addr = Addressing.record_address(record_id)
        agent_pub_key = self.public_key
        agent_addr = Addressing.agent_address(agent_pub_key)
        application_addr = Addressing.application_address(record_id)
        inputs = [agent_addr, application_addr, record_addr]
        outputs = [application_addr]

        return self.create_tp_process_request(
            SupplyChainPayload.APPLICATION_CANCEL,
            ApplicationCancelPayload(record_identifier=record_id,
                                     applicant=applicant_id,
                                     type=application_type),
            inputs, outputs)
Exemplo n.º 18
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)])
Exemplo n.º 19
0
    def test_create_application(self):
        """
        Test if the supplychain processor can create a application.
        """
        try:
            validator = self.validator
            factory = self.factory

            record_id = "serial number"
            record_addr = Addressing.record_address(record_id)
            agent_pub_key = factory.public_key
            agent_addr = Addressing.agent_address(agent_pub_key)
            application_addr = Addressing.application_address(record_id)
            timestamp = int(time.time())
            terms = "Please take this."

            # 1. -> Send a set transaction
            #    <- Expect a state get request
            validator.send(
                factory.create_application_tp_process_request(
                    agent_pub_key, record_id, timestamp, Application.OWNER,
                    terms))

            # test for record existance
            received = self.expect_get(
                [agent_addr, application_addr, record_addr])

            agent = factory.create_agent(agent_pub_key)
            record = factory.create_record(record_id)
            self.respond_get(received, [(agent_addr, agent),
                                        (application_addr, None),
                                        (record_addr, record)])

            # Expect test for existance
            application = factory.create_application(record_id, agent_pub_key,
                                                     Application.OWNER,
                                                     Application.OPEN,
                                                     timestamp, terms)
            received = self.expect_set([(application_addr, application)])
            self.respond_set(received, [application_addr])

            self.expect_ok()
        except Exception:
            LOGGER.exception("test_create_application exception")
            raise
Exemplo n.º 20
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)])
Exemplo n.º 21
0
    def create_application_accept_tp_process_request(self,
                                                     record_id,
                                                     applicant_id,
                                                     application_type,
                                                     timestamp=0):
        record_addr = Addressing.record_address(record_id)
        agent_pub_key = self.public_key
        agent_addr = Addressing.agent_address(agent_pub_key)
        inputs = [agent_addr, record_addr]
        outputs = [record_addr]

        return self.create_tp_process_request(
            SupplyChainPayload.APPLICATION_ACCEPT,
            ApplicationAcceptPayload(record_identifier=record_id,
                                     applicant=applicant_id,
                                     type=application_type,
                                     timestamp=timestamp),
            inputs, outputs)
Exemplo n.º 22
0
    def create_application_tp_process_request(self,
                                              agent_pub_key,
                                              record_id,
                                              timestamp,
                                              application_type,
                                              terms=None):
        record_addr = Addressing.record_address(record_id)
        agent_pub_key = self.public_key
        agent_addr = Addressing.agent_address(agent_pub_key)
        inputs = [agent_addr, record_addr]
        outputs = [record_addr]

        return self.create_tp_process_request(
            SupplyChainPayload.APPLICATION_CREATE,
            ApplicationCreatePayload(record_identifier=record_id,
                                     creation_time=timestamp,
                                     type=application_type,
                                     terms=terms),
            inputs, outputs)
Exemplo n.º 23
0
    def test_record_create(self):
        """
        Test if the supplychain processor can create a record.
        """
        try:
            validator = self.validator
            factory = self.factory

            record_identifier = 'serial number'
            record_addr = Addressing.record_address(record_identifier)
            agent_pub_key = factory.public_key
            agent_addr = Addressing.agent_address(agent_pub_key)
            record_timestamp = int(time.time())

            # 1. -> Send a set transaction
            #    <- Expect a state get request
            validator.send(factory.create_record_tp_process_request(
                record_identifier, record_timestamp))

            # test for record existance
            received = self.expect_get([agent_addr, record_addr])
            agent = factory.create_agent(agent_pub_key)
            self.respond_get(received,
                             [(agent_addr, agent),
                              (record_addr, None)])

            # Expect create Record
            record = factory.create_record(record_identifier,
                                           record_timestamp)
            factory.add_agent_record(record.owners,
                                     agent_pub_key,
                                     record_timestamp)
            factory.add_agent_record(record.custodians,
                                     agent_pub_key,
                                     record_timestamp)
            received = self.expect_set([(record_addr, record)])
            self.respond_set(received, [record_addr])

            self.expect_ok()
        except Exception:
            LOGGER.exception("test_record_create exception")
            raise
Exemplo n.º 24
0
 def application_cancel(self,
                        record_identifier,
                        application_type,
                        wait=None):
     outputs = [
         Addressing.application_address(record_identifier),
         Addressing.record_address(record_identifier)
     ]
     inputs = outputs + [
         Addressing.agent_address(self.public_key),
         Addressing.record_address(record_identifier)
     ]
     return self._send_txn(SupplyChainPayload.APPLICATION_CANCEL,
                           ApplicationCancelPayload(
                               record_identifier=record_identifier,
                               applicant=self._public_key,
                               type=application_type),
                           inputs=inputs,
                           outputs=outputs,
                           wait=wait)
Exemplo n.º 25
0
    def _agent_create(self, state, originator, data):
        txn_data = AgentCreatePayload()
        txn_data.ParseFromString(data)

        agent_addr = Addressing.agent_address(originator)
        state_items = self._get(state, [agent_addr])
        agents = state_items.get(agent_addr, AgentContainer())

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

        # create the new agent
        agent = agents.entries.add()
        agent.identifier = originator
        agent.name = txn_data.name

        # send back the updated agents list
        self._set(state, [(agent_addr, agents)])
Exemplo n.º 26
0
 def application_create(self,
                        record_identifier,
                        application_type,
                        terms="",
                        creation_time=None,
                        wait=None):
     outputs = [Addressing.application_address(record_identifier)]
     inputs = outputs + [
         Addressing.agent_address(self.public_key),
         Addressing.record_address(record_identifier)
     ]
     return self._send_txn(
         SupplyChainPayload.APPLICATION_CREATE,
         ApplicationCreatePayload(record_identifier=record_identifier,
                                  creation_time=creation_time
                                  or int(time.time()),
                                  type=application_type,
                                  terms=terms),
         inputs=inputs,
         outputs=outputs,
         wait=wait)
Exemplo n.º 27
0
    def _agent_create(self, state, originator, data):
        txn_data = AgentCreatePayload()
        txn_data.ParseFromString(data)

        agent_addr = Addressing.agent_address(originator)
        LOGGER.debug("_agent_create: %s %s", originator, agent_addr)
        state_items = self._get(state, [agent_addr])
        agents = state_items.get(agent_addr, AgentContainer())

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

        # create the new agent
        agent = agents.entries.add()
        agent.identifier = originator
        agent.name = txn_data.name

        # send back the updated agents list
        self._set(state, [(agent_addr, agents)])
Exemplo n.º 28
0
    def test_record_create(self):
        """
        Test if the supplychain processor can create a record.
        """
        try:
            validator = self.validator
            factory = self.factory

            record_identifier = 'serial number'
            record_addr = Addressing.record_address(record_identifier)
            agent_pub_key = factory.public_key
            agent_addr = Addressing.agent_address(agent_pub_key)
            record_timestamp = int(time.time())

            # 1. -> Send a set transaction
            #    <- Expect a state get request
            validator.send(
                factory.create_record_tp_process_request(
                    record_identifier, record_timestamp))

            # test for record existance
            received = self.expect_get([agent_addr, record_addr])
            agent = factory.create_agent(agent_pub_key)
            self.respond_get(received, [(agent_addr, agent),
                                        (record_addr, None)])

            # Expect create Record
            record = factory.create_record(record_identifier, record_timestamp)
            factory.add_agent_record(record.owners, agent_pub_key,
                                     record_timestamp)
            factory.add_agent_record(record.custodians, agent_pub_key,
                                     record_timestamp)
            received = self.expect_set([(record_addr, record)])
            self.respond_set(received, [record_addr])

            self.expect_ok()
        except Exception:
            LOGGER.exception("test_record_create exception")
            raise
Exemplo n.º 29
0
 def application_accept(self,
                        record_identifier,
                        applicant_public_key,
                        application_type,
                        timestamp=None,
                        wait=None):
     outputs = [
         Addressing.application_address(record_identifier),
         Addressing.record_address(record_identifier)
     ]
     inputs = outputs + [
         Addressing.agent_address(self.public_key),
         Addressing.record_address(record_identifier)
     ]
     return self._send_txn(SupplyChainPayload.APPLICATION_ACCEPT,
                           ApplicationAcceptPayload(
                               record_identifier=record_identifier,
                               applicant=applicant_public_key,
                               type=application_type,
                               timestamp=timestamp or int(time.time())),
                           inputs=inputs,
                           outputs=outputs,
                           wait=wait)
Exemplo n.º 30
0
 def agent_create(self, name, wait=None):
     addrs = [Addressing.agent_address(self._public_key)]
     return self._send_txn(
         SupplyChainPayload.AGENT_CREATE,
         AgentCreatePayload(name=name),
         addrs, addrs, wait=wait)