Skip to content
Jiwon edited this page Apr 28, 2025 · 5 revisions

How Mimir Works

Mimir loads data from the Nine Chronicles blockchain into MongoDB, making it easy to query through GraphQL (GQL). The Mimir.Worker module handles data storage in MongoDB, while Mimir provides a GraphQL API to access that data.

image By indexing data in MongoDB, Mimir enables fast queries for information like rankings and shop data, which would otherwise be difficult to retrieve directly from the Nine Chronicles blockchain (Headless). image ## Worker In order to query data, it must first be stored. Mimir's Worker updates MongoDB using two main approaches:

Updates via Diff Queries

Updates via Transaction (Tx) Analysis

The first method is suitable for states with separate Account Addresses such as Avatar or WorldInformation. The second method is used for legacy states that exist under LegacyAddress.

Diff Worker

Headless provides an accountDiff query, which lists changes between two block indices.

image Since it's impossible to monitor all state changes on the blockchain in real time, Diff Workers use this query to list the addresses that need to be updated. Workers are implemented by extending [BaseDiffHandler](https://github.com/planetarium/mimir/blob/main/Mimir.Worker/Handler/BaseDiffHandler.cs), such as [AgentStateHandler](https://github.com/planetarium/mimir/blob/main/Mimir.Worker/Handler/AgentStateHandler.cs).

The update process follows this flow:

flowchart TD
    A[Run diff query to list addresses to update] --> B[Decode Bencodex values into objects]
    B --> C[Convert objects to JSON]
    C --> D[Store JSON data in MongoDB]
Loading

Action Worker

Action Workers also aim to store state data in MongoDB. They are implemented by extending BaseActionHandler, like ItemSlotStateHandler.

For legacy states, it’s not possible to precisely list all changes using a diff. Therefore, Action Workers query the transaction list, inspect the actions executed within each transaction, and update any states that could potentially have been affected.

Initializer

Workers can update states in real-time from the moment they start running. However, states from earlier blocks would not exist in MongoDB initially. To address this, Mimir uses SnapshotInitializer to iterate through all existing states on the blockchain once and store them in MongoDB.

Worker Summary

Mimir’s operation is simple: First, the Initializer loads all existing states into MongoDB. Then, Workers keep updating the database in real-time to provide up-to-date data.

GraphQL

With states now stored in MongoDB, Mimir exposes them through GraphQL. However, the Nine Chronicles blockchain has a vast number of state types, and manually creating a GraphQL schema for each one would be highly time-consuming.

To solve this, Mimir uses Hot Chocolate to automatically generate schemas based on state models. Directly using the original state models in Lib9c isn't possible, so Mimir defines its own models tailored for Hot Chocolate.

You can find these models here: https://github.com/planetarium/mimir/blob/main/Lib9c.Models/States

With these models, state data can be queried easily through GQL: https://github.com/planetarium/mimir/blob/main/Mimir/GraphQL/Queries/Query.cs

Clone this wiki locally