Validator List Is Not Cleared On Add_validators – Causes Non-Deterministic Assignment And Test Failures
Validator List Is Not Cleared on add_validators – Causes Non-Deterministic Assignment and Test Failures
Problem
The current implementation of add_validators
in the system appends new validators to the existing validator list, rather than replacing or clearing the list. This leads to several issues that need to be addressed:
- Accumulation of duplicates and outdated addresses: The validator list accumulates duplicates and outdated addresses, which can lead to incorrect assignments and test failures.
- Biased random assignment: Random assignment of validators to pools is biased toward the original addresses, especially when the list is dominated by one address.
- Non-deterministic test failures: Tests that expect new validators to be used (such as
test_limited_validators_assignment
) can fail non-deterministically, as the random selection may not pick the newly added validator(s).
How to Reproduce
To reproduce this issue, follow these steps:
-
Add a single validator multiple times:
add_validators(['validator1_address']) add_validators(['validator1_address'])
-
Later, add a new validator (with or without duplicates):
add_validators(['validator2_address'])
-
Assign validators to a new pool and check the assigned addresses.
The new validator may not be assigned, causing assertions like:
assigned = assign_validators_to_pool(pool_id, num_validators=1) assert 'validator2_address' in assigned # AssertionError: 'validator2_address' not in ['validator1_address', ...]
Expected Behavior
The expected behavior is to have a way to clear or replace the validator list so that only the intended validators are available for assignment. Random assignment should select only from the current, unique set of validators.
Impact
The current implementation has the following impacts:
- Unreliable tests: Tests are unreliable and can fail randomly.
- Incorrect validator assignment: The validator assignment logic does not reflect the intended validator set.
Proposed Solution
To address this issue, the following solutions can be proposed:
- Implement a function to clear the validator list, e.g.:
clear_validators()
- Or modify
add_validators
to replace the existing list with the new set of validators (removing all previous entries). - Ensure uniqueness by storing only unique validator addresses internally.
Blocking
This issue is blocking the PR #143 to upgrade the protocol to 3 validators, as the current behavior prevents reliable testing and correct validator assignment.
Solution Implementation
To implement the proposed solution, the following steps can be taken:
- Create a new function to clear the validator list:
def clear_validators(): # Clear the validator list validators.clear()
- Modify
add_validators
to replace the existing list:def add_validators(validators): # Clear the existing list clear_validators() # Add the new validators for validator in validators: # Add the validator to the list validators.append(validator)
- Ensure uniqueness by storing only unique validator addresses ```python
def add_validators(validators):
# Clear the existing list
clear_validators()
# Add the new validators
for validator in validators:
# Check if the validator is already in the list
if validator not in validators:
# Add the validator to the list
validators.append(validator)
Testing
To ensure that the proposed solution works correctly, the following tests can be written:
- Test that the validator list is cleared:
def test_clear_validators(): # Add some validators to the list add_validators(['validator1_address', 'validator2_address']) # Clear the validator list clear_validators() # Check that the list is empty assert validators == []
- Test that the new validators are added correctly:
def test_add_validators(): # Add some validators to the list add_validators(['validator1_address', 'validator2_address']) # Add a new validator add_validators(['validator3_address']) # Check that the new validator is in the list assert 'validator3_address' in validators
- Test that the validator assignment logic works correctly:
def test_validator_assignment(): # Add some validators to the list add_validators(['validator1_address', 'validator2_address']) # Assign validators to a new pool assigned = assign_validators_to_pool(pool_id, num_validators=1) # Check that the assigned validator is correct assert 'validator2_address' in assigned
Conclusion
The current implementation of add_validators
appends new validators to the existing validator list, rather than replacing or clearing the list. This leads to several issues, including accumulation of duplicates and outdated addresses, biased random assignment, and non-deterministic test failures. To address this issue, a new function to clear the validator list can be implemented, or add_validators
can be modified to replace the existing list with the new set of validators. Additionally, uniqueness can be ensured by storing only unique validator addresses internally. The proposed solution can be tested using the provided test cases to ensure that it works correctly.
Validator List Is Not Cleared on add_validators – Causes Non-Deterministic Assignment and Test Failures: Q&A
Q: What is the current implementation of add_validators
?
A: The current implementation of add_validators
appends new validators to the existing validator list, rather than replacing or clearing the list.
Q: What are the issues caused by the current implementation of add_validators
?
A: The current implementation of add_validators
causes several issues, including:
- Accumulation of duplicates and outdated addresses
- Biased random assignment of validators to pools
- Non-deterministic test failures
Q: How can the issues caused by the current implementation of add_validators
be reproduced?
A: The issues can be reproduced by following these steps:
- Add a single validator multiple times
- Later, add a new validator (with or without duplicates)
- Assign validators to a new pool and check the assigned addresses
Q: What is the expected behavior of add_validators
?
A: The expected behavior of add_validators
is to have a way to clear or replace the validator list so that only the intended validators are available for assignment. Random assignment should select only from the current, unique set of validators.
Q: What are the impacts of the current implementation of add_validators
?
A: The current implementation of add_validators
has the following impacts:
- Unreliable tests
- Incorrect validator assignment logic
Q: What is the proposed solution to address the issues caused by the current implementation of add_validators
?
A: The proposed solution is to implement a function to clear the validator list, or modify add_validators
to replace the existing list with the new set of validators. Additionally, uniqueness can be ensured by storing only unique validator addresses internally.
Q: How can the proposed solution be implemented?
A: The proposed solution can be implemented by creating a new function to clear the validator list, modifying add_validators
to replace the existing list, and ensuring uniqueness by storing only unique validator addresses internally.
Q: What are the test cases that can be used to ensure that the proposed solution works correctly?
A: The following test cases can be used to ensure that the proposed solution works correctly:
- Test that the validator list is cleared
- Test that the new validators are added correctly
- Test that the validator assignment logic works correctly
Q: Why is this issue blocking the PR #143 to upgrade the protocol to 3 validators?
A: This issue is blocking the PR #143 to upgrade the protocol to 3 validators because the current behavior prevents reliable testing and correct validator assignment.
Q: What are the benefits of implementing the proposed solution?
A: The benefits of implementing the proposed solution are:
- Reliable tests
- Correct validator assignment logic
- Improved overall system performance
Q: How can the proposed solution be deployed in a production environment?
A: The proposed solution can be deployed in a production environment by following these steps:
- Implement the proposed solution
- Test the proposed solution thoroughly
- Deploy the proposed solution in a production environment
Q: What are the potential risks and challenges associated with implementing the proposed solution?
A: The potential risks and challenges associated with implementing the proposed solution are:
- Potential for unexpected behavior
- Potential for performance issues
- Potential for security vulnerabilities
Q: How can the potential risks and challenges associated with implementing the proposed solution be mitigated?
A: The potential risks and challenges associated with implementing the proposed solution can be mitigated by:
- Thoroughly testing the proposed solution
- Implementing robust error handling and logging
- Conducting regular security audits and vulnerability assessments