Architecture

Overview

The EVM precompile system bridges Solidity smart contracts with Cosmos SDK modules, enabling native access to blockchain functionality from EVM-compatible code.

System Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Solidity Smart Contract                  β”‚
β”‚  (EVM Address: 0x0000000000000000000000000000000000001001)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                         β”‚
                         β”‚ ABI-encoded calls
                         β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              Precompile Contract (Go)                       β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚  ABI Decoder                                         β”‚   β”‚
β”‚  β”‚  - Method ID resolution                             β”‚   β”‚
β”‚  β”‚  - Parameter unpacking                              β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚  Type Converter                                      β”‚   β”‚
β”‚  β”‚  - Solidity β†’ Cosmos SDK types                      β”‚   β”‚
β”‚  β”‚  - Validation & sanitization                        β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚  Security Layer                                      β”‚   β”‚
β”‚  β”‚  - Caller verification                              β”‚   β”‚
β”‚  β”‚  - Input validation                                 β”‚   β”‚
β”‚  β”‚  - DoS protection                                   β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                         β”‚
                         β”‚ Cosmos SDK messages
                         β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              Cosmos SDK Module (Keeper)                      β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚  Tokenization Keeper                                 β”‚   β”‚
β”‚  β”‚  - State management                                  β”‚   β”‚
β”‚  β”‚  - Business logic                                    β”‚   β”‚
β”‚  β”‚  - Validation                                        β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Components

1. Precompile Contract

The precompile contract (x/evm/precompiles/tokenization/precompile.go) implements the vm.PrecompiledContract interface:

  • RequiredGas(): Calculates gas costs based on input complexity

  • Run(): Executes the precompile method

  • Execute(): Dispatches to appropriate handler based on method name

2. ABI Definition

The ABI (abi.json) defines the Solidity interface:

  • Method signatures

  • Parameter types

  • Return types

  • Event definitions

3. Type Registry

Solidity type definitions (contracts/types/TokenizationTypes.sol) mirror Cosmos SDK proto types:

  • Struct definitions

  • Type mappings

  • Helper functions

4. Conversion Layer

Type conversion utilities (conversions.go) handle:

  • Solidity structs β†’ Cosmos SDK proto types

  • Cosmos SDK proto types β†’ Solidity structs

  • Validation and error handling

5. Handler Methods

Transaction and query handlers (handlers.go) implement:

  • Input validation

  • Type conversion

  • Keeper method calls

  • Response formatting

Data Flow

Transaction Flow

  1. Solidity Call: Contract calls precompile method

  2. ABI Decoding: Precompile decodes method ID and parameters

  3. Type Conversion: Solidity types converted to Cosmos SDK types

  4. Validation: Input validation and security checks

  5. Keeper Call: Cosmos SDK keeper method invoked

  6. State Update: Blockchain state updated

  7. Event Emission: Events emitted for indexing

  8. Response: Success/failure returned to Solidity

Query Flow

  1. Solidity Call: Contract calls query method

  2. ABI Decoding: Precompile decodes method ID and parameters

  3. Type Conversion: Solidity types converted to Cosmos SDK types

  4. Keeper Query: Cosmos SDK query method invoked

  5. Response Encoding: Results encoded as protobuf bytes

  6. Return: Bytes returned to Solidity for decoding

Address Conversion

EVM addresses (20 bytes) are automatically converted to Cosmos addresses:

Gas Calculation

Gas costs are calculated dynamically based on operation complexity:

See Gas & Costs for detailed gas calculation.

Security Architecture

Caller Verification

All transaction methods verify the caller:

Input Validation

All inputs are validated before processing:

  • Type checking

  • Range validation

  • Size limits (DoS protection)

  • Business rule validation

Error Handling

Structured errors prevent information leakage:

See Security for security details.

Integration with Cosmos SDK

The precompile integrates with Cosmos SDK through:

  • Keeper Interface: Direct access to module keeper

  • Context: SDK context for state access

  • Messages: Standard Cosmos SDK message types

  • Queries: Standard Cosmos SDK query types

For more information on Cosmos SDK EVM integration, see the official documentationarrow-up-right.

Performance Considerations

Gas Optimization

  • Dynamic gas calculation based on actual complexity

  • Efficient type conversion

  • Minimal overhead for simple operations

Caching

  • ABI loaded once at initialization

  • Method lookup optimized (O(1) map lookup)

  • Type conversion cached where possible

Extension Points

Adding New Methods

  1. Define method in ABI (abi.json)

  2. Add method constant (precompile.go)

  3. Implement handler (handlers.go)

  4. Add type conversions if needed (conversions.go)

  5. Update gas calculation (gas.go)

Adding New Precompiles

  1. Create new precompile package

  2. Implement vm.PrecompiledContract interface

  3. Register in EVM module

  4. Define ABI and Solidity interface

Resources

Last updated