Skip to content

Commit

Permalink
Dict hash fix (#1548)
Browse files Browse the repository at this point in the history
* use dict() when hasing body objects to not convert arbitrary objects to str

* recreate synapse in axon dependency to avoid duplicating code

* black
  • Loading branch information
ifrit98 authored Oct 17, 2023
1 parent e6c02b6 commit de71c4a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
15 changes: 5 additions & 10 deletions bittensor/axon.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,16 +526,11 @@ async def some_endpoint(body_dict: dict = Depends(verify_body_integrity)):

# Load the body dict and check if all required field hashes match
body_dict = json.loads(request_body)
field_hashes = []
for required_field in required_hash_fields:
# Hash the field in the body to compare against the header hashes
body_value = body_dict.get(required_field, None)
if body_value == None:
raise ValueError(f"Missing required field {required_field}")
field_hash = bittensor.utils.hash(str(body_value))
field_hashes.append(field_hash)

parsed_body_hash = bittensor.utils.hash("".join(field_hashes))

# Reconstruct the synapse object from the body dict and recompute the hash
syn = self.forward_class_types[request_name](**body_dict)
parsed_body_hash = syn.body_hash # Rehash the body from request

body_hash = request.headers.get("computed_body_hash", "")
if parsed_body_hash != body_hash:
raise ValueError(
Expand Down
2 changes: 1 addition & 1 deletion bittensor/synapse.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ def body_hash(self) -> str:
hashes = []

# Getting the fields of the instance
instance_fields = self.__dict__
instance_fields = self.dict()

for field, value in instance_fields.items():
# If the field is required in the subclass schema, hash and add it.
Expand Down

0 comments on commit de71c4a

Please sign in to comment.