# navyaaasingh/electionmanagement architecture

> Architecture diagram of the GitHub repository navyaaasingh/electionmanagement, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/navyaaasingh/electionmanagement
- Repository: https://github.com/navyaaasingh/electionmanagement

## Overview

1. **Repository shape**
   This is a multi-surface election platform monorepo. The main runtime subsystems are: **backend API**, **Hyperledger Fabric blockchain**, **IoT voting terminal**, **ML fraud detection service**, and **frontend UIs**. Supporting layers include **PostgreSQL/MongoDB/Redis/Kafka**, Kubernetes/Docker deployment, and CI/testing scripts.

2. **Primary end-to-end flow**
   A vote starts at the **IoT terminal** or voter-facing UI, is authenticated by the **backend**, persisted in operational databases, written to the **Fabric ledger**, and emitted as telemetry onto **Kafka**. From there, the **ML service** analyzes events and the **observer/admin dashboards** receive live updates over **WebSocket**.

3. **Backend is the orchestration hub**
   `backend/src` is a Node.js/Express service organized by **controllers**, **routes**, **services**, **middleware**, **models**, and **context-specific modules**. It is the central integration point for authentication, voting, results, auditing, and operational workflows.

4. **Backend boundaries and cross-cutting controls**
   The backend enforces security and policy through middleware for **auth**, **CSRF**, **rate limiting**, and **MAC filtering**. It also includes services for **biometric auth**, **encryption**, **ZKP**, **Fabric integration**, **IoT**, **Kafka production**, **observability**, **results**, and **reconciliation/saga processing**.

5. **Data layer and workflow state**
   The backend models show a split between transactional and audit/async state: **students, voters, elections, candidates, admin users, audit logs, disputes, passkeys, vote nonces, voting records, outbox events, dead-letter events, and saga status**. This suggests a design that combines synchronous API writes with asynchronous, recoverable workflows.

6. **Eventing and live operations**
   Kafka is used as the event backbone for telemetry and downstream analysis. WebSocket support is present for pushing live status to dashboards. Redis appears as supporting infrastructure for session/rate-limit style operational state.

7. **Blockchain subsystem**
   `blockchain/chaincode/voting` contains the **Hyperledger Fabric smart contract** in Go, with its own test file and chaincode structs. The `blockchain/network` configs and shell scripts define the Fabric network topology and lifecycle. The ledger is the immutable enforcement boundary for vote validity and recording.

8. **Edge voting terminal**
   `iot-terminal` combines ESP32 firmware/C++ and a Python runtime layer for the terminal’s operational logic. It includes **biometric handling**, **offline cache**, **tamper detection**, **MQTT connectivity**, and **VVPAT printing**, making it the resilience layer for disconnected or partially connected voting sites.

9. **Fraud detection and monitoring**
   `ml-service` is a Python analytics service centered on **stream processing and anomaly detection**. The README indicates an ensemble approach; the repo contains API, consumer, alerting, fraud detection, and monitoring notebooks/scripts. It subscribes to vote/event streams rather than serving the main voting path.

10. **Frontend surfaces**
   The root `frontend/` directory is the main **React + Vite unified shell** for multiple roles, with API clients and components for admin, voter, observer, verification, and public pages. There are also separate `admin-portal`, `observer-dashboard`, and `voter-ui` directories, which look like role-specific UI variants or earlier/smaller shells. `shared-ui/tokens.css` provides common styling tokens.

11. **Infrastructure and deployment**
   `infrastructure/` defines both **Docker** images and **Kubernetes** manifests. The K8s layer includes backend, ML, monitoring, Kafka, MongoDB, Postgres, Redis, ingress, secrets, HPA, and a reconciliation saga worker. Monitoring is built around **Prometheus** and **Grafana** dashboards/rules.

12. **Quality, simulation, and operations**
   The repo includes CI/CD workflows, security tests, load tests, chaos drills, and simulation scripts. These are not part of the runtime path, but they matter architecturally because they validate the vote pipeline, fraud flow, and failure recovery behavior under load and fault injection.

13. **Documentation as architecture contract**
   The `docs/` tree is unusually central: it defines API contracts, data model, blockchain schema, lifecycle/roles, threat model, deployment, monitoring, and user guides. For diagramming, these docs are effectively the authoritative boundaries and responsibilities of each subsystem.

