uAgents icon indicating copy to clipboard operation
uAgents copied to clipboard

Digest is based on a partially ordered model schema blocking reproducibility

Open Luceium opened this issue 1 year ago • 3 comments
trafficstars

Prerequisites

  • [X] I checked the documentation and found no answer to my problem
  • [X] I checked the existing issues and made sure there are no similar bug reports

Category

Bug (unexpected behavior)

Expected Behavior

Digests should be based on completely sorted json. Completely sorted means recursively sorting all keys, arrays, and nested objects. It seems that nested objects are sorted by key but, arrays are not.

Observed Behavior

Digest is based on a json representation of the open api model schema. However, the json is only ordered by keys. For example given a model

class SuperImportantCheck(Model):
    """Plus random docstring"""
    check: bool
    message: str
    counter: int

when Model.build_schema_digest() is called it runs schema = model.schema_json(indent=None, sort_keys=True) for the given SuperImportantCheck Model, schema will equal

{"description": "Plus random docstring", "properties": {"check": {"title": "Check", "type": "boolean"}, "counter": {"title": "Counter", "type": "integer"}, "message": {"title": "Message", "type": "string"}}, "required": ["check", "message", "counter"], "title": "SuperImportantCheck", "type": "object"}

Notably, schema.required maps to an array with the unordered parameters ["check", "message", "counter"].

The unordered nature of the schema json means other agents hoping to interact with an agent must implement a Model with the same ordering such that the digests match.

To Reproduce

Modify uAgents/python/src/model.py to contain

 @staticmethod
    def build_schema_digest(model: Union["Model", Type["Model"]]) -> str:
        schema = model.schema_json(indent=None, sort_keys=True)
        print(schema)
        digest = hashlib.sha256(schema.encode("utf8")).digest().hex()

        return f"model:{digest}"

Then run python python/tests/test_model.py

Version

v0.17.0

Environment Details (Optional)

No response

Failure Logs (Optional)

No response

Additional Information (Optional)

No response

Luceium avatar Oct 25 '24 04:10 Luceium