# Worker Node Process Diagrams

## Reaching Consensus

{% @mermaid/diagram content="sequenceDiagram
    participant A as External event data
    participant B as DDHub
    participant G as Client GW (DDHub)
    participant C as Worker Node 1
    participant D as Worker Node ...n
    Participant E as Voting Contract
    Participant H as Event Listener Node
    Participant F as Energy Web Chain
    Note over G: Every x minutes
    G->>A: Receives data and encrypts
    G->>B: Pushes encrypted data
    C->>B: Authenticates using DID
    D->>B: Authenticates using DID
    C->>B: Fetches encrypted input data
    D->>B: Fetches encrypted input data
    C->>C: Decrypts data using DID
    D->>D: Decrypts data using DID
    C->>C: Executes business logic & derives root hash
    D->>D: Executes business logic & derives root hash
    C->>E: Submits merkle tree root hash result
    D->>E: Submits merkle tree root hash result
    E->>E: Reaches consensus
    E->>F: Notarize root hash on-chain
    H->>F: Listens for Events
    H->>B: Passes notarized root hash" %}



## Voting Manager

{% @mermaid/diagram content="sequenceDiagram
participant VotingManager
participant Worker
participant Overseer
participant VotingContract
participant Data Storage

VotingManager ->> VotingContract: add workers
loop Business Logic
  Worker->>Data Storage: get input data
  Data Storage-->>Worker: 

  Worker->>Worker: Logic Execution
  
  Worker->>VotingContract: send vote
end

loop VotingResults
  Overseer->>VotingContract: get Result events
  VotingContract-->>Overseer: 
  Overseer->>Data Storage: save voting results
end" %}

## Caching Results

{% @mermaid/diagram content="sequenceDiagram
    participant A as DDHub
    participant G as Worker Node
    Participant E as Cache
    Participant D as Energy Web Chain
    Participant F as Reporting Backend
    E->>D: Listens for Events
    D->>E: Passes notarized root hash
    G->>G: Encrypts result data with root hash subject
    G->>A: Sends encrypted result data
    E->>A: Authenticates using DID/key
    E->>A: Fetches encrypted result data
    E->>E: Decrypts result data using DID
    F->>E: Fetches decrypted result data" %}
