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

Add Equal method for CrossSigningKey #444

Merged
merged 4 commits into from
Jan 6, 2025
Merged

Add Equal method for CrossSigningKey #444

merged 4 commits into from
Jan 6, 2025

Conversation

S7evinK
Copy link
Contributor

@S7evinK S7evinK commented Dec 21, 2024

Playing around with Copilot, code is mostly generated (but reviewed)

Also fixes CI by using the correct repository for Dendrite.

@codecov-commenter
Copy link

codecov-commenter commented Dec 21, 2024

Codecov Report

Attention: Patch coverage is 86.04651% with 6 lines in your changes missing coverage. Please review.

Project coverage is 59.83%. Comparing base (e86ab16) to head (30cbdb4).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
fclient/crosssigning.go 86.04% 4 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #444      +/-   ##
==========================================
+ Coverage   59.68%   59.83%   +0.15%     
==========================================
  Files          51       51              
  Lines        7158     7201      +43     
==========================================
+ Hits         4272     4309      +37     
- Misses       2489     2493       +4     
- Partials      397      399       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -46,6 +47,49 @@ type CrossSigningKey struct {

func (s *CrossSigningKey) isCrossSigningBody() {} // implements CrossSigningBody

func (s *CrossSigningKey) Equal(other *CrossSigningKey) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we just use reflect.DeepEqual here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is does not allocate at all, while reflect.DeepEqual does. (not that it matters much on that endpoint)

> go test -bench=Equal -run=^# -benchmem -count=10
goos: linux
goarch: amd64
pkg: github.com/matrix-org/gomatrixserverlib/fclient
cpu: 12th Gen Intel(R) Core(TM) i5-12500H
BenchmarkReflectDeepEqual-16    	  468159	      2381 ns/op	     288 B/op	      14 allocs/op
BenchmarkReflectDeepEqual-16    	  460086	      2376 ns/op	     288 B/op	      14 allocs/op
BenchmarkReflectDeepEqual-16    	  494133	      2333 ns/op	     288 B/op	      14 allocs/op
BenchmarkReflectDeepEqual-16    	  496840	      2338 ns/op	     288 B/op	      14 allocs/op
BenchmarkReflectDeepEqual-16    	  481978	      2351 ns/op	     288 B/op	      14 allocs/op
BenchmarkReflectDeepEqual-16    	  461346	      2327 ns/op	     288 B/op	      14 allocs/op
BenchmarkReflectDeepEqual-16    	  511146	      2390 ns/op	     288 B/op	      14 allocs/op
BenchmarkReflectDeepEqual-16    	  501966	      2443 ns/op	     288 B/op	      14 allocs/op
BenchmarkReflectDeepEqual-16    	  503883	      2345 ns/op	     288 B/op	      14 allocs/op
BenchmarkReflectDeepEqual-16    	  503308	      2415 ns/op	     288 B/op	      14 allocs/op
BenchmarkEqual-16               	 5371452	       214.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkEqual-16               	 5578748	       227.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkEqual-16               	 5169262	       230.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkEqual-16               	 5215892	       234.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkEqual-16               	 5207256	       232.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkEqual-16               	 5564870	       244.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkEqual-16               	 4907815	       229.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkEqual-16               	 5178279	       233.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkEqual-16               	 4840867	       247.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkEqual-16               	 5193268	       215.0 ns/op	       0 B/op	       0 allocs/op

return false
}
for i := range s.Usage {
if s.Usage[i] != other.Usage[i] {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this slice guaranteed to be ordered? If not you might get some false negatives.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, but does usage currently even contain more than one value?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per spec no, but you want to be forwards compatible, otherwise you might need to prevent such cross signing keys from being uploaded.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 30cbdb4

@@ -46,6 +47,49 @@ type CrossSigningKey struct {

func (s *CrossSigningKey) isCrossSigningBody() {} // implements CrossSigningBody

func (s *CrossSigningKey) Equal(other *CrossSigningKey) bool {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to document what the comparison is taking into account, i.e. there can be different equality implementations depending on the use-case. I presume here we care about the fact that a new object needs to be stored in the DB and we test for equality of all fields.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. The Equal method here checks all fields and receives the MasterKey, SelfSigningKeys and UserSigningKeys from Dendrite, see this (which should be much like element-hq/synapse#16943

@@ -57,6 +58,14 @@ func (s *CrossSigningKey) Equal(other *CrossSigningKey) bool {
if len(s.Usage) != len(other.Usage) {
return false
}

// Make sure the slices are sorted before we compare them.
if !slices.IsSorted(s.Usage) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily needed to check, but is slightly faster doing so.

@S7evinK S7evinK merged commit bf86bc9 into main Jan 6, 2025
5 checks passed
@S7evinK S7evinK deleted the s7evink/msc3967 branch January 6, 2025 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants