-
Notifications
You must be signed in to change notification settings - Fork 45
feat(feature-activation): implement feature service #613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
glevco
merged 11 commits into
feat/feature-activation/signal-bits
from
feat/feature-activation/feature-service
May 30, 2023
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
720bc26
feat(feature-activation): implement feature service
glevco e45afce
feat(feature-activation): implement feature service
glevco ba437b1
feat(feature-activation): implement first feature service tests
glevco 38b6823
feat(feature-activation): implement missing bit count tests
glevco 6a4564d
feat(feature-activation): implement get_bits_description test
glevco 0f13ad0
feat(feature-activation): implement get_state tests
glevco cc28ad9
feat(feature-activation): lint code
glevco 32f7b58
feat(feature-activation): add missing test
glevco 15bc22d
feat(feature-activation): more missing tests
glevco ff07351
feat(feature-activation): implement PR suggestions
glevco 537e883
feat(feature-activation): implement get_height
glevco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| # Copyright 2023 Hathor Labs | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from hathor.feature_activation.feature import Feature | ||
| from hathor.feature_activation.model.criteria import Criteria | ||
| from hathor.feature_activation.model.feature_description import FeatureDescription | ||
| from hathor.feature_activation.model.feature_state import FeatureState | ||
| from hathor.feature_activation.settings import Settings | ||
| from hathor.transaction import Block | ||
|
|
||
|
|
||
| class FeatureService: | ||
| __slots__ = ('_settings',) | ||
|
|
||
| def __init__(self, *, settings: Settings) -> None: | ||
| self._settings = settings | ||
|
|
||
| def is_feature_active(self, *, block: Block, feature: Feature) -> bool: | ||
| """Returns whether a Feature is active at a certain block.""" | ||
| state = self.get_state(block=block, feature=feature) | ||
|
|
||
| return state == FeatureState.ACTIVE | ||
|
|
||
| def get_state(self, *, block: Block, feature: Feature) -> FeatureState: | ||
| """Returns the state of a feature at a certain block.""" | ||
|
|
||
| # per definition, the genesis block is in the DEFINED state for all features | ||
| if block.is_genesis: | ||
| return FeatureState.DEFINED | ||
|
|
||
| # All blocks within the same evaluation interval have the same state, that is, the state is only defined for | ||
| # the block in each interval boundary. Therefore, we get the state of the previous boundary. | ||
| height = block.get_height() | ||
| offset_to_boundary = height % self._settings.evaluation_interval | ||
| offset_to_previous_boundary = offset_to_boundary or self._settings.evaluation_interval | ||
| previous_boundary_height = height - offset_to_previous_boundary | ||
| previous_boundary_block = _get_ancestor_at_height(block=block, height=previous_boundary_height) | ||
| previous_state = self.get_state(block=previous_boundary_block, feature=feature) | ||
|
|
||
| if offset_to_boundary != 0: | ||
| return previous_state | ||
|
|
||
| return self._calculate_new_state( | ||
| boundary_block=block, | ||
| feature=feature, | ||
| previous_state=previous_state | ||
| ) | ||
|
|
||
| def _calculate_new_state( | ||
| self, | ||
| *, | ||
| boundary_block: Block, | ||
| feature: Feature, | ||
| previous_state: FeatureState | ||
| ) -> FeatureState: | ||
| """Returns the new feature state based on the new block, the criteria, and the previous state.""" | ||
| height = boundary_block.get_height() | ||
| assert height % self._settings.evaluation_interval == 0, ( | ||
| 'cannot calculate new state for a non-boundary block' | ||
| ) | ||
| criteria = self._get_criteria(feature=feature) | ||
|
|
||
| if previous_state is FeatureState.DEFINED: | ||
| if height >= criteria.start_height: | ||
| return FeatureState.STARTED | ||
|
|
||
| return FeatureState.DEFINED | ||
|
|
||
| if previous_state is FeatureState.STARTED: | ||
| if height >= criteria.timeout_height and not criteria.activate_on_timeout: | ||
| return FeatureState.FAILED | ||
|
|
||
| if ( | ||
| height >= criteria.timeout_height | ||
| and criteria.activate_on_timeout | ||
| and height >= criteria.minimum_activation_height | ||
| ): | ||
| return FeatureState.ACTIVE | ||
|
|
||
| count = self.get_bit_count(boundary_block=boundary_block, bit=criteria.bit) | ||
| threshold = criteria.threshold if criteria.threshold is not None else self._settings.default_threshold | ||
|
|
||
| if ( | ||
| height < criteria.timeout_height | ||
| and count >= threshold | ||
| and height >= criteria.minimum_activation_height | ||
| ): | ||
| return FeatureState.ACTIVE | ||
|
|
||
| return FeatureState.STARTED | ||
|
|
||
| if previous_state is FeatureState.ACTIVE: | ||
| return FeatureState.ACTIVE | ||
|
|
||
| if previous_state is FeatureState.FAILED: | ||
| return FeatureState.FAILED | ||
|
|
||
| raise ValueError(f'Unknown previous state: {previous_state}') | ||
|
|
||
| def _get_criteria(self, *, feature: Feature) -> Criteria: | ||
msbrogli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Get the Criteria defined for a specific Feature.""" | ||
| criteria = self._settings.features.get(feature) | ||
|
|
||
| if not criteria: | ||
| raise ValueError(f"Criteria not defined for feature '{feature}'.") | ||
|
|
||
| return criteria | ||
|
|
||
| def get_bits_description(self, *, block: Block) -> dict[Feature, FeatureDescription]: | ||
| """Returns the criteria definition and feature state for all features at a certain block.""" | ||
| return { | ||
| feature: FeatureDescription( | ||
| criteria=criteria, | ||
| state=self.get_state(block=block, feature=feature) | ||
| ) | ||
| for feature, criteria in self._settings.features.items() | ||
| } | ||
|
|
||
| def get_bit_count(self, *, boundary_block: Block, bit: int) -> int: | ||
| """Returns the count of blocks with this bit enabled in the previous evaluation interval.""" | ||
| assert not boundary_block.is_genesis, 'cannot calculate bit count for genesis' | ||
| assert boundary_block.get_height() % self._settings.evaluation_interval == 0, ( | ||
| 'cannot calculate bit count for a non-boundary block' | ||
| ) | ||
| count = 0 | ||
| block = boundary_block | ||
|
|
||
| # TODO: We can implement this as O(1) instead of O(evaluation_interval) | ||
| # by persisting the count in block metadata incrementally | ||
| for _ in range(self._settings.evaluation_interval): | ||
msbrogli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| block = block.get_block_parent() | ||
| feature_bits = block.get_feature_activation_bits() | ||
| bit_is_active = (feature_bits >> bit) & 1 | ||
|
|
||
| if bit_is_active: | ||
| count += 1 | ||
|
|
||
| return count | ||
|
|
||
|
|
||
| def _get_ancestor_at_height(*, block: Block, height: int) -> Block: | ||
| """Given a block, returns its ancestor at a specific height.""" | ||
| # TODO: there may be more optimized ways of doing this using the height index, | ||
| # but what if we're not in the best blockchain? | ||
| assert height < block.get_height(), ( | ||
| f"ancestor height must be lower than the block's height: {height} >= {block.get_height()}" | ||
| ) | ||
|
|
||
| ancestor = block | ||
| while ancestor.get_height() > height: | ||
| ancestor = ancestor.get_block_parent() | ||
|
|
||
| return ancestor | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Copyright 2023 Hathor Labs | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from typing import NamedTuple | ||
|
|
||
| from hathor.feature_activation.model.criteria import Criteria | ||
| from hathor.feature_activation.model.feature_state import FeatureState | ||
|
|
||
|
|
||
| class FeatureDescription(NamedTuple): | ||
| """Represents all information related to one feature, that is, its criteria and state.""" | ||
| criteria: Criteria | ||
| state: FeatureState |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.