Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove methods returning/using EventReference #383

Merged
merged 15 commits into from May 24, 2023
Merged
123 changes: 4 additions & 119 deletions event.go
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"
"reflect"
"strings"
"unsafe"

"github.com/matrix-org/gomatrixserverlib/spec"
"github.com/tidwall/gjson"
Expand Down Expand Up @@ -52,12 +51,10 @@ func (e EventValidationError) Error() string {
// The fields have different formats depending on the room version - see
// eventFormatV1Fields, eventFormatV2Fields.
type event struct {
redacted bool
eventID string
eventJSON []byte
fields interface {
CacheCost() int
}
redacted bool
eventID string
eventJSON []byte
fields any
roomVersion RoomVersion
}

Expand Down Expand Up @@ -89,56 +86,6 @@ type eventFormatV2Fields struct {
AuthEvents []string `json:"auth_events"`
}

func (e *event) CacheCost() int {
return int(unsafe.Sizeof(*e)) +
len(e.eventID) +
(cap(e.eventJSON) * 2) +
len(e.roomVersion) +
1 + // redacted bool
e.fields.CacheCost()
}

func (e *eventFields) CacheCost() int {
cost := int(unsafe.Sizeof(*e)) +
len(e.RoomID) +
len(e.Sender) +
len(e.Type) +
cap(e.Content) +
len(e.Redacts) +
4 + // depth int64
cap(e.Unsigned) +
4 // originserverts timestamp as uint64
if e.StateKey != nil {
cost += len(*e.StateKey)
}
return cost
}

func (e eventFormatV1Fields) CacheCost() int {
cost := e.eventFields.CacheCost() +
int(unsafe.Sizeof(e)) +
len(e.EventID)
for _, v := range e.PrevEvents {
cost += len(v.EventID) + cap(v.EventSHA256)
}
for _, v := range e.AuthEvents {
cost += len(v.EventID) + cap(v.EventSHA256)
}
return cost
}

func (e eventFormatV2Fields) CacheCost() int {
cost := e.eventFields.CacheCost() +
int(unsafe.Sizeof(e))
for _, v := range e.PrevEvents {
cost += len(v)
}
for _, v := range e.AuthEvents {
cost += len(v)
}
return cost
}

var emptyEventReferenceList = []EventReference{}

// newEventFromUntrustedJSON loads a new event from some JSON that may be invalid.
Expand Down Expand Up @@ -417,18 +364,6 @@ func (e *event) updateUnsignedFields(unsigned []byte) error {
return nil
}

// EventReference returns an EventReference for the event.
// The reference can be used to refer to this event from other events.
func (e *event) EventReference() EventReference {
reference, err := referenceOfEvent(e.eventJSON, e.roomVersion)
if err != nil {
// This is unreachable for events created with EventBuilder.Build or NewEventFromUntrustedJSON
// This can be reached if NewEventFromTrustedJSON is given JSON from an untrusted source.
panic(fmt.Errorf("gomatrixserverlib: invalid event %v (%q)", err, string(e.eventJSON)))
}
return reference
}

// Sign returns a copy of the event with an additional signature.
func (e *event) Sign(signingName string, keyID KeyID, privateKey ed25519.PrivateKey) PDU {
eventJSON, err := signEvent(signingName, keyID, privateKey, e.eventJSON, e.roomVersion)
Expand Down Expand Up @@ -669,33 +604,6 @@ func (e *event) Content() []byte {
}
}

// PrevEvents returns references to the direct ancestors of the event.
func (e *event) PrevEvents() []EventReference {
switch fields := e.fields.(type) {
case eventFormatV1Fields:
return fields.PrevEvents
case eventFormatV2Fields:
result := make([]EventReference, 0, len(fields.PrevEvents))
for _, id := range fields.PrevEvents {
// In the new event format, the event ID is already the hash of
// the event. Since we will have generated the event ID before
// now, we can just knock the sigil $ off the front and use that
// as the event SHA256.
var sha spec.Base64Bytes
if err := sha.Decode(id[1:]); err != nil {
panic("gomatrixserverlib: event ID is malformed: " + err.Error())
}
result = append(result, EventReference{
EventID: id,
EventSHA256: sha,
})
}
return result
default:
panic(e.invalidFieldType())
}
}

// PrevEventIDs returns the event IDs of the direct ancestors of the event.
func (e *event) PrevEventIDs() []string {
switch fields := e.fields.(type) {
Expand Down Expand Up @@ -795,29 +703,6 @@ func (e *event) PowerLevels() (*PowerLevelContent, error) {
return &c, nil
}

// AuthEvents returns references to the events needed to auth the event.
func (e *event) AuthEvents() []EventReference {
switch fields := e.fields.(type) {
case eventFormatV1Fields:
return fields.AuthEvents
case eventFormatV2Fields:
result := make([]EventReference, 0, len(fields.AuthEvents))
for _, id := range fields.AuthEvents {
var sha spec.Base64Bytes
if err := sha.Decode(id[1:]); err != nil {
panic("gomatrixserverlib: event ID is malformed: " + err.Error())
}
result = append(result, EventReference{
EventID: id,
EventSHA256: sha,
})
}
return result
default:
panic(e.invalidFieldType())
}
}

// AuthEventIDs returns the event IDs of the events needed to auth the event.
func (e *event) AuthEventIDs() []string {
switch fields := e.fields.(type) {
Expand Down
10 changes: 5 additions & 5 deletions eventauth.go
Expand Up @@ -70,35 +70,35 @@
if e, err = provider.Create(); err != nil {
return
} else if e != nil {
refs = append(refs, e.EventReference())
refs = append(refs, EventReference{EventID: e.EventID(), EventSHA256: spec.Base64Bytes(e.EventID())})

Check warning on line 73 in eventauth.go

View check run for this annotation

Codecov / codecov/patch

eventauth.go#L73

Added line #L73 was not covered by tests
}
}
if s.JoinRules {
if e, err = provider.JoinRules(); err != nil {
return
} else if e != nil {
refs = append(refs, e.EventReference())
refs = append(refs, EventReference{EventID: e.EventID(), EventSHA256: spec.Base64Bytes(e.EventID())})

Check warning on line 80 in eventauth.go

View check run for this annotation

Codecov / codecov/patch

eventauth.go#L80

Added line #L80 was not covered by tests
}
}
if s.PowerLevels {
if e, err = provider.PowerLevels(); err != nil {
return
} else if e != nil {
refs = append(refs, e.EventReference())
refs = append(refs, EventReference{EventID: e.EventID(), EventSHA256: spec.Base64Bytes(e.EventID())})

Check warning on line 87 in eventauth.go

View check run for this annotation

Codecov / codecov/patch

eventauth.go#L87

Added line #L87 was not covered by tests
}
}
for _, userID := range s.Member {
if e, err = provider.Member(userID); err != nil {
return
} else if e != nil {
refs = append(refs, e.EventReference())
refs = append(refs, EventReference{EventID: e.EventID(), EventSHA256: spec.Base64Bytes(e.EventID())})

Check warning on line 94 in eventauth.go

View check run for this annotation

Codecov / codecov/patch

eventauth.go#L94

Added line #L94 was not covered by tests
}
}
for _, token := range s.ThirdPartyInvite {
if e, err = provider.ThirdPartyInvite(token); err != nil {
return
} else if e != nil {
refs = append(refs, e.EventReference())
refs = append(refs, EventReference{EventID: e.EventID(), EventSHA256: spec.Base64Bytes(e.EventID())})

Check warning on line 101 in eventauth.go

View check run for this annotation

Codecov / codecov/patch

eventauth.go#L101

Added line #L101 was not covered by tests
}
}
return
Expand Down
2 changes: 0 additions & 2 deletions pdu.go
Expand Up @@ -32,7 +32,6 @@ type PDU interface {
// Redacted returns whether the event is redacted.
Redacted() bool
PrevEventIDs() []string
PrevEvents() []EventReference // TODO: remove, used in Dendrite in (d *EventDatabase) StoreEvent
OriginServerTS() spec.Timestamp
// Redact redacts the event.
Redact()
Expand All @@ -49,7 +48,6 @@ type PDU interface {
SetUnsignedField(path string, value interface{}) error
// Sign returns a copy of the event with an additional signature.
Sign(signingName string, keyID KeyID, privateKey ed25519.PrivateKey) PDU
EventReference() EventReference // TODO: remove
Depth() int64 // TODO: remove
JSON() []byte // TODO: remove
AuthEventIDs() []string // TODO: remove
Expand Down