|
| 1 | +--- |
| 2 | +title: Architecture Guide |
| 3 | +--- |
| 4 | + |
| 5 | +This document describes the high-level architecture of OpenZeppelin Monitor, including the core components, their interactions, and the overall system design. It provides a technical overview of how the service processes blockchain data and triggers notifications based on configurable conditions. |
| 6 | + |
| 7 | +## System Overview |
| 8 | + |
| 9 | +OpenZeppelin Monitor is organized as a data processing pipeline that spans from blockchain data collection to notification delivery. The system follows a modular architecture with distinct components for each step in the monitoring process, designed for scalability and extensibility. |
| 10 | + |
| 11 | +### High-Level Architecture |
| 12 | + |
| 13 | +The diagram below shows the core processing pipeline of OpenZeppelin Monitor, from blockchain networks and configuration through to notification channels: |
| 14 | + |
| 15 | +```mermaid |
| 16 | +flowchart LR |
| 17 | + subgraph "Blockchain Networks" |
| 18 | + EVMN["EVM Networks"] |
| 19 | + STLN["Stellar Networks"] |
| 20 | + end |
| 21 | + subgraph "Configuration" |
| 22 | + NETCFG["Network Configs"] |
| 23 | + MONCFG["Monitor Configs"] |
| 24 | + TRICFG["Trigger Configs"] |
| 25 | + end |
| 26 | + subgraph "Core Processing Pipeline" |
| 27 | + BWS["BlockWatcherService"] |
| 28 | + FS["FilterService"] |
| 29 | + TS["TriggerService"] |
| 30 | + NS["NotificationService"] |
| 31 | + end |
| 32 | + subgraph "Notification Channels" |
| 33 | + Slack |
| 34 | + Email |
| 35 | + Discord |
| 36 | + Telegram |
| 37 | + Webhook |
| 38 | + Scripts["Custom Scripts"] |
| 39 | + end |
| 40 | +
|
| 41 | + %% Data and config flow |
| 42 | + EVMN --> BWS |
| 43 | + STLN --> BWS |
| 44 | + NETCFG --> BWS |
| 45 | + MONCFG --> FS |
| 46 | + TRICFG --> TS |
| 47 | + BWS --> FS |
| 48 | + FS --> TS |
| 49 | + TS --> NS |
| 50 | + NS --> Slack |
| 51 | + NS --> Email |
| 52 | + NS --> Discord |
| 53 | + NS --> Telegram |
| 54 | + NS --> Webhook |
| 55 | + NS --> Scripts |
| 56 | +
|
| 57 | + %% Styling for major blocks and notification channels |
| 58 | + classDef blockchain fill:#fff3e0,stroke:#ef6c00,stroke-width:2px; |
| 59 | + classDef config fill:#e8f5e9,stroke:#2e7d32,stroke-width:2px; |
| 60 | + classDef mainBlock fill:#e1f5fe,stroke:#01579b,stroke-width:2px; |
| 61 | + classDef notification fill:#fce4ec,stroke:#c2185b,stroke-width:2px; |
| 62 | + class EVMN,STLN blockchain; |
| 63 | + class NETCFG,MONCFG,TRICFG config; |
| 64 | + class BWS,FS,TS,NS mainBlock; |
| 65 | + class Slack,Email,Discord,Telegram,Webhook,Scripts notification; |
| 66 | +``` |
| 67 | + |
| 68 | +## Component Architecture |
| 69 | + |
| 70 | +The system consists of several core services that are initialized at startup and work together to process blockchain data and trigger notifications. The service initialization and dependencies are managed through the bootstrap module. |
| 71 | + |
| 72 | +### Service Initialization Flow |
| 73 | + |
| 74 | +```mermaid |
| 75 | +graph TD |
| 76 | + subgraph Entry Point |
| 77 | + MAIN[main.rs] |
| 78 | + end |
| 79 | +
|
| 80 | + subgraph Bootstrap |
| 81 | + BOOTSTRAP[Bootstrap::initialize_service] |
| 82 | + end |
| 83 | +
|
| 84 | + subgraph Block Processing |
| 85 | + BT[BlockTracker] |
| 86 | + BS[BlockStorage] |
| 87 | + BWS[BlockWatcherService] |
| 88 | + BH[create_block_handler] |
| 89 | + end |
| 90 | +
|
| 91 | + subgraph Core Services |
| 92 | + MS[MonitorService] |
| 93 | + NS[NetworkService] |
| 94 | + TS[TriggerService] |
| 95 | + FS[FilterService] |
| 96 | + TES[TriggerExecutionService] |
| 97 | + NOTS[NotificationService] |
| 98 | + end |
| 99 | +
|
| 100 | + subgraph Client Layer |
| 101 | + CP[ClientPool] |
| 102 | + EVMC[EVMClient] |
| 103 | + SC[StellarClient] |
| 104 | + end |
| 105 | +
|
| 106 | + %% Initialization Flow |
| 107 | + MAIN --> BOOTSTRAP |
| 108 | + BOOTSTRAP --> CP |
| 109 | + BOOTSTRAP --> NS |
| 110 | + BOOTSTRAP --> MS |
| 111 | + BOOTSTRAP --> TS |
| 112 | + BOOTSTRAP --> FS |
| 113 | + BOOTSTRAP --> TES |
| 114 | + BOOTSTRAP --> NOTS |
| 115 | +
|
| 116 | + %% Block Processing Setup |
| 117 | + BOOTSTRAP --> BT |
| 118 | + BOOTSTRAP --> BS |
| 119 | + BOOTSTRAP --> BWS |
| 120 | + BOOTSTRAP --> BH |
| 121 | +
|
| 122 | + %% Client Dependencies |
| 123 | + CP --> EVMC |
| 124 | + CP --> SC |
| 125 | + BWS --> CP |
| 126 | +
|
| 127 | + %% Service Dependencies |
| 128 | + BWS --> BS |
| 129 | + BWS --> BT |
| 130 | + MS --> NS |
| 131 | + MS --> TS |
| 132 | + FS --> TES |
| 133 | + TES --> NOTS |
| 134 | +
|
| 135 | + %% Block Handler Connection |
| 136 | + BH --> FS |
| 137 | + BWS --> BH |
| 138 | +
|
| 139 | + style MAIN fill:#e1f5fe,stroke:#01579b |
| 140 | + style BOOTSTRAP fill:#fff3e0,stroke:#ef6c00 |
| 141 | + classDef blockProcessing fill:#e8f5e9,stroke:#2e7d32 |
| 142 | + classDef coreServices fill:#f3e5f5,stroke:#7b1fa2 |
| 143 | + classDef clients fill:#fce4ec,stroke:#c2185b |
| 144 | +
|
| 145 | + class BT,BS,BWS,BH blockProcessing |
| 146 | + class MS,NS,TS,FS,TES,NOTS coreServices |
| 147 | + class CP,EVMC,SC clients |
| 148 | +``` |
| 149 | + |
| 150 | +### Core Components |
| 151 | + |
| 152 | +#### Block Processing Components |
| 153 | + |
| 154 | +* ***BlockWatcherService***: Orchestrates the block monitoring process by polling blockchain networks for new blocks and coordinating the processing pipeline. |
| 155 | +* ***BlockTracker***: Tracks processed block numbers to prevent duplicate processing and ensure data consistency across service restarts. |
| 156 | +* ***BlockStorage***: Persists block processing state for recovery and maintains the last processed block number for each network. |
| 157 | + |
| 158 | +#### Client Layer Components |
| 159 | + |
| 160 | +* ***ClientPool***: Manages blockchain client instances and provides network connectivity with connection pooling and failover capabilities. |
| 161 | +* ***EVMClient***: Handles communication with Ethereum Virtual Machine compatible networks (Ethereum, Polygon, BSC, etc.). |
| 162 | +* ***StellarClient***: Manages connections to Stellar blockchain networks with protocol-specific optimizations. |
| 163 | + |
| 164 | +#### Processing Pipeline Components |
| 165 | + |
| 166 | +* ***FilterService***: Applies monitor filters to blockchain data, evaluating conditions and match expressions to identify relevant transactions and events. |
| 167 | +* ***TriggerExecutionService***: Executes triggers based on matched monitor conditions, evaluating trigger logic and preparing notification payloads. |
| 168 | +* ***NotificationService***: Delivers notifications through configured channels (Slack, Email, Discord, Telegram, Webhooks, Scripts). |
| 169 | + |
| 170 | +#### Configuration Management Components |
| 171 | + |
| 172 | +* ***MonitorService***: Manages monitor configurations and provides access to active monitors with validation and lifecycle management. |
| 173 | +* ***NetworkService***: Manages network configurations and provides network details for client connections and monitoring operations. |
| 174 | +* ***TriggerService***: Manages trigger configurations and provides trigger details for notification execution. |
| 175 | + |
| 176 | +### Service Responsibilities |
| 177 | + |
| 178 | +The following table describes the key responsibilities of each service in the OpenZeppelin Monitor architecture: |
| 179 | + |
| 180 | +| Service | Responsibility | |
| 181 | +| --- | --- | |
| 182 | +| **MonitorService** | Manages monitor configurations and provides access to active monitors | |
| 183 | +| **NetworkService** | Manages network configurations and provides network details | |
| 184 | +| **TriggerService** | Manages trigger configurations and provides trigger details | |
| 185 | +| **FilterService** | Filters blockchain data based on monitor conditions and match expressions | |
| 186 | +| **TriggerExecutionService** | Executes triggers based on matched monitor conditions | |
| 187 | +| **NotificationService** | Delivers notifications through configured channels | |
| 188 | +| **BlockWatcherService** | Polls blockchain networks for new blocks and coordinates processing | |
| 189 | +| **BlockTracker** | Tracks processed block numbers to prevent duplicate processing | |
| 190 | +| **BlockStorage** | Persists block processing state for recovery | |
| 191 | +| **ClientPool** | Manages blockchain client instances and provides network connectivity | |
| 192 | + |
| 193 | +### Block Processing Workflow |
| 194 | + |
| 195 | +The following _runtime flow_ illustrates how data moves through the system, from blockchain networks to notification channels. This sequence represents the core monitoring loop that executes for each configured network. |
| 196 | + |
| 197 | +```mermaid |
| 198 | +sequenceDiagram |
| 199 | + participant BWS as BlockWatcherService |
| 200 | + participant BS as BlockStorage |
| 201 | + participant BC as BlockchainClient |
| 202 | + participant FS as FilterService |
| 203 | + participant TES as TriggerExecutionService |
| 204 | + participant NS as NotificationService |
| 205 | +
|
| 206 | + rect rgb(232, 245, 233) |
| 207 | + Note over BWS: Orchestrates block processing |
| 208 | + BWS->>BS: Get last processed block |
| 209 | + end |
| 210 | +
|
| 211 | + rect rgb(225, 245, 254) |
| 212 | + Note over BC: Blockchain interface |
| 213 | + BWS->>BC: Get latest block number |
| 214 | + BWS->>BC: Get blocks (last+1 to latest) |
| 215 | + end |
| 216 | +
|
| 217 | + loop For each block |
| 218 | + rect rgb(243, 229, 245) |
| 219 | + Note over FS: Applies monitor filters |
| 220 | + BWS->>FS: filter_block(block) |
| 221 | + FS->>FS: Apply monitor filters |
| 222 | + FS-->>BWS: Monitor matches |
| 223 | + end |
| 224 | +
|
| 225 | + rect rgb(255, 248, 225) |
| 226 | + Note over TES: Evaluates trigger conditions |
| 227 | + BWS->>TES: Process monitor matches |
| 228 | + TES->>TES: Run trigger conditions |
| 229 | + end |
| 230 | +
|
| 231 | + rect rgb(252, 228, 236) |
| 232 | + Note over NS: Delivers notifications |
| 233 | + TES->>NS: Execute notifications |
| 234 | + end |
| 235 | + end |
| 236 | +
|
| 237 | + rect rgb(255, 243, 224) |
| 238 | + Note over BS: Persists processing state |
| 239 | + BWS->>BS: Store latest processed block |
| 240 | + end |
| 241 | +``` |
| 242 | + |
| 243 | +### Data Flow Architecture |
| 244 | + |
| 245 | +#### 1. Block Discovery Phase |
| 246 | +The `BlockWatcherService` initiates the monitoring cycle by: |
| 247 | + |
| 248 | +* Retrieving the last processed block number from `BlockStorage` |
| 249 | +* Querying the blockchain for the latest block number |
| 250 | +* Calculating the range of new blocks to process |
| 251 | + |
| 252 | +#### 2. Data Retrieval Phase |
| 253 | +The `BlockchainClient` fetches block data: |
| 254 | + |
| 255 | +* Connects to the appropriate blockchain network via RPC |
| 256 | +* Retrieves full block data including transactions and events |
| 257 | +* Handles network-specific data formats and protocols |
| 258 | + |
| 259 | +#### 3. Filtering Phase |
| 260 | +The `FilterService` processes each block: |
| 261 | + |
| 262 | +* Applies monitor-specific filters to transactions and events |
| 263 | +* Evaluates match expressions and conditions |
| 264 | +* Identifies relevant data that matches monitoring criteria |
| 265 | + |
| 266 | +#### 4. Trigger Evaluation Phase |
| 267 | +The `TriggerExecutionService` processes matches: |
| 268 | + |
| 269 | +* Evaluates trigger conditions for matched data |
| 270 | +* Prepares notification payloads with relevant context |
| 271 | +* Determines which notification channels to activate |
| 272 | + |
| 273 | +#### 5. Notification Delivery Phase |
| 274 | +The `NotificationService` delivers alerts: |
| 275 | + |
| 276 | +* Formats messages for each notification channel |
| 277 | +* Handles channel-specific delivery protocols |
| 278 | +* Manages delivery retries and error handling |
| 279 | + |
| 280 | +#### 6. State Persistence Phase |
| 281 | +The `BlockStorage` updates processing state: |
| 282 | + |
| 283 | +* Records the latest processed block number |
| 284 | +* Ensures data consistency for recovery scenarios |
| 285 | +* Maintains processing history for debugging |
| 286 | + |
| 287 | +### Error Handling and Resilience |
| 288 | + |
| 289 | +The architecture includes several resilience mechanisms: |
| 290 | + |
| 291 | +* ***Connection Pooling***: The `ClientPool` manages multiple connections to prevent single points of failure |
| 292 | +* ***State Recovery***: `BlockStorage` enables the service to resume from the last known good state after restarts |
| 293 | +* ***Retry Logic***: Notification delivery includes configurable retry mechanisms for transient failures |
| 294 | +* ***Graceful Degradation***: Individual component failures don’t cascade to the entire system |
| 295 | + |
| 296 | +For detailed information about RPC logic and network communication, see the [RPC section](/monitor/1.0.x/rpc). |
| 297 | + |
| 298 | +## Configuration Architecture |
| 299 | + |
| 300 | +The system uses a JSON-based configuration system organized into distinct categories: |
| 301 | + |
| 302 | +### Configuration Categories |
| 303 | + |
| 304 | +* ***Network Configurations***: Define blockchain network connections, RPC endpoints, and network parameters |
| 305 | +* ***Monitor Configurations***: Specify monitoring rules, conditions, and network/trigger references |
| 306 | +* ***Trigger Configurations***: Define notification settings and script definitions |
| 307 | +* ***Filter Configurations***: Contain match filter scripts for data filtering |
| 308 | + |
| 309 | +### Configuration Validation |
| 310 | + |
| 311 | +The system implements comprehensive validation: |
| 312 | + |
| 313 | +* Cross-reference validation between monitors, networks, and triggers |
| 314 | +* Schema validation for all configuration files |
| 315 | +* Runtime validation of configuration references during service startup |
| 316 | + |
| 317 | + |
| 318 | + |
| 319 | +For configuration examples and best practices, see the [Configuration Guidelines](/monitor/1.0.x#configuration-guidelines) section in the user documentation. |
| 320 | + |
| 321 | + |
| 322 | +## Extensibility Points |
| 323 | + |
| 324 | +The architecture is designed for extensibility in several key areas: |
| 325 | + |
| 326 | +### Blockchain Support |
| 327 | +* ***Client Layer***: New blockchain protocols can be added by implementing the `BlockchainClient` trait |
| 328 | +* ***Transport Layer***: Protocol-specific transport clients handle network communication details |
| 329 | +* ***Filter Layer***: Chain-specific filters process protocol-dependent data formats |
| 330 | + |
| 331 | +### Notification Channels |
| 332 | +* ***Channel Plugins***: New notification channels can be added by implementing the notification interface |
| 333 | +* ***Script Support***: Custom notification logic can be implemented using Python, JavaScript, or Bash scripts |
| 334 | + |
| 335 | +### Monitoring Logic |
| 336 | +* ***Expression Engine***: Flexible expression evaluation for complex monitoring conditions |
| 337 | +* ***Script Triggers***: Custom trigger logic can be implemented using supported scripting languages |
| 338 | + |
| 339 | +## Performance Considerations |
| 340 | + |
| 341 | +The architecture is optimized for: |
| 342 | + |
| 343 | +* ***Concurrent Processing***: Multiple networks can be monitored simultaneously |
| 344 | +* ***Efficient Block Processing***: Batch processing of blocks to minimize RPC calls |
| 345 | +* ***Memory Management***: Streaming processing of large blocks to prevent memory issues |
| 346 | +* ***Connection Reuse***: Client pooling reduces connection overhead |
| 347 | + |
| 348 | +## Security Architecture |
| 349 | + |
| 350 | +The system implements several security measures: |
| 351 | + |
| 352 | +* ***Secure Protocols***: Support for HTTPS/WSS |
| 353 | +* ***Secret Management***: Secure handling of API keys and sensitive configuration data |
| 354 | +* ***Input Validation***: Comprehensive validation of all external inputs and configurations |
| 355 | + |
| 356 | +## Related Documentation |
| 357 | + |
| 358 | +For detailed information about the project structure, source code organization, and development resources, see the [Project Structure](/monitor/1.0.x/project-structure) guide. |
| 359 | + |
| 360 | +For information about RPC logic and network communication, see the [RPC section](/monitor/1.0.x/rpc). |
| 361 | + |
| 362 | +For configuration examples and best practices, see the [Configuration Guidelines](/monitor/1.0.x#configuration-guidelines) section in the user documentation. |
0 commit comments