Exemple #1
0
    def monitor_direct(self) -> None:
        # Check if node is accessible
        self._logger.debug('Checking if %s is alive', self._node)
        self._data_wrapper.ping_node(self._node.ws_url)
        self._node.set_as_up(self.channels, self.logger)

        # Get system_health
        system_health = self.data_wrapper.get_system_health(self._node.ws_url)

        # Get finalized block header
        finalized_head = self.data_wrapper.get_finalized_head(self.node.ws_url)
        finalized_block_header = self.data_wrapper.get_header(
            self.node.ws_url, finalized_head)

        # Set is-syncing
        is_syncing = system_health['isSyncing']
        self._logger.debug('%s is syncing: %s', self._node, is_syncing)
        self._node.set_is_syncing(is_syncing, self.channels, self.logger)

        # Set number of peers
        no_of_peers = system_health['peers']
        self._logger.debug('%s no. of peers: %s', self._node, no_of_peers)
        self._node.set_no_of_peers(no_of_peers, self.channels, self.logger)

        # Update finalized block
        finalized_block_height = parse_int_from_string(
            str(finalized_block_header['number']))
        self._logger.debug('%s finalized_block_height: %s', self._node,
                           finalized_block_height)
        self._node.update_finalized_block_height(finalized_block_height,
                                                 self.logger, self.channels)

        # Set API as up
        self.data_wrapper.set_api_as_up(self.monitor_name, self.channels)
Exemple #2
0
    def monitor(self) -> None:
        # Get new data.
        new_referendum_count = parse_int_from_string(
            str(
                self._data_wrapper.get_referendum_count(
                    self.data_source.ws_url)))
        new_council_prop_count = parse_int_from_string(
            str(
                self._data_wrapper.get_council_proposal_count(
                    self.data_source.ws_url)))
        new_public_prop_count = parse_int_from_string(
            str(
                self._data_wrapper.get_public_proposal_count(
                    self.data_source.ws_url)))
        session_validators = self._data_wrapper.get_session_validators(
            self.data_source.ws_url)
        new_validator_set_size = len(session_validators)

        # Check for referendums
        self._logger.debug('%s referendum_count: %s', self.blockchain,
                           new_referendum_count)
        self._check_for_new_referendums(new_referendum_count)

        # Set council prop count
        self._logger.debug('%s council_prop_count: %s', self.blockchain,
                           new_council_prop_count)
        self.blockchain.set_council_prop_count(new_council_prop_count,
                                               self.channels, self.logger)

        # Set public prop count
        self._logger.debug('%s public_prop_count: %s', self.blockchain,
                           new_public_prop_count)
        self.blockchain.set_public_prop_count(new_public_prop_count,
                                              self.channels, self.logger)

        # Set validator set size
        self._logger.debug('%s validator_set_size: %s', self.blockchain,
                           new_validator_set_size)
        self.blockchain.set_validator_set_size(new_validator_set_size,
                                               self.channels, self.logger)

        # Set API as up and declare the used node as connected with the API
        self.data_wrapper.set_api_as_up(self.monitor_name, self.channels)
        self.last_data_source_used.connect_with_api(self.channels, self.logger)

        self.logger.info('%s status: %s', self._monitor_name, self.status())
Exemple #3
0
    def set_referendum_count(self, new_referendum_count: int,
                             channels: ChannelSet, logger: logging.Logger,
                             referendum_info: PolkadotWrapperType = None) \
            -> None:
        logger.debug('%s set_referendum_count: referendum_count(currently)=%s, '
                     'channels=%s', self, self.referendum_count, channels)

        # If referendum is new and still ongoing, alert the operator.
        if self._referendum_count not in [None, new_referendum_count] \
                and referendum_info is not None:
            if 'Ongoing' in referendum_info:
                end_block = parse_int_from_string(str(
                    referendum_info['Ongoing']['end']
                ))
                channels.alert_info(NewReferendumAlert(
                    self.referendum_count, end_block)
                )
        self._referendum_count = new_referendum_count
Exemple #4
0
    def monitor_direct(self) -> None:

        # Check if node is accessible
        self._logger.debug('Checking if %s is alive', self._node)
        self._data_wrapper.ping_node(self._node.api_url, self._node.name)
        self._node.set_as_up(self.channels, self.logger)

        # Get isSyncing Status
        is_syncing = not self.data_wrapper.get_is_syncing(
            self._node.api_url, self._node.name)
        self._logger.debug('%s is syncing: %s', self._node, is_syncing)
        self._node.set_is_syncing(is_syncing, self.channels, self.logger)

        # Set number of peers
        no_of_peers = self.data_wrapper.get_prometheus_gauge(
            self._node.api_url, self._node.name, "tendermint_p2p_peers")
        self._logger.debug('%s no. of peers: %s', self._node, no_of_peers)
        self._node.set_no_of_peers(int(float(no_of_peers)), self.channels,
                                   self.logger)

        # Update finalized block height
        finalized_block_header = self.data_wrapper.get_block_header(
            self._node.api_url, self._node.name)

        # Update finalized block
        finalized_block_height = parse_int_from_string(
            str(finalized_block_header['height']))

        self._logger.debug('%s finalized_block_height: %s', self._node,
                           finalized_block_height)

        self._node.update_finalized_block_height(finalized_block_height,
                                                 self.logger, self.channels)

        # Set API as up, and declare that node was connected to the API
        self.data_wrapper.set_api_as_up(self.monitor_name, self.channels)
        self.node.connect_with_api(self.channels, self.logger)
Exemple #5
0
    def _monitor_indirect_validator(self) -> None:
        session_validators = self.data_wrapper.get_session_validators(
            self.data_source_indirect.ws_url)
        stakers_json = self.data_wrapper.get_stakers(
            self.data_source_indirect.ws_url, self._node.stash_account_address)
        council_members = self.data_wrapper.get_council_members(
            self.data_source_indirect.ws_url)
        elected_validators = self.data_wrapper.get_current_elected(
            self.data_source_indirect.ws_url)
        new_session_index = self.data_wrapper.get_current_index(
            self.data_source_indirect.ws_url)
        new_number_of_blocks_authored = self.data_wrapper. \
            get_authored_blocks(self.data_source_indirect.ws_url,
                                new_session_index,
                                self.node.stash_account_address)
        disabled_validators = self.data_wrapper.get_disabled_validators(
            self.data_source_indirect.ws_url)

        # Set active
        is_active = self._node.stash_account_address in session_validators
        self._logger.debug('%s active: %s', self._node, is_active)
        self.node.set_active(is_active, self.channels, self.logger)

        # Set auth_index
        if self._node.is_active:
            new_auth_index = session_validators.index(
                self.node.stash_account_address)
            self._logger.debug('%s auth_index: %s', self._node, new_auth_index)
            self._node.set_auth_index(new_auth_index, self.logger)

        # Set disabled
        is_disabled = self.node.auth_index in disabled_validators
        self._logger.debug('%s disabled: %s', self._node, is_disabled)
        self.node.set_disabled(is_disabled, new_session_index, self.channels,
                               self.logger)

        # Set elected
        is_elected = self._node.stash_account_address in elected_validators
        self._logger.debug('%s elected: %s', self._node, is_elected)
        self.node.set_elected(is_elected, self.channels, self.logger)

        # Set bonded_balance
        bonded_balance = parse_int_from_string(str(stakers_json['total']))
        self._logger.debug('%s bonded_balance: %s', self._node, bonded_balance)
        self._node.set_bonded_balance(bonded_balance, self.channels,
                                      self.logger)

        # Set council_member
        is_council_member = self._node.stash_account_address in council_members
        self._logger.debug('%s is council member: %s', self._node,
                           is_council_member)
        self.node.set_council_member(is_council_member, self.channels,
                                     self.logger)

        # Set session index
        self._check_for_new_session(new_session_index)

        # Set number of blocks authored
        self._logger.debug('%s number_of_blocks_authored: %s', self._node,
                           new_number_of_blocks_authored)
        self._node.set_no_of_blocks_authored(self.channels, self.logger,
                                             new_number_of_blocks_authored,
                                             self._session_index)

        if not self._archive_alerts_disabled:
            self._monitor_archive_state()
Exemple #6
0
 def test_parse_int_from_str_returns_as_expected_when_given_hex_val_non_0(
         self) -> None:
     self.assertEqual(15, parse_int_from_string("0xF"))
Exemple #7
0
 def test_parse_int_from_str_returns_as_expected_when_given_val_0(
         self) -> None:
     self.assertEqual(0, parse_int_from_string("0"))
Exemple #8
0
    def _monitor_indirect_validator(self) -> None:

        # Get dictionary of validators at current block height
        session_validators = self.data_wrapper.get_session_validators(
            self.data_source_indirect.api_url, self.data_source_indirect.name)

        # Attempt to return validator from list of session_validators
        validator_data = list(
            filter(
                lambda validator: validator['id'] == self._node.
                node_public_key, session_validators))

        is_active = False if len(validator_data) == 0 else True
        self._logger.debug('%s active: %s', self._node, is_active)
        self.node.set_active(is_active, self.channels, self.logger)

        voting_power = 0 if is_active == False \
            else validator_data[0]['voting_power']

        self._logger.debug('%s voting power: %s', self.node, voting_power)
        self.node.set_voting_power(int(voting_power), self.channels,
                                   self.logger)

        # Get node status and from that the last height to be checked
        latestblock = self.data_wrapper.get_consensus_block(
            self.data_source_indirect.api_url, self.data_source_indirect.name)

        # The Precommits(Signatures) are of the block before
        last_height_to_check = int(latestblock['height'])

        # If the chain has not started, return as there are no blocks to get
        if last_height_to_check == 0:
            return

        # If this is the first height being checked, ignore previous heights
        if self._last_height_checked == NONE:
            self._last_height_checked = last_height_to_check - 1

        # Consider any height that is after the previous last height
        height = self._last_height_checked
        if last_height_to_check - self._last_height_checked > \
                self._node_monitor_max_catch_up_blocks:
            height = last_height_to_check - \
                     self._node_monitor_max_catch_up_blocks
            self._check_block(height)
            self._check_events(height, None)
            self._last_height_checked = height
        elif height <= last_height_to_check:
            self._check_block(height)
            self._check_events(height, None)
            self._last_height_checked = height

        if last_height_to_check - self._last_height_checked > 2:
            self._monitor_is_catching_up = True
        else:
            self._monitor_is_catching_up = False

        # Retrieve the bonding balance and set it
        staking_account = self.data_wrapper.get_staking_account(
            self.data_source_indirect.api_url, self.data_source_indirect.name,
            self._node.staking_address)

        bonded_balance = parse_int_from_string(
            str(staking_account['escrow']['active']['balance']))
        self._logger.debug('%s bonded_balance: %s', self._node, bonded_balance)
        self._node.set_bonded_balance(bonded_balance, self.channels,
                                      self.logger)

        debonding_balance = parse_int_from_string(
            str(staking_account['escrow']['debonding']['balance']))
        self._logger.debug('%s debonding_balance: %s', self._node,
                           debonding_balance)
        self._node.set_debonding_balance(debonding_balance, self.channels,
                                         self.logger)

        # Staking Delegations for self Entity ID
        staking_delegations = self.data_wrapper.get_staking_delegations(
            self.data_source_indirect.api_url, self.data_source_indirect.name,
            self._node.staking_address)

        shares = 0
        for i, j in enumerate(staking_delegations):
            shares += int(staking_delegations[j]['shares'])

        # Set shares balance
        self._logger.debug('%s shares balance: %s', self._node, shares)
        self._node.set_shares_balance(shares, self.channels, self.logger)

        if not self._archive_alerts_disabled:
            self._monitor_archive_state()

        self._last_height_checked += 1