-
Notifications
You must be signed in to change notification settings - Fork 441
Header fix #1532
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
Merged
Header fix #1532
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5bd8da1
rm md5 -> sha3
ifrit98 f162a82
pass required_hash_fields as attach argument
ifrit98 813a99a
make default_verify async
ifrit98 7145430
move req_hash_fields to a field instead of prop
ifrit98 48b3c9f
black
ifrit98 953cdb6
make verify_body_integirty member of self to accesss required_hash_fi…
ifrit98 9364327
correct default value
ifrit98 5adafac
proper request name for key
ifrit98 549ff76
update tests
ifrit98 a921f40
black
ifrit98 947f490
parse required_hash_fields directly from protocol definition itself
ifrit98 ed4b29d
run black
ifrit98 a44bdc6
move parsing req_hash_fields below signature checks
ifrit98 6d2c25f
remove arg docstring for attach()
ifrit98 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
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 |
|---|---|---|
|
|
@@ -306,8 +306,8 @@ def set_name_type(cls, values) -> dict: | |
| repr=False, | ||
| ) | ||
|
|
||
| hash_fields: Optional[List[str]] = pydantic.Field( | ||
| title="hash_fields", | ||
| required_hash_fields: Optional[List[str]] = pydantic.Field( | ||
| title="required_hash_fields", | ||
| description="The list of required fields to compute the body hash.", | ||
| examples=["roles", "messages"], | ||
| default=[], | ||
|
|
@@ -319,61 +319,12 @@ def __setattr__(self, name: str, value: Any): | |
| """ | ||
| Override the __setattr__ method to make the `required_hash_fields` property read-only. | ||
| """ | ||
| if name == "required_hash_fields": | ||
| raise AttributeError( | ||
| "required_hash_fields property is read-only and cannot be overridden." | ||
| ) | ||
| if name == "body_hash": | ||
| raise AttributeError( | ||
| "body_hash property is read-only and cannot be overridden." | ||
| ) | ||
| super().__setattr__(name, value) | ||
|
|
||
| @property | ||
| def required_hash_fields(self) -> List[str]: | ||
| """ | ||
| Retrieve the list of non-optional fields of the Synapse instance. | ||
|
|
||
| This default method identifies and returns the names of non-optional attributes of the Synapse | ||
| instance that have non-null values, excluding specific attributes such as `name`, `timeout`, | ||
| `total_size`, `header_size`, `dendrite`, and `axon`. The determination of whether a field is | ||
| optional or not is based on the schema definition for the Synapse class. | ||
|
|
||
| Subclasses are encouraged to override this method to provide their own implementation for | ||
| determining required fields. If not overridden, the default implementation provided by the | ||
| Synapse superclass will be used, which returns the fields based on the schema definition. | ||
|
|
||
| Returns: | ||
| List[str]: A list of names of the non-optional fields of the Synapse instance. | ||
| """ | ||
| fields = [] | ||
| # Getting the fields of the instance | ||
| instance_fields = self.__dict__ | ||
|
|
||
| # Iterating over the fields of the instance | ||
| for field, value in instance_fields.items(): | ||
| # If the object is not optional and non-null, add to the list of returned body fields | ||
| required = schema([self.__class__])["definitions"][self.name].get( | ||
| "required" | ||
| ) | ||
| if ( | ||
| required | ||
| and field in required | ||
| and value != None | ||
| and field | ||
| not in [ | ||
| "name", | ||
| "timeout", | ||
| "total_size", | ||
| "header_size", | ||
| "dendrite", | ||
| "axon", | ||
| ] | ||
| and "_hash" not in field | ||
| ): | ||
| fields.append(field) | ||
| return fields | ||
|
|
||
| def get_total_size(self) -> int: | ||
| """ | ||
| Get the total size of the current object. | ||
|
|
@@ -542,33 +493,30 @@ def to_headers(self) -> dict: | |
| headers["header_size"] = str(sys.getsizeof(headers)) | ||
| headers["total_size"] = str(self.get_total_size()) | ||
| headers["computed_body_hash"] = self.body_hash | ||
| headers["hash_fields"] = base64.b64encode( | ||
| json.dumps(self.required_hash_fields).encode() | ||
| ).decode("utf-8") | ||
|
|
||
| return headers | ||
|
|
||
| @property | ||
| def body_hash(self) -> str: | ||
| """ | ||
| Compute a SHA-256 hash of the serialized body of the Synapse instance. | ||
| Compute a SHA3-256 hash of the serialized body of the Synapse instance. | ||
|
|
||
| The body of the Synapse instance comprises its serialized and encoded | ||
| non-optional fields. This property retrieves these fields using the | ||
| `get_body` method, then concatenates their string representations, and | ||
| finally computes a SHA-256 hash of the resulting string. | ||
| `required_fields_hash` field, then concatenates their string representations, | ||
| and finally computes a SHA3-256 hash of the resulting string. | ||
|
|
||
| Returns: | ||
| str: The hexadecimal representation of the SHA-256 hash of the instance's body. | ||
| str: The hexadecimal representation of the SHA3-256 hash of the instance's body. | ||
| """ | ||
| # Hash the body for verification | ||
| hashes = [] | ||
|
|
||
| # Getting the fields of the instance | ||
| instance_fields = self.__dict__ | ||
|
|
||
| # Iterating over the fields of the instance | ||
| for field, value in instance_fields.items(): | ||
| # If the field is required in the subclass schema, add it. | ||
| # If the field is required in the subclass schema, hash and add it. | ||
| if field in self.required_hash_fields: | ||
| hashes.append(bittensor.utils.hash(str(value))) | ||
|
|
||
|
|
@@ -689,11 +637,6 @@ def parse_headers_to_inputs(cls, headers: dict) -> dict: | |
| inputs_dict["header_size"] = headers.get("header_size", None) | ||
| inputs_dict["total_size"] = headers.get("total_size", None) | ||
| inputs_dict["computed_body_hash"] = headers.get("computed_body_hash", None) | ||
| inputs_dict["hash_fields"] = json.loads( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (same question as the previous one) does the axon need to know the other parsed hash_fields other then the required_hash_field? |
||
| base64.b64decode(headers.get("hash_fields", "W10=").encode()).decode( | ||
| "utf-8" | ||
| ) | ||
| ) | ||
|
|
||
| return inputs_dict | ||
|
|
||
|
|
||
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
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
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.
does the axon need to know the other parsed hash_fields other then the required_hash_field?
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.
Nope, that is a hold over, nice catch! Will remove.