コード例 #1
0
ファイル: handler.py プロジェクト: suparnadhar/SuparnaGit
    def apply(self, transaction, state):
        txn_header = TransactionHeader()
        txn_header.ParseFromString(transaction.header)
        pubkey = txn_header.signer_pubkey

        val_reg_payload = ValidatorRegistryPayload()
        val_reg_payload.ParseFromString(transaction.payload)

        # Check name
        validator_name = val_reg_payload.name
        if len(validator_name) > 64:
            raise InvalidTransaction(
                'Illegal validator name {}'.format(validator_name))

        # Check registering validator matches transaction signer.
        validator_id = val_reg_payload.id
        if validator_id != pubkey:
            raise InvalidTransaction(
                'Signature mismatch on validator registration with validator'
                ' {} signed by {}'.format(validator_id, pubkey))

        public_key_hash = hashlib.sha256(pubkey.encode()).hexdigest()
        signup_info = val_reg_payload.signup_info

        try:
            self._verify_signup_info(
                signup_info=signup_info,
                originator_public_key_hash=public_key_hash,
                val_reg_payload=val_reg_payload,
                state=state)

        except ValueError as error:
            raise InvalidTransaction(
                'Invalid Signup Info: {0}, Reason: {1}'.format(
                    signup_info,
                    error))

        validator_info = ValidatorInfo(
            registered="registered",
            name=validator_name,
            id=validator_id,
            signup_info=val_reg_payload.signup_info,
            transaction_id=transaction.signature
        )

        _update_validator_state(state,
                                validator_id,
                                signup_info.anti_sybil_id,
                                validator_info.SerializeToString())
コード例 #2
0
    def test_invalid_report_key_pem(self):
        """
        Testing validator registry unable to succcessfully parse the report
        public key PEM from the config setting.
        """
        signup_info = self.factory.create_signup_info(
            self.factory.public_key_hash, "000")

        payload = ValidatorRegistryPayload(
            verb="reg", name="val_1", id=self.factory.public_key,
            signup_info=signup_info)

        # Send validator registry payload
        self.validator.send(
            self.factory.create_tp_process_request(payload.id, payload))

        # Expect Request for the address for report key PEM
        received = self.validator.expect(
            self.factory.create_get_request_report_key_pem())

        # Respond with empty report key PEM
        self.validator.respond(
            self.factory.create_get_response_report_key_pem(pem='invalid'),
            received)

        # Expect that the transaction will be rejected
        self._expect_invalid_transaction()
コード例 #3
0
    def test_invalid_poet_pubkey(self):
        """
        Test that a transaction without a poet_public_key returns an invalid
        transaction.
        """
        signup_info = self.factory.create_signup_info(
            self.factory.pubkey_hash, "000")

        signup_info.poet_public_key = "bad"

        payload = ValidatorRegistryPayload(
            verb="reg",
            name="val_1",
            id=self.factory.public_key,
            signup_info=signup_info)

        # Send validator registry payload
        self.validator.send(
            self.factory.create_tp_process_request(payload.id, payload))

        # Expect Request for the address for report key PEM
        received = self.validator.expect(
            self.factory.create_get_request_report_key_pem())

        # Respond with simulator report key PEM
        self.validator.respond(
            self.factory.create_get_response_simulator_report_key_pem(),
            received)

        self._expect_invalid_transaction()
コード例 #4
0
    def _test_bad_signup_info(self, signup_info):
        payload = ValidatorRegistryPayload(verb="reg",
                                           name="val_1",
                                           id=self.factory.public_key,
                                           signup_info=signup_info)

        # Send validator registry payload
        self.validator.send(
            self.factory.create_tp_process_request(payload.id, payload))
        self._expect_invalid_transaction()
コード例 #5
0
    def test_missing_enclave_basenames(self):
        """
        Testing validator registry unable to retrieve the valid enclave
        basenames from the config setting.
        """
        signup_info = self.factory.create_signup_info(
            self.factory.public_key_hash, "000")

        payload = ValidatorRegistryPayload(
            verb="reg", name="val_1", id=self.factory.public_key,
            signup_info=signup_info)

        # Send validator registry payload
        self.validator.send(
            self.factory.create_tp_process_request(payload.id, payload))

        # Expect Request for the address for report key PEM
        received = self.validator.expect(
            self.factory.create_get_request_report_key_pem())

        # Respond with the simulator report key PEM
        self.validator.respond(
            self.factory.create_get_response_simulator_report_key_pem(),
            received)

        # Expect Request for the address for valid enclave measurements
        received = self.validator.expect(
            self.factory.create_get_request_enclave_measurements())

        # Respond with simulator valid enclave measurements
        self.validator.respond(
            self.factory.create_get_response_simulator_enclave_measurements(),
            received)

        # Expect Request for the address for valid enclave basenames
        received = self.validator.expect(
            self.factory.create_get_request_enclave_basenames())

        # Respond with empty enclave basenames
        self.validator.respond(
            self.factory.create_get_response_enclave_basenames(),
            received)

        # Expect that the transaction will be rejected
        self._expect_invalid_transaction()
