3

I want to implement a business logic that needs to know if the node of validators in waiting state are up or not. So for that according to me pallet_im_online was the best solution (but open to any other solutions). After doing some R&D, I found the function below referenced:

fn is_online_aux(authority_index: AuthIndex, authority: &ValidatorId<T>) -> bool {
    let current_session = T::ValidatorSet::session_index();

    ReceivedHeartbeats::<T>::contains_key(&current_session, &authority_index) ||
        AuthoredBlocks::<T>::get(&current_session, authority) != 0
}

From this I understand if an authority-node is authoring blocks than it is online, or if it does not get the chance to author block it will send a Heartbeat.

But all the above logic works for only the validators that are in active_set as I have mentioned in the question also I want this for validators in waiting_set.

What I have done till now:

  • I was able to get the validators in waiting_set and when I passed it fn is_online_aux, it was showing in offenders_list instead of giving a Heartbeat.
  • Which I find later on that it was getting the list of online validators from fn on_new_session<'a, I: 'a>(_changed: bool, validators: I, _queued_validators: I) and using it find if authority node is online or not.

Can someone tell me how to achieve it?

1 Answer 1

1

If you want to check whether a validator in the waiting set is online or not, you can modify the is_online_aux function to take into consideration the validators in the waiting set. Here's one way to do it:

fn is_online_aux(
    authority_index: AuthIndex,
    authority: &ValidatorId<T>,
) -> bool {
    let current_session = T::ValidatorSet::session_index();

    // Check if the authority is in the active set and has authored blocks
    if T::ValidatorSet::is_active_validator(authority_index)
        && AuthoredBlocks::<T>::get(&current_session, authority) != 0
    {
        return true;
    }

    // Check if the authority has sent a heartbeat in the current session
    if ReceivedHeartbeats::<T>::contains_key(&current_session, &authority_index) {
        return true;
    }

    // Check if the authority is in the waiting set and has sent a heartbeat in the previous session
    let previous_session = current_session.checked_sub(1).unwrap_or(0);
    if T::ValidatorSet::is_waiting_validator(authority_index)
        && ReceivedHeartbeats::<T>::contains_key(&previous_session, &authority_index)
    {
        return true;
    }

    false
}

In this modified version of the function, we first check if the authority is in the active set and has authored blocks. If so, we consider it online.

If not, we check if the authority has sent a heartbeat in the current session. If it has, we consider it online.

If the authority is neither in the active set nor has sent a heartbeat in the current session, we check if it is in the waiting set and has sent a heartbeat in the previous session. If it has, we consider it online.

If none of the above conditions are met, we consider the authority offline.

Note that the is_active_validator and is_waiting_validator functions are provided by the ValidatorSet trait, which is implemented by the runtime's pallet_session module. You'll need to make sure that your runtime includes this module and that you're using a version of pallet_im_online that is compatible with your version of pallet_session.

1
  • There is no direct way to get waiting validators from runtime, I need to derive it and its not that simple to check if waiting validators are online or not as you answered ,my first thought was also the same as you answered but there are many other factors involved but thanks for the reply. Apr 25, 2023 at 4:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.