2020-09-02 18:13:15 +02:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-09-02 14:47:31 +02:00
|
|
|
package perform
|
2020-04-28 12:46:47 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-02 14:47:31 +02:00
|
|
|
"fmt"
|
2020-04-28 12:46:47 +02:00
|
|
|
|
2022-08-05 11:12:41 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 16:50:33 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-08-05 11:12:41 +02:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2021-11-24 11:45:23 +01:00
|
|
|
federationAPI "github.com/matrix-org/dendrite/federationapi/api"
|
2020-09-02 14:47:31 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2020-04-29 19:41:45 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/auth"
|
2020-09-02 14:47:31 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/helpers"
|
2023-03-03 14:03:17 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/state"
|
2020-04-28 12:46:47 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
)
|
|
|
|
|
2020-10-06 19:09:02 +02:00
|
|
|
// the max number of servers to backfill from per request. If this is too low we may fail to backfill when
|
|
|
|
// we could've from another server. If this is too high we may take far too long to successfully backfill
|
|
|
|
// as we try dead servers.
|
|
|
|
const maxBackfillServers = 5
|
|
|
|
|
2020-09-02 14:47:31 +02:00
|
|
|
type Backfiller struct {
|
2023-04-19 16:50:33 +02:00
|
|
|
IsLocalServerName func(spec.ServerName) bool
|
2022-11-15 16:05:23 +01:00
|
|
|
DB storage.Database
|
|
|
|
FSAPI federationAPI.RoomserverFederationAPI
|
|
|
|
KeyRing gomatrixserverlib.JSONVerifier
|
2023-06-14 16:23:46 +02:00
|
|
|
Querier api.QuerySenderIDAPI
|
2020-10-06 19:09:02 +02:00
|
|
|
|
|
|
|
// The servers which should be preferred above other servers when backfilling
|
2023-04-19 16:50:33 +02:00
|
|
|
PreferServers []spec.ServerName
|
2020-09-02 14:47:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PerformBackfill implements api.RoomServerQueryAPI
|
|
|
|
func (r *Backfiller) PerformBackfill(
|
|
|
|
ctx context.Context,
|
|
|
|
request *api.PerformBackfillRequest,
|
|
|
|
response *api.PerformBackfillResponse,
|
|
|
|
) error {
|
|
|
|
// if we are requesting the backfill then we need to do a federation hit
|
|
|
|
// TODO: we could be more sensible and fetch as many events we already have then request the rest
|
|
|
|
// which is what the syncapi does already.
|
2022-11-15 16:05:23 +01:00
|
|
|
if r.IsLocalServerName(request.ServerName) {
|
2020-09-02 14:47:31 +02:00
|
|
|
return r.backfillViaFederation(ctx, request, response)
|
|
|
|
}
|
|
|
|
// someone else is requesting the backfill, try to service their request.
|
|
|
|
var err error
|
|
|
|
var front []string
|
|
|
|
|
|
|
|
// The limit defines the maximum number of events to retrieve, so it also
|
|
|
|
// defines the highest number of elements in the map below.
|
|
|
|
visited := make(map[string]bool, request.Limit)
|
|
|
|
|
|
|
|
// this will include these events which is what we want
|
|
|
|
front = request.PrevEventIDs()
|
|
|
|
|
|
|
|
info, err := r.DB.RoomInfo(ctx, request.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-01 16:29:19 +02:00
|
|
|
if info == nil || info.IsStub() {
|
2020-09-02 14:47:31 +02:00
|
|
|
return fmt.Errorf("PerformBackfill: missing room info for room %s", request.RoomID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan the event tree for events to send back.
|
2023-06-14 16:23:46 +02:00
|
|
|
resultNIDs, redactEventIDs, err := helpers.ScanEventTree(ctx, r.DB, info, front, visited, request.Limit, request.ServerName, r.Querier)
|
2020-09-02 14:47:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-27 16:52:32 +01:00
|
|
|
// Retrieve events from the list that was filled previously. If we fail to get
|
|
|
|
// events from the database then attempt once to get them from federation instead.
|
2023-05-02 16:03:16 +02:00
|
|
|
var loadedEvents []gomatrixserverlib.PDU
|
2023-03-01 17:06:47 +01:00
|
|
|
loadedEvents, err = helpers.LoadEvents(ctx, r.DB, info, resultNIDs)
|
2020-09-02 14:47:31 +02:00
|
|
|
if err != nil {
|
2022-01-27 16:52:32 +01:00
|
|
|
if _, ok := err.(types.MissingEventError); ok {
|
|
|
|
return r.backfillViaFederation(ctx, request, response)
|
|
|
|
}
|
2020-09-02 14:47:31 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, event := range loadedEvents {
|
2022-10-11 16:04:02 +02:00
|
|
|
if _, ok := redactEventIDs[event.EventID()]; ok {
|
|
|
|
event.Redact()
|
|
|
|
}
|
2023-05-02 16:03:16 +02:00
|
|
|
response.Events = append(response.Events, &types.HeaderedEvent{PDU: event})
|
2020-09-02 14:47:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Backfiller) backfillViaFederation(ctx context.Context, req *api.PerformBackfillRequest, res *api.PerformBackfillResponse) error {
|
|
|
|
info, err := r.DB.RoomInfo(ctx, req.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-01 16:29:19 +02:00
|
|
|
if info == nil || info.IsStub() {
|
2020-09-02 14:47:31 +02:00
|
|
|
return fmt.Errorf("backfillViaFederation: missing room info for room %s", req.RoomID)
|
|
|
|
}
|
2023-06-14 16:23:46 +02:00
|
|
|
requester := newBackfillRequester(r.DB, r.FSAPI, r.Querier, req.VirtualHost, r.IsLocalServerName, req.BackwardsExtremities, r.PreferServers)
|
2020-09-02 14:47:31 +02:00
|
|
|
// Request 100 items regardless of what the query asks for.
|
|
|
|
// We don't want to go much higher than this.
|
|
|
|
// We can't honour exactly the limit as some sytests rely on requesting more for tests to pass
|
|
|
|
// (so we don't need to hit /state_ids which the test has no listener for)
|
|
|
|
// Specifically the test "Outbound federation can backfill events"
|
|
|
|
events, err := gomatrixserverlib.RequestBackfill(
|
2022-11-15 16:05:23 +01:00
|
|
|
ctx, req.VirtualHost, requester,
|
2023-06-14 16:23:46 +02:00
|
|
|
r.KeyRing, req.RoomID, info.RoomVersion, req.PrevEventIDs(), 100, func(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
|
|
|
return r.Querier.QueryUserIDForSender(ctx, roomID, senderID)
|
2023-06-06 22:55:18 +02:00
|
|
|
},
|
2022-11-15 16:05:23 +01:00
|
|
|
)
|
2023-01-05 09:24:00 +01:00
|
|
|
// Only return an error if we really couldn't get any events.
|
|
|
|
if err != nil && len(events) == 0 {
|
2022-11-15 16:05:23 +01:00
|
|
|
logrus.WithError(err).Errorf("gomatrixserverlib.RequestBackfill failed")
|
2020-09-02 14:47:31 +02:00
|
|
|
return err
|
|
|
|
}
|
2023-01-05 09:24:00 +01:00
|
|
|
// If we got an error but still got events, that's fine, because a server might have returned a 404 (or something)
|
|
|
|
// but other servers could provide the missing event.
|
|
|
|
logrus.WithError(err).WithField("room_id", req.RoomID).Infof("backfilled %d events", len(events))
|
2020-09-02 14:47:31 +02:00
|
|
|
|
|
|
|
// persist these new events - auth checks have already been done
|
2023-06-14 16:23:46 +02:00
|
|
|
roomNID, backfilledEventMap := persistEvents(ctx, r.DB, r.Querier, events)
|
2020-09-02 14:47:31 +02:00
|
|
|
|
|
|
|
for _, ev := range backfilledEventMap {
|
|
|
|
// now add state for these events
|
|
|
|
stateIDs, ok := requester.eventIDToBeforeStateIDs[ev.EventID()]
|
|
|
|
if !ok {
|
|
|
|
// this should be impossible as all events returned must have pass Step 5 of the PDU checks
|
|
|
|
// which requires a list of state IDs.
|
|
|
|
logrus.WithError(err).WithField("event_id", ev.EventID()).Error("backfillViaFederation: failed to find state IDs for event which passed auth checks")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var entries []types.StateEntry
|
2022-08-18 18:06:13 +02:00
|
|
|
if entries, err = r.DB.StateEntriesForEventIDs(ctx, stateIDs, true); err != nil {
|
2020-09-02 14:47:31 +02:00
|
|
|
// attempt to fetch the missing events
|
2022-11-15 16:05:23 +01:00
|
|
|
r.fetchAndStoreMissingEvents(ctx, info.RoomVersion, requester, stateIDs, req.VirtualHost)
|
2020-09-02 14:47:31 +02:00
|
|
|
// try again
|
2022-08-18 18:06:13 +02:00
|
|
|
entries, err = r.DB.StateEntriesForEventIDs(ctx, stateIDs, true)
|
2020-09-02 14:47:31 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).WithField("event_id", ev.EventID()).Error("backfillViaFederation: failed to get state entries for event")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var beforeStateSnapshotNID types.StateSnapshotNID
|
|
|
|
if beforeStateSnapshotNID, err = r.DB.AddState(ctx, roomNID, nil, entries); err != nil {
|
|
|
|
logrus.WithError(err).WithField("event_id", ev.EventID()).Error("backfillViaFederation: failed to persist state entries to get snapshot nid")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = r.DB.SetState(ctx, ev.EventNID, beforeStateSnapshotNID); err != nil {
|
|
|
|
logrus.WithError(err).WithField("event_id", ev.EventID()).Error("backfillViaFederation: failed to persist snapshot nid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: update backwards extremities, as that should be moved from syncapi to roomserver at some point.
|
|
|
|
|
2023-04-27 13:54:20 +02:00
|
|
|
res.Events = make([]*types.HeaderedEvent, len(events))
|
|
|
|
for i := range events {
|
2023-05-02 16:03:16 +02:00
|
|
|
res.Events[i] = &types.HeaderedEvent{PDU: events[i]}
|
2023-04-27 13:54:20 +02:00
|
|
|
}
|
2022-08-19 11:04:26 +02:00
|
|
|
res.HistoryVisibility = requester.historyVisiblity
|
2020-09-02 14:47:31 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchAndStoreMissingEvents does a best-effort fetch and store of missing events specified in stateIDs. Returns no error as it is just
|
|
|
|
// best effort.
|
|
|
|
func (r *Backfiller) fetchAndStoreMissingEvents(ctx context.Context, roomVer gomatrixserverlib.RoomVersion,
|
2023-04-19 16:50:33 +02:00
|
|
|
backfillRequester *backfillRequester, stateIDs []string, virtualHost spec.ServerName) {
|
2020-09-02 14:47:31 +02:00
|
|
|
|
|
|
|
servers := backfillRequester.servers
|
|
|
|
|
|
|
|
// work out which are missing
|
|
|
|
nidMap, err := r.DB.EventNIDs(ctx, stateIDs)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Warn("cannot query missing events")
|
|
|
|
return
|
|
|
|
}
|
2023-04-27 13:54:20 +02:00
|
|
|
missingMap := make(map[string]*types.HeaderedEvent) // id -> event
|
2020-09-02 14:47:31 +02:00
|
|
|
for _, id := range stateIDs {
|
|
|
|
if _, ok := nidMap[id]; !ok {
|
|
|
|
missingMap[id] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
util.GetLogger(ctx).Infof("Fetching %d missing state events (from %d possible servers)", len(missingMap), len(servers))
|
|
|
|
|
|
|
|
// fetch the events from federation. Loop the servers first so if we find one that works we stick with them
|
|
|
|
for _, srv := range servers {
|
|
|
|
for id, ev := range missingMap {
|
|
|
|
if ev != nil {
|
|
|
|
continue // already found
|
|
|
|
}
|
|
|
|
logger := util.GetLogger(ctx).WithField("server", srv).WithField("event_id", id)
|
2022-11-15 16:05:23 +01:00
|
|
|
res, err := r.FSAPI.GetEvent(ctx, virtualHost, srv, id)
|
2020-09-02 14:47:31 +02:00
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Warn("failed to get event from server")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
loader := gomatrixserverlib.NewEventsLoader(roomVer, r.KeyRing, backfillRequester, backfillRequester.ProvideEvents, false)
|
2023-06-14 16:23:46 +02:00
|
|
|
result, err := loader.LoadAndVerify(ctx, res.PDUs, gomatrixserverlib.TopologicalOrderByPrevEvents, func(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
|
|
|
return r.Querier.QueryUserIDForSender(ctx, roomID, senderID)
|
2023-06-06 22:55:18 +02:00
|
|
|
})
|
2020-09-02 14:47:31 +02:00
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Warn("failed to load and verify event")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
logger.Infof("returned %d PDUs which made events %+v", len(res.PDUs), result)
|
|
|
|
for _, res := range result {
|
2022-07-01 11:49:26 +02:00
|
|
|
switch err := res.Error.(type) {
|
|
|
|
case nil:
|
|
|
|
case gomatrixserverlib.SignatureErr:
|
|
|
|
// The signature of the event might not be valid anymore, for example if
|
|
|
|
// the key ID was reused with a different signature.
|
|
|
|
logger.WithError(err).Errorf("event failed PDU checks, storing anyway")
|
|
|
|
case gomatrixserverlib.AuthChainErr, gomatrixserverlib.AuthRulesErr:
|
|
|
|
logger.WithError(err).Warn("event failed PDU checks")
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
logger.WithError(err).Warn("event failed PDU checks")
|
2020-09-02 14:47:31 +02:00
|
|
|
continue
|
|
|
|
}
|
2023-05-02 16:03:16 +02:00
|
|
|
missingMap[id] = &types.HeaderedEvent{PDU: res.Event}
|
2020-09-02 14:47:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-02 16:03:16 +02:00
|
|
|
var newEvents []gomatrixserverlib.PDU
|
2020-09-02 14:47:31 +02:00
|
|
|
for _, ev := range missingMap {
|
|
|
|
if ev != nil {
|
2023-05-02 16:03:16 +02:00
|
|
|
newEvents = append(newEvents, ev.PDU)
|
2020-09-02 14:47:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
util.GetLogger(ctx).Infof("Persisting %d new events", len(newEvents))
|
2023-06-14 16:23:46 +02:00
|
|
|
persistEvents(ctx, r.DB, r.Querier, newEvents)
|
2020-09-02 14:47:31 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 12:46:47 +02:00
|
|
|
// backfillRequester implements gomatrixserverlib.BackfillRequester
|
|
|
|
type backfillRequester struct {
|
2022-11-15 16:05:23 +01:00
|
|
|
db storage.Database
|
|
|
|
fsAPI federationAPI.RoomserverFederationAPI
|
2023-06-14 16:23:46 +02:00
|
|
|
querier api.QuerySenderIDAPI
|
2023-04-19 16:50:33 +02:00
|
|
|
virtualHost spec.ServerName
|
|
|
|
isLocalServerName func(spec.ServerName) bool
|
|
|
|
preferServer map[spec.ServerName]bool
|
2022-11-15 16:05:23 +01:00
|
|
|
bwExtrems map[string][]string
|
2020-04-28 12:46:47 +02:00
|
|
|
|
|
|
|
// per-request state
|
2023-04-19 16:50:33 +02:00
|
|
|
servers []spec.ServerName
|
2020-04-28 12:46:47 +02:00
|
|
|
eventIDToBeforeStateIDs map[string][]string
|
2023-04-27 17:35:19 +02:00
|
|
|
eventIDMap map[string]gomatrixserverlib.PDU
|
2022-08-19 11:04:26 +02:00
|
|
|
historyVisiblity gomatrixserverlib.HistoryVisibility
|
2023-02-24 09:40:20 +01:00
|
|
|
roomInfo types.RoomInfo
|
2020-04-28 12:46:47 +02:00
|
|
|
}
|
|
|
|
|
2020-10-06 19:09:02 +02:00
|
|
|
func newBackfillRequester(
|
2022-11-15 16:05:23 +01:00
|
|
|
db storage.Database, fsAPI federationAPI.RoomserverFederationAPI,
|
2023-06-14 16:23:46 +02:00
|
|
|
querier api.QuerySenderIDAPI,
|
2023-04-19 16:50:33 +02:00
|
|
|
virtualHost spec.ServerName,
|
|
|
|
isLocalServerName func(spec.ServerName) bool,
|
|
|
|
bwExtrems map[string][]string, preferServers []spec.ServerName,
|
2020-10-06 19:09:02 +02:00
|
|
|
) *backfillRequester {
|
2023-04-19 16:50:33 +02:00
|
|
|
preferServer := make(map[spec.ServerName]bool)
|
2020-10-06 19:09:02 +02:00
|
|
|
for _, p := range preferServers {
|
|
|
|
preferServer[p] = true
|
|
|
|
}
|
2020-04-28 12:46:47 +02:00
|
|
|
return &backfillRequester{
|
|
|
|
db: db,
|
2020-09-02 16:26:30 +02:00
|
|
|
fsAPI: fsAPI,
|
2023-06-14 16:23:46 +02:00
|
|
|
querier: querier,
|
2022-11-15 16:05:23 +01:00
|
|
|
virtualHost: virtualHost,
|
|
|
|
isLocalServerName: isLocalServerName,
|
2020-04-28 12:46:47 +02:00
|
|
|
eventIDToBeforeStateIDs: make(map[string][]string),
|
2023-04-27 17:35:19 +02:00
|
|
|
eventIDMap: make(map[string]gomatrixserverlib.PDU),
|
2020-05-20 17:04:31 +02:00
|
|
|
bwExtrems: bwExtrems,
|
2020-10-06 19:09:02 +02:00
|
|
|
preferServer: preferServer,
|
2022-08-19 11:04:26 +02:00
|
|
|
historyVisiblity: gomatrixserverlib.HistoryVisibilityShared,
|
2020-04-28 12:46:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 17:35:19 +02:00
|
|
|
func (b *backfillRequester) StateIDsBeforeEvent(ctx context.Context, targetEvent gomatrixserverlib.PDU) ([]string, error) {
|
2023-04-27 13:54:20 +02:00
|
|
|
b.eventIDMap[targetEvent.EventID()] = targetEvent
|
2020-04-28 12:46:47 +02:00
|
|
|
if ids, ok := b.eventIDToBeforeStateIDs[targetEvent.EventID()]; ok {
|
|
|
|
return ids, nil
|
|
|
|
}
|
2020-05-19 19:42:55 +02:00
|
|
|
if len(targetEvent.PrevEventIDs()) == 0 && targetEvent.Type() == "m.room.create" && targetEvent.StateKeyEquals("") {
|
|
|
|
util.GetLogger(ctx).WithField("room_id", targetEvent.RoomID()).Info("Backfilled to the beginning of the room")
|
|
|
|
b.eventIDToBeforeStateIDs[targetEvent.EventID()] = []string{}
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-04-28 12:46:47 +02:00
|
|
|
// if we have exactly 1 prev event and we know the state of the room at that prev event, then just roll forward the prev event.
|
|
|
|
// Else, we have to hit /state_ids because either we don't know the state at all at this event (new backwards extremity) or
|
|
|
|
// we don't know the result of state res to merge forks (2 or more prev_events)
|
|
|
|
if len(targetEvent.PrevEventIDs()) == 1 {
|
|
|
|
prevEventID := targetEvent.PrevEventIDs()[0]
|
|
|
|
prevEvent, ok := b.eventIDMap[prevEventID]
|
|
|
|
if !ok {
|
|
|
|
goto FederationHit
|
|
|
|
}
|
|
|
|
prevEventStateIDs, ok := b.eventIDToBeforeStateIDs[prevEventID]
|
|
|
|
if !ok {
|
|
|
|
goto FederationHit
|
|
|
|
}
|
2023-04-27 13:54:20 +02:00
|
|
|
newStateIDs := b.calculateNewStateIDs(targetEvent, prevEvent, prevEventStateIDs)
|
2020-04-28 12:46:47 +02:00
|
|
|
if newStateIDs != nil {
|
|
|
|
b.eventIDToBeforeStateIDs[targetEvent.EventID()] = newStateIDs
|
|
|
|
return newStateIDs, nil
|
|
|
|
}
|
|
|
|
// else we failed to calculate the new state, so fallthrough
|
|
|
|
}
|
|
|
|
|
|
|
|
FederationHit:
|
|
|
|
var lastErr error
|
|
|
|
logrus.WithField("event_id", targetEvent.EventID()).Info("Requesting /state_ids at event")
|
|
|
|
for _, srv := range b.servers { // hit any valid server
|
|
|
|
c := gomatrixserverlib.FederatedStateProvider{
|
2020-09-02 16:26:30 +02:00
|
|
|
FedClient: b.fsAPI,
|
2020-04-29 19:41:45 +02:00
|
|
|
RememberAuthEvents: false,
|
|
|
|
Server: srv,
|
2023-01-05 09:24:00 +01:00
|
|
|
Origin: b.virtualHost,
|
2020-04-28 12:46:47 +02:00
|
|
|
}
|
|
|
|
res, err := c.StateIDsBeforeEvent(ctx, targetEvent)
|
|
|
|
if err != nil {
|
|
|
|
lastErr = err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
b.eventIDToBeforeStateIDs[targetEvent.EventID()] = res
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
return nil, lastErr
|
|
|
|
}
|
|
|
|
|
2023-04-27 17:35:19 +02:00
|
|
|
func (b *backfillRequester) calculateNewStateIDs(targetEvent, prevEvent gomatrixserverlib.PDU, prevEventStateIDs []string) []string {
|
2020-04-28 12:46:47 +02:00
|
|
|
newStateIDs := prevEventStateIDs[:]
|
|
|
|
if prevEvent.StateKey() == nil {
|
|
|
|
// state is the same as the previous event
|
|
|
|
b.eventIDToBeforeStateIDs[targetEvent.EventID()] = newStateIDs
|
|
|
|
return newStateIDs
|
|
|
|
}
|
|
|
|
|
|
|
|
missingState := false // true if we are missing the info for a state event ID
|
|
|
|
foundEvent := false // true if we found a (type, state_key) match
|
|
|
|
// find which state ID to replace, if any
|
|
|
|
for i, id := range newStateIDs {
|
|
|
|
ev, ok := b.eventIDMap[id]
|
|
|
|
if !ok {
|
|
|
|
missingState = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// The state IDs BEFORE the target event are the state IDs BEFORE the prev_event PLUS the prev_event itself
|
2020-08-25 22:04:35 +02:00
|
|
|
if ev.Type() == prevEvent.Type() && ev.StateKeyEquals(*prevEvent.StateKey()) {
|
2020-04-28 12:46:47 +02:00
|
|
|
newStateIDs[i] = prevEvent.EventID()
|
|
|
|
foundEvent = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !foundEvent && !missingState {
|
|
|
|
// we can be certain that this is new state
|
|
|
|
newStateIDs = append(newStateIDs, prevEvent.EventID())
|
|
|
|
foundEvent = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if foundEvent {
|
|
|
|
b.eventIDToBeforeStateIDs[targetEvent.EventID()] = newStateIDs
|
|
|
|
return newStateIDs
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-29 19:41:45 +02:00
|
|
|
func (b *backfillRequester) StateBeforeEvent(ctx context.Context, roomVer gomatrixserverlib.RoomVersion,
|
2023-04-27 17:35:19 +02:00
|
|
|
event gomatrixserverlib.PDU, eventIDs []string) (map[string]gomatrixserverlib.PDU, error) {
|
2020-04-29 19:41:45 +02:00
|
|
|
|
2020-04-28 12:46:47 +02:00
|
|
|
// try to fetch the events from the database first
|
|
|
|
events, err := b.ProvideEvents(roomVer, eventIDs)
|
|
|
|
if err != nil {
|
|
|
|
// non-fatal, fallthrough
|
|
|
|
logrus.WithError(err).Info("Failed to fetch events")
|
|
|
|
} else {
|
|
|
|
logrus.Infof("Fetched %d/%d events from the database", len(events), len(eventIDs))
|
|
|
|
if len(events) == len(eventIDs) {
|
2023-04-27 17:35:19 +02:00
|
|
|
result := make(map[string]gomatrixserverlib.PDU)
|
2020-04-28 12:46:47 +02:00
|
|
|
for i := range events {
|
2020-11-16 16:44:53 +01:00
|
|
|
result[events[i].EventID()] = events[i]
|
2020-04-28 12:46:47 +02:00
|
|
|
b.eventIDMap[events[i].EventID()] = events[i]
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-01 11:49:26 +02:00
|
|
|
var lastErr error
|
|
|
|
for _, srv := range b.servers {
|
|
|
|
c := gomatrixserverlib.FederatedStateProvider{
|
|
|
|
FedClient: b.fsAPI,
|
|
|
|
RememberAuthEvents: false,
|
|
|
|
Server: srv,
|
2023-01-05 09:24:00 +01:00
|
|
|
Origin: b.virtualHost,
|
2022-07-01 11:49:26 +02:00
|
|
|
}
|
|
|
|
result, err := c.StateBeforeEvent(ctx, roomVer, event, eventIDs)
|
|
|
|
if err != nil {
|
|
|
|
lastErr = err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for eventID, ev := range result {
|
|
|
|
b.eventIDMap[eventID] = ev
|
|
|
|
}
|
|
|
|
return result, nil
|
2020-04-28 12:46:47 +02:00
|
|
|
}
|
2022-07-01 11:49:26 +02:00
|
|
|
return nil, lastErr
|
2020-04-28 12:46:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServersAtEvent is called when trying to determine which server to request from.
|
|
|
|
// It returns a list of servers which can be queried for backfill requests. These servers
|
|
|
|
// will be servers that are in the room already. The entries at the beginning are preferred servers
|
|
|
|
// and will be tried first. An empty list will fail the request.
|
2023-04-19 16:50:33 +02:00
|
|
|
func (b *backfillRequester) ServersAtEvent(ctx context.Context, roomID, eventID string) []spec.ServerName {
|
2020-05-20 17:04:31 +02:00
|
|
|
// eventID will be a prev_event ID of a backwards extremity, meaning we will not have a database entry for it. Instead, use
|
|
|
|
// its successor, so look it up.
|
|
|
|
successor := ""
|
|
|
|
FindSuccessor:
|
|
|
|
for sucID, prevEventIDs := range b.bwExtrems {
|
|
|
|
for _, pe := range prevEventIDs {
|
|
|
|
if pe == eventID {
|
|
|
|
successor = sucID
|
|
|
|
break FindSuccessor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if successor == "" {
|
|
|
|
logrus.WithField("event_id", eventID).Error("ServersAtEvent: failed to find successor of this event to determine room state")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
eventID = successor
|
|
|
|
|
2020-04-28 12:46:47 +02:00
|
|
|
// getMembershipsBeforeEventNID requires a NID, so retrieving the NID for
|
|
|
|
// the event is necessary.
|
|
|
|
NIDs, err := b.db.EventNIDs(ctx, []string{eventID})
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithField("event_id", eventID).WithError(err).Error("ServersAtEvent: failed to get event NID for event")
|
2020-05-20 17:04:31 +02:00
|
|
|
return nil
|
2020-04-28 12:46:47 +02:00
|
|
|
}
|
|
|
|
|
2020-09-02 11:02:48 +02:00
|
|
|
info, err := b.db.RoomInfo(ctx, roomID)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).WithField("room_id", roomID).Error("ServersAtEvent: failed to get RoomInfo for room")
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-01 16:29:19 +02:00
|
|
|
if info == nil || info.IsStub() {
|
2020-09-02 11:02:48 +02:00
|
|
|
logrus.WithField("room_id", roomID).Error("ServersAtEvent: failed to get RoomInfo for room, room is missing")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-14 16:23:46 +02:00
|
|
|
stateEntries, err := helpers.StateBeforeEvent(ctx, b.db, info, NIDs[eventID].EventNID, b.querier)
|
2020-04-29 19:41:45 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithField("event_id", eventID).WithError(err).Error("ServersAtEvent: failed to load state before event")
|
2020-05-20 17:04:31 +02:00
|
|
|
return nil
|
2020-04-29 19:41:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// possibly return all joined servers depending on history visiblity
|
2023-06-14 16:23:46 +02:00
|
|
|
memberEventsFromVis, visibility, err := joinEventsFromHistoryVisibility(ctx, b.db, b.querier, info, stateEntries, b.virtualHost)
|
2022-08-19 11:04:26 +02:00
|
|
|
b.historyVisiblity = visibility
|
2020-04-29 19:41:45 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("ServersAtEvent: failed calculate servers from history visibility rules")
|
2020-05-20 17:04:31 +02:00
|
|
|
return nil
|
2020-04-29 19:41:45 +02:00
|
|
|
}
|
|
|
|
logrus.Infof("ServersAtEvent including %d current events from history visibility", len(memberEventsFromVis))
|
|
|
|
|
2020-04-28 12:46:47 +02:00
|
|
|
// Retrieve all "m.room.member" state events of "join" membership, which
|
|
|
|
// contains the list of users in the room before the event, therefore all
|
|
|
|
// the servers in it at that moment.
|
2023-03-01 17:06:47 +01:00
|
|
|
memberEvents, err := helpers.GetMembershipsAtState(ctx, b.db, info, stateEntries, true)
|
2020-04-28 12:46:47 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithField("event_id", eventID).WithError(err).Error("ServersAtEvent: failed to get memberships before event")
|
2020-05-20 17:04:31 +02:00
|
|
|
return nil
|
2020-04-28 12:46:47 +02:00
|
|
|
}
|
2020-04-29 19:41:45 +02:00
|
|
|
memberEvents = append(memberEvents, memberEventsFromVis...)
|
2020-04-28 12:46:47 +02:00
|
|
|
|
|
|
|
// Store the server names in a temporary map to avoid duplicates.
|
2023-04-19 16:50:33 +02:00
|
|
|
serverSet := make(map[spec.ServerName]bool)
|
2020-04-29 19:41:45 +02:00
|
|
|
for _, event := range memberEvents {
|
2023-06-14 16:23:46 +02:00
|
|
|
validRoomID, err := spec.NewRoomID(event.RoomID())
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if sender, err := b.querier.QueryUserIDForSender(ctx, *validRoomID, event.SenderID()); err == nil {
|
2023-06-06 22:55:18 +02:00
|
|
|
serverSet[sender.Domain()] = true
|
2022-09-26 18:35:35 +02:00
|
|
|
}
|
2020-04-28 12:46:47 +02:00
|
|
|
}
|
2023-04-19 16:50:33 +02:00
|
|
|
var servers []spec.ServerName
|
2020-04-28 12:46:47 +02:00
|
|
|
for server := range serverSet {
|
2022-11-15 16:05:23 +01:00
|
|
|
if b.isLocalServerName(server) {
|
2020-04-28 12:46:47 +02:00
|
|
|
continue
|
|
|
|
}
|
2020-10-06 19:09:02 +02:00
|
|
|
if b.preferServer[server] { // insert at the front
|
2023-04-19 16:50:33 +02:00
|
|
|
servers = append([]spec.ServerName{server}, servers...)
|
2020-10-06 19:09:02 +02:00
|
|
|
} else { // insert at the back
|
|
|
|
servers = append(servers, server)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(servers) > maxBackfillServers {
|
|
|
|
servers = servers[:maxBackfillServers]
|
2020-04-28 12:46:47 +02:00
|
|
|
}
|
2020-10-06 19:09:02 +02:00
|
|
|
|
2020-04-28 12:46:47 +02:00
|
|
|
b.servers = servers
|
2020-05-20 17:04:31 +02:00
|
|
|
return servers
|
2020-04-28 12:46:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Backfill performs a backfill request to the given server.
|
|
|
|
// https://matrix.org/docs/spec/server_server/latest#get-matrix-federation-v1-backfill-roomid
|
2023-04-19 16:50:33 +02:00
|
|
|
func (b *backfillRequester) Backfill(ctx context.Context, origin, server spec.ServerName, roomID string,
|
2020-09-02 16:26:30 +02:00
|
|
|
limit int, fromEventIDs []string) (gomatrixserverlib.Transaction, error) {
|
2020-04-29 19:41:45 +02:00
|
|
|
|
2022-11-15 16:05:23 +01:00
|
|
|
tx, err := b.fsAPI.Backfill(ctx, origin, server, roomID, limit, fromEventIDs)
|
2020-09-02 16:26:30 +02:00
|
|
|
return tx, err
|
2020-04-28 12:46:47 +02:00
|
|
|
}
|
|
|
|
|
2023-04-27 17:35:19 +02:00
|
|
|
func (b *backfillRequester) ProvideEvents(roomVer gomatrixserverlib.RoomVersion, eventIDs []string) ([]gomatrixserverlib.PDU, error) {
|
2020-04-28 12:46:47 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
nidMap, err := b.db.EventNIDs(ctx, eventIDs)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).WithField("event_ids", eventIDs).Error("Failed to find events")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
eventNIDs := make([]types.EventNID, len(nidMap))
|
|
|
|
i := 0
|
2023-02-24 09:40:20 +01:00
|
|
|
roomNID := b.roomInfo.RoomNID
|
2020-04-28 12:46:47 +02:00
|
|
|
for _, nid := range nidMap {
|
2023-02-24 09:40:20 +01:00
|
|
|
eventNIDs[i] = nid.EventNID
|
2020-04-28 12:46:47 +02:00
|
|
|
i++
|
2023-02-24 09:40:20 +01:00
|
|
|
if roomNID == 0 {
|
|
|
|
roomNID = nid.RoomNID
|
|
|
|
}
|
2020-04-28 12:46:47 +02:00
|
|
|
}
|
2023-05-08 21:25:44 +02:00
|
|
|
eventsWithNids, err := b.db.Events(ctx, b.roomInfo.RoomVersion, eventNIDs)
|
2020-04-28 12:46:47 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).WithField("event_nids", eventNIDs).Error("Failed to load events")
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-27 17:35:19 +02:00
|
|
|
events := make([]gomatrixserverlib.PDU, len(eventsWithNids))
|
2020-04-28 12:46:47 +02:00
|
|
|
for i := range eventsWithNids {
|
2023-05-02 16:03:16 +02:00
|
|
|
events[i] = eventsWithNids[i].PDU
|
2020-04-28 12:46:47 +02:00
|
|
|
}
|
|
|
|
return events, nil
|
|
|
|
}
|
2020-04-29 19:41:45 +02:00
|
|
|
|
2020-11-16 11:47:16 +01:00
|
|
|
// joinEventsFromHistoryVisibility returns all CURRENTLY joined members if our server can read the room history
|
2022-08-05 11:12:41 +02:00
|
|
|
//
|
2020-04-29 19:41:45 +02:00
|
|
|
// TODO: Long term we probably want a history_visibility table which stores eventNID | visibility_enum so we can just
|
2022-08-05 11:12:41 +02:00
|
|
|
// pull all events and then filter by that table.
|
2020-04-29 19:41:45 +02:00
|
|
|
func joinEventsFromHistoryVisibility(
|
2023-06-14 16:23:46 +02:00
|
|
|
ctx context.Context, db storage.RoomDatabase, querier api.QuerySenderIDAPI, roomInfo *types.RoomInfo, stateEntries []types.StateEntry,
|
2023-04-19 16:50:33 +02:00
|
|
|
thisServer spec.ServerName) ([]types.Event, gomatrixserverlib.HistoryVisibility, error) {
|
2020-04-29 19:41:45 +02:00
|
|
|
|
|
|
|
var eventNIDs []types.EventNID
|
|
|
|
for _, entry := range stateEntries {
|
|
|
|
// Filter the events to retrieve to only keep the membership events
|
|
|
|
if entry.EventTypeNID == types.MRoomHistoryVisibilityNID && entry.EventStateKeyNID == types.EmptyStateKeyNID {
|
|
|
|
eventNIDs = append(eventNIDs, entry.EventNID)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all of the events in this state
|
2023-05-08 21:25:44 +02:00
|
|
|
if roomInfo == nil {
|
|
|
|
return nil, gomatrixserverlib.HistoryVisibilityJoined, types.ErrorInvalidRoomInfo
|
|
|
|
}
|
|
|
|
stateEvents, err := db.Events(ctx, roomInfo.RoomVersion, eventNIDs)
|
2020-04-29 19:41:45 +02:00
|
|
|
if err != nil {
|
2022-08-19 11:04:26 +02:00
|
|
|
// even though the default should be shared, restricting the visibility to joined
|
|
|
|
// feels more secure here.
|
|
|
|
return nil, gomatrixserverlib.HistoryVisibilityJoined, err
|
2020-04-29 19:41:45 +02:00
|
|
|
}
|
2023-05-02 16:03:16 +02:00
|
|
|
events := make([]gomatrixserverlib.PDU, len(stateEvents))
|
2020-04-29 19:41:45 +02:00
|
|
|
for i := range stateEvents {
|
2023-05-02 16:03:16 +02:00
|
|
|
events[i] = stateEvents[i].PDU
|
2020-04-29 19:41:45 +02:00
|
|
|
}
|
2020-11-16 11:47:16 +01:00
|
|
|
|
|
|
|
// Can we see events in the room?
|
2023-06-14 16:23:46 +02:00
|
|
|
canSeeEvents := auth.IsServerAllowed(ctx, querier, thisServer, true, events)
|
2023-02-24 09:40:20 +01:00
|
|
|
visibility := auth.HistoryVisibilityForRoom(events)
|
2020-11-16 11:47:16 +01:00
|
|
|
if !canSeeEvents {
|
2022-08-19 11:04:26 +02:00
|
|
|
logrus.Infof("ServersAtEvent history not visible to us: %s", visibility)
|
|
|
|
return nil, visibility, nil
|
2020-04-29 19:41:45 +02:00
|
|
|
}
|
|
|
|
// get joined members
|
2023-02-24 09:40:20 +01:00
|
|
|
joinEventNIDs, err := db.GetMembershipEventNIDsForRoom(ctx, roomInfo.RoomNID, true, false)
|
2020-04-29 19:41:45 +02:00
|
|
|
if err != nil {
|
2022-08-19 11:04:26 +02:00
|
|
|
return nil, visibility, err
|
2020-04-29 19:41:45 +02:00
|
|
|
}
|
2023-05-08 21:25:44 +02:00
|
|
|
evs, err := db.Events(ctx, roomInfo.RoomVersion, joinEventNIDs)
|
2022-08-19 11:04:26 +02:00
|
|
|
return evs, visibility, err
|
2020-04-29 19:41:45 +02:00
|
|
|
}
|
2020-09-02 14:47:31 +02:00
|
|
|
|
2023-06-14 16:23:46 +02:00
|
|
|
func persistEvents(ctx context.Context, db storage.Database, querier api.QuerySenderIDAPI, events []gomatrixserverlib.PDU) (types.RoomNID, map[string]types.Event) {
|
2020-09-02 14:47:31 +02:00
|
|
|
var roomNID types.RoomNID
|
2021-12-09 16:03:26 +01:00
|
|
|
var eventNID types.EventNID
|
2020-09-02 14:47:31 +02:00
|
|
|
backfilledEventMap := make(map[string]types.Event)
|
|
|
|
for j, ev := range events {
|
|
|
|
nidMap, err := db.EventNIDs(ctx, ev.AuthEventIDs())
|
|
|
|
if err != nil { // this shouldn't happen as RequestBackfill already found them
|
|
|
|
logrus.WithError(err).WithField("auth_events", ev.AuthEventIDs()).Error("Failed to find one or more auth events")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
authNids := make([]types.EventNID, len(nidMap))
|
|
|
|
i := 0
|
|
|
|
for _, nid := range nidMap {
|
2023-02-24 09:40:20 +01:00
|
|
|
authNids[i] = nid.EventNID
|
2020-09-02 14:47:31 +02:00
|
|
|
i++
|
|
|
|
}
|
2023-02-24 09:40:20 +01:00
|
|
|
|
2023-04-27 13:54:20 +02:00
|
|
|
roomInfo, err := db.GetOrCreateRoomInfo(ctx, ev)
|
2023-02-24 09:40:20 +01:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("failed to get or create roomNID")
|
|
|
|
continue
|
|
|
|
}
|
2023-03-07 15:35:08 +01:00
|
|
|
roomNID = roomInfo.RoomNID
|
2023-02-24 09:40:20 +01:00
|
|
|
|
|
|
|
eventTypeNID, err := db.GetOrCreateEventTypeNID(ctx, ev.Type())
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("failed to get or create eventType NID")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
eventStateKeyNID, err := db.GetOrCreateEventStateKeyNID(ctx, ev.StateKey())
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("failed to get or create eventStateKey NID")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-04-27 13:54:20 +02:00
|
|
|
eventNID, _, err = db.StoreEvent(ctx, ev, roomInfo, eventTypeNID, eventStateKeyNID, authNids, false)
|
2020-09-02 14:47:31 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).WithField("event_id", ev.EventID()).Error("Failed to persist event")
|
|
|
|
continue
|
|
|
|
}
|
2023-03-01 17:06:47 +01:00
|
|
|
|
2023-06-14 16:23:46 +02:00
|
|
|
resolver := state.NewStateResolution(db, roomInfo, querier)
|
2023-03-03 14:03:17 +01:00
|
|
|
|
2023-04-27 13:54:20 +02:00
|
|
|
_, redactedEvent, err := db.MaybeRedactEvent(ctx, roomInfo, eventNID, ev, &resolver)
|
2023-03-01 17:06:47 +01:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).WithField("event_id", ev.EventID()).Error("Failed to redact event")
|
|
|
|
continue
|
|
|
|
}
|
2020-09-02 14:47:31 +02:00
|
|
|
// If storing this event results in it being redacted, then do so.
|
|
|
|
// It's also possible for this event to be a redaction which results in another event being
|
|
|
|
// redacted, which we don't care about since we aren't returning it in this backfill.
|
2023-03-01 17:06:47 +01:00
|
|
|
if redactedEvent != nil && redactedEvent.EventID() == ev.EventID() {
|
2023-04-27 13:54:20 +02:00
|
|
|
ev = redactedEvent
|
2020-09-02 14:47:31 +02:00
|
|
|
events[j] = ev
|
|
|
|
}
|
|
|
|
backfilledEventMap[ev.EventID()] = types.Event{
|
2021-12-09 16:03:26 +01:00
|
|
|
EventNID: eventNID,
|
2023-05-02 16:03:16 +02:00
|
|
|
PDU: ev,
|
2020-09-02 14:47:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return roomNID, backfilledEventMap
|
|
|
|
}
|