Skip to content
This repository was archived by the owner on Nov 25, 2024. It is now read-only.

Commit be9be25

Browse files
Resolve over old and new extremities (#2457)
* Feed existing state into state res when calculating state from new extremities * Remove duplicates * Fix bug * Sort and unique * Update to matrix-org/gomatrixserverlib#308 * Trim the slice properly * Update gomatrixserverlib again * Update to matrix-org/gomatrixserverlib#308
1 parent cafc2d2 commit be9be25

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ require (
3030
github.com/matrix-org/dugong v0.0.0-20210921133753-66e6b1c67e2e
3131
github.com/matrix-org/go-sqlite3-js v0.0.0-20220419092513-28aa791a1c91
3232
github.com/matrix-org/gomatrix v0.0.0-20210324163249-be2af5ef2e16
33-
github.com/matrix-org/gomatrixserverlib v0.0.0-20220509120958-8d818048c34c
33+
github.com/matrix-org/gomatrixserverlib v0.0.0-20220513103617-eee8fd528433
3434
github.com/matrix-org/pinecone v0.0.0-20220408153826-2999ea29ed48
3535
github.com/matrix-org/util v0.0.0-20200807132607-55161520e1d4
3636
github.com/mattn/go-sqlite3 v1.14.10

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,8 +795,8 @@ github.com/matrix-org/go-sqlite3-js v0.0.0-20220419092513-28aa791a1c91/go.mod h1
795795
github.com/matrix-org/gomatrix v0.0.0-20190528120928-7df988a63f26/go.mod h1:3fxX6gUjWyI/2Bt7J1OLhpCzOfO/bB3AiX0cJtEKud0=
796796
github.com/matrix-org/gomatrix v0.0.0-20210324163249-be2af5ef2e16 h1:ZtO5uywdd5dLDCud4r0r55eP4j9FuUNpl60Gmntcop4=
797797
github.com/matrix-org/gomatrix v0.0.0-20210324163249-be2af5ef2e16/go.mod h1:/gBX06Kw0exX1HrwmoBibFA98yBk/jxKpGVeyQbff+s=
798-
github.com/matrix-org/gomatrixserverlib v0.0.0-20220509120958-8d818048c34c h1:KqzqFWxvs90pcDaW9QEveW+Q5JcEYuNnKyaqXc+ohno=
799-
github.com/matrix-org/gomatrixserverlib v0.0.0-20220509120958-8d818048c34c/go.mod h1:V5eO8rn/C3rcxig37A/BCeKerLFS+9Avg/77FIeTZ48=
798+
github.com/matrix-org/gomatrixserverlib v0.0.0-20220513103617-eee8fd528433 h1:nwAlThHGPI2EAAJklXvgMcdhXF6ZiHp60+fmaYMoaDA=
799+
github.com/matrix-org/gomatrixserverlib v0.0.0-20220513103617-eee8fd528433/go.mod h1:V5eO8rn/C3rcxig37A/BCeKerLFS+9Avg/77FIeTZ48=
800800
github.com/matrix-org/pinecone v0.0.0-20220408153826-2999ea29ed48 h1:W0sjjC6yjskHX4mb0nk3p0fXAlbU5bAFUFeEtlrPASE=
801801
github.com/matrix-org/pinecone v0.0.0-20220408153826-2999ea29ed48/go.mod h1:ulJzsVOTssIVp1j/m5eI//4VpAGDkMt5NrRuAVX7wpc=
802802
github.com/matrix-org/util v0.0.0-20190711121626-527ce5ddefc7/go.mod h1:vVQlW/emklohkZnOPwD3LrZUBqdfsbiyO3p1lNV8F6U=

roomserver/internal/input/input_latest_events.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,19 @@ func (u *latestEventsUpdater) latestState() error {
233233
}
234234
}
235235

236-
// Get a list of the current latest events. This may or may not
237-
// include the new event from the input path, depending on whether
238-
// it is a forward extremity or not.
239-
latestStateAtEvents := make([]types.StateAtEvent, len(u.latest))
240-
for i := range u.latest {
241-
latestStateAtEvents[i] = u.latest[i].StateAtEvent
236+
// Take the old set of extremities and the new set of extremities and
237+
// mash them together into a list. This may or may not include the new event
238+
// from the input path, depending on whether it became a forward extremity
239+
// or not. We'll then run state resolution across all of them to determine
240+
// the new current state of the room. Including the old extremities here
241+
// ensures that new forward extremities with bad state snapshots (from
242+
// possible malicious actors) can't completely corrupt the room state
243+
// away from what it was before.
244+
combinedExtremities := types.StateAtEventAndReferences(append(u.oldLatest, u.latest...))
245+
combinedExtremities = combinedExtremities[:util.SortAndUnique(combinedExtremities)]
246+
latestStateAtEvents := make([]types.StateAtEvent, len(combinedExtremities))
247+
for i := range combinedExtremities {
248+
latestStateAtEvents[i] = combinedExtremities[i].StateAtEvent
242249
}
243250

244251
// Takes the NIDs of the latest events and creates a state snapshot

roomserver/types/types.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package types
1818
import (
1919
"encoding/json"
2020
"sort"
21+
"strings"
2122

2223
"github.com/matrix-org/gomatrixserverlib"
2324
"golang.org/x/crypto/blake2b"
@@ -166,6 +167,20 @@ type StateAtEventAndReference struct {
166167
gomatrixserverlib.EventReference
167168
}
168169

170+
type StateAtEventAndReferences []StateAtEventAndReference
171+
172+
func (s StateAtEventAndReferences) Less(a, b int) bool {
173+
return strings.Compare(s[a].EventID, s[b].EventID) < 0
174+
}
175+
176+
func (s StateAtEventAndReferences) Len() int {
177+
return len(s)
178+
}
179+
180+
func (s StateAtEventAndReferences) Swap(a, b int) {
181+
s[a], s[b] = s[b], s[a]
182+
}
183+
169184
// An Event is a gomatrixserverlib.Event with the numeric event ID attached.
170185
// It is when performing bulk event lookup in the database.
171186
type Event struct {

0 commit comments

Comments
 (0)