-
Notifications
You must be signed in to change notification settings - Fork 26
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
refactor(verification): externalize verification dependencies [part 1/2] #859
Closed
glevco
wants to merge
1
commit into
refactor/feature-activation/enable-usage
from
refactor/verification-dependencies
Closed
Changes from all commits
Commits
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 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
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
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
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
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,88 @@ | ||
# 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 dataclasses import dataclass | ||
|
||
from typing_extensions import Self | ||
|
||
from hathor.daa import DifficultyAdjustmentAlgorithm | ||
from hathor.feature_activation.feature import Feature | ||
from hathor.feature_activation.feature_service import BlockSignalingState, FeatureService | ||
from hathor.feature_activation.model.feature_description import FeatureInfo | ||
from hathor.transaction import Block | ||
from hathor.transaction.storage.simple_memory_storage import SimpleMemoryStorage | ||
from hathor.transaction.transaction import Transaction | ||
|
||
|
||
@dataclass(frozen=True, slots=True) | ||
class VertexDependencies: | ||
"""A dataclass of dependencies necessary for vertex verification.""" | ||
storage: SimpleMemoryStorage | ||
|
||
|
||
@dataclass(frozen=True, slots=True) | ||
class BasicBlockDependencies(VertexDependencies): | ||
"""A dataclass of dependencies necessary for basic block verification.""" | ||
|
||
@classmethod | ||
def create(cls, block: Block, daa: DifficultyAdjustmentAlgorithm, *, skip_weight_verification: bool) -> Self: | ||
"""Create a basic block dependencies instance.""" | ||
assert block.storage is not None | ||
simple_storage = SimpleMemoryStorage() | ||
daa_deps = [] if skip_weight_verification else daa.get_block_dependencies(block) | ||
deps = block.parents + daa_deps | ||
|
||
simple_storage.add_vertices_from_storage(block.storage, deps) | ||
|
||
return cls(simple_storage) | ||
|
||
|
||
@dataclass(frozen=True, slots=True) | ||
class BlockDependencies(VertexDependencies): | ||
"""A dataclass of dependencies necessary for block verification.""" | ||
signaling_state: BlockSignalingState | ||
feature_info: dict[Feature, FeatureInfo] | ||
|
||
@classmethod | ||
def create(cls, block: Block, feature_service: FeatureService) -> Self: | ||
"""Create a block dependencies instance.""" | ||
assert block.storage is not None | ||
signaling_state = feature_service.is_signaling_mandatory_features(block) | ||
feature_info = feature_service.get_feature_info(block=block) | ||
simple_storage = SimpleMemoryStorage() | ||
|
||
simple_storage.add_vertices_from_storage(block.storage, block.parents) | ||
simple_storage.add_vertex(block) # we add the block itself so its metadata can be used as a dependency. | ||
|
||
return cls( | ||
storage=simple_storage, | ||
signaling_state=signaling_state, | ||
feature_info=feature_info, | ||
) | ||
|
||
|
||
class TransactionDependencies(VertexDependencies): | ||
"""A dataclass of dependencies necessary for transaction verification.""" | ||
|
||
@classmethod | ||
def create(cls, tx: Transaction) -> Self: | ||
"""Create a transaction dependencies instance.""" | ||
assert tx.storage is not None | ||
simple_storage = SimpleMemoryStorage() | ||
spent_txs = [tx_input.tx_id for tx_input in tx.inputs] | ||
deps = tx.parents + spent_txs | ||
|
||
simple_storage.add_vertices_from_storage(tx.storage, deps) | ||
|
||
return cls(storage=simple_storage) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it part of the dependencies?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because since the Feature Activation process is dynamic, it is not straightforward to determine which blocks are necessary to calculate this state in advance (differently from the DAA, for example, where it's easy to determine the number of necessary parent blocks).
In order to determine the necessary blocks, we would have to run the algorithm anyway, so there's almost no difference in cost between determining all block dependencies in advance, or determining the state itself in advance.
I suggest we implement it like this for now, and then after the verification is moved to another process, we measure the cost of doing this calculation in main process, to decide if it's worth the refactor (we would need to change Feature Activation code in order to get just the required blocks).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed in our call, we'll keep this for now. After completing the Multiprocess Verification, we'll measure its performance and determine if it should be improved or not.