コード例 #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)])
コード例 #2
0
ファイル: handler.py プロジェクト: feihujiang/sawtooth-core
    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)])
コード例 #3
0
ファイル: handler.py プロジェクト: feihujiang/sawtooth-core
    def _application_accept(self, state, originator, data):
        txn_data = ApplicationAcceptPayload()
        txn_data.ParseFromString(data)

        record_addr = Addressing.record_address(txn_data.record_identifier)
        application_addr = Addressing.application_address(
            txn_data.record_identifier)

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

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

        # check that the recorf exists
        records = state_items.get(record_addr, None)
        record = self._find_record(records, application.record_identifier)
        if record is None:
            raise InvalidTransaction("Record does not exists.")

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

        # verify the txn signer is qualified to accept the application
        if application.type == Application.OWNER:
            owner = record.owners[len(record.owners) - 1]
            if owner.agent_identifier != originator:
                raise InvalidTransaction(
                    "Application can only be accepted by owner.")
        elif application.type == Application.CUSTODIAN:
            custodian = record.custodians[len(record.custodians) - 1]
            if custodian.agent_identifier != originator:
                raise InvalidTransaction(
                    "Application can only be accepted by custodian.")
        else:
            raise InvalidTransaction("Invalid application type.")

        # update the application
        application.status = Application.ACCEPTED

        # update the record
        if application.type == Application.OWNER:
            agent_record = record.owners.add()
        elif application.type == Application.CUSTODIAN:
            agent_record = record.custodians.add()
        agent_record.agent_identifier = application.applicant
        agent_record.start_time = txn_data.timestamp

        # send back the updated application list
        self._set(state, [(application_addr, applications),
                          (record_addr, records)])
コード例 #4
0
    def _application_accept(self, state, originator, data):
        txn_data = ApplicationAcceptPayload()
        txn_data.ParseFromString(data)

        record_addr = Addressing.record_address(txn_data.record_identifier)
        application_addr = Addressing.application_address(
            txn_data.record_identifier)

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

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

        # check that the recorf exists
        records = state_items.get(record_addr, None)
        record = self._find_record(records, application.record_identifier)
        if record is None:
            raise InvalidTransaction("Record does not exists.")

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

        # verify the txn signer is qualified to accept the application
        if application.type == Application.OWNER:
            owner = record.owners[len(record.owners) - 1]
            if owner.agent_identifier != originator:
                raise InvalidTransaction(
                    "Application can only be accepted by owner.")
        elif application.type == Application.CUSTODIAN:
            custodian = record.custodians[len(record.custodians) - 1]
            if custodian.agent_identifier != originator:
                raise InvalidTransaction(
                    "Application can only be accepted by custodian.")
        else:
            raise InvalidTransaction("Invalid application type.")

        # update the application
        application.status = Application.ACCEPTED

        # update the record
        if application.type == Application.OWNER:
            agent_record = record.owners.add()
        elif application.type == Application.CUSTODIAN:
            agent_record = record.custodians.add()
        agent_record.agent_identifier = application.applicant
        agent_record.start_time = txn_data.timestamp

        # send back the updated application list
        self._set(state, [(application_addr, applications),
                          (record_addr, records)])
コード例 #5
0
ファイル: client.py プロジェクト: feihujiang/sawtooth-core
    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)
コード例 #6
0
 def __init__(self, private=None, public=None):
     self._factory = MessageFactory(
         encoding='application/protobuf',
         family_name=SUPPLYCHAIN_FAMILY_NAME,
         family_version=SUPPLYCHAIN_VERSION,
         namespace=[Addressing.agent_namespace(),
                    Addressing.application_namespace(),
                    Addressing.record_namespace()],
         private=private,
         public=public
     )
コード例 #7
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)
コード例 #8
0
ファイル: client.py プロジェクト: sthagen/sawtooth-core
    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)
コード例 #9
0
ファイル: client.py プロジェクト: feihujiang/sawtooth-core
 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)
コード例 #10
0
ファイル: client.py プロジェクト: feihujiang/sawtooth-core
 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)