コード例 #6
0
    def test_invalid_id(self):
        """
        Test that a transaction with an id that does not match the
        signer_pubkey returns an invalid transaction.
        """
        signup_info = self.factory.create_signup_info(self.factory.pubkey_hash,
                                                      "000")

        # The idea should match the signer_pubkey in the transaction_header
        payload = ValidatorRegistryPayload(verb="reg",
                                           name="val_1",
                                           id="bad",
                                           signup_info=signup_info)

        # Send validator registry payload
        self.validator.send(
            self.factory.create_tp_process_request(payload.id, payload))

        self._expect_invalid_transaction()
コード例 #7
0
    def test_invalid_poet_pubkey(self):
        """
        Test that a transaction without a poet_public_key returns an invalid
        transaction.
        """
        signup_info = self.factory.create_signup_info(self.factory.pubkey_hash,
                                                      "000")

        signup_info.poet_public_key = "bad"

        payload = ValidatorRegistryPayload(verb="reg",
                                           name="val_1",
                                           id=self.factory.public_key,
                                           signup_info=signup_info)

        # Send validator registry payload
        self.validator.send(
            self.factory.create_tp_process_request(payload.id, payload))

        self._expect_invalid_transaction()
コード例 #8
0
    def _test_bad_signup_info(self, signup_info, expect_config=True):
        payload = ValidatorRegistryPayload(
            verb="reg",
            name="val_1",
            id=self.factory.public_key,
            signup_info=signup_info)

        # Send validator registry payload
        self.validator.send(
            self.factory.create_tp_process_request(payload.id, payload))

        if expect_config:
            # Expect Request for the address for report key PEM
            received = self.validator.expect(
                self.factory.create_get_request_report_key_pem())

            # Respond with simulator report key PEM
            self.validator.respond(
                self.factory.create_get_response_simulator_report_key_pem(),
                received)

            # Expect Request for the address for valid enclave measurements
            received = self.validator.expect(
                self.factory.create_get_request_enclave_measurements())

            # Respond with the simulator valid enclave measurements
            self.validator.respond(
                self.factory.
                create_get_response_simulator_enclave_measurements(),
                received)

            # Expect Request for the address for valid enclave basenames
            received = self.validator.expect(
                self.factory.create_get_request_enclave_basenames())

            # Respond with the simulator valid enclave basenames
            self.validator.respond(
                self.factory.create_get_response_simulator_enclave_basenames(),
                received)

        self._expect_invalid_transaction()
コード例 #9
0
    def test_invalid_name(self):
        """
        Test that a transaction with an invalid name returns an invalid
        transaction.
        """
        signup_info = self.factory.create_signup_info(
            self.factory.pubkey_hash, "000")

        # The name is longer the 64 characters
        payload = ValidatorRegistryPayload(
            verb="reg",
            name="val_11111111111111111111111111111111111111111111111111111111"
                 "11111",
            id=self.factory.public_key,
            signup_info=signup_info)

        # Send validator registry payload
        self.tester.send(
            self.factory.create_tp_process_request(payload.id, payload))

        self._expect_invalid_transaction()
コード例 #10
0
    def test_valid_signup_info(self):
        """
        Testing valid validator_registry transaction. This includes sending new
        signup info for a validator that has already been registered.
        """
        signup_info = self.factory.create_signup_info(
            self.factory.pubkey_hash, "000")

        payload = ValidatorRegistryPayload(
            verb="reg", name="val_1", id=self.factory.public_key,
            signup_info=signup_info)
        # Send validator registry payload
        self.tester.send(
            self.factory.create_tp_process_request(payload.id, payload))

        # Expect Request for the ValidatorMap
        received = self.tester.expect(
            self.factory.create_get_request_validator_map())

        # Respond with a empty validator Map
        self.tester.respond(
            self.factory.create_get_empty_response_validator_map(), received)

        # Expect a set the new validator to the ValidatorMap
        received = self.tester.expect(
            self.factory.create_set_request_validator_map())

        # Respond with the ValidatorMap address
        self.tester.respond(self.factory.create_set_response_validator_map(),
                            received)

        # Expect a request to set ValidatorInfo for val_1
        received = self.tester.expect(
            self.factory.create_set_request_validator_info("val_1",
                                                           "registered"))

        # Respond with address for val_1
        # val_1 address is derived from the validators id
        # val id is the same as the pubkey for the factory
        self.tester.respond(self.factory.create_set_response_validator_info(),
                            received)

        self._expect_ok()
        # --------------------------
        signup_info = self.factory.create_signup_info(
            self.factory.pubkey_hash, "000")

        payload = ValidatorRegistryPayload(
            verb="reg", name="val_1", id=self.factory.public_key,
            signup_info=signup_info)

        # Send validator registry payload
        self.tester.send(
            self.factory.create_tp_process_request(payload.id, payload))

        # Expect Request for the ValidatorMap
        received = self.tester.expect(
            self.factory.create_get_request_validator_map())

        # Respond with a validator Map
        self.tester.respond(self.factory.create_get_response_validator_map(),
                            received)
        # Expect to receive a validator_info request
        received = self.tester.expect(
            self.factory.create_get_request_validator_info())

        # Respond with the ValidatorInfo
        self.tester.respond(
            self.factory.create_get_response_validator_info("val_1"), received)

        # Expect a request to set ValidatorInfo for val_1
        received = self.tester.expect(
            self.factory.create_set_request_validator_info("val_1", "revoked"))

        # Respond with address for val_1
        # val_1 address is derived from the validators id
        # val id is the same as the pubkey for the factory
        self.tester.respond(
            self.factory.create_set_response_validator_info(), received)

        # Expect a request to set ValidatorInfo for val_1
        received = self.tester.expect(
            self.factory.create_set_request_validator_info("val_1",
                                                           "registered"))

        # Respond with address for val_1
        # val_1 address is derived from the validators id
        # val id is the same as the pubkey for the factory
        self.tester.respond(self.factory.create_set_response_validator_info(),
                            received)

        self._expect_ok()
コード例 #11
0
    def _test_valid_signup_info(self, signup_info):
        """
        Testing valid validator_registry transaction.
        """
        payload = ValidatorRegistryPayload(
            verb="reg", name="val_1", id=self.factory.public_key,
            signup_info=signup_info)
        # Send validator registry payload
        transaction_message =\
            self.factory.create_tp_process_request(payload.id, payload)
        transaction_id = transaction_message.signature
        self.validator.send(
            transaction_message)

        # Expect Request for the address for report key PEM
        received = self.validator.expect(
            self.factory.create_get_request_report_key_pem())

        # Respond with simulator report key PEM
        self.validator.respond(
            self.factory.create_get_response_simulator_report_key_pem(),
            received)

        # Expect Request for the address for valid enclave measurements
        received = self.validator.expect(
            self.factory.create_get_request_enclave_measurements())

        # Respond with the simulator valid enclave measurements
        self.validator.respond(
            self.factory.create_get_response_simulator_enclave_measurements(),
            received)

        # Expect Request for the address for valid enclave basenames
        received = self.validator.expect(
            self.factory.create_get_request_enclave_basenames())

        # Respond with the simulator valid enclave basenames
        self.validator.respond(
            self.factory.create_get_response_simulator_enclave_basenames(),
            received)

        # Expect Request for the ValidatorMap
        received = self.validator.expect(
            self.factory.create_get_request_validator_map())

        # Respond with a empty validator Map
        self.validator.respond(
            self.factory.create_get_empty_response_validator_map(), received)

        # Expect a set the new validator to the ValidatorMap
        received = self.validator.expect(
            self.factory.create_set_request_validator_map())

        # Respond with the ValidatorMap address
        self.validator.respond(
            self.factory.create_set_response_validator_map(),
            received)

        # Expect a request to set ValidatorInfo for val_1
        received = self.validator.expect(
            self.factory.create_set_request_validator_info(
                "val_1", transaction_id, signup_info))

        # Respond with address for val_1
        # val_1 address is derived from the validators id
        # val id is the same as the public_key for the factory
        self.validator.respond(
            self.factory.create_set_response_validator_info(),
            received)

        self._expect_ok()
コード例 #12
0
    def test_valid_signup_info(self):
        """
        Testing valid validator_registry transaction. This includes sending new
        signup info for a validator that has already been registered.
        """
        signup_info = self.factory.create_signup_info(
            self.factory.pubkey_hash, "000")

        payload = ValidatorRegistryPayload(
            verb="reg", name="val_1", id=self.factory.public_key,
            signup_info=signup_info)
        # Send validator registry payload
        self.validator.send(
            self.factory.create_tp_process_request(payload.id, payload))

        # Expect Request for the address for report key PEM
        received = self.validator.expect(
            self.factory.create_get_request_report_key_pem())

        # Respond with simulator report key PEM
        self.validator.respond(
            self.factory.create_get_response_simulator_report_key_pem(),
            received)

        # Expect Request for the address for valid enclave measurements
        received = self.validator.expect(
            self.factory.create_get_request_enclave_measurements())

        # Respond with the simulator valid enclave measurements
        self.validator.respond(
            self.factory.create_get_response_simulator_enclave_measurements(),
            received)

        # Expect Request for the address for valid enclave basenames
        received = self.validator.expect(
            self.factory.create_get_request_enclave_basenames())

        # Respond with the simulator valid enclave basenames
        self.validator.respond(
            self.factory.create_get_response_simulator_enclave_basenames(),
            received)

        # Expect Request for the ValidatorMap
        received = self.validator.expect(
            self.factory.create_get_request_validator_map())

        # Respond with a empty validator Map
        self.validator.respond(
            self.factory.create_get_empty_response_validator_map(), received)

        # Expect a set the new validator to the ValidatorMap
        received = self.validator.expect(
            self.factory.create_set_request_validator_map())

        # Respond with the ValidatorMap address
        self.validator.respond(
            self.factory.create_set_response_validator_map(),
            received)

        # Expect a request to set ValidatorInfo for val_1
        received = self.validator.expect(
            self.factory.create_set_request_validator_info("val_1"))

        # Respond with address for val_1
        # val_1 address is derived from the validators id
        # val id is the same as the pubkey for the factory
        self.validator.respond(
            self.factory.create_set_response_validator_info(),
            received)

        self._expect_ok()
        # --------------------------
        signup_info = self.factory.create_signup_info(
            self.factory.pubkey_hash, "000")

        payload = ValidatorRegistryPayload(
            verb="reg", name="val_1", id=self.factory.public_key,
            signup_info=signup_info)

        # Send validator registry payload
        self.validator.send(
            self.factory.create_tp_process_request(payload.id, payload))

        # Expect Request for the address for report key PEM
        received = self.validator.expect(
            self.factory.create_get_request_report_key_pem())

        # Respond with simulator report key PEM
        self.validator.respond(
            self.factory.create_get_response_simulator_report_key_pem(),
            received)

        # Expect Request for the address for valid enclave measurements
        received = self.validator.expect(
            self.factory.create_get_request_enclave_measurements())

        # Respond with the simulator valid enclave measurements
        self.validator.respond(
            self.factory.create_get_response_simulator_enclave_measurements(),
            received)

        # Expect Request for the address for valid enclave basenames
        received = self.validator.expect(
            self.factory.create_get_request_enclave_basenames())

        # Respond with the simulator valid enclave basenames
        self.validator.respond(
            self.factory.create_get_response_simulator_enclave_basenames(),
            received)

        # Expect Request for the ValidatorMap
        received = self.validator.expect(
            self.factory.create_get_request_validator_map())

        # Respond with a validator Map
        self.validator.respond(
            self.factory.create_get_response_validator_map(),
            received)

        # Clear the validator info for the address
        # Expect del validator info
        received = self.validator.expect(
            self.factory.create_del_request_validator_info())

        # Respond with an empty data message
        self.validator.respond(
            self.factory.create_del_response_validator_info(), received)

        # Set the Map
        # Expect set request ValidatorMap
        received = self.validator.expect(
            self.factory.create_set_request_validator_map())

        # Respond with the ValidatorMap address
        self.validator.respond(
            self.factory.create_set_response_validator_map(),
            received)

        # Set the validator address
        # Expect a request to set ValidatorInfo for val_1
        received = self.validator.expect(
            self.factory.create_set_request_validator_info("val_1"))

        # Respond with address for val_1
        # val_1 address is derived from the validators id
        # val id is the same as the pubkey for the factory
        self.validator.respond(
            self.factory.create_set_response_validator_info(),
            received)

        self._expect_ok()