Skip to content

Conversation

@allenday
Copy link

feat: add circular dependency resolution for validation schemas

Summary

  • Implement automatic detection of circular dependencies in buf/validate types
  • Use bytes fields with encode/decode helpers to break circular references
  • Enable buf/validate schemas to compile successfully in Solidity

Changes

Automatic Circular Dependency Detection

  • Added CIRCULAR_DEPENDENCY_MAP for FieldRules ↔ MapRules ↔ RepeatedRules relationships
  • Implemented is_circular_dependency() and should_use_bytes_for_field() functions
  • Automatic detection based on message and field type analysis

Bytes Field Solution

  • Circular fields are converted to bytes representation with descriptive comments
  • Original protobuf semantics preserved through encode/decode operations
  • No manual intervention required - transparent to developers

Integration Across Generation Pipeline

  • Updated struct generation to handle circular fields with comments
  • Modified encoder/decoder to process bytes fields for circular references
  • Applied field name mangling consistently throughout

Technical Details

Files Modified:

  • protobuf-solidity/src/protoc/plugin/gen_util.py - Added circular dependency detection logic
  • protobuf-solidity/src/protoc/plugin/gen_decoder.py - Updated to use bytes decoder for circular fields
  • protobuf-solidity/src/protoc/plugin/gen_encoder.py - Updated to handle circular fields as bytes
  • protobuf-solidity/src/protoc/plugin/gen_sol.py - Updated struct generation with circular field handling
  • README.md - Added Circular Dependency Resolution documentation section

Key Functions Added:

  • is_circular_dependency(msg, field) - Detects circular references
  • should_use_bytes_for_field(msg, field) - Determines if bytes conversion needed

Circular Dependencies Resolved:

  • FieldRulesMapRules: Field rules can reference map validation rules
  • FieldRulesRepeatedRules: Field rules can reference repeated field validation rules
  • MapRulesFieldRules: Map rules reference field rules for key/value validation

Example Output

struct BufValidateFieldRules {
    bytes map_field;        // Circular reference: use encode/decode helpers
    bytes repeated_field;   // Circular reference: use encode/decode helpers
    // ... other fields maintain their natural types
}

Test Plan

  • Verify buf/validate schemas compile without circular dependency errors
  • Test generated contracts build successfully with Forge/Hardhat
  • Confirm circular fields can be serialized/deserialized correctly
  • Validate non-circular fields maintain original type system

Dependencies

This PR builds on feat/solidity-compatibility for the field name mangling functionality used throughout the circular dependency resolution system.

Impact

Enables buf/validate and other validation schemas with circular dependencies to work seamlessly with the Solidity protobuf generator, expanding the plugin's capability to handle complex validation scenarios.

- Add comprehensive Solidity reserved keywords detection and mangling
- Implement automatic field name mangling with _field suffix for conflicts
- Fix boolean parameter parsing to support flag-style options (gen_runtime=true)
- Update parameter processing to handle both boolean flags and value parameters
- Add file path normalization for cross-platform compatibility
- Update documentation with Solidity compatibility features
- Implement automatic detection of circular dependencies in buf/validate types
- Add CIRCULAR_DEPENDENCY_MAP for FieldRules, MapRules, and RepeatedRules
- Use bytes fields with encode/decode helpers to break circular references
- Apply field name mangling consistently across encoder/decoder/struct generation
- Add comprehensive circular dependency handling in gen_decoder.py
- Add struct generation support for circular fields with descriptive comments
- Update encoder to handle circular dependencies as simple byte assignments
- Extend runtime type mappings to support float/double validation constraints
- Update documentation with circular dependency resolution examples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant