# bxptr/plutus architecture

> Architecture diagram of the GitHub repository bxptr/plutus, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bxptr/plutus
- Repository: https://github.com/bxptr/plutus

## Overview

##### Main Components

A. Core Engine Components:
- Matching Engine (Central Component)
- Order Book Manager
- Event Loop System
- Network Interface
- Message Parser
- Engine Controller
- Session Manager

B. Supporting Systems:
- Memory Pool
- Order ID Generator
- Logging System
- Replay System

##### Component Relationships and Data Flow

Layer 1 (External Interface):
- Client connections at the top
- Network Interface component handling incoming connections
- Session Manager for client session handling

Layer 2 (Processing Pipeline):
- Message Parser for incoming order processing
- Engine Controller as the orchestrator
- Event Loop managing async operations

Layer 3 (Core Business Logic):
- Matching Engine as the central component
- Order Book maintaining bid/ask orders
- Memory Pool for efficient memory management

##### Key Interactions to Highlight

a) Client Flow:
**Client → Network Interface → Session Manager → Message Parser → Engine Controller → Matching Engine**

b) Order Processing Flow:
**Matching Engine ↔ Order Book**
- ↓
- Trade Execution
- ↓
- Client Notification

##### Technical Details

- Kqueue event system integration
- Memory pool's connection to components
- Supported order types (Limit, Market, Stop-loss, Iceberg)
- Time-in-force options (GTC, IOC, FOK)

##### Performance Aspects

Annotations for:
- Non-blocking I/O paths
- Multi-threaded API endpoints
- Low-latency critical paths
- Memory pool optimization points

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Style definitions
    classDef external fill:#e1eedd,stroke:#333
    classDef core fill:#ff9b9b,stroke:#333
    classDef processing fill:#ffd4b2,stroke:#333
    classDef storage fill:#aec3ae,stroke:#333
    classDef system fill:#98b4aa,stroke:#333
    
    %% Legend
    subgraph Legend
        L1[External Components]:::external
        L2[Core Components]:::core
        L3[Processing Components]:::processing
        L4[Storage Components]:::storage
        L5[System Components]:::system
    end

    %% External Layer
    Client[Client Interface]:::external
    
    %% Processing Layer
    Network[Network Interface]:::processing
    Session[Session Manager]:::processing
    Parser[Message Parser]:::processing
    Controller[Engine Controller]:::processing
    EventLoop[Event Loop System]:::processing

    %% Core Layer
    Engine[Matching Engine]:::core
    OrderBook[Order Book Manager]:::storage
    
    %% System Components
    MemPool[Memory Pool]:::system
    OrderGen[Order ID Generator]:::system
    Logger[Logging System]:::system
    Replay[Replay System]:::system
    SymConfig[Symbol Config]:::system

    %% Relationships
    Client --> |"New Orders"| Network
    Network --> |"Process Connection"| Session
    Session --> |"Parse Message"| Parser
    Parser --> |"Process Order"| Controller
    Controller --> |"Match Order"| Engine
    Engine <--> |"Update"| OrderBook
    EventLoop --> |"Events"| Controller
    
    %% System Interactions
    MemPool --> |"Memory Management"| Engine
    MemPool --> |"Memory Management"| OrderBook
    OrderGen --> |"Generate IDs"| Engine
    Logger --> |"Log Events"| Engine
    Replay --> |"Replay Orders"| Controller
    SymConfig --> |"Configure"| Engine

    %% Click Events
    click Client "https://github.com/bxptr/plutus/blob/master/client/client.py"
    click Network "https://github.com/bxptr/plutus/blob/master/src/NetworkInterface.h"
    click Session "https://github.com/bxptr/plutus/blob/master/src/Session.h"
    click Parser "https://github.com/bxptr/plutus/blob/master/src/MessageParser.h"
    click Controller "https://github.com/bxptr/plutus/blob/master/src/EngineController.h"
    click Engine "https://github.com/bxptr/plutus/blob/master/src/MatchingEngine.h"
    click OrderBook "https://github.com/bxptr/plutus/blob/master/src/OrderBook.h"
    click EventLoop "https://github.com/bxptr/plutus/blob/master/src/EventLoop.h"
    click MemPool "https://github.com/bxptr/plutus/blob/master/src/MemoryPool.h"
    click OrderGen "https://github.com/bxptr/plutus/blob/master/src/OrderIdGenerator.h"
    click Logger "https://github.com/bxptr/plutus/blob/master/src/Logging.h"
    click Replay "https://github.com/bxptr/plutus/blob/master/src/Replay.h"
    click SymConfig "https://github.com/bxptr/plutus/blob/master/src/SymbolConfig.h"
```