コード例 #11
0
    def test_application_accept(self):
        """
        Test if the supplychain processor can create a application.
        """
        try:
            validator = self.validator
            factory = self.factory

            applicant_id = "applicant pub key"

            record_id = "serial number"
            record_addr = Addressing.record_address(record_id)
            agent_pub_key = factory.public_key
            application_addr = Addressing.application_address(record_id)
            timestamp = int(time.time())

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

            # test for application and record existance
            received = self.expect_get([application_addr, record_addr])
            application = factory.create_application(
                record_id,
                applicant_id,
                Application.OWNER,
                Application.OPEN)
            record = factory.create_record(record_id)
            factory.add_agent_record(record.owners, agent_pub_key)

            self.respond_get(received,
                             [(application_addr, application),
                              (record_addr, record)])

            # Update record and application
            application.status = Application.ACCEPTED
            factory.add_agent_record(record.owners, applicant_id, timestamp)
            received = self.expect_set(
                [(application_addr, application),
                 (record_addr, record)])
            self.respond_set(received,
                             [application_addr, record_addr])
            self.expect_ok()
        except Exception:
            LOGGER.exception("test_application_accept exception")
            raise
コード例 #12
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
コード例 #13
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)
コード例 #14
0
ファイル: client.py プロジェクト: feihujiang/sawtooth-core
 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)
コード例 #15
0
ファイル: client.py プロジェクト: sthagen/sawtooth-core
 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)
コード例 #16
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)])
コード例 #17
0
    def test_application_cancel(self):
        """
        Test if the supplychain processor can cancel an application.
        """
        try:
            validator = self.validator
            factory = self.factory

            record_id = "serial number"
            agent_pub_key = factory.public_key
            application_addr = Addressing.application_address(record_id)

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

            # test for application existance
            received = self.expect_get([application_addr])
            application = factory.create_application(record_id, agent_pub_key,
                                                     Application.OWNER,
                                                     Application.OPEN)
            self.respond_get(received, [(application_addr, application)])

            # Update application
            application.status = Application.CANCELED
            received = self.expect_set([(application_addr, application)])
            self.respond_set(received, [application_addr])

            self.expect_ok()
        except Exception:
            LOGGER.exception("test_application_cancel exception")
            raise
コード例 #18
0
ファイル: handler.py プロジェクト: feihujiang/sawtooth-core
    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)])
コード例 #19
0
    def test_record_finalize(self):
        """
        Test if the supplychain processor can finalize a record.
        """
        try:
            validator = self.validator
            factory = self.factory

            record_id = "serial number"
            record_addr = Addressing.record_address(record_id)
            agent_pub_key = factory.public_key

            # 1. -> Send a set transaction
            #    <- Expect a state get request
            validator.send(
                factory.finalize_record_tp_process_request(record_id))

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

            record = factory.create_record(record_id)
            factory.add_agent_record(record.owners, agent_pub_key)
            factory.add_agent_record(record.custodians, agent_pub_key)
            self.respond_get(received, [(record_addr, record)])

            # Update record
            record.final = True
            received = self.expect_set([(record_addr, record)])
            self.respond_set(received, [record_addr])

            self.expect_ok()
        except Exception:
            LOGGER.exception("test_record_finalize exception")
            raise
コード例 #20
0
    def test_record_finalize(self):
        """
        Test if the supplychain processor can finalize a record.
        """
        try:
            validator = self.validator
            factory = self.factory

            record_id = "serial number"
            record_addr = Addressing.record_address(record_id)
            agent_pub_key = factory.public_key

            # 1. -> Send a set transaction
            #    <- Expect a state get request
            validator.send(
                factory.finalize_record_tp_process_request(
                    record_id))

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

            record = factory.create_record(record_id)
            factory.add_agent_record(record.owners, agent_pub_key)
            factory.add_agent_record(record.custodians, agent_pub_key)
            self.respond_get(received, [(record_addr, record)])

            # Update record
            record.final = True
            received = self.expect_set([(record_addr, record)])
            self.respond_set(received, [record_addr])

            self.expect_ok()
        except Exception:
            LOGGER.exception("test_record_finalize exception")
            raise
コード例 #21
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
コード例 #22
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
コード例 #23
0
ファイル: handler.py プロジェクト: feihujiang/sawtooth-core
    def _application_cancel(self, state, originator, data):
        txn_data = ApplicationCancelPayload()
        txn_data.ParseFromString(data)

        application_addr = Addressing.application_address(
            txn_data.record_identifier)

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

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

        # verify the txn signer is qualified to accept the application
        if application.applicant != originator:
            raise InvalidTransaction("Only Applicant can cancel Application.")

        # update the application
        application.status = Application.CANCELED

        # send back the updated application list
        self._set(state, [(application_addr, applications)])
コード例 #24
0
    def _application_cancel(self, state, originator, data):
        txn_data = ApplicationCancelPayload()
        txn_data.ParseFromString(data)

        application_addr = Addressing.application_address(
            txn_data.record_identifier)

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

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

        # verify the txn signer is qualified to accept the application
        if application.applicant != originator:
            raise InvalidTransaction("Only Applicant can cancel Application.")

        # update the application
        application.status = Application.CANCELED

        # send back the updated application list
        self._set(state, [(application_addr, applications)])
コード例 #25
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)
コード例 #26
0
ファイル: client.py プロジェクト: sthagen/sawtooth-core
 def record_finalize(self, record_identifier, wait=None):
     addrs = [Addressing.record_address(record_identifier)]
     return self._send_txn(
         SupplyChainPayload.RECORD_FINALIZE,
         RecordFinalizePayload(identifier=record_identifier),
         addrs,
         addrs,
         wait=wait)
コード例 #27
0
ファイル: handler.py プロジェクト: feihujiang/sawtooth-core
    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)])
コード例 #28
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)])
コード例 #29
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
コード例 #30
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)
コード例 #31
0
    def test_application_accept(self):
        """
        Test if the supplychain processor can create a application.
        """
        try:
            validator = self.validator
            factory = self.factory

            applicant_id = "applicant pub key"

            record_id = "serial number"
            record_addr = Addressing.record_address(record_id)
            agent_pub_key = factory.public_key
            application_addr = Addressing.application_address(record_id)
            timestamp = int(time.time())

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

            # test for application and record existance
            received = self.expect_get([application_addr, record_addr])
            application = factory.create_application(record_id, applicant_id,
                                                     Application.OWNER,
                                                     Application.OPEN)
            record = factory.create_record(record_id)
            factory.add_agent_record(record.owners, agent_pub_key)

            self.respond_get(received, [(application_addr, application),
                                        (record_addr, record)])

            # Update record and application
            application.status = Application.ACCEPTED
            factory.add_agent_record(record.owners, applicant_id, timestamp)
            received = self.expect_set([(application_addr, application),
                                        (record_addr, record)])
            self.respond_set(received, [application_addr, record_addr])
            self.expect_ok()
        except Exception:
            LOGGER.exception("test_application_accept exception")
            raise
コード例 #32
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)
コード例 #33
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)
コード例 #34
0
ファイル: client.py プロジェクト: feihujiang/sawtooth-core
 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
コード例 #35
0
ファイル: client.py プロジェクト: sthagen/sawtooth-core
 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
コード例 #36
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
コード例 #37
0
ファイル: client.py プロジェクト: feihujiang/sawtooth-core
    def record_get(self, public_key):
        address = Addressing.record_address(public_key)
        result = self._send_get("state/{}".format(address))

        try:
            data = self._get_result_data(result)
            records = _decode_record_container(data)
            return next((record for record in records.entries
                         if record.identifier == public_key), None)
        except BaseException:
            return None
コード例 #38
0
ファイル: client.py プロジェクト: sthagen/sawtooth-core
 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)
コード例 #39
0
ファイル: client.py プロジェクト: sthagen/sawtooth-core
    def record_get(self, public_key):
        address = Addressing.record_address(public_key)
        result = self._send_get("state/{}".format(address))

        try:
            data = self._get_result_data(result)
            records = _decode_record_container(data)
            return next((record for record in records.entries
                         if record.identifier == public_key), None)
        except BaseException:
            return None
コード例 #40
0
ファイル: client.py プロジェクト: sthagen/sawtooth-core
 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)
コード例 #41
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 {}
コード例 #42
0
ファイル: handler.py プロジェクト: feihujiang/sawtooth-core
 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 {}
コード例 #43
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
コード例 #44
0
ファイル: client.py プロジェクト: feihujiang/sawtooth-core
 def record_list(self):
     result = self._send_get("state?address={}".format(
         Addressing.record_namespace()))
     try:
         data = self._get_result_list(result)
         out = []
         for state_obj in data:
             container = _decode_record_container(
                 _b64_dec(state_obj['data']))
             out.extend(container.entries)
         return out
     except BaseException:
         return None
コード例 #45
0
ファイル: client.py プロジェクト: sthagen/sawtooth-core
 def record_list(self):
     result = self._send_get("state?address={}".format(
         Addressing.record_namespace()))
     try:
         data = self._get_result_list(result)
         out = []
         for state_obj in data:
             container = _decode_record_container(
                 _b64_dec(state_obj['data']))
             out.extend(container.entries)
         return out
     except BaseException:
         return None
コード例 #46
0
ファイル: client.py プロジェクト: sthagen/sawtooth-core
    def application_get(self, record_identifier, applicant, application_type):
        address = Addressing.application_address(record_identifier)
        result = self._send_get("state/{}".format(address))

        try:
            data = self._get_result_data(result)
            applications = _decode_application_container(data)
            return next(
                (app for app in applications.entries
                 if app.record_identifier == record_identifier and
                 app.applicant == applicant and app.type == application_type),
                None)
        except BaseException:
            return None
コード例 #47
0
ファイル: client.py プロジェクト: sthagen/sawtooth-core
 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)
コード例 #48
0
ファイル: client.py プロジェクト: feihujiang/sawtooth-core
    def application_get(self, record_identifier, applicant,
                        application_type):
        address = Addressing.application_address(record_identifier)
        result = self._send_get("state/{}".format(address))

        try:
            data = self._get_result_data(result)
            applications = _decode_application_container(data)
            return next((app for app in applications.entries
                         if app.record_identifier == record_identifier and
                         app.applicant == applicant and
                         app.type == application_type
                         ), None)
        except BaseException:
            return None
コード例 #49
0
ファイル: handler.py プロジェクト: suparnadhar/SuparnaGit
    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)])
コード例 #50
0
ファイル: handler.py プロジェクト: feihujiang/sawtooth-core
    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)])
コード例 #51
0
    def test_application_cancel(self):
        """
        Test if the supplychain processor can cancel an application.
        """
        try:
            validator = self.validator
            factory = self.factory

            record_id = "serial number"
            agent_pub_key = factory.public_key
            application_addr = Addressing.application_address(record_id)

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

            # test for application existance
            received = self.expect_get([application_addr])
            application = factory.create_application(
                record_id,
                agent_pub_key,
                Application.OWNER,
                Application.OPEN)
            self.respond_get(
                received,
                [(application_addr, application)])

            # Update application
            application.status = Application.CANCELED
            received = self.expect_set([(application_addr, application)])
            self.respond_set(received, [application_addr])

            self.expect_ok()
        except Exception:
            LOGGER.exception("test_application_cancel exception")
            raise
コード例 #52
0
ファイル: client.py プロジェクト: feihujiang/sawtooth-core
 def record_finalize(self, record_identifier, wait=None):
     addrs = [Addressing.record_address(record_identifier)]
     return self._send_txn(
         SupplyChainPayload.RECORD_FINALIZE,
         RecordFinalizePayload(identifier=record_identifier),
         addrs, addrs, wait=wait)
コード例 #53
0
ファイル: client.py プロジェクト: feihujiang/sawtooth-core
 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)