Main entry points: [`backend/src/server.js`](https://github.com/navyaaasingh/electionmanagement/blob/main/backend/src/server.js) (API server), [`backend/src/services`](https://github.com/navyaaasingh/electionmanagement/tree/main/backend/src/services) (Core services), [`infrastructure/kubernetes`](https://github.com/navyaaasingh/electionmanagement/tree/main/infrastructure/kubernetes) (Kubernetes), [`scripts`](https://github.com/navyaaasingh/electionmanagement/tree/main/scripts) (Ops scripts)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_backend["Backend"]
  node_node_backend_server["API server<br/>express app<br/>[server.js]"]
  node_node_backend_contexts["Contexts<br/>domain modules"]
  node_node_backend_middleware["Policy guards<br/>middleware"]
  node_node_backend_services["Core services<br/>service layer"]
  node_node_backend_models[("Data models<br/>persistence models")]
  node_node_backend_routes["API routes<br/>http routes"]
  node_node_backend_workers["Saga worker<br/>background worker"]
end

subgraph group_group_blockchain["Blockchain"]
  node_node_blockchain_contract["Voting contract<br/>fabric chaincode<br/>[voting.go]"]
  node_node_blockchain_network["Fabric network<br/>network config"]
end

subgraph group_group_edge["Edge terminal"]
  node_node_iot_terminal["Terminal runtime<br/>edge app<br/>[terminal_main.py]"]
  node_node_iot_firmware["Firmware<br/>esp32 cpp<br/>[main.cpp]"]
  node_node_iot_printer["VVPAT printer<br/>printer module<br/>[vvpat_printer.py]"]
end

subgraph group_group_ml["ML service"]
  node_node_ml_api["ML API<br/>python api<br/>[api.py]"]
  node_node_ml_consumer["Stream consumer<br/>kafka consumer<br/>[kafka_consumer.py]"]
  node_node_ml_detector["Fraud detector<br/>anomaly engine<br/>[fraud_detector.py]"]
  node_node_ml_alerts["Alerting<br/>notification service<br/>[alert_service.py]"]
end

subgraph group_group_frontend["Frontend surfaces"]
  node_node_frontend_shell["Web shell<br/>react app<br/>[App.jsx]"]
  node_node_frontend_shared["Shared UI<br/>client lib<br/>[tokens.css]"]
  node_node_frontend_admin["Admin portal<br/>react ui"]
  node_node_frontend_observer["Observer dashboard<br/>react ui<br/>[App.jsx]"]
  node_node_frontend_voter["Voter UI<br/>react ui<br/>[App.jsx]"]
end

subgraph group_group_infra["Infrastructure"]
  node_node_infra_k8s["Kubernetes<br/>deployment manifests"]
  node_node_infra_monitoring["Monitoring<br/>prometheus/grafana"]
end

subgraph group_group_ops["Operations"]
  node_node_ops_scripts["Ops scripts<br/>validation scripts"]
end

node_node_frontend_voter -->|"auth/vote API"| node_node_backend_server
node_node_iot_terminal -->|"terminal API"| node_node_backend_server
node_node_backend_server -->|"enforces"| node_node_backend_middleware
node_node_backend_server -->|"dispatches"| node_node_backend_contexts
node_node_backend_server -->|"serves"| node_node_backend_routes
node_node_backend_server -->|"coordinates"| node_node_backend_services
node_node_backend_server -->|"persists"| node_node_backend_models
node_node_backend_services -->|"writes ledger"| node_node_blockchain_contract
node_node_backend_services -->|"publishes telemetry"| node_node_ml_consumer
node_node_backend_services -->|"live updates"| node_node_frontend_observer
node_node_backend_services -->|"reconciles"| node_node_backend_workers
node_node_backend_models -->|"async state"| node_node_backend_workers
node_node_blockchain_network -->|"hosts"| node_node_blockchain_contract
node_node_iot_firmware -->|"boots device"| node_node_iot_terminal
node_node_iot_terminal -->|"prints receipt"| node_node_iot_printer
node_node_ml_consumer -->|"analyzes"| node_node_ml_detector
node_node_ml_detector -->|"triggers"| node_node_ml_alerts
node_node_ml_api -->|"serves analysis"| node_node_ml_detector
node_node_frontend_shell -->|"uses"| node_node_frontend_shared
node_node_frontend_admin -->|"admin API"| node_node_backend_server
node_node_frontend_observer -->|"observer API"| node_node_backend_server
node_node_frontend_voter -->|"voter API"| node_node_backend_server
node_node_infra_k8s -->|"deploys"| node_node_backend_server
node_node_infra_k8s -->|"deploys"| node_node_ml_api
node_node_infra_k8s -->|"orchestrates"| node_node_blockchain_network
node_node_infra_k8s -->|"deploys"| node_node_backend_workers
node_node_infra_monitoring -->|"observes"| node_node_backend_services
node_node_ops_scripts -->|"tests"| node_node_backend_server
node_node_ops_scripts -->|"simulates"| node_node_ml_consumer
node_node_ops_scripts -->|"drills"| node_node_blockchain_network

click node_node_backend_server "https://github.com/navyaaasingh/electionmanagement/blob/main/backend/src/server.js"
click node_node_backend_contexts "https://github.com/navyaaasingh/electionmanagement/tree/main/backend/src/contexts"
click node_node_backend_middleware "https://github.com/navyaaasingh/electionmanagement/tree/main/backend/src/middleware"
click node_node_backend_services "https://github.com/navyaaasingh/electionmanagement/tree/main/backend/src/services"
click node_node_backend_models "https://github.com/navyaaasingh/electionmanagement/tree/main/backend/src/models"
click node_node_backend_routes "https://github.com/navyaaasingh/electionmanagement/tree/main/backend/src/routes"
click node_node_backend_workers "https://github.com/navyaaasingh/electionmanagement/blob/main/backend/src/workers/reconciliationSagaWorker.js"
click node_node_blockchain_contract "https://github.com/navyaaasingh/electionmanagement/blob/main/blockchain/chaincode/voting/voting.go"
click node_node_blockchain_network "https://github.com/navyaaasingh/electionmanagement/tree/main/blockchain/network"
click node_node_iot_terminal "https://github.com/navyaaasingh/electionmanagement/blob/main/iot-terminal/src/terminal_main.py"
click node_node_iot_firmware "https://github.com/navyaaasingh/electionmanagement/blob/main/iot-terminal/firmware/main.cpp"
click node_node_iot_printer "https://github.com/navyaaasingh/electionmanagement/blob/main/iot-terminal/src/printer/vvpat_printer.py"
click node_node_ml_api "https://github.com/navyaaasingh/electionmanagement/blob/main/ml-service/api.py"
click node_node_ml_consumer "https://github.com/navyaaasingh/electionmanagement/blob/main/ml-service/kafka_consumer.py"
click node_node_ml_detector "https://github.com/navyaaasingh/electionmanagement/blob/main/ml-service/fraud_detector.py"
click node_node_ml_alerts "https://github.com/navyaaasingh/electionmanagement/blob/main/ml-service/alert_service.py"
click node_node_frontend_shell "https://github.com/navyaaasingh/electionmanagement/blob/main/frontend/src/App.jsx"
click node_node_frontend_shared "https://github.com/navyaaasingh/electionmanagement/blob/main/shared-ui/tokens.css"
click node_node_frontend_admin "https://github.com/navyaaasingh/electionmanagement/blob/main/admin-portal/src/pages/MonitoringDashboard.jsx"
click node_node_frontend_observer "https://github.com/navyaaasingh/electionmanagement/blob/main/observer-dashboard/src/App.jsx"
click node_node_frontend_voter "https://github.com/navyaaasingh/electionmanagement/blob/main/voter-ui/src/App.jsx"
click node_node_infra_k8s "https://github.com/navyaaasingh/electionmanagement/tree/main/infrastructure/kubernetes"
click node_node_infra_monitoring "https://github.com/navyaaasingh/electionmanagement/tree/main/infrastructure/monitoring"
click node_node_ops_scripts "https://github.com/navyaaasingh/electionmanagement/tree/main/scripts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_backend_server,node_node_backend_contexts,node_node_backend_middleware,node_node_backend_services,node_node_backend_models,node_node_backend_routes,node_node_backend_workers,node_node_ops_scripts toneBlue
class node_node_blockchain_contract,node_node_blockchain_network toneAmber
class node_node_iot_terminal,node_node_iot_firmware,node_node_iot_printer toneMint
class node_node_ml_api,node_node_ml_consumer,node_node_ml_detector,node_node_ml_alerts toneRose
class node_node_frontend_shell,node_node_frontend_shared,node_node_frontend_admin,node_node_frontend_observer,node_node_frontend_voter toneIndigo
class node_node_infra_k8s,node_node_infra_monitoring toneTeal
```

## Components

### Backend

Node/Express orchestration hub with domain contexts, policy controls, persistence, and workflow services.

- [API server](https://github.com/navyaaasingh/electionmanagement/blob/main/backend/src/server.js): express app · Main backend entrypoint wiring routes, middleware, contexts, and services. · `backend/src/server.js`
- [Contexts](https://github.com/navyaaasingh/electionmanagement/tree/main/backend/src/contexts): domain modules · Election, vote, and voter bounded-context services that organize core business logic. · `backend/src/contexts`
- [Policy guards](https://github.com/navyaaasingh/electionmanagement/tree/main/backend/src/middleware): middleware · Auth, CSRF, rate limiting, and MAC filtering controls. · `backend/src/middleware`
- [Core services](https://github.com/navyaaasingh/electionmanagement/tree/main/backend/src/services): service layer · Biometric auth, encryption, Fabric, IoT, Kafka, observability, results, ZKP, and saga coordination. · `backend/src/services`
- [Data models](https://github.com/navyaaasingh/electionmanagement/tree/main/backend/src/models): persistence models · Transactional, audit, outbox, and saga state for voters, elections, and voting workflows. · `backend/src/models`
- [API routes](https://github.com/navyaaasingh/electionmanagement/tree/main/backend/src/routes): http routes · Authentication, election, vote, terminal, results, audit, and operations endpoints. · `backend/src/routes`
- [Saga worker](https://github.com/navyaaasingh/electionmanagement/blob/main/backend/src/workers/reconciliationSagaWorker.js): background worker · Asynchronous reconciliation and recovery processor for long-running workflows. · `backend/src/workers/reconciliationSagaWorker.js`

### Blockchain

Hyperledger Fabric smart contract and network lifecycle for immutable vote recording.

- [Voting contract](https://github.com/navyaaasingh/electionmanagement/blob/main/blockchain/chaincode/voting/voting.go): fabric chaincode · Go smart contract enforcing vote validity and ledger writes. · `blockchain/chaincode/voting/voting.go`
- [Fabric network](https://github.com/navyaaasingh/electionmanagement/tree/main/blockchain/network): network config · Topology, crypto material, and lifecycle scripts for the blockchain network. · `blockchain/network`

### Edge terminal

IoT voting terminal firmware and runtime for disconnected or assisted voting sites.

- [Terminal runtime](https://github.com/navyaaasingh/electionmanagement/blob/main/iot-terminal/src/terminal_main.py): edge app · Python-side terminal control flow for device operation and voting interactions. · `iot-terminal/src/terminal_main.py`
- [Firmware](https://github.com/navyaaasingh/electionmanagement/blob/main/iot-terminal/firmware/main.cpp): esp32 cpp · Embedded firmware handling biometric input, offline cache, tamper detection, and networking. · `iot-terminal/firmware/main.cpp`
- [VVPAT printer](https://github.com/navyaaasingh/electionmanagement/blob/main/iot-terminal/src/printer/vvpat_printer.py): printer module · Paper verification output for voter-verified audit trail. · `iot-terminal/src/printer/vvpat_printer.py`

### ML service

Streaming fraud detection, alerting, and monitoring analytics.

- [ML API](https://github.com/navyaaasingh/electionmanagement/blob/main/ml-service/api.py): python api · Service entrypoint for fraud analysis and operational exposure. · `ml-service/api.py`
- [Stream consumer](https://github.com/navyaaasingh/electionmanagement/blob/main/ml-service/kafka_consumer.py): kafka consumer · Consumes event streams for telemetry, detection, and downstream processing. · `ml-service/kafka_consumer.py`
- [Fraud detector](https://github.com/navyaaasingh/electionmanagement/blob/main/ml-service/fraud_detector.py): anomaly engine · Ensemble fraud detection and monitoring logic. · `ml-service/fraud_detector.py`
- [Alerting](https://github.com/navyaaasingh/electionmanagement/blob/main/ml-service/alert_service.py): notification service · Produces fraud and monitoring alerts from analytic signals. · `ml-service/alert_service.py`

### Frontend surfaces

Role-based web UIs and shared client-side presentation layer.

- [Web shell](https://github.com/navyaaasingh/electionmanagement/blob/main/frontend/src/App.jsx): react app · Unified React/Vite client shell with shared APIs and role-specific pages. · `frontend/src/App.jsx`
- [Shared UI](https://github.com/navyaaasingh/electionmanagement/blob/main/shared-ui/tokens.css): client lib · Common socket, snapshot, and styling primitives used across web surfaces. · `shared-ui/tokens.css`
- [Admin portal](https://github.com/navyaaasingh/electionmanagement/blob/main/admin-portal/src/pages/MonitoringDashboard.jsx): react ui · Administrative dashboard for election operations and monitoring. · `admin-portal/src/pages/MonitoringDashboard.jsx`
- [Observer dashboard](https://github.com/navyaaasingh/electionmanagement/blob/main/observer-dashboard/src/App.jsx): react ui · Live observer surface for election visibility and status. · `observer-dashboard/src/App.jsx`
- [Voter UI](https://github.com/navyaaasingh/electionmanagement/blob/main/voter-ui/src/App.jsx): react ui · Voter-facing interface including biometric authentication. · `voter-ui/src/App.jsx`

### Infrastructure

Container, Kubernetes, and monitoring deployment assets.

- [Kubernetes](https://github.com/navyaaasingh/electionmanagement/tree/main/infrastructure/kubernetes): deployment manifests · Cluster manifests for backend, ML, databases, Kafka, ingress, secrets, and reconciliation worker. · `infrastructure/kubernetes`
- [Monitoring](https://github.com/navyaaasingh/electionmanagement/tree/main/infrastructure/monitoring): prometheus/grafana · Prometheus scrape config, alert rules, and Grafana dashboards. · `infrastructure/monitoring`

### Operations

Simulation, testing, and chaos scripts that validate the end-to-end election pipeline.

- [Ops scripts](https://github.com/navyaaasingh/electionmanagement/tree/main/scripts): validation scripts · Load tests, chaos drills, and simulation harnesses for end-to-end verification. · `scripts`

## Connections

- Voter UI → API server: auth/vote API · Voter actions are submitted to the backend for authentication and voting.
- Terminal runtime → API server: terminal API · The edge terminal exchanges operational and voting requests with the backend.
- API server → Policy guards: enforces · Requests pass through security and policy middleware.
- API server → Contexts: dispatches · Domain contexts implement vote, election, and voter workflow logic.
- API server → API routes: serves · The server exposes the REST surface through route modules.
- API server → Core services: coordinates · Core services handle encryption, Fabric, Kafka, WebSocket, and reconciliation.
- API server → Data models: persists · Business operations store transactional and audit state in the model layer.
- Core services → Voting contract: writes ledger · Fabric integration submits validated vote records to chaincode.
- Core services → Stream consumer: publishes telemetry · Kafka event production feeds downstream analytics and monitoring.
- Core services → Observer dashboard: live updates · WebSocket services push operational status to dashboards.
- Core services → Saga worker: reconciles · Saga coordination is completed by a background worker.
- Data models → Saga worker: async state · Outbox, dead-letter, and saga models support recoverable processing.
- Fabric network → Voting contract: hosts · The network config and scripts deploy and run the chaincode.
- Firmware → Terminal runtime: boots device · Embedded firmware provides the device-level runtime.
- Terminal runtime → VVPAT printer: prints receipt · The terminal produces voter verification output through the printer layer.
- Stream consumer → Fraud detector: analyzes · Streamed events are scored for anomalies and fraud patterns.
- Fraud detector → Alerting: triggers · Detections create alerts and monitoring events.
- ML API → Fraud detector: serves analysis · The API exposes fraud detection capabilities.
- Web shell → Shared UI: uses · Shared client-side tokens and socket helpers support the unified shell.
- Admin portal → API server: admin API · Admin and monitoring pages consume backend endpoints.
- Observer dashboard → API server: observer API · Observer dashboard reads election status and operational endpoints.
- Voter UI → API server: voter API · Voter UI exchanges account, election, and vote actions with the backend.
- Kubernetes → API server: deploys · Cluster manifests run the backend service.
- Kubernetes → ML API: deploys · Cluster manifests run the ML service.
- Kubernetes → Fabric network: orchestrates · Infrastructure includes blockchain-related runtime components.
- Kubernetes → Saga worker: deploys · The reconciliation worker is managed as a separate workload.
- Monitoring → Core services: observes · Prometheus and Grafana track backend health and workflow metrics.
- Ops scripts → API server: tests · Load and security scripts exercise the backend pipeline.
- Ops scripts → Stream consumer: simulates · Traffic simulation drives the fraud detection stream.
- Ops scripts → Fabric network: drills · Chaos drills validate blockchain and recovery behavior.
