How fantom-foundation/sonic works
Below are guidelines tailored to this project:
Main Components
• Command Line Tool (sonictool): Found in ‘cmd/sonictool’, this component is used for managing genesis creation, configuration dump, and validator key operations.
• Network & Consensus Layer: The gossip package (and related sub-packages like eventcheck, inter, and protocols directories) implements the peer-to-peer messaging and consensus functions. This includes the handling of events, block propagation, and Lachesis-specific algorithms.
• Ethereum Virtual Machine (EVM) Layer: Components such as ‘evmcore’, ‘ethapi’, and parts of ‘ftmclient’ implement EVM functionalities to make the chain EVM-compatible and provide API access.
• Data Persistence & Database Utilities: The topicsdb directory along with numerous utilities (in utils, config, and dbutil folders) establish persistent storage for blockchain data, state, and logs.
• Configuration & Validator Keys: The config package (with default configuration, parameter management, etc.) and the valkeystore package manage node configuration and validator key management (including signing and encrypting keys).
Relationships and Interactions
• Sonicd as the central node runtime that interacts with both the consensus (gossip and protocols) and the EVM core modules.
• Data flow between the network layer (gossip) and the core blockchain processing components. Indicate that gossip distributes events/transactions to be processed by the node’s consensus and state machine.
• EVM components (evmcore, ethapi) to the smart contract execution and transaction processing parts, highlighting how they interface with both the state management and networking layers.
• Storage subsystems (topicsdb and utilities for database management) as backing components where blockchain state, receipts, and historical data are stored.
• Configuration and logging modules (config, logger) as supporting infrastructure ensuring operational stability and debugging.
Important Architectural Patterns
• Modular Design: The separation in directories (cmd, config, gossip, evmcore, etc.) indicates a modular architecture where each package plays a distinct role.
• Layered Architecture: Clearly distinguish between the client’s CLI/tools (sonictool and sonicd startup), network and consensus layer (gossip and protocols), application (EVM processing), and storage.
• Adaptability & Extensibility: The existence of multiple adapters in the utils folder (for different database interfaces or EVM storage) shows an emphasis on decoupling and flexibility.
• Cross-Platform/Conditional Compilation: The presence of OS-specific implementations (e.g., diskusage for OpenBSD and Windows, memory packages) highlights attention to cross-platform support.