Example #1
0
def sync_from_primary_validators_initial_block(*, primary_validator):
    """
    Sync from the primary validators initial block (seed_block_identifier or root_account_file_hash)

    Invoked when self (confirmation validator):
    - is first being initialized
    - has a blockchain that is out of sync with the PV
    """

    try:
        self_configuration = get_self_configuration(exception_class=RuntimeError)
        download_root_account_file(url=primary_validator.root_account_file)

        self_configuration.root_account_file = get_root_account_file_url()
        self_configuration.root_account_file_hash = get_file_hash(settings.ROOT_ACCOUNT_FILE_PATH)
        self_configuration.seed_block_identifier = primary_validator.seed_block_identifier
        self_configuration.save()

        sync_accounts_table_to_root_account_file()
    except Exception as e:
        logger.exception(e)
        raise RuntimeError(e)

    rebuild_cache(head_block_hash=get_initial_block_identifier())
    send_confirmation_block_history_request()
Example #2
0
def sync_from_primary_validators_initial_block(*, primary_validator):
    """
    Sync from the primary validators initial block (seed_block_identifier or root_account_file_hash)

    Invoked when self (confirmation validator):
    - is first being initialized
    - has a blockchain that is out of sync with the PV
    """

    try:
        download_root_account_file(
            url=primary_validator.root_account_file,
            destination_file_path=settings.LOCAL_ROOT_ACCOUNT_FILE_PATH)
        file_hash = get_file_hash(settings.LOCAL_ROOT_ACCOUNT_FILE_PATH)

        # TODO: root_account_file should not be a copy of PVs URL but rather a unique path
        # TODO: this way, every validator maintains their own copy
        self_configuration = get_self_configuration(
            exception_class=RuntimeError)
        self_configuration.root_account_file = primary_validator.root_account_file
        self_configuration.root_account_file_hash = file_hash
        self_configuration.seed_block_identifier = primary_validator.seed_block_identifier
        self_configuration.save()

        sync_accounts_table_to_root_account_file()
    except Exception as e:
        logger.exception(e)
        raise RuntimeError(e)

    rebuild_cache(head_block_hash=get_initial_block_identifier())
    send_confirmation_block_history_request()
    def get_root_account_file(self, value=None):
        """
        Get root account file from user
        """

        valid = False

        while not valid:
            if self.unattended:
                root_account_file = value
            else:
                root_account_file = input(
                    'Enter root account file URL (required): ')

            if not root_account_file:
                self._error('root_account_file required')
                continue

            try:
                url_validator = URLValidator(schemes=['http', 'https'])
                url_validator(root_account_file)
            except ValidationError:
                self._error('Invalid URL')
                continue

            if Path(root_account_file).suffix != '.json':
                self._error('JSON file required')
                continue

            try:
                download_root_account_file(url=root_account_file)
            except Exception as e:
                capture_exception(e)
                logger.exception(e)
                self.stdout.write(
                    self.style.ERROR(f'Error downloading {root_account_file}'))
                self.stdout.write(self.style.ERROR(e))
                continue

            file_hash = get_file_hash(settings.ROOT_ACCOUNT_FILE_PATH)

            if not self.required_input['head_block_hash']:
                self.required_input['head_block_hash'] = file_hash

            self_address = format_address(
                ip_address=self.required_input.get('ip_address'),
                port=self.required_input.get('port'),
                protocol=self.required_input.get('protocol'),
            )
            root_account_file = get_root_account_file_url(address=self_address)

            self.required_input.update({
                'root_account_file': root_account_file,
                'root_account_file_hash': file_hash
            })
            valid = True
Example #4
0
    def get_root_account_file(self):
        """
        Get root account file from user
        """

        valid = False

        while not valid:
            root_account_file = input(
                'Enter root account file URL (required): ')

            if not root_account_file:
                self._error('root_account_file required')
                continue

            try:
                url_validator = URLValidator(schemes=['http', 'https'])
                url_validator(root_account_file)
            except ValidationError:
                self._error('Invalid URL')
                continue

            if Path(root_account_file).suffix != '.json':
                self._error('JSON file required')
                continue

            try:
                download_root_account_file(url=root_account_file,
                                           destination_file_path=settings.
                                           LOCAL_ROOT_ACCOUNT_FILE_PATH)
            except Exception as e:
                logger.exception(e)
                self.stdout.write(
                    self.style.ERROR(f'Error downloading {root_account_file}'))
                self.stdout.write(self.style.ERROR(e))

            file_hash = get_file_hash(settings.LOCAL_ROOT_ACCOUNT_FILE_PATH)

            if not self.required_input['head_block_hash']:
                self.required_input['head_block_hash'] = file_hash

            self.required_input.update({
                'root_account_file': root_account_file,
                'root_account_file_hash': file_hash
            })
            valid = True