v2

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2026 License: MPL-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package v2 implements the Matrix state resolution algorithm for rooms version 2 and later. It implements both state resolution version 2 (room version 2 through 11) and version 2.1 (room version 12).

Due to limitations with the Mautrix PDU struct, only room version 3 and later is supported.

Example

See: https://codeberg.org/timedout/venator/src/branch/dev/stateres/README.md#example

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidResolutionOptions = errors.New("invalid resolution options")
	ErrNoCreateEvent            = errors.New("room create event did not appear in unconflicted state map")
	ErrMissingCreateEvent       = errors.New("failed to fetch room create event")
	ErrBadCreateEvent           = errors.New("failed to load room create event")
)

Functions

func AuthChain

func AuthChain(ctx context.Context, eventID id.EventID, fetchEvent FetchEventFunc) []id.EventID

AuthChain recursively calculates the auth chain for a given event (i.e. every auth event leading up to it).

See: https://spec.matrix.org/v1.17/rooms/v12/#definitions.

func AuthDifference

func AuthDifference(ctx context.Context, eventIDs []id.EventID, fetchEvent FetchEventFunc) []id.EventID

AuthDifference calculates the difference in auth chains between a set of events.

In essence, this calculates all auth events that aren't mutual to every event in the set. It calculates auth chains concurrently.

See: https://spec.matrix.org/v1.17/rooms/v12/#definitions.

func ConflictedStateSubgraph

func ConflictedStateSubgraph(
	ctx context.Context,
	conflictedStateSet sets.Set[id.EventID],
	fetchEvent FetchEventFunc,
) sets.Set[id.EventID]

ConflictedStateSubgraph computes the subgraph of auth events that connect events in the conflicted state set. Starting from an event in the conflicted state set and following auth_events edges may lead to another event in the conflicted state set. The union of all such paths between any pair of events in the conflicted state set (including endpoints) forms a subgraph of the original auth_event graph, called the conflicted state subgraph.

See: https://spec.matrix.org/v1.17/rooms/v12/#definitions.

func IsPowerEvent

func IsPowerEvent(evt *pdu.PDU) bool

IsPowerEvent determines whether the given event is a "power event".

The spec defines a power event as any of the following:

  • m.room.join_rules
  • m.room.power_levels
  • m.room.member (where membership is leave or ban, and the sender differs from the state key)

func LexicographicalTopologicalSort

func LexicographicalTopologicalSort(graph map[id.EventID]*pdu.PDU, tree TreeKind) []id.EventID

LexicographicalTopologicalSort performs a reverse topological sort with lexicographical ordering. That is, the tie-break is based on solely the event IDs themselves. Most callers will prefer ReverseTopologicalPowerSort, which also accounts for power levels.

func MainlineOrdering

func MainlineOrdering(
	ctx context.Context,
	eventIDs []id.EventID,
	powerLevelEvent *pdu.PDU,
	opts *ResolveOpts,
) []id.EventID

MainlineOrdering sorts the given slice of event IDs based on their mainline position.

See also: https://spec.matrix.org/v1.17/rooms/v12/#definitions

func ReverseTopologicalPowerSort

func ReverseTopologicalPowerSort(
	ctx context.Context,
	roomVersion id.RoomVersion,
	eventIDs []id.EventID,
	fullConflictedSet sets.Set[id.EventID],
	fetchEvent FetchEventFunc,
) []id.EventID

ReverseTopologicalPowerSort sorts the incoming event IDs based on their reversed topological power ordering.

See: https://spec.matrix.org/v1.18/rooms/v12/#definitions

See also (internal): reverseTopologicalSort.

func ReverseTopologicalSort

func ReverseTopologicalSort(
	graph map[id.EventID]*pdu.PDU,
	sortWithKey func(a, b id.EventID) int,
	tree TreeKind,
) []id.EventID

ReverseTopologicalSort performs a reverse topological sort on the given graph (returns dependencies before their dependents).

