Skip to content

Add a getter for all active validationIDs #738

@cam-schultz

Description

@cam-schultz

Context and scope
Validator information (including validationIDs) are stored in a mapping in the validator manager contract's state. Mappings are not iterable, so there's no way to fetch a value from this mapping without already knowing the validationID. This is poor UX for any methods that use the validationID, since it requires that the caller know the validationID a priori through some means other than the validator manager contract.

A getter of the form getActiveValidators() public view returns ([]bytes32) would improve this UX.

Discussion and alternatives
One way to implement this is to define two new state variables:

  1. An array bytes32 validationIDs
  2. A mapping of bytes32 => (int index, bool status),validationIDIdx where the key is the validationID, and the value is a tuple of the validationID's index in (1), and it's status (true if active, false otherwise)

When a new validator is registered, it's validationID is appended to validationIDs, and validationIDIdx[validationID] is set to (len(validationIDs)-1, true)

When a validator is removed, validationIDIdx[validationID] is set to (validationIDIdx[validationID].first, false), acting as a tombstone.

Finally, when getActiveValidators is called, we iterate over validationIDs, populating the return array with elements whose corresponding status in validationIDIdx is true.

Gas cost
Validator information is stored in a mapping rather than an array in order to prevent common operations such as initiateValidatorRegistration from performing an O(n) iteration over the entire validator set. The approach described above would move that expensive operation to a view method that is not called by any core validator management functions.

Open questions

  • This same principle can be applied to fetch active delegators from StakingManager.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    Status

    Backlog 🧊

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions