-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(feature-activation): implement get endpoint
- Loading branch information
Showing
7 changed files
with
269 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Empty file.
This file contains 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,144 @@ | ||
# 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 Optional | ||
|
||
from hathor.api_util import Resource, set_cors | ||
from hathor.cli.openapi_files.register import register_resource | ||
from hathor.feature_activation.feature import Feature | ||
from hathor.feature_activation.feature_service import FeatureService | ||
from hathor.feature_activation.model.feature_state import FeatureState | ||
from hathor.feature_activation.settings import Settings | ||
from hathor.transaction.storage import TransactionStorage | ||
from hathor.utils.api import Response | ||
|
||
|
||
@register_resource | ||
class FeatureResource(Resource): | ||
__slots__ = () | ||
|
||
isLeaf = True | ||
|
||
def __init__( | ||
self, | ||
*, | ||
settings: Settings, | ||
feature_service: FeatureService, | ||
tx_storage: TransactionStorage | ||
) -> None: | ||
super().__init__() | ||
self._settings = settings | ||
self._feature_service = feature_service | ||
self.tx_storage = tx_storage | ||
|
||
def render_GET(self, request): | ||
request.setHeader(b'content-type', b'application/json; charset=utf-8') | ||
set_cors(request, 'GET') | ||
|
||
best_block = self.tx_storage.get_best_block() | ||
bit_counts = best_block.get_feature_activation_bit_counts() | ||
features = [] | ||
|
||
for feature, criteria in self._settings.features.items(): | ||
state = self._feature_service.get_state(block=best_block, feature=feature) | ||
acceptance_count = bit_counts[criteria.bit] | ||
acceptance = ( | ||
acceptance_count / self._settings.evaluation_interval if state is FeatureState.STARTED else None | ||
) | ||
threshold_count = ( | ||
criteria.threshold if criteria.threshold is not None else self._settings.default_threshold | ||
) | ||
|
||
feature_response = GetFeatureResponse( | ||
name=feature, | ||
state=state.name, | ||
acceptance=acceptance, | ||
threshold=threshold_count / self._settings.evaluation_interval, | ||
start_height=criteria.start_height, | ||
minimum_activation_height=criteria.minimum_activation_height, | ||
timeout_height=criteria.timeout_height, | ||
activate_on_timeout=criteria.activate_on_timeout, | ||
version=criteria.version | ||
) | ||
|
||
features.append(feature_response) | ||
|
||
response = GetFeaturesResponse(features=features) | ||
|
||
return response.json_dumpb() | ||
|
||
|
||
class GetFeatureResponse(Response, use_enum_values=True): | ||
name: Feature | ||
state: str | ||
acceptance: Optional[float] | ||
threshold: float | ||
start_height: int | ||
minimum_activation_height: int | ||
timeout_height: int | ||
activate_on_timeout: bool | ||
version: str | ||
|
||
|
||
class GetFeaturesResponse(Response): | ||
features: list[GetFeatureResponse] | ||
|
||
|
||
FeatureResource.openapi = { | ||
'/feature': { | ||
'x-visibility': 'private', | ||
'get': { | ||
'operationId': 'feature', | ||
'summary': 'Feature Activation', | ||
'description': 'Returns information about features in the Feature Activation process', | ||
'responses': { | ||
'200': { | ||
'description': 'Success', | ||
'content': { | ||
'application/json': { | ||
'examples': { | ||
'success': { | ||
'features': [ | ||
{ | ||
'name': 'NOP_FEATURE_1', | ||
'state': 'ACTIVE', | ||
'acceptance': None, | ||
'threshold': 0.75, | ||
'start_height': 0, | ||
'minimum_activation_height': 0, | ||
'timeout_height': 100, | ||
'activate_on_timeout': False, | ||
'version': '0.1.0' | ||
}, | ||
{ | ||
'name': 'NOP_FEATURE_2', | ||
'state': 'STARTED', | ||
'acceptance': 0.25, | ||
'threshold': 0.5, | ||
'start_height': 200, | ||
'minimum_activation_height': 0, | ||
'timeout_height': 300, | ||
'activate_on_timeout': False, | ||
'version': '0.2.0' | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains 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
Empty file.
This file contains 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,103 @@ | ||
# 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 unittest.mock import Mock | ||
|
||
import pytest | ||
|
||
from hathor.feature_activation.feature import Feature | ||
from hathor.feature_activation.feature_service import FeatureService | ||
from hathor.feature_activation.model.criteria import Criteria | ||
from hathor.feature_activation.model.feature_state import FeatureState | ||
from hathor.feature_activation.resources.feature import FeatureResource | ||
from hathor.feature_activation.settings import Settings | ||
from hathor.transaction import Block | ||
from hathor.transaction.storage import TransactionStorage | ||
from tests.resources.base_resource import StubSite | ||
|
||
|
||
@pytest.fixture | ||
def web(): | ||
best_block = Mock(spec_set=Block) | ||
best_block.get_feature_activation_bit_counts = Mock(return_value=[0, 1, 0, 0]) | ||
|
||
tx_storage = Mock(spec_set=TransactionStorage) | ||
tx_storage.get_best_block = Mock(return_value=best_block) | ||
|
||
def get_state(*, block: Block, feature: Feature) -> FeatureState: | ||
return FeatureState.ACTIVE if feature is Feature.NOP_FEATURE_1 else FeatureState.STARTED | ||
|
||
feature_service = Mock(spec_set=FeatureService) | ||
feature_service.get_state = Mock(side_effect=get_state) | ||
|
||
settings = Settings( | ||
evaluation_interval=4, | ||
default_threshold=3, | ||
features={ | ||
Feature.NOP_FEATURE_1: Criteria( | ||
bit=0, | ||
start_height=0, | ||
timeout_height=100, | ||
version='0.1.0' | ||
), | ||
Feature.NOP_FEATURE_2: Criteria( | ||
bit=1, | ||
start_height=200, | ||
threshold=2, | ||
timeout_height=300, | ||
version='0.2.0' | ||
) | ||
} | ||
) | ||
|
||
feature_resource = FeatureResource( | ||
settings=settings, | ||
feature_service=feature_service, | ||
tx_storage=tx_storage | ||
) | ||
|
||
return StubSite(feature_resource) | ||
|
||
|
||
def test_get_features(web): | ||
response = web.get('feature') | ||
result = response.result.json_value() | ||
expected = dict( | ||
features=[ | ||
dict( | ||
name='NOP_FEATURE_1', | ||
state='ACTIVE', | ||
acceptance=None, | ||
threshold=0.75, | ||
start_height=0, | ||
minimum_activation_height=0, | ||
timeout_height=100, | ||
activate_on_timeout=False, | ||
version='0.1.0' | ||
), | ||
dict( | ||
name='NOP_FEATURE_2', | ||
state='STARTED', | ||
acceptance=0.25, | ||
threshold=0.5, | ||
start_height=200, | ||
minimum_activation_height=0, | ||
timeout_height=300, | ||
activate_on_timeout=False, | ||
version='0.2.0' | ||
) | ||
] | ||
) | ||
|
||
assert result == expected |