The graph provided to this function is not mutated. Which tree (auth or prev) is walked can be changed by supplying a different TreeKind.

func SplitConflicts

func SplitConflicts(incoming []StateMap) (StateMap, StateSet)

SplitConflicts separates the conflicting events in the given incoming state maps, returning (non-conflicting, conflicting). See also: StateMap, StateSet.

func TopologicalSort

func TopologicalSort(
	graph map[id.EventID]*pdu.PDU,
	sortWithKey func(a, b id.EventID) int,
	tree TreeKind,
) []id.EventID

TopologicalSort performs a topological sort on the given graph (returns dependants before their dependencies). This literally just calls slices.Reverse on the result of reverseTopologicalSort.

The graph provided to this function is not mutated. Which tree (auth or prev) is walked can be changed by supplying a different TreeKind.

Types

type EventMap deprecated

type EventMap map[id.EventID]*pdu.PDU

Deprecated: unused type.

type FetchEventFunc

type FetchEventFunc func(context.Context, id.EventID) (*pdu.PDU, error)

FetchEventFunc is a generic function signature that is used to fetch events by their ID. If the event cannot be found, nil should be returned. The Context provided is the same one passed to the Resolve function.

Important: the function really should cache its results. The same call may be made potentially hundreds of times per resolve task.

type ResolveOpts

type ResolveOpts struct {
	RoomVersion     id.RoomVersion
	RoomID          id.RoomID
	FetchEvent      FetchEventFunc
	FetchSigningKey pdu.GetKeyFunc
}

type StateMap

type StateMap map[pdu.StateKey]id.EventID

StateMap is a type alias to a map of {(type, key): V}, where V is the ID of a state event.

func IterativeAuthChecks

func IterativeAuthChecks(ctx context.Context, eventIDs []id.EventID, base StateMap, opts *ResolveOpts) StateMap

IterativeAuthChecks runs a set of events through the iterative auth checks defined in the spec: https://spec.matrix.org/v1.17/rooms/v12/#definitions.

func Resolve

func Resolve(ctx context.Context, incoming []StateMap, opts ResolveOpts) (final StateMap, err error)

Resolve runs the state resolution v2 or v2.1 algorithm against the provided incoming states.

Spec: https://spec.matrix.org/v1.17/rooms/v12/#state-resolution

Errors

Resolve typically doesn't resolve errors unless something is very wrong. Here's an exhaustive list:

ErrInvalidResolutionOptions: An option in `opts` was wrong in some capacity.

ErrNoCreateEvent: There were conflicting events, but `m.room.create` was not present in the unconflicted set.

ErrMissingCreateEvent: The `m.room.create` event could not be fetched by `opts.FetchEvent`.

ErrBadCreateEvent: The `m.room.create` event was catastrophically invalid or malformed somehow.

Logging

Resolve will emit logs via the standard zerolog.Logger returned by zerolog.Ctx on the ctx. All logs will be at debug or trace, and can consequently be used for performance measurements. In production, it is recommended a logger from zerolog.Nop is provided in order to avoid wasting compute cycles waiting for logging to flush.

type StateSet

type StateSet map[pdu.StateKey]sets.Set[id.EventID]

StateSet represents a map of {(type, key): Set[V]}, where V is a set of event IDs that belong to state events. Generally this is used to represent state keys with multiple, conflicting state events as values.

Each map key should always have more than one entry - otherwise, the sole value should go in a StateMap instead.

func (StateSet) Flattened

func (ss StateSet) Flattened() sets.Set[id.EventID]

type TreeKind

type TreeKind uint8

TreeKind indicates what type of tree should be walked in ReverseTopologicalSort.

const (
	// AuthEventTree instructs ReverseTopologicalSort to walk the `auth_events` tree.
	AuthEventTree TreeKind = iota
	// PrevEventTree instructs ReverseTopologicalSort to walk the `prev_events` tree.
	PrevEventTree
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL