2020-10-21 09:37:07 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2022-05-30 08:38:50 -05:00
|
|
|
"sort"
|
2020-10-21 09:37:07 -05:00
|
|
|
"strconv"
|
2022-05-30 08:38:50 -05:00
|
|
|
"strings"
|
2022-07-11 08:31:31 -05:00
|
|
|
"time"
|
2020-10-21 09:37:07 -05:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
2022-05-30 09:11:48 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/state"
|
2020-10-21 09:37:07 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2020-12-02 11:41:00 -06:00
|
|
|
"github.com/matrix-org/dendrite/setup"
|
2022-05-25 07:37:15 -05:00
|
|
|
"github.com/matrix-org/dendrite/setup/base"
|
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2020-10-21 09:37:07 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This is a utility for inspecting state snapshots and running state resolution
|
|
|
|
// against real snapshots in an actual database.
|
|
|
|
// It takes one or more state snapshot NIDs as arguments, along with a room version
|
|
|
|
// to use for unmarshalling events, and will produce resolved output.
|
|
|
|
//
|
|
|
|
// Usage: ./resolve-state --roomversion=version snapshot [snapshot ...]
|
|
|
|
// e.g. ./resolve-state --roomversion=5 1254 1235 1282
|
|
|
|
|
|
|
|
var roomVersion = flag.String("roomversion", "5", "the room version to parse events as")
|
2022-05-25 07:37:15 -05:00
|
|
|
var filterType = flag.String("filtertype", "", "the event types to filter on")
|
2022-06-13 04:16:30 -05:00
|
|
|
var difference = flag.Bool("difference", false, "whether to calculate the difference between snapshots")
|
2020-10-21 09:37:07 -05:00
|
|
|
|
2022-06-13 04:16:30 -05:00
|
|
|
// nolint:gocyclo
|
2020-10-21 09:37:07 -05:00
|
|
|
func main() {
|
|
|
|
ctx := context.Background()
|
|
|
|
cfg := setup.ParseFlags(true)
|
2022-05-25 07:37:15 -05:00
|
|
|
cfg.Logging = append(cfg.Logging[:0], config.LogrusHook{
|
|
|
|
Type: "std",
|
|
|
|
Level: "error",
|
|
|
|
})
|
2022-06-13 04:46:59 -05:00
|
|
|
cfg.ClientAPI.RegistrationDisabled = true
|
2023-02-14 05:47:47 -06:00
|
|
|
base := base.NewBaseDendrite(cfg, base.DisableMetrics)
|
2022-05-25 07:37:15 -05:00
|
|
|
args := flag.Args()
|
2020-10-21 09:37:07 -05:00
|
|
|
|
|
|
|
fmt.Println("Room version", *roomVersion)
|
|
|
|
|
|
|
|
snapshotNIDs := []types.StateSnapshotNID{}
|
|
|
|
for _, arg := range args {
|
|
|
|
if i, err := strconv.Atoi(arg); err == nil {
|
|
|
|
snapshotNIDs = append(snapshotNIDs, types.StateSnapshotNID(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Fetching", len(snapshotNIDs), "snapshot NIDs")
|
|
|
|
|
2022-07-11 08:31:31 -05:00
|
|
|
roomserverDB, err := storage.Open(
|
|
|
|
base, &cfg.RoomServer.Database,
|
|
|
|
caching.NewRistrettoCache(128*1024*1024, time.Hour, true),
|
|
|
|
)
|
2020-10-21 09:37:07 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-05-30 09:11:48 -05:00
|
|
|
stateres := state.NewStateResolution(roomserverDB, &types.RoomInfo{
|
|
|
|
RoomVersion: gomatrixserverlib.RoomVersion(*roomVersion),
|
|
|
|
})
|
2020-10-21 09:37:07 -05:00
|
|
|
|
2022-06-13 04:16:30 -05:00
|
|
|
if *difference {
|
|
|
|
if len(snapshotNIDs) != 2 {
|
|
|
|
panic("need exactly two state snapshot NIDs to calculate difference")
|
|
|
|
}
|
2022-06-13 04:46:59 -05:00
|
|
|
var removed, added []types.StateEntry
|
|
|
|
removed, added, err = stateres.DifferenceBetweeenStateSnapshots(ctx, snapshotNIDs[0], snapshotNIDs[1])
|
2022-06-13 04:16:30 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-09-05 08:17:04 -05:00
|
|
|
eventNIDMap := map[types.EventNID]struct{}{}
|
2022-06-13 04:16:30 -05:00
|
|
|
for _, entry := range append(removed, added...) {
|
2022-09-05 08:17:04 -05:00
|
|
|
eventNIDMap[entry.EventNID] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
eventNIDs := make([]types.EventNID, 0, len(eventNIDMap))
|
|
|
|
for eventNID := range eventNIDMap {
|
|
|
|
eventNIDs = append(eventNIDs, eventNID)
|
2022-06-13 04:16:30 -05:00
|
|
|
}
|
|
|
|
|
2022-06-13 04:46:59 -05:00
|
|
|
var eventEntries []types.Event
|
|
|
|
eventEntries, err = roomserverDB.Events(ctx, eventNIDs)
|
2022-06-13 04:16:30 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
events := make(map[types.EventNID]*gomatrixserverlib.Event, len(eventEntries))
|
|
|
|
for _, entry := range eventEntries {
|
|
|
|
events[entry.EventNID] = entry.Event
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(removed) > 0 {
|
|
|
|
fmt.Println("Removed:")
|
|
|
|
for _, r := range removed {
|
|
|
|
event := events[r.EventNID]
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Printf("* %s %s %q\n", event.EventID(), event.Type(), *event.StateKey())
|
|
|
|
fmt.Printf(" %s\n", string(event.Content()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(removed) > 0 && len(added) > 0 {
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(added) > 0 {
|
|
|
|
fmt.Println("Added:")
|
|
|
|
for _, a := range added {
|
|
|
|
event := events[a.EventNID]
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Printf("* %s %s %q\n", event.EventID(), event.Type(), *event.StateKey())
|
|
|
|
fmt.Printf(" %s\n", string(event.Content()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-30 09:11:48 -05:00
|
|
|
var stateEntries []types.StateEntry
|
|
|
|
for _, snapshotNID := range snapshotNIDs {
|
|
|
|
var entries []types.StateEntry
|
|
|
|
entries, err = stateres.LoadStateAtSnapshot(ctx, snapshotNID)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2020-10-21 09:37:07 -05:00
|
|
|
}
|
|
|
|
stateEntries = append(stateEntries, entries...)
|
|
|
|
}
|
|
|
|
|
2022-09-05 08:17:04 -05:00
|
|
|
eventNIDMap := map[types.EventNID]struct{}{}
|
2020-10-21 09:37:07 -05:00
|
|
|
for _, entry := range stateEntries {
|
2022-09-05 08:17:04 -05:00
|
|
|
eventNIDMap[entry.EventNID] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
eventNIDs := make([]types.EventNID, 0, len(eventNIDMap))
|
|
|
|
for eventNID := range eventNIDMap {
|
|
|
|
eventNIDs = append(eventNIDs, eventNID)
|
2020-10-21 09:37:07 -05:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:17:04 -05:00
|
|
|
fmt.Println("Fetching", len(eventNIDMap), "state events")
|
2020-10-21 09:37:07 -05:00
|
|
|
eventEntries, err := roomserverDB.Events(ctx, eventNIDs)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
authEventIDMap := make(map[string]struct{})
|
2020-11-16 09:44:53 -06:00
|
|
|
events := make([]*gomatrixserverlib.Event, len(eventEntries))
|
2020-10-21 09:37:07 -05:00
|
|
|
for i := range eventEntries {
|
2020-11-16 09:44:53 -06:00
|
|
|
events[i] = eventEntries[i].Event
|
2020-10-21 09:37:07 -05:00
|
|
|
for _, authEventID := range eventEntries[i].AuthEventIDs() {
|
|
|
|
authEventIDMap[authEventID] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
authEventIDs := make([]string, 0, len(authEventIDMap))
|
|
|
|
for authEventID := range authEventIDMap {
|
|
|
|
authEventIDs = append(authEventIDs, authEventID)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Fetching", len(authEventIDs), "auth events")
|
|
|
|
authEventEntries, err := roomserverDB.EventsFromIDs(ctx, authEventIDs)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-11-16 09:44:53 -06:00
|
|
|
authEvents := make([]*gomatrixserverlib.Event, len(authEventEntries))
|
2020-10-21 09:37:07 -05:00
|
|
|
for i := range authEventEntries {
|
2020-11-16 09:44:53 -06:00
|
|
|
authEvents[i] = authEventEntries[i].Event
|
2020-10-21 09:37:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Resolving state")
|
2022-05-30 08:38:50 -05:00
|
|
|
var resolved Events
|
|
|
|
resolved, err = gomatrixserverlib.ResolveConflicts(
|
2020-10-21 09:37:07 -05:00
|
|
|
gomatrixserverlib.RoomVersion(*roomVersion),
|
|
|
|
events,
|
|
|
|
authEvents,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Resolved state contains", len(resolved), "events")
|
2022-05-30 08:38:50 -05:00
|
|
|
sort.Sort(resolved)
|
2022-05-25 07:37:15 -05:00
|
|
|
filteringEventType := *filterType
|
|
|
|
count := 0
|
2020-10-21 09:37:07 -05:00
|
|
|
for _, event := range resolved {
|
2022-05-25 07:37:15 -05:00
|
|
|
if filteringEventType != "" && event.Type() != filteringEventType {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
count++
|
2020-10-21 09:37:07 -05:00
|
|
|
fmt.Println()
|
|
|
|
fmt.Printf("* %s %s %q\n", event.EventID(), event.Type(), *event.StateKey())
|
|
|
|
fmt.Printf(" %s\n", string(event.Content()))
|
|
|
|
}
|
2022-05-25 07:37:15 -05:00
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Println("Returned", count, "state events after filtering")
|
2020-10-21 09:37:07 -05:00
|
|
|
}
|
2022-05-30 08:38:50 -05:00
|
|
|
|
|
|
|
type Events []*gomatrixserverlib.Event
|
|
|
|
|
|
|
|
func (e Events) Len() int {
|
|
|
|
return len(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Events) Swap(i, j int) {
|
|
|
|
e[i], e[j] = e[j], e[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Events) Less(i, j int) bool {
|
|
|
|
typeDelta := strings.Compare(e[i].Type(), e[j].Type())
|
|
|
|
if typeDelta < 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if typeDelta > 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
stateKeyDelta := strings.Compare(*e[i].StateKey(), *e[j].StateKey())
|
|
|
|
return stateKeyDelta < 0
|
|
|
|
}
|