Skip to content

Feat/generate contract interfaces#51

Open
allenday wants to merge 8 commits intodatachainlab:masterfrom
allenday:feat/generate-contract-interfaces
Open

Feat/generate contract interfaces#51
allenday wants to merge 8 commits intodatachainlab:masterfrom
allenday:feat/generate-contract-interfaces

Conversation

@allenday
Copy link

Add Contract Interface Generation from Proto Services

Overview

This PR adds the ability to generate Solidity contract interfaces directly from Protocol Buffer service definitions, enabling true proto-first blockchain development.

What This Adds

Service-to-Interface Generation

  • Generates Solidity interface declarations from proto service definitions
  • Maps proto RPC methods to Solidity function signatures
  • Maintains type safety between proto definitions and contract implementations

Type System Integration

  • Automatic type mapping from proto fields to Solidity types
  • Support for complex nested message types
  • Generic library naming for reusable components

Development Workflow

  • Proto services define contract APIs
  • Generated interfaces ensure compile-time compliance
  • Contracts implement generated interfaces, not ad-hoc signatures

Example

Proto Service Definition:

service ContractInterface {
  rpc RegisterIdentity(RegisterIdentityRequest) returns (RegisterIdentityResponse);
  rpc GetIdentities(GetIdentitiesRequest) returns (GetIdentitiesResponse);
}

Generated Solidity Interface:

interface IContractInterface {
    function registerIdentity(string memory domain, string memory identifier)
        external returns (bool success, string memory error, string memory transaction_hash);
    function getIdentities()
        external returns (StandaloneAttestorStandaloneIdentity.Data[] memory identities);
}

Contract Implementation:

contract MyContract is IContractInterface {
    // Compiler enforces interface compliance
    function registerIdentity(string memory domain, string memory identifier)
        external override returns (bool success, string memory error, string memory transaction_hash) {
        // Implementation
    }
}

Benefits

  • Single Source of Truth: Proto files define both frontend types and backend contracts
  • Type Safety: Compile-time verification that contracts match specifications
  • Consistency: Same API structure across different implementations
  • Maintainability: Proto changes automatically propagate to generated interfaces

Files Changed

  • src/protoc/plugin/gen_sol.py: Added gen_contract_interface() function and service processing
  • README.md: Added comprehensive documentation with examples

Testing

This feature has been validated with a real-world attestor contract implementation, demonstrating:

  • Successful interface generation from complex proto services
  • Contract compilation with interface inheritance
  • Full test suite compatibility with generated interfaces

Backward Compatibility

This change is fully backward compatible. Existing proto files that only contain messages will continue to work exactly as before. The new interface generation only activates when proto services are present.

- Add _decode_float and _encode_float functions for 32-bit float support
- Add _decode_double and _encode_double functions for 64-bit double support
- Include GoogleProtobufDuration.sol runtime library
- Include GoogleProtobufTimestamp.sol runtime library
- Include GoogleProtobufFieldDescriptorProto.sol runtime library
- Update documentation with runtime enhancements section
- 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
- Add buf.plugin.yaml for Buf Schema Registry distribution
- Include plugin metadata, dependencies, and runtime requirements
- Add comprehensive BSR integration documentation
- Provide buf.gen.yaml examples for local development
- Document plugin options and configuration parameters
- Respect original datachainlab project attribution
- Add service-to-interface code generation capability
- Generate interface functions for each RPC method in proto services
- Support proto field type mapping to Solidity types
- Use generic library naming instead of hardcoded prefixes
- Enable 1:1 mapping between proto services and contract interfaces
- Add comprehensive README documentation
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