ConvergeLedger.Storage.Store (Converge Ledger v0.1.2)

View Source

Mnesia-backed append-only context store.

This store is derivative, not authoritative:

  • It remembers what already happened
  • It never decides or coordinates convergence
  • It may lose data (engine can regenerate)
  • It never mutates or rewrites history

The Rust engine remains the single semantic authority.

Integrity Features

  • Merkle roots: Snapshots include a Merkle root for tamper detection
  • Lamport clocks: Optional causal ordering of entries
  • Content hashes: Optional integrity verification per entry

Summary

Functions

Appends an entry to the context.

Gets the current Lamport clock time for a context.

Gets the current sequence number for a context.

Gets entries from a context.

Loads a context from a snapshot blob.

Creates a snapshot of the entire context.

Functions

append(context_id, key, payload, metadata \\ %{})

Appends an entry to the context.

Each entry is assigned:

  • A sequence number (monotonically increasing per context)
  • A Lamport clock timestamp (for causal ordering across contexts)
  • A content hash (SHA-256 for integrity verification)

Returns {:ok, entry} with the full entry including assigned sequence number, or {:error, reason} on failure.

append_with_received_time(context_id, key, payload, received_lamport_time, metadata \\ %{})

Appends an entry with a received Lamport timestamp.

Use this when receiving entries from another context/node to maintain causal ordering. The local clock will be updated to max(local, received) + 1.

Returns {:ok, entry} or {:error, reason}.

current_lamport_time(context_id)

Gets the current Lamport clock time for a context.

Returns {:ok, lamport_time} or {:error, reason}.

current_sequence(context_id)

Gets the current sequence number for a context.

Returns {:ok, sequence} or {:error, reason}.

get(context_id, opts \\ [])

Gets entries from a context.

Options:

  • :key - filter by context key
  • :after_sequence - only entries with sequence > this value
  • :limit - maximum number of entries to return

Returns {:ok, entries, latest_sequence} or {:error, reason}.

load(context_id, snapshot_blob, opts \\ [])

Loads a context from a snapshot blob.

If fail_if_exists is true, returns an error if the context already has entries.

Options:

  • :verify_integrity - If true, verifies Merkle root before loading (default: true)

Returns {:ok, entries_restored, latest_sequence} or {:error, reason}.

snapshot(context_id)

Creates a snapshot of the entire context.

Returns {:ok, snapshot_blob, sequence, metadata} or {:error, reason